vikunja-api/pkg/models/teams_test.go

113 lines
3.1 KiB
Go
Raw Normal View History

// Vikunja is a todo-list application to facilitate your life.
2020-01-09 18:33:22 +01:00
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
2018-11-26 21:17:33 +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-11-26 21:17:33 +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-11-26 21:17:33 +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-11-26 21:17:33 +01:00
2018-07-27 16:28:09 +02:00
package models
import (
"code.vikunja.io/api/pkg/user"
2018-07-27 16:28:09 +02:00
"github.com/stretchr/testify/assert"
"reflect"
2018-07-27 16:28:28 +02:00
"testing"
2018-07-27 16:28:09 +02:00
)
func TestTeam_Create(t *testing.T) {
//Dummyteam
dummyteam := Team{
2018-07-27 16:28:28 +02:00
Name: "Testteam293",
Description: "Lorem Ispum",
2018-07-27 16:28:09 +02:00
}
// Doer
doer, err := user.GetUserByID(1)
2018-07-27 16:28:09 +02:00
assert.NoError(t, err)
// Insert it
2019-08-14 21:59:31 +02:00
allowed, _ := dummyteam.CanCreate(doer)
2019-03-24 13:35:50 +01:00
assert.True(t, allowed)
2019-08-14 21:59:31 +02:00
err = dummyteam.Create(doer)
2018-07-27 16:28:09 +02:00
assert.NoError(t, err)
// Check if it was inserted and we're admin
2018-07-27 16:28:28 +02:00
tm := Team{ID: dummyteam.ID}
2018-07-27 16:28:09 +02:00
err = tm.ReadOne()
assert.NoError(t, err)
assert.Equal(t, 1, len(tm.Members))
assert.Equal(t, doer.ID, tm.Members[0].User.ID)
assert.True(t, tm.Members[0].Admin)
2019-08-14 21:59:31 +02:00
allowed, _ = dummyteam.CanRead(doer)
2019-03-24 13:35:50 +01:00
assert.True(t, allowed)
2018-07-27 16:28:09 +02:00
2018-12-16 14:21:32 +01:00
// Try getting a team with an ID < 0
_, err = GetTeamByID(-1)
assert.Error(t, err)
assert.True(t, IsErrTeamDoesNotExist(err))
2018-07-27 16:28:09 +02:00
// Get all teams the user is part of
2019-10-23 23:11:40 +02:00
ts, _, _, err := tm.ReadAll(doer, "", 1, 50)
2018-07-27 16:28:09 +02:00
assert.NoError(t, err)
assert.Equal(t, reflect.TypeOf(ts).Kind(), reflect.Slice)
s := reflect.ValueOf(ts)
assert.Equal(t, 9, s.Len())
2018-07-27 16:28:09 +02:00
// Check inserting it with an empty name
dummyteam.Name = ""
2019-08-14 21:59:31 +02:00
err = dummyteam.Create(doer)
2018-07-27 16:28:09 +02:00
assert.Error(t, err)
assert.True(t, IsErrTeamNameCannotBeEmpty(err))
// update it (still no name, should fail)
2019-08-14 21:59:31 +02:00
allowed, _ = dummyteam.CanUpdate(doer)
2019-03-24 13:35:50 +01:00
assert.True(t, allowed)
2018-07-27 16:28:09 +02:00
err = dummyteam.Update()
assert.Error(t, err)
assert.True(t, IsErrTeamNameCannotBeEmpty(err))
// Update it, this time with a name
dummyteam.Name = "Lorem"
err = dummyteam.Update()
assert.NoError(t, err)
// Delete it
2019-08-14 21:59:31 +02:00
allowed, err = dummyteam.CanDelete(doer)
2019-04-01 21:48:48 +02:00
assert.NoError(t, err)
2019-03-24 13:35:50 +01:00
assert.True(t, allowed)
2018-07-27 16:28:09 +02:00
err = dummyteam.Delete()
assert.NoError(t, err)
// Try deleting a (now) nonexistant team
2019-08-14 21:59:31 +02:00
allowed, err = dummyteam.CanDelete(doer)
2019-04-01 21:48:48 +02:00
assert.False(t, allowed)
2018-07-27 16:28:09 +02:00
assert.Error(t, err)
assert.True(t, IsErrTeamDoesNotExist(err))
// Try updating the (now) nonexistant team
err = dummyteam.Update()
assert.Error(t, err)
assert.True(t, IsErrTeamDoesNotExist(err))
}
func TestIsErrInvalidRight(t *testing.T) {
assert.NoError(t, RightAdmin.isValid())
assert.NoError(t, RightRead.isValid())
assert.NoError(t, RightWrite.isValid())
2018-07-27 16:28:09 +02:00
// Check invalid
2019-02-18 20:32:41 +01:00
var tr Right = 938
2018-07-27 16:28:09 +02:00
err := tr.isValid()
assert.Error(t, err)
assert.True(t, IsErrInvalidRight(err))
2018-07-27 16:28:09 +02:00
}