diff --git a/pkg/db/test.go b/pkg/db/test.go index 719ddd43..7ef1303e 100644 --- a/pkg/db/test.go +++ b/pkg/db/test.go @@ -73,8 +73,8 @@ func InitTestFixtures(tablenames ...string) (err error) { return nil } -// AssertDBExists checks and asserts the existence of certain entries in the db -func AssertDBExists(t *testing.T, table string, values map[string]interface{}, custom bool) { +// AssertExists checks and asserts the existence of certain entries in the db +func AssertExists(t *testing.T, table string, values map[string]interface{}, custom bool) { var exists bool var err error v := make(map[string]interface{}) @@ -94,8 +94,8 @@ func AssertDBExists(t *testing.T, table string, values map[string]interface{}, c assert.True(t, exists, fmt.Sprintf("Entries %v do not exist in table %s", values, table)) } -// AssertDBMissing checks and asserts the nonexiste nce of certain entries in the db -func AssertDBMissing(t *testing.T, table string, values map[string]interface{}) { +// AssertMissing checks and asserts the nonexiste nce of certain entries in the db +func AssertMissing(t *testing.T, table string, values map[string]interface{}) { v := make(map[string]interface{}) exists, err := x.Table(table).Where(values).Exist(&v) assert.NoError(t, err, fmt.Sprintf("Failed to assert entries don't exist in db, error was: %s", err)) diff --git a/pkg/models/kanban_test.go b/pkg/models/kanban_test.go index 64c41ee9..cd1dc513 100644 --- a/pkg/models/kanban_test.go +++ b/pkg/models/kanban_test.go @@ -78,6 +78,10 @@ func TestBucket_Delete(t *testing.T) { err = x.Where("bucket_id = ?", 1).Find(&tasks) assert.NoError(t, err) assert.Len(t, tasks, 15) + db.AssertMissing(t, "buckets", map[string]interface{}{ + "id": 2, + "list_id": 1, + }) }) t.Run("last bucket in list", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -88,5 +92,9 @@ func TestBucket_Delete(t *testing.T) { err := b.Delete() assert.Error(t, err) assert.True(t, IsErrCannotRemoveLastBucket(err)) + db.AssertExists(t, "buckets", map[string]interface{}{ + "id": 34, + "list_id": 18, + }, false) }) } diff --git a/pkg/models/label_task_test.go b/pkg/models/label_task_test.go index 91551db8..27f0348b 100644 --- a/pkg/models/label_task_test.go +++ b/pkg/models/label_task_test.go @@ -202,6 +202,13 @@ func TestLabelTask_Create(t *testing.T) { if (err != nil) && tt.wantErr && !tt.errType(err) { t.Errorf("LabelTask.Create() Wrong error type! Error = %v, want = %v", err, runtime.FuncForPC(reflect.ValueOf(tt.errType).Pointer()).Name()) } + if !tt.wantErr { + db.AssertExists(t, "label_task", map[string]interface{}{ + "id": l.ID, + "task_id": l.TaskID, + "label_id": l.LabelID, + }, false) + } }) } } @@ -291,6 +298,12 @@ func TestLabelTask_Delete(t *testing.T) { if (err != nil) && tt.wantErr && !tt.errType(err) { t.Errorf("LabelTask.Delete() Wrong error type! Error = %v, want = %v", err, runtime.FuncForPC(reflect.ValueOf(tt.errType).Pointer()).Name()) } + if !tt.wantForbidden { + db.AssertMissing(t, "label_task", map[string]interface{}{ + "label_id": l.LabelID, + "task_id": l.TaskID, + }) + } }) } } diff --git a/pkg/models/label_test.go b/pkg/models/label_test.go index 85d70adf..c5d3dc38 100644 --- a/pkg/models/label_test.go +++ b/pkg/models/label_test.go @@ -314,6 +314,14 @@ func TestLabel_Create(t *testing.T) { if err := l.Create(tt.args.a); (err != nil) != tt.wantErr { t.Errorf("Label.Create() error = %v, wantErr %v", err, tt.wantErr) } + if !tt.wantErr { + db.AssertExists(t, "labels", map[string]interface{}{ + "id": l.ID, + "title": l.Title, + "description": l.Description, + "hex_color": l.HexColor, + }, false) + } }) } } @@ -396,6 +404,12 @@ func TestLabel_Update(t *testing.T) { if err := l.Update(); (err != nil) != tt.wantErr { t.Errorf("Label.Update() error = %v, wantErr %v", err, tt.wantErr) } + if !tt.wantErr && !tt.wantForbidden { + db.AssertExists(t, "labels", map[string]interface{}{ + "id": tt.fields.ID, + "title": tt.fields.Title, + }, false) + } }) } } @@ -474,6 +488,11 @@ func TestLabel_Delete(t *testing.T) { if err := l.Delete(); (err != nil) != tt.wantErr { t.Errorf("Label.Delete() error = %v, wantErr %v", err, tt.wantErr) } + if !tt.wantErr && !tt.wantForbidden { + db.AssertMissing(t, "labels", map[string]interface{}{ + "id": l.ID, + }) + } }) } } diff --git a/pkg/models/list_team_test.go b/pkg/models/list_team_test.go index 18b09e01..94454bfc 100644 --- a/pkg/models/list_team_test.go +++ b/pkg/models/list_team_test.go @@ -27,92 +27,150 @@ import ( "time" ) -func TestTeamList(t *testing.T) { - db.LoadAndAssertFixtures(t) +func TestTeamList_ReadAll(t *testing.T) { + u := &user.User{ID: 1} - // Dummy relation - tl := TeamList{ - TeamID: 1, - ListID: 1, - Right: RightAdmin, - } + t.Run("normal", func(t *testing.T) { + tl := TeamList{ + TeamID: 1, + ListID: 3, + } + db.LoadAndAssertFixtures(t) + teams, _, _, err := tl.ReadAll(u, "", 1, 50) + assert.NoError(t, err) + assert.Equal(t, reflect.TypeOf(teams).Kind(), reflect.Slice) + s := reflect.ValueOf(teams) + assert.Equal(t, s.Len(), 1) + }) + t.Run("nonexistant list", func(t *testing.T) { + tl := TeamList{ + ListID: 99999, + } + db.LoadAndAssertFixtures(t) + _, _, _, err := tl.ReadAll(u, "", 1, 50) + assert.Error(t, err) + assert.True(t, IsErrListDoesNotExist(err)) + }) + t.Run("namespace owner", func(t *testing.T) { + tl := TeamList{ + TeamID: 1, + ListID: 2, + Right: RightAdmin, + } + db.LoadAndAssertFixtures(t) + _, _, _, err := tl.ReadAll(u, "", 1, 50) + assert.NoError(t, err) + }) + t.Run("no access", func(t *testing.T) { + tl := TeamList{ + TeamID: 1, + ListID: 5, + Right: RightAdmin, + } + db.LoadAndAssertFixtures(t) + _, _, _, err := tl.ReadAll(u, "", 1, 50) + assert.Error(t, err) + assert.True(t, IsErrNeedToHaveListReadAccess(err)) + }) +} - // Dummyuser - u, err := user.GetUserByID(1) - assert.NoError(t, err) +func TestTeamList_Create(t *testing.T) { + u := &user.User{ID: 1} + t.Run("normal", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + tl := TeamList{ + TeamID: 1, + ListID: 1, + Right: RightAdmin, + } + allowed, _ := tl.CanCreate(u) + assert.True(t, allowed) + err := tl.Create(u) + assert.NoError(t, err) + db.AssertExists(t, "team_list", map[string]interface{}{ + "team_id": 1, + "list_id": 1, + "right": RightAdmin, + }, false) + }) + t.Run("team already has access", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + tl := TeamList{ + TeamID: 1, + ListID: 3, + Right: RightAdmin, + } + err := tl.Create(u) + assert.Error(t, err) + assert.True(t, IsErrTeamAlreadyHasAccess(err)) + }) + t.Run("wrong rights", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + tl := TeamList{ + TeamID: 1, + ListID: 1, + Right: RightUnknown, + } + err := tl.Create(u) + assert.Error(t, err) + assert.True(t, IsErrInvalidRight(err)) + }) + t.Run("nonexistant team", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + tl := TeamList{ + TeamID: 9999, + ListID: 1, + } + err := tl.Create(u) + assert.Error(t, err) + assert.True(t, IsErrTeamDoesNotExist(err)) + }) + t.Run("nonexistant list", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + tl := TeamList{ + TeamID: 1, + ListID: 9999, + } + err := tl.Create(u) + assert.Error(t, err) + assert.True(t, IsErrListDoesNotExist(err)) + }) +} - // Check normal creation - allowed, _ := tl.CanCreate(u) - assert.True(t, allowed) - err = tl.Create(u) - assert.NoError(t, err) - - // Check again - err = tl.Create(u) - assert.Error(t, err) - assert.True(t, IsErrTeamAlreadyHasAccess(err)) - - // Check with wrong rights - tl2 := tl - tl2.Right = RightUnknown - err = tl2.Create(u) - assert.Error(t, err) - assert.True(t, IsErrInvalidRight(err)) - - // Check with inexistant team - tl3 := tl - tl3.TeamID = 3253 - err = tl3.Create(u) - assert.Error(t, err) - assert.True(t, IsErrTeamDoesNotExist(err)) - - // Check with inexistant list - tl4 := tl - tl4.ListID = 3252 - err = tl4.Create(u) - assert.Error(t, err) - assert.True(t, IsErrListDoesNotExist(err)) - - // Test Read all - teams, _, _, err := tl.ReadAll(u, "", 1, 50) - 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(u, "", 1, 50) - assert.Error(t, err) - assert.True(t, IsErrListDoesNotExist(err)) - - // Test Read all for a list where the user is owner of the namespace this list belongs to - tl5 := tl - tl5.ListID = 2 - _, _, _, err = tl5.ReadAll(u, "", 1, 50) - assert.NoError(t, err) - - // Test read all for a list where the user not has access - tl6 := tl - tl6.ListID = 5 - _, _, _, err = tl6.ReadAll(u, "", 1, 50) - assert.Error(t, err) - assert.True(t, IsErrNeedToHaveListReadAccess(err)) - - // Delete - allowed, _ = tl.CanDelete(u) - assert.True(t, allowed) - 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, IsErrTeamDoesNotHaveAccessToList(err)) +func TestTeamList_Delete(t *testing.T) { + t.Run("normal", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + tl := TeamList{ + TeamID: 1, + ListID: 3, + } + err := tl.Delete() + assert.NoError(t, err) + db.AssertMissing(t, "team_list", map[string]interface{}{ + "team_id": 1, + "list_id": 3, + }) + }) + t.Run("nonexistant team", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + tl := TeamList{ + TeamID: 9999, + ListID: 1, + } + err := tl.Delete() + assert.Error(t, err) + assert.True(t, IsErrTeamDoesNotExist(err)) + }) + t.Run("nonexistant list", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + tl := TeamList{ + TeamID: 1, + ListID: 9999, + } + err := tl.Delete() + assert.Error(t, err) + assert.True(t, IsErrTeamDoesNotHaveAccessToList(err)) + }) } func TestTeamList_Update(t *testing.T) { @@ -188,6 +246,13 @@ func TestTeamList_Update(t *testing.T) { if (err != nil) && tt.wantErr && !tt.errType(err) { t.Errorf("TeamList.Update() Wrong error type! Error = %v, want = %v", err, runtime.FuncForPC(reflect.ValueOf(tt.errType).Pointer()).Name()) } + if !tt.wantErr { + db.AssertExists(t, "team_list", map[string]interface{}{ + "list_id": tt.fields.ListID, + "team_id": tt.fields.TeamID, + "right": tt.fields.Right, + }, false) + } }) } } diff --git a/pkg/models/list_test.go b/pkg/models/list_test.go index e3a80476..2146a7c7 100644 --- a/pkg/models/list_test.go +++ b/pkg/models/list_test.go @@ -41,6 +41,12 @@ func TestList_CreateOrUpdate(t *testing.T) { } err := list.Create(usr) assert.NoError(t, err) + db.AssertExists(t, "list", map[string]interface{}{ + "id": list.ID, + "title": list.Title, + "description": list.Description, + "namespace_id": list.NamespaceID, + }, false) }) t.Run("nonexistant namespace", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -88,6 +94,12 @@ func TestList_CreateOrUpdate(t *testing.T) { } err := list.Create(usr) assert.NoError(t, err) + db.AssertExists(t, "list", map[string]interface{}{ + "id": list.ID, + "title": list.Title, + "description": list.Description, + "namespace_id": list.NamespaceID, + }, false) }) }) @@ -103,7 +115,12 @@ func TestList_CreateOrUpdate(t *testing.T) { list.Description = "Lorem Ipsum dolor sit amet." err := list.Update() assert.NoError(t, err) - + db.AssertExists(t, "list", map[string]interface{}{ + "id": list.ID, + "title": list.Title, + "description": list.Description, + "namespace_id": list.NamespaceID, + }, false) }) t.Run("nonexistant", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -139,6 +156,9 @@ func TestList_Delete(t *testing.T) { } err := list.Delete() assert.NoError(t, err) + db.AssertMissing(t, "list", map[string]interface{}{ + "id": 1, + }) } func TestList_ReadAll(t *testing.T) { diff --git a/pkg/models/list_users_test.go b/pkg/models/list_users_test.go index 63ab4d3e..dce067bb 100644 --- a/pkg/models/list_users_test.go +++ b/pkg/models/list_users_test.go @@ -126,6 +126,12 @@ func TestListUser_Create(t *testing.T) { if (err != nil) && tt.wantErr && !tt.errType(err) { t.Errorf("ListUser.Create() Wrong error type! Error = %v, want = %v", err, runtime.FuncForPC(reflect.ValueOf(tt.errType).Pointer()).Name()) } + if !tt.wantErr { + db.AssertExists(t, "users_list", map[string]interface{}{ + "user_id": ul.UserID, + "list_id": tt.fields.ListID, + }, false) + } }) } } @@ -299,6 +305,13 @@ func TestListUser_Update(t *testing.T) { if (err != nil) && tt.wantErr && !tt.errType(err) { t.Errorf("ListUser.Update() Wrong error type! Error = %v, want = %v", err, runtime.FuncForPC(reflect.ValueOf(tt.errType).Pointer()).Name()) } + if !tt.wantErr { + db.AssertExists(t, "users_list", map[string]interface{}{ + "list_id": tt.fields.ListID, + "user_id": lu.UserID, + "right": tt.fields.Right, + }, false) + } }) } } @@ -307,6 +320,7 @@ func TestListUser_Delete(t *testing.T) { type fields struct { ID int64 Username string + UserID int64 ListID int64 Right Right Created time.Time @@ -342,6 +356,7 @@ func TestListUser_Delete(t *testing.T) { name: "Try deleting normally", fields: fields{ Username: "user1", + UserID: 1, ListID: 3, }, }, @@ -367,6 +382,12 @@ func TestListUser_Delete(t *testing.T) { if (err != nil) && tt.wantErr && !tt.errType(err) { t.Errorf("ListUser.Delete() Wrong error type! Error = %v, want = %v", err, runtime.FuncForPC(reflect.ValueOf(tt.errType).Pointer()).Name()) } + if !tt.wantErr { + db.AssertMissing(t, "users_list", map[string]interface{}{ + "user_id": tt.fields.UserID, + "list_id": tt.fields.ListID, + }) + } }) } } diff --git a/pkg/models/namespace_team_test.go b/pkg/models/namespace_team_test.go index 738096be..8e92473f 100644 --- a/pkg/models/namespace_team_test.go +++ b/pkg/models/namespace_team_test.go @@ -27,84 +27,142 @@ import ( "time" ) -func TestTeamNamespace(t *testing.T) { - db.LoadAndAssertFixtures(t) +func TestTeamNamespace_ReadAll(t *testing.T) { + u := &user.User{ID: 1} - // Dummy team <-> namespace relation - tn := TeamNamespace{ - TeamID: 1, - NamespaceID: 1, - Right: RightAdmin, - } + t.Run("normal", func(t *testing.T) { + tn := TeamNamespace{ + NamespaceID: 3, + } + db.LoadAndAssertFixtures(t) + teams, _, _, err := tn.ReadAll(u, "", 1, 50) + assert.NoError(t, err) + assert.Equal(t, reflect.TypeOf(teams).Kind(), reflect.Slice) + s := reflect.ValueOf(teams) + assert.Equal(t, s.Len(), 2) + }) + t.Run("nonexistant namespace", func(t *testing.T) { + tn := TeamNamespace{ + NamespaceID: 9999, + } + db.LoadAndAssertFixtures(t) + _, _, _, err := tn.ReadAll(u, "", 1, 50) + assert.Error(t, err) + assert.True(t, IsErrNamespaceDoesNotExist(err)) + }) + t.Run("no right for namespace", func(t *testing.T) { + tn := TeamNamespace{ + NamespaceID: 17, + } + db.LoadAndAssertFixtures(t) + _, _, _, err := tn.ReadAll(u, "", 1, 50) + assert.Error(t, err) + assert.True(t, IsErrNeedToHaveNamespaceReadAccess(err)) + }) +} - dummyuser, err := user.GetUserByID(1) - assert.NoError(t, err) +func TestTeamNamespace_Create(t *testing.T) { + u := &user.User{ID: 1} - // Test normal creation - allowed, _ := tn.CanCreate(dummyuser) - assert.True(t, allowed) - err = tn.Create(dummyuser) - assert.NoError(t, err) + t.Run("normal", func(t *testing.T) { + tn := TeamNamespace{ + TeamID: 1, + NamespaceID: 1, + Right: RightAdmin, + } + db.LoadAndAssertFixtures(t) + allowed, _ := tn.CanCreate(u) + assert.True(t, allowed) + err := tn.Create(u) + assert.NoError(t, err) + db.AssertExists(t, "team_namespaces", map[string]interface{}{ + "team_id": 1, + "namespace_id": 1, + "right": RightAdmin, + }, false) + }) + t.Run("team already has access", func(t *testing.T) { + tn := TeamNamespace{ + TeamID: 1, + NamespaceID: 3, + Right: RightRead, + } + db.LoadAndAssertFixtures(t) + err := tn.Create(u) + assert.Error(t, err) + assert.True(t, IsErrTeamAlreadyHasAccess(err)) + }) + t.Run("invalid team right", func(t *testing.T) { + tn := TeamNamespace{ + TeamID: 1, + NamespaceID: 3, + Right: RightUnknown, + } + db.LoadAndAssertFixtures(t) + err := tn.Create(u) + assert.Error(t, err) + assert.True(t, IsErrInvalidRight(err)) + }) + t.Run("nonexistant team", func(t *testing.T) { + tn := TeamNamespace{ + TeamID: 9999, + NamespaceID: 1, + } + db.LoadAndAssertFixtures(t) + err := tn.Create(u) + assert.Error(t, err) + assert.True(t, IsErrTeamDoesNotExist(err)) + }) + t.Run("nonexistant namespace", func(t *testing.T) { + tn := TeamNamespace{ + TeamID: 1, + NamespaceID: 9999, + } + db.LoadAndAssertFixtures(t) + err := tn.Create(u) + assert.Error(t, err) + assert.True(t, IsErrNamespaceDoesNotExist(err)) + }) +} - // Test again (should fail) - err = tn.Create(dummyuser) - assert.Error(t, err) - assert.True(t, IsErrTeamAlreadyHasAccess(err)) +func TestTeamNamespace_Delete(t *testing.T) { + u := &user.User{ID: 1} - // Test with invalid team right - tn2 := tn - tn2.Right = RightUnknown - err = tn2.Create(dummyuser) - assert.Error(t, err) - assert.True(t, IsErrInvalidRight(err)) - - // Check with inexistant team - tn3 := tn - tn3.TeamID = 324 - err = tn3.Create(dummyuser) - assert.Error(t, err) - assert.True(t, IsErrTeamDoesNotExist(err)) - - // Check with a namespace which does not exist - tn4 := tn - tn4.NamespaceID = 423 - err = tn4.Create(dummyuser) - assert.Error(t, err) - assert.True(t, IsErrNamespaceDoesNotExist(err)) - - // Check readall - teams, _, _, err := tn.ReadAll(dummyuser, "", 1, 50) - 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, "", 1, 50) - assert.Error(t, err) - assert.True(t, IsErrNamespaceDoesNotExist(err)) - - // Check with no right to read the namespace - nouser := &user.User{ID: 393} - _, _, _, err = tn.ReadAll(nouser, "", 1, 50) - assert.Error(t, err) - assert.True(t, IsErrNeedToHaveNamespaceReadAccess(err)) - - // Delete it - allowed, _ = tn.CanDelete(dummyuser) - assert.True(t, allowed) - err = tn.Delete() - assert.NoError(t, err) - - // Try deleting with a nonexisting team - err = tn3.Delete() - assert.Error(t, err) - assert.True(t, IsErrTeamDoesNotExist(err)) - - // Try deleting with a nonexistant namespace - err = tn4.Delete() - assert.Error(t, err) - assert.True(t, IsErrTeamDoesNotHaveAccessToNamespace(err)) + t.Run("normal", func(t *testing.T) { + tn := TeamNamespace{ + TeamID: 7, + NamespaceID: 9, + } + db.LoadAndAssertFixtures(t) + allowed, _ := tn.CanDelete(u) + assert.True(t, allowed) + err := tn.Delete() + assert.NoError(t, err) + db.AssertMissing(t, "team_namespaces", map[string]interface{}{ + "team_id": 7, + "namespace_id": 9, + }) + }) + t.Run("nonexistant team", func(t *testing.T) { + tn := TeamNamespace{ + TeamID: 9999, + NamespaceID: 3, + } + db.LoadAndAssertFixtures(t) + err := tn.Delete() + assert.Error(t, err) + assert.True(t, IsErrTeamDoesNotExist(err)) + }) + t.Run("nonexistant namespace", func(t *testing.T) { + tn := TeamNamespace{ + TeamID: 1, + NamespaceID: 9999, + } + db.LoadAndAssertFixtures(t) + err := tn.Delete() + assert.Error(t, err) + assert.True(t, IsErrTeamDoesNotHaveAccessToNamespace(err)) + }) } func TestTeamNamespace_Update(t *testing.T) { @@ -180,6 +238,13 @@ func TestTeamNamespace_Update(t *testing.T) { if (err != nil) && tt.wantErr && !tt.errType(err) { t.Errorf("TeamNamespace.Update() Wrong error type! Error = %v, want = %v", err, runtime.FuncForPC(reflect.ValueOf(tt.errType).Pointer()).Name()) } + if !tt.wantErr { + db.AssertExists(t, "team_namespaces", map[string]interface{}{ + "team_id": tt.fields.TeamID, + "namespace_id": tt.fields.NamespaceID, + "right": tt.fields.Right, + }, false) + } }) } } diff --git a/pkg/models/namespace_test.go b/pkg/models/namespace_test.go index 5e7667f2..27819b3c 100644 --- a/pkg/models/namespace_test.go +++ b/pkg/models/namespace_test.go @@ -37,6 +37,10 @@ func TestNamespace_Create(t *testing.T) { db.LoadAndAssertFixtures(t) err := dummynamespace.Create(user1) assert.NoError(t, err) + db.AssertExists(t, "namespaces", map[string]interface{}{ + "title": "Test", + "description": "Lorem Ipsum", + }, false) }) t.Run("no title", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -81,6 +85,10 @@ func TestNamespace_Update(t *testing.T) { } err := n.Update() assert.NoError(t, err) + db.AssertExists(t, "namespaces", map[string]interface{}{ + "id": 1, + "title": "Lorem Ipsum", + }, false) }) t.Run("nonexisting", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -122,6 +130,9 @@ func TestNamespace_Delete(t *testing.T) { } err := n.Delete() assert.NoError(t, err) + db.AssertMissing(t, "namespaces", map[string]interface{}{ + "id": 1, + }) }) t.Run("nonexisting", func(t *testing.T) { db.LoadAndAssertFixtures(t) diff --git a/pkg/models/namespace_users_test.go b/pkg/models/namespace_users_test.go index e0067996..2950c2e2 100644 --- a/pkg/models/namespace_users_test.go +++ b/pkg/models/namespace_users_test.go @@ -31,6 +31,7 @@ func TestNamespaceUser_Create(t *testing.T) { type fields struct { ID int64 Username string + UserID int64 NamespaceID int64 Right Right Created time.Time @@ -52,6 +53,7 @@ func TestNamespaceUser_Create(t *testing.T) { name: "NamespaceUsers Create normally", fields: fields{ Username: "user1", + UserID: 1, NamespaceID: 2, }, }, @@ -123,6 +125,12 @@ func TestNamespaceUser_Create(t *testing.T) { if (err != nil) && tt.wantErr && !tt.errType(err) { t.Errorf("NamespaceUser.Create() Wrong error type! Error = %v, want = %v", err, runtime.FuncForPC(reflect.ValueOf(tt.errType).Pointer()).Name()) } + if !tt.wantErr { + db.AssertExists(t, "users_namespace", map[string]interface{}{ + "user_id": tt.fields.UserID, + "namespace_id": tt.fields.NamespaceID, + }, false) + } }) } } @@ -228,6 +236,7 @@ func TestNamespaceUser_Update(t *testing.T) { type fields struct { ID int64 Username string + UserID int64 NamespaceID int64 Right Right Created time.Time @@ -246,6 +255,7 @@ func TestNamespaceUser_Update(t *testing.T) { fields: fields{ NamespaceID: 3, Username: "user1", + UserID: 1, Right: RightAdmin, }, }, @@ -254,6 +264,7 @@ func TestNamespaceUser_Update(t *testing.T) { fields: fields{ NamespaceID: 3, Username: "user1", + UserID: 1, Right: RightWrite, }, }, @@ -262,6 +273,7 @@ func TestNamespaceUser_Update(t *testing.T) { fields: fields{ NamespaceID: 3, Username: "user1", + UserID: 1, Right: RightRead, }, }, @@ -297,6 +309,13 @@ func TestNamespaceUser_Update(t *testing.T) { if (err != nil) && tt.wantErr && !tt.errType(err) { t.Errorf("NamespaceUser.Update() Wrong error type! Error = %v, want = %v", err, runtime.FuncForPC(reflect.ValueOf(tt.errType).Pointer()).Name()) } + if !tt.wantErr { + db.AssertExists(t, "users_namespace", map[string]interface{}{ + "user_id": tt.fields.UserID, + "namespace_id": tt.fields.NamespaceID, + "right": tt.fields.Right, + }, false) + } }) } } @@ -305,6 +324,7 @@ func TestNamespaceUser_Delete(t *testing.T) { type fields struct { ID int64 Username string + UserID int64 NamespaceID int64 Right Right Created time.Time @@ -340,6 +360,7 @@ func TestNamespaceUser_Delete(t *testing.T) { name: "Try deleting normally", fields: fields{ Username: "user1", + UserID: 1, NamespaceID: 3, }, }, @@ -365,6 +386,12 @@ func TestNamespaceUser_Delete(t *testing.T) { if (err != nil) && tt.wantErr && !tt.errType(err) { t.Errorf("NamespaceUser.Delete() Wrong error type! Error = %v, want = %v", err, runtime.FuncForPC(reflect.ValueOf(tt.errType).Pointer()).Name()) } + if !tt.wantErr { + db.AssertMissing(t, "users_namespace", map[string]interface{}{ + "user_id": tt.fields.UserID, + "namespace_id": tt.fields.NamespaceID, + }) + } }) } } diff --git a/pkg/models/saved_filters_test.go b/pkg/models/saved_filters_test.go index 4656f09b..5838d711 100644 --- a/pkg/models/saved_filters_test.go +++ b/pkg/models/saved_filters_test.go @@ -65,7 +65,7 @@ func TestSavedFilter_Create(t *testing.T) { vals["filters::jsonb"] = vals["filters"].(string) + "::jsonb" delete(vals, "filters") } - db.AssertDBExists(t, "saved_filters", vals, true) + db.AssertExists(t, "saved_filters", vals, true) } func TestSavedFilter_ReadOne(t *testing.T) { @@ -92,7 +92,7 @@ func TestSavedFilter_Update(t *testing.T) { } err := sf.Update() assert.NoError(t, err) - db.AssertDBExists(t, "saved_filters", map[string]interface{}{ + db.AssertExists(t, "saved_filters", map[string]interface{}{ "id": 1, "title": "NewTitle", "description": "", @@ -106,7 +106,7 @@ func TestSavedFilter_Delete(t *testing.T) { } err := sf.Delete() assert.NoError(t, err) - db.AssertDBMissing(t, "saved_filters", map[string]interface{}{ + db.AssertMissing(t, "saved_filters", map[string]interface{}{ "id": 1, }) } diff --git a/pkg/models/task_comments_test.go b/pkg/models/task_comments_test.go index 618761c6..f89632f4 100644 --- a/pkg/models/task_comments_test.go +++ b/pkg/models/task_comments_test.go @@ -35,6 +35,12 @@ func TestTaskComment_Create(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "test", tc.Comment) assert.Equal(t, int64(1), tc.Author.ID) + db.AssertExists(t, "task_comments", map[string]interface{}{ + "id": tc.ID, + "author_id": u.ID, + "comment": "test", + "task_id": 1, + }, false) }) t.Run("nonexisting task", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -54,6 +60,9 @@ func TestTaskComment_Delete(t *testing.T) { tc := &TaskComment{ID: 1} err := tc.Delete() assert.NoError(t, err) + db.AssertMissing(t, "task_comments", map[string]interface{}{ + "id": 1, + }) }) t.Run("nonexisting comment", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -73,6 +82,10 @@ func TestTaskComment_Update(t *testing.T) { } err := tc.Update() assert.NoError(t, err) + db.AssertExists(t, "task_comments", map[string]interface{}{ + "id": 1, + "comment": "testing", + }, false) }) t.Run("nonexisting comment", func(t *testing.T) { db.LoadAndAssertFixtures(t) diff --git a/pkg/models/task_relation_test.go b/pkg/models/task_relation_test.go index b804f0e3..cbb3387a 100644 --- a/pkg/models/task_relation_test.go +++ b/pkg/models/task_relation_test.go @@ -35,6 +35,12 @@ func TestTaskRelation_Create(t *testing.T) { } err := rel.Create(&user.User{ID: 1}) assert.NoError(t, err) + db.AssertExists(t, "task_relations", map[string]interface{}{ + "task_id": 1, + "other_task_id": 2, + "relation_kind": RelationKindSubtask, + "created_by_id": 1, + }, false) }) t.Run("Two Tasks In Different Lists", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -46,6 +52,12 @@ func TestTaskRelation_Create(t *testing.T) { } err := rel.Create(&user.User{ID: 1}) assert.NoError(t, err) + db.AssertExists(t, "task_relations", map[string]interface{}{ + "task_id": 1, + "other_task_id": 13, + "relation_kind": RelationKindSubtask, + "created_by_id": 1, + }, false) }) t.Run("Already Existing", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -83,6 +95,11 @@ func TestTaskRelation_Delete(t *testing.T) { } err := rel.Delete() assert.NoError(t, err) + db.AssertMissing(t, "task_relations", map[string]interface{}{ + "task_id": 1, + "other_task_id": 29, + "relation_kind": RelationKindSubtask, + }) }) t.Run("Not existing", func(t *testing.T) { db.LoadAndAssertFixtures(t) diff --git a/pkg/models/tasks_test.go b/pkg/models/tasks_test.go index aaaaf937..3cd02820 100644 --- a/pkg/models/tasks_test.go +++ b/pkg/models/tasks_test.go @@ -49,6 +49,14 @@ func TestTask_Create(t *testing.T) { assert.Equal(t, int64(18), task.Index) // Assert moving it into the default bucket assert.Equal(t, int64(1), task.BucketID) + db.AssertExists(t, "tasks", map[string]interface{}{ + "id": task.ID, + "title": "Lorem", + "description": "Lorem Ipsum Dolor", + "list_id": 1, + "created_by_id": 1, + "bucket_id": 1, + }, false) }) t.Run("empty title", func(t *testing.T) { @@ -110,6 +118,12 @@ func TestTask_Update(t *testing.T) { } err := task.Update() assert.NoError(t, err) + db.AssertExists(t, "tasks", map[string]interface{}{ + "id": 1, + "title": "test10000", + "description": "Lorem Ipsum Dolor", + "list_id": 1, + }, false) }) t.Run("nonexistant task", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -159,6 +173,9 @@ func TestTask_Delete(t *testing.T) { } err := task.Delete() assert.NoError(t, err) + db.AssertMissing(t, "tasks", map[string]interface{}{ + "id": 1, + }) }) } diff --git a/pkg/models/team_members_test.go b/pkg/models/team_members_test.go index 79446e55..2bda54a4 100644 --- a/pkg/models/team_members_test.go +++ b/pkg/models/team_members_test.go @@ -37,6 +37,11 @@ func TestTeamMember_Create(t *testing.T) { } err := tm.Create(doer) assert.NoError(t, err) + db.AssertExists(t, "team_members", map[string]interface{}{ + "id": tm.ID, + "team_id": 1, + "user_id": 3, + }, false) }) t.Run("already existing", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -79,6 +84,10 @@ func TestTeamMember_Delete(t *testing.T) { } err := tm.Delete() assert.NoError(t, err) + db.AssertMissing(t, "team_members", map[string]interface{}{ + "team_id": 1, + "user_id": 1, + }) }) } @@ -92,7 +101,12 @@ func TestTeamMember_Update(t *testing.T) { } err := tm.Update() assert.NoError(t, err) - assert.False(t, tm.Admin) + assert.False(t, tm.Admin) // Since this endpoint toggles the right, we should get a false for admin back. + db.AssertExists(t, "team_members", map[string]interface{}{ + "team_id": 1, + "user_id": 1, + "admin": false, + }, false) }) // This should have the same result as the normal run as the update function // should ignore what was passed. @@ -106,5 +120,10 @@ func TestTeamMember_Update(t *testing.T) { err := tm.Update() assert.NoError(t, err) assert.False(t, tm.Admin) + db.AssertExists(t, "team_members", map[string]interface{}{ + "team_id": 1, + "user_id": 1, + "admin": false, + }, false) }) } diff --git a/pkg/models/teams_test.go b/pkg/models/teams_test.go index 57c9001d..02409bce 100644 --- a/pkg/models/teams_test.go +++ b/pkg/models/teams_test.go @@ -37,6 +37,11 @@ func TestTeam_Create(t *testing.T) { } err := team.Create(doer) assert.NoError(t, err) + db.AssertExists(t, "teams", map[string]interface{}{ + "id": team.ID, + "name": "Testteam293", + "description": "Lorem Ispum", + }, false) }) t.Run("empty name", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -95,6 +100,10 @@ func TestTeam_Update(t *testing.T) { } err := team.Update() assert.NoError(t, err) + db.AssertExists(t, "teams", map[string]interface{}{ + "id": team.ID, + "name": "SomethingNew", + }, false) }) t.Run("empty name", func(t *testing.T) { db.LoadAndAssertFixtures(t) @@ -126,6 +135,9 @@ func TestTeam_Delete(t *testing.T) { } err := team.Delete() assert.NoError(t, err) + db.AssertMissing(t, "teams", map[string]interface{}{ + "id": 1, + }) }) }