Add better tests for namespaces

This commit is contained in:
kolaente 2020-08-15 22:16:36 +02:00
parent bdfb804bb2
commit d192c36c39
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B

View file

@ -20,15 +20,10 @@ import (
"code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/user" "code.vikunja.io/api/pkg/user"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"reflect"
"testing" "testing"
) )
func TestNamespace_Create(t *testing.T) { func TestNamespace_Create(t *testing.T) {
db.LoadAndAssertFixtures(t)
// Create test database
//assert.NoError(t, LoadFixtures())
// Dummy namespace // Dummy namespace
dummynamespace := Namespace{ dummynamespace := Namespace{
@ -36,95 +31,137 @@ func TestNamespace_Create(t *testing.T) {
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
} }
// Doer user1 := &user.User{ID: 1}
doer, err := user.GetUserByID(1)
assert.NoError(t, err)
// Try creating it t.Run("normal", func(t *testing.T) {
allowed, _ := dummynamespace.CanCreate(doer) db.LoadAndAssertFixtures(t)
assert.True(t, allowed) err := dummynamespace.Create(user1)
err = dummynamespace.Create(doer) assert.NoError(t, err)
assert.NoError(t, err) })
t.Run("no title", func(t *testing.T) {
// check if it really exists db.LoadAndAssertFixtures(t)
allowed, _, err = dummynamespace.CanRead(doer) n2 := Namespace{}
assert.NoError(t, err) err := n2.Create(user1)
assert.True(t, allowed) assert.Error(t, err)
err = dummynamespace.ReadOne() assert.True(t, IsErrNamespaceNameCannotBeEmpty(err))
assert.NoError(t, err) })
assert.Equal(t, dummynamespace.Title, "Test") t.Run("nonexistant user", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
// Try creating one without a name nUser := &user.User{ID: 9482385}
n2 := Namespace{} dnsp2 := dummynamespace
err = n2.Create(doer) err := dnsp2.Create(nUser)
assert.Error(t, err) assert.Error(t, err)
assert.True(t, IsErrNamespaceNameCannotBeEmpty(err)) assert.True(t, user.IsErrUserDoesNotExist(err))
})
// Try inserting one with a nonexistant user }
nUser := &user.User{ID: 9482385}
dnsp2 := dummynamespace func TestNamespace_ReadOne(t *testing.T) {
err = dnsp2.Create(nUser) t.Run("normal", func(t *testing.T) {
assert.Error(t, err) n := &Namespace{ID: 1}
assert.True(t, user.IsErrUserDoesNotExist(err)) db.LoadAndAssertFixtures(t)
err := n.ReadOne()
// Update it assert.NoError(t, err)
allowed, err = dummynamespace.CanUpdate(doer) assert.Equal(t, n.Title, "testnamespace")
assert.NoError(t, err) })
assert.True(t, allowed) t.Run("nonexistant", func(t *testing.T) {
dummynamespace.Description = "Dolor sit amet." n := &Namespace{ID: 99999}
err = dummynamespace.Update() db.LoadAndAssertFixtures(t)
assert.NoError(t, err) err := n.ReadOne()
assert.Error(t, err)
// Check if it was updated assert.True(t, IsErrNamespaceDoesNotExist(err))
assert.Equal(t, "Dolor sit amet.", dummynamespace.Description) })
// Get it and check it again }
allowed, _, err = dummynamespace.CanRead(doer)
assert.NoError(t, err) func TestNamespace_Update(t *testing.T) {
assert.True(t, allowed) t.Run("normal", func(t *testing.T) {
err = dummynamespace.ReadOne() db.LoadAndAssertFixtures(t)
assert.NoError(t, err) n := &Namespace{
assert.Equal(t, "Dolor sit amet.", dummynamespace.Description) ID: 1,
Title: "Lorem Ipsum",
// Try updating one with a nonexistant owner }
dummynamespace.Owner.ID = 999999 err := n.Update()
err = dummynamespace.Update() assert.NoError(t, err)
assert.Error(t, err) })
assert.True(t, user.IsErrUserDoesNotExist(err)) t.Run("nonexisting", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
// Try updating without a name n := &Namespace{
dummynamespace.Title = "" ID: 99999,
err = dummynamespace.Update() Title: "Lorem Ipsum",
assert.Error(t, err) }
assert.True(t, IsErrNamespaceNameCannotBeEmpty(err)) err := n.Update()
assert.Error(t, err)
// Try updating a nonexistant one assert.True(t, IsErrNamespaceDoesNotExist(err))
n := Namespace{ID: 284729, Title: "Lorem"} })
err = n.Update() t.Run("nonexisting owner", func(t *testing.T) {
assert.Error(t, err) db.LoadAndAssertFixtures(t)
assert.True(t, IsErrNamespaceDoesNotExist(err)) n := &Namespace{
ID: 1,
// Delete it Title: "Lorem Ipsum",
allowed, err = dummynamespace.CanDelete(doer) Owner: &user.User{ID: 99999},
assert.NoError(t, err) }
assert.True(t, allowed) err := n.Update()
err = dummynamespace.Delete() assert.Error(t, err)
assert.NoError(t, err) assert.True(t, user.IsErrUserDoesNotExist(err))
})
// Try deleting a nonexistant one t.Run("no title", func(t *testing.T) {
err = n.Delete() db.LoadAndAssertFixtures(t)
assert.Error(t, err) n := &Namespace{
assert.True(t, IsErrNamespaceDoesNotExist(err)) ID: 1,
}
// Check if it was successfully deleted err := n.Update()
allowed, _, err = dummynamespace.CanRead(doer) assert.Error(t, err)
assert.False(t, allowed) assert.True(t, IsErrNamespaceNameCannotBeEmpty(err))
assert.Error(t, err) })
assert.True(t, IsErrNamespaceDoesNotExist(err)) }
// Get all namespaces of a user func TestNamespace_Delete(t *testing.T) {
nsps, _, _, err := n.ReadAll(doer, "", 1, 50) t.Run("normal", func(t *testing.T) {
assert.NoError(t, err) db.LoadAndAssertFixtures(t)
assert.Equal(t, reflect.TypeOf(nsps).Kind(), reflect.Slice) n := &Namespace{
s := reflect.ValueOf(nsps) ID: 1,
assert.Equal(t, 9, s.Len()) }
err := n.Delete()
assert.NoError(t, err)
})
t.Run("nonexisting", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
n := &Namespace{
ID: 9999,
}
err := n.Delete()
assert.Error(t, err)
assert.True(t, IsErrNamespaceDoesNotExist(err))
})
}
func TestNamespace_ReadAll(t *testing.T) {
user1 := &user.User{ID: 1}
t.Run("normal", func(t *testing.T) {
n := &Namespace{}
nn, _, _, err := n.ReadAll(user1, "", 1, -1)
namespaces := nn.([]*NamespaceWithLists)
assert.NoError(t, err)
assert.NotNil(t, namespaces)
assert.Len(t, namespaces, 9) // Total of 9 including shared
assert.Equal(t, int64(-1), namespaces[0].ID) // The first one should be the one with the shared namespaces
// Ensure every list and namespace are not archived
for _, namespace := range namespaces {
assert.False(t, namespace.IsArchived)
for _, list := range namespace.Lists {
assert.False(t, list.IsArchived)
}
}
})
t.Run("archived", func(t *testing.T) {
n := &Namespace{
IsArchived: true,
}
nn, _, _, err := n.ReadAll(user1, "", 1, -1)
namespaces := nn.([]*NamespaceWithLists)
assert.NoError(t, err)
assert.NotNil(t, namespaces)
assert.Len(t, namespaces, 10) // Total of 10 including shared, one is archived
assert.Equal(t, int64(-1), namespaces[0].ID) // The first one should be the one with the shared namespaces
})
} }