2020-01-26 18:08:06 +01:00
|
|
|
// Copyright 2018-2020 Vikunja and contriubtors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This file is part of Vikunja.
|
|
|
|
//
|
|
|
|
// Vikunja is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Vikunja is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Vikunja. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2019-07-18 18:38:21 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-10-16 22:52:29 +02:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/user"
|
2019-07-18 18:38:21 +02:00
|
|
|
"gopkg.in/d4l3k/messagediff.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestListUsersFromList(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser1 := &user.User{
|
2020-03-01 21:30:37 +01:00
|
|
|
ID: 1,
|
|
|
|
Username: "user1",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
IsActive: true,
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser2 := &user.User{
|
2020-03-01 21:30:37 +01:00
|
|
|
ID: 2,
|
|
|
|
Username: "user2",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser3 := &user.User{
|
2019-07-18 18:38:21 +02:00
|
|
|
ID: 3,
|
|
|
|
Username: "user3",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
PasswordResetToken: "passwordresettesttoken",
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser4 := &user.User{
|
2019-07-18 18:38:21 +02:00
|
|
|
ID: 4,
|
|
|
|
Username: "user4",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
IsActive: false,
|
|
|
|
EmailConfirmToken: "tiepiQueed8ahc7zeeFe1eveiy4Ein8osooxegiephauph2Ael",
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser5 := &user.User{
|
2019-07-18 18:38:21 +02:00
|
|
|
ID: 5,
|
|
|
|
Username: "user5",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
IsActive: false,
|
|
|
|
EmailConfirmToken: "tiepiQueed8ahc7zeeFe1eveiy4Ein8osooxegiephauph2Ael",
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser6 := &user.User{
|
2020-03-01 21:30:37 +01:00
|
|
|
ID: 6,
|
|
|
|
Username: "user6",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
IsActive: true,
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser7 := &user.User{
|
2020-03-01 21:30:37 +01:00
|
|
|
ID: 7,
|
|
|
|
Username: "user7",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
IsActive: true,
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser8 := &user.User{
|
2020-03-01 21:30:37 +01:00
|
|
|
ID: 8,
|
|
|
|
Username: "user8",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
IsActive: true,
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser9 := &user.User{
|
2020-03-01 21:30:37 +01:00
|
|
|
ID: 9,
|
|
|
|
Username: "user9",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
IsActive: true,
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser10 := &user.User{
|
2020-03-01 21:30:37 +01:00
|
|
|
ID: 10,
|
|
|
|
Username: "user10",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
IsActive: true,
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser11 := &user.User{
|
2020-03-01 21:30:37 +01:00
|
|
|
ID: 11,
|
|
|
|
Username: "user11",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
IsActive: true,
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser12 := &user.User{
|
2020-03-01 21:30:37 +01:00
|
|
|
ID: 12,
|
|
|
|
Username: "user12",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
IsActive: true,
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
testuser13 := &user.User{
|
2020-03-01 21:30:37 +01:00
|
|
|
ID: 13,
|
|
|
|
Username: "user13",
|
|
|
|
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
|
|
|
|
IsActive: true,
|
2020-11-21 17:38:58 +01:00
|
|
|
Issuer: "local",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: testCreatedTime,
|
|
|
|
Updated: testUpdatedTime,
|
2019-07-18 18:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
l *List
|
|
|
|
search string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2020-01-26 18:08:06 +01:00
|
|
|
wantUsers []*user.User
|
2019-07-18 18:38:21 +02:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Check owner only",
|
|
|
|
args: args{l: &List{ID: 18, OwnerID: 7}},
|
2020-01-26 18:08:06 +01:00
|
|
|
wantUsers: []*user.User{testuser7},
|
2019-07-18 18:38:21 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// This list has another different user shared for each possible method
|
|
|
|
name: "Check with owner and other users",
|
|
|
|
args: args{l: &List{ID: 19, OwnerID: 7}},
|
2020-01-26 18:08:06 +01:00
|
|
|
wantUsers: []*user.User{
|
2019-07-18 18:38:21 +02:00
|
|
|
testuser1, // Shared Via Team readonly
|
|
|
|
testuser2, // Shared Via Team write
|
|
|
|
testuser3, // Shared Via Team admin
|
|
|
|
|
|
|
|
testuser4, // Shared Via User readonly
|
|
|
|
testuser5, // Shared Via User write
|
|
|
|
testuser6, // Shared Via User admin
|
|
|
|
|
|
|
|
testuser7, // Owner
|
|
|
|
|
|
|
|
testuser8, // Shared Via NamespaceTeam readonly
|
|
|
|
testuser9, // Shared Via NamespaceTeam write
|
|
|
|
testuser10, // Shared Via NamespaceTeam admin
|
|
|
|
|
|
|
|
testuser11, // Shared Via NamespaceUser readonly
|
|
|
|
testuser12, // Shared Via NamespaceUser write
|
|
|
|
testuser13, // Shared Via NamespaceUser admin
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
|
2019-07-18 18:38:21 +02:00
|
|
|
gotUsers, err := ListUsersFromList(tt.args.l, tt.args.search)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("ListUsersFromList() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if diff, equal := messagediff.PrettyDiff(tt.wantUsers, gotUsers); !equal {
|
|
|
|
t.Errorf("Test %s, LabelTask.ReadAll() = %v, want %v, \ndiff: %v", tt.name, gotUsers, tt.wantUsers, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|