vikunja-api/pkg/models/teams_rights_test.go

116 lines
3.3 KiB
Go
Raw Normal View History

2020-02-07 17:27:45 +01:00
// Vikunja is a to-do list application to facilitate your life.
2020-01-09 18:33:22 +01:00
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
2018-12-16 14:21:32 +01:00
//
// This program 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.
2018-12-16 14:21:32 +01:00
//
// This program 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.
2018-12-16 14:21:32 +01:00
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-12-16 14:21:32 +01:00
package models
import (
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/user"
2018-12-16 14:21:32 +01:00
"testing"
"time"
2018-12-16 14:21:32 +01:00
"code.vikunja.io/web"
)
func TestTeam_CanDoSomething(t *testing.T) {
type fields struct {
ID int64
Name string
Description string
CreatedByID int64
CreatedBy *user.User
2018-12-16 14:21:32 +01:00
Members []*TeamUser
Created time.Time
Updated time.Time
2018-12-16 14:21:32 +01:00
CRUDable web.CRUDable
Rights web.Rights
}
type args struct {
a web.Auth
}
tests := []struct {
name string
fields fields
args args
want map[string]bool
}{
{
name: "CanDoSomething Normally",
fields: fields{
ID: 1,
},
args: args{
a: &user.User{ID: 1},
2018-12-16 14:21:32 +01:00
},
want: map[string]bool{"CanCreate": true, "IsAdmin": true, "CanRead": true, "CanDelete": true, "CanUpdate": true},
},
{
name: "CanDoSomething for a nonexistant namespace",
fields: fields{
ID: 300,
},
args: args{
a: &user.User{ID: 1},
2018-12-16 14:21:32 +01:00
},
want: map[string]bool{"CanCreate": true, "IsAdmin": false, "CanRead": false, "CanDelete": false, "CanUpdate": false},
},
{
name: "CanDoSomething where the user does not have the rights",
fields: fields{
ID: 1,
},
args: args{
a: &user.User{ID: 4},
2018-12-16 14:21:32 +01:00
},
want: map[string]bool{"CanCreate": true, "IsAdmin": false, "CanRead": false, "CanDelete": false, "CanUpdate": false},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db.LoadAndAssertFixtures(t)
2018-12-16 14:21:32 +01:00
tm := &Team{
ID: tt.fields.ID,
Name: tt.fields.Name,
Description: tt.fields.Description,
CreatedByID: tt.fields.CreatedByID,
CreatedBy: tt.fields.CreatedBy,
Members: tt.fields.Members,
Created: tt.fields.Created,
Updated: tt.fields.Updated,
CRUDable: tt.fields.CRUDable,
Rights: tt.fields.Rights,
}
2019-03-24 13:35:50 +01:00
if got, _ := tm.CanCreate(tt.args.a); got != tt.want["CanCreate"] { // CanCreate is currently always true
2018-12-16 14:21:32 +01:00
t.Errorf("Team.CanCreate() = %v, want %v", got, tt.want["CanCreate"])
}
2019-03-24 13:35:50 +01:00
if got, _ := tm.CanDelete(tt.args.a); got != tt.want["CanDelete"] {
2018-12-16 14:21:32 +01:00
t.Errorf("Team.CanDelete() = %v, want %v", got, tt.want["CanDelete"])
}
2019-03-24 13:35:50 +01:00
if got, _ := tm.CanUpdate(tt.args.a); got != tt.want["CanUpdate"] {
2018-12-16 14:21:32 +01:00
t.Errorf("Team.CanUpdate() = %v, want %v", got, tt.want["CanUpdate"])
}
2019-03-24 13:35:50 +01:00
if got, _ := tm.CanRead(tt.args.a); got != tt.want["CanRead"] {
2018-12-16 14:21:32 +01:00
t.Errorf("Team.CanRead() = %v, want %v", got, tt.want["CanRead"])
}
2019-03-24 13:35:50 +01:00
if got, _ := tm.IsAdmin(tt.args.a); got != tt.want["IsAdmin"] {
2018-12-16 14:21:32 +01:00
t.Errorf("Team.IsAdmin() = %v, want %v", got, tt.want["IsAdmin"])
}
})
}
}