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-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +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
|
|
|
//
|
2019-12-04 20:39:56 +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
|
|
|
//
|
2019-12-04 20:39:56 +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 19:20:49 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
|
|
|
"code.vikunja.io/api/pkg/user"
|
2018-07-27 19:20:49 +02:00
|
|
|
"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) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2018-07-27 19:20:49 +02:00
|
|
|
|
|
|
|
// Dummy team member
|
|
|
|
dummyteammember := TeamMember{
|
2019-05-25 11:47:16 +02:00
|
|
|
TeamID: 1,
|
|
|
|
Username: "user3",
|
2018-07-27 19:20:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Doer
|
2020-01-26 18:08:06 +01:00
|
|
|
doer, err := user.GetUserByID(1)
|
2018-07-27 19:20:49 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Insert a new team member
|
2019-08-14 21:59:31 +02:00
|
|
|
allowed, _ := dummyteammember.CanCreate(doer)
|
2019-03-24 13:35:50 +01:00
|
|
|
assert.True(t, allowed)
|
2019-08-14 21:59:31 +02:00
|
|
|
err = dummyteammember.Create(doer)
|
2018-07-27 19:20:49 +02:00
|
|
|
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
|
2019-08-14 21:59:31 +02:00
|
|
|
err = dummyteammember.Create(doer)
|
2018-07-27 19:20:49 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrUserIsMemberOfTeam(err))
|
|
|
|
|
|
|
|
// Delete it
|
2019-08-14 21:59:31 +02:00
|
|
|
allowed, _ = dummyteammember.CanDelete(doer)
|
2019-03-24 13:35:50 +01:00
|
|
|
assert.True(t, allowed)
|
2018-07-27 19:20:49 +02:00
|
|
|
err = dummyteammember.Delete()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Delete the other one
|
2019-05-25 11:47:16 +02:00
|
|
|
tm := TeamMember{TeamID: 1, Username: "user2"}
|
2018-07-27 19:20:49 +02:00
|
|
|
err = tm.Delete()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Try deleting the last one
|
2019-05-25 11:47:16 +02:00
|
|
|
tm = TeamMember{TeamID: 1, Username: "user1"}
|
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
|
2019-05-25 11:47:16 +02:00
|
|
|
dummyteammember.Username = "user9484"
|
2019-08-14 21:59:31 +02:00
|
|
|
err = dummyteammember.Create(doer)
|
2018-07-27 19:20:49 +02:00
|
|
|
assert.Error(t, err)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.True(t, user.IsErrUserDoesNotExist(err))
|
2018-07-27 19:20:49 +02:00
|
|
|
|
|
|
|
// Try adding a user to a team which does not exist
|
2019-05-25 11:47:16 +02:00
|
|
|
tm = TeamMember{TeamID: 94824, Username: "user1"}
|
2019-08-14 21:59:31 +02:00
|
|
|
err = tm.Create(doer)
|
2018-07-27 19:20:49 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTeamDoesNotExist(err))
|
|
|
|
}
|