vikunja-api/pkg/models/team_members_test.go

64 lines
1.4 KiB
Go
Raw Normal View History

2018-07-27 19:20:49 +02:00
package models
import (
"github.com/stretchr/testify/assert"
2018-07-27 19:28:12 +02:00
"testing"
2018-07-27 19:20:49 +02:00
)
func TestTeamMember_Create(t *testing.T) {
// Dummy team member
dummyteammember := TeamMember{
TeamID: 1,
UserID: 3,
}
// Doer
2018-08-30 19:14:02 +02:00
doer, err := GetUserByID(1)
2018-07-27 19:20:49 +02:00
assert.NoError(t, err)
// Insert a new team member
assert.True(t, dummyteammember.CanCreate(&doer))
err = dummyteammember.Create(&doer)
assert.NoError(t, err)
// Check he's in there
2018-07-27 19:28:12 +02:00
team := Team{ID: 1}
2018-07-27 19:20:49 +02:00
err = team.ReadOne()
assert.NoError(t, err)
assert.Equal(t, 3, len(team.Members))
// Try inserting a user twice
err = dummyteammember.Create(&doer)
assert.Error(t, err)
assert.True(t, IsErrUserIsMemberOfTeam(err))
// Delete it
assert.True(t, dummyteammember.CanDelete(&doer))
err = dummyteammember.Delete()
assert.NoError(t, err)
// Delete the other one
2018-07-27 19:28:12 +02:00
tm := TeamMember{TeamID: 1, UserID: 2}
2018-07-27 19:20:49 +02:00
err = tm.Delete()
assert.NoError(t, err)
// Try deleting the last one
2018-07-27 19:28:12 +02:00
tm = TeamMember{TeamID: 1, UserID: 1}
2018-07-27 19:20:49 +02:00
err = tm.Delete()
assert.Error(t, err)
assert.True(t, IsErrCannotDeleteLastTeamMember(err))
// Try inserting a user which does not exist
dummyteammember.UserID = 9484
err = dummyteammember.Create(&doer)
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))
// Try adding a user to a team which does not exist
2018-07-27 19:28:12 +02:00
tm = TeamMember{TeamID: 94824, UserID: 1}
2018-07-27 19:20:49 +02:00
err = tm.Create(&doer)
assert.Error(t, err)
assert.True(t, IsErrTeamDoesNotExist(err))
}