Created
March 28, 2018 09:37
-
-
Save tlaitinen/ca3696a9448d48096fefe57b83e42f88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package db | |
| import ( | |
| "github.com/jmoiron/sqlx" | |
| "github.com/lib/pq" | |
| "gopkg.in/guregu/null.v3" | |
| "pipekeeper/testutil" | |
| "testing" | |
| ) | |
| func testUserGroups(tx *sqlx.Tx) func(*testing.T) { | |
| return func(t *testing.T) { | |
| var adminAuth = User{Admin: true} | |
| var ug1In = UserGroup{ | |
| Name: "ug1", | |
| UserGroupId: null.Int{}, | |
| } | |
| ug1, err := InsertUserGroup(tx, adminAuth, ug1In) | |
| ug1In.Id = ug1.Id | |
| testutil.AssertNoErrorEqual(t, err, "User Group Insert", ug1In, ug1) | |
| ug1Sub, err := InsertUserGroup(tx, adminAuth, UserGroup{ | |
| Name: "ug1 sub 1", | |
| UserGroupId: null.IntFrom(ug1.Id), | |
| }) | |
| testutil.FailIfError(t, err, "User Group Insert") | |
| ug2, err := InsertUserGroup(tx, adminAuth, UserGroup{ | |
| Name: "ug2", | |
| }) | |
| ugs, err := SelectUserGroups(tx, adminAuth, UserGroupsQuery{}) | |
| testutil.AssertNoErrorEqual(t, err, "Select user groups", ugs, []UserGroup{ug1, ug1Sub, ug2}) | |
| ugs, err = SelectUserGroups(tx, adminAuth, UserGroupsQuery{ | |
| Offset: null.IntFrom(int64(1)), | |
| Limit: null.IntFrom(int64(1)), | |
| }) | |
| testutil.AssertNoErrorEqual(t, err, "Select user groups offset limit", ugs, []UserGroup{ug1Sub}) | |
| ugs, err = SelectUserGroups(tx, adminAuth, UserGroupsQuery{ | |
| Ids: pq.Int64Array{ug1Sub.Id, ug2.Id}, | |
| }) | |
| testutil.AssertNoErrorEqual(t, err, "Select user groups ids", ugs, []UserGroup{ug1Sub, ug2}) | |
| ug1Sub.Name = "ug1 sub1" | |
| err = UpdateUserGroup(tx, adminAuth, ug1Sub) | |
| testutil.FailIfError(t, err, "Update user group") | |
| ugs, err = SelectUserGroups(tx, adminAuth, UserGroupsQuery{ | |
| Ids: pq.Int64Array{ug1Sub.Id}, | |
| }) | |
| testutil.AssertNoErrorEqual(t, err, "Select user group after update", ugs, []UserGroup{ug1Sub}) | |
| var groupAdmin = User{UserGroupId: 2, GroupAdmin: true} | |
| ugs, err = SelectUserGroups(tx, groupAdmin, UserGroupsQuery{}) | |
| testutil.AssertNoErrorEqual(t, err, "Select user groups as group admin", ugs, []UserGroup{ug1Sub}) | |
| _, err = InsertUserGroup(tx, groupAdmin, ug1Sub) | |
| _, ok := err.(*AccessDenied) | |
| testutil.FailIfFalse(t, ok, "Insert user group as group admin. Expected AccessDenied, got %v", err) | |
| err = UpdateUserGroup(tx, groupAdmin, ug1Sub) | |
| _, ok = err.(*AccessDenied) | |
| testutil.FailIfFalse(t, ok, "Update user group as group admin. Expected AccessDenied, got %v", err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment