Added tests for team <-> list relations

This commit is contained in:
konrad 2018-08-29 08:50:52 +02:00 committed by kolaente
parent 950533df21
commit 98e3d3e884
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
6 changed files with 99 additions and 13 deletions

View file

@ -4,3 +4,9 @@
description: Lorem Ipsum
owner_id: 1
namespace_id: 1
-
id: 2
title: Test2
description: Lorem Ipsum
owner_id: 3
namespace_id: 1

View file

@ -13,7 +13,7 @@ func TestList_ReadAll(t *testing.T) {
// Get all lists for our namespace
lists, err := GetListsByNamespaceID(1)
assert.NoError(t, err)
assert.Equal(t, len(lists), 1)
assert.Equal(t, len(lists), 2)
// Get all lists our user has access to
user, _, err := GetUserByID(1)

View file

@ -1,6 +1,6 @@
package models
// ReadAll implements the method to read all teams of a namespace
// ReadAll implements the method to read all teams of a list
func (tl *TeamList) ReadAll(user *User) (interface{}, error) {
// Check if the user can read the namespace
l, err := GetListByID(tl.ListID)

86
models/team_list_test.go Normal file
View file

@ -0,0 +1,86 @@
package models
import (
"testing"
"github.com/stretchr/testify/assert"
"reflect"
)
func TestTeamList(t *testing.T) {
// Dummy relation
tl := TeamList{
TeamID: 1,
ListID: 1,
Right: TeamRightAdmin,
}
// Dummyuser
user, _, err := GetUserByID(1)
assert.NoError(t, err)
// Check normal creation
assert.True(t, tl.CanCreate(&user))
err = tl.Create(&user)
assert.NoError(t, err)
// Check again
err = tl.Create(&user)
assert.Error(t, err)
assert.True(t, IsErrTeamAlreadyHasAccess(err))
// Check with wrong rights
tl2 := tl
tl2.Right = TeamRightUnknown
err = tl2.Create(&user)
assert.Error(t, err)
assert.True(t, IsErrInvalidTeamRight(err))
// Check with inexistant team
tl3 := tl
tl3.TeamID = 3253
err = tl3.Create(&user)
assert.Error(t, err)
assert.True(t, IsErrTeamDoesNotExist(err))
// Check with inexistant list
tl4 := tl
tl4.ListID = 3252
err = tl4.Create(&user)
assert.Error(t, err)
assert.True(t, IsErrListDoesNotExist(err))
// Test Read all
teams, err := tl.ReadAll(&user)
assert.NoError(t, err)
assert.Equal(t, reflect.TypeOf(teams).Kind(), reflect.Slice)
s := reflect.ValueOf(teams)
assert.Equal(t, s.Len(), 1)
// Test Read all for nonexistant list
_, err = tl4.ReadAll(&user)
assert.Error(t, err)
assert.True(t, IsErrListDoesNotExist(err))
// Test Read all for a list where the user does not have access
tl5 := tl
tl5.ListID = 2
_, err = tl5.ReadAll(&user)
assert.Error(t, err)
assert.True(t, IsErrNeedToHaveListReadAccess(err))
// Delete
assert.True(t, tl.CanDelete(&user))
err = tl.Delete()
assert.NoError(t, err)
// Delete a nonexistant team
err = tl3.Delete()
assert.Error(t, err)
assert.True(t, IsErrTeamDoesNotExist(err))
// Delete with a nonexistant list
err = tl4.Delete()
assert.Error(t, err)
assert.True(t, IsErrListDoesNotExist(err))
}

View file

@ -3,6 +3,7 @@ package models
import (
"testing"
"github.com/stretchr/testify/assert"
"reflect"
)
func TestTeamNamespace(t *testing.T) {
@ -48,8 +49,11 @@ func TestTeamNamespace(t *testing.T) {
assert.True(t, IsErrNamespaceDoesNotExist(err))
// Check readall
_, err = tn.ReadAll(&dummyuser)
teams, err := tn.ReadAll(&dummyuser)
assert.NoError(t, err)
assert.Equal(t, reflect.TypeOf(teams).Kind(), reflect.Slice)
s := reflect.ValueOf(teams)
assert.Equal(t, s.Len(), 1)
// Check readall for a nonexistant namespace
_, err = tn4.ReadAll(&dummyuser)

View file

@ -60,16 +60,6 @@ type TeamUser struct {
Admin bool `json:"admin"`
}
// GetAllTeamsByNamespaceID returns all teams for a namespace
func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) {
err = x.Table("teams").
Join("INNER", "team_namespaces", "teams.id = team_id").
Where("teams.namespace_id = ?", id).
Find(teams)
return
}
// GetTeamByID gets a team by its ID
func GetTeamByID(id int64) (team Team, err error) {
exists, err := x.Where("id = ?", id).Get(&team)