More user test refactoring.

Added a utfmb4 username test.
This commit is contained in:
Azareal 2018-10-04 14:59:14 +10:00
parent beb2d99469
commit f517fc2e65
1 changed files with 48 additions and 90 deletions

View File

@ -51,7 +51,7 @@ func TestUserStore(t *testing.T) {
userStoreTest(t, 2) userStoreTest(t, 2)
common.Users, err = common.NewDefaultUserStore(nil) common.Users, err = common.NewDefaultUserStore(nil)
expectNilErr(t, err) expectNilErr(t, err)
userStoreTest(t, 4) userStoreTest(t, 5)
} }
func userStoreTest(t *testing.T, newUserID int) { func userStoreTest(t *testing.T, newUserID int) {
ucache := common.Users.GetCache() ucache := common.Users.GetCache()
@ -236,109 +236,53 @@ func userStoreTest(t *testing.T, newUserID int) {
bytesBuffer := bytes.NewBuffer([]byte("")) bytesBuffer := bytes.NewBuffer([]byte(""))
dummyRequest1 := httptest.NewRequest("", "/forum/"+strconv.Itoa(reportsForumID), bytesBuffer) dummyRequest1 := httptest.NewRequest("", "/forum/"+strconv.Itoa(reportsForumID), bytesBuffer)
dummyRequest2 := httptest.NewRequest("", "/forum/"+strconv.Itoa(generalForumID), bytesBuffer) dummyRequest2 := httptest.NewRequest("", "/forum/"+strconv.Itoa(generalForumID), bytesBuffer)
var user2 *common.User
err = user.ChangeGroup(1) var changeGroupTest = func(oldGroup int, newGroup int) {
err = user.ChangeGroup(newGroup)
expectNilErr(t, err) expectNilErr(t, err)
expect(t, user.Group == common.Config.DefaultGroup, "Someone's mutated this pointer elsewhere") // ! I don't think ChangeGroup should be changing the value of user... Investigate this.
expect(t, oldGroup == user.Group, "Someone's mutated this pointer elsewhere")
user, err = common.Users.Get(newUserID) user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID) recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
var user2 *common.User = common.BlankUser() user2 = common.BlankUser()
*user2 = *user *user2 = *user
expectUser(user, newUserID, "Sam", 1, false, true, true, false) }
var changeGroupTest2 = func(rank string, firstShouldBe bool, secondShouldBe bool) {
_, ferr := common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID) _, ferr := common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck") expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user.Perms.ViewTopic, "Admins should be able to access the reports forum") expect(t, user.Perms.ViewTopic == firstShouldBe, rank+" should be able to access the reports forum")
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID) _, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck") expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum") expect(t, user2.Perms.ViewTopic == secondShouldBe, "Sam should be able to access the general forum")
}
err = user.ChangeGroup(2) changeGroupTest(common.Config.DefaultGroup, 1)
expectNilErr(t, err) expectUser(user, newUserID, "Sam", 1, false, true, true, false)
expect(t, user.Group == 1, "Someone's mutated this pointer elsewhere") changeGroupTest2("Admins", true, true)
user, err = common.Users.Get(newUserID) changeGroupTest(1, 2)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
user2 = common.BlankUser()
*user2 = *user
expectUser(user, newUserID, "Sam", 2, false, false, true, false) expectUser(user, newUserID, "Sam", 2, false, false, true, false)
changeGroupTest2("Mods", true, true)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID) changeGroupTest(2, 3)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user.Perms.ViewTopic, "Mods should be able to access the reports forum")
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum")
err = user.ChangeGroup(3)
expectNilErr(t, err)
expect(t, user.Group == 2, "Someone's mutated this pointer elsewhere")
user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
user2 = common.BlankUser()
*user2 = *user
expectUser(user, newUserID, "Sam", 3, false, false, false, false) expectUser(user, newUserID, "Sam", 3, false, false, false, false)
changeGroupTest2("Members", false, true)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, !user.Perms.ViewTopic, "Members shouldn't be able to access the reports forum")
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum")
expect(t, user.Perms.ViewTopic != user2.Perms.ViewTopic, "user.Perms.ViewTopic and user2.Perms.ViewTopic should never match") expect(t, user.Perms.ViewTopic != user2.Perms.ViewTopic, "user.Perms.ViewTopic and user2.Perms.ViewTopic should never match")
err = user.ChangeGroup(4) changeGroupTest(3, 4)
expectNilErr(t, err)
expect(t, user.Group == 3, "Someone's mutated this pointer elsewhere")
user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
user2 = common.BlankUser()
*user2 = *user
expectUser(user, newUserID, "Sam", 4, false, false, false, true) expectUser(user, newUserID, "Sam", 4, false, false, false, true)
changeGroupTest2("Members", false, true)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID) changeGroupTest(4, 5)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, !user.Perms.ViewTopic, "Members shouldn't be able to access the reports forum")
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum")
err = user.ChangeGroup(5)
expectNilErr(t, err)
expect(t, user.Group == 4, "Someone's mutated this pointer elsewhere")
user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
user2 = common.BlankUser()
*user2 = *user
expectUser(user, newUserID, "Sam", 5, false, false, false, false) expectUser(user, newUserID, "Sam", 5, false, false, false, false)
changeGroupTest2("Members", false, true)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID) changeGroupTest(5, 6)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, !user.Perms.ViewTopic, "Members shouldn't be able to access the reports forum")
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum")
err = user.ChangeGroup(6)
expectNilErr(t, err)
expect(t, user.Group == 5, "Someone's mutated this pointer elsewhere")
user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
user2 = common.BlankUser()
*user2 = *user
expectUser(user, newUserID, "Sam", 6, false, false, false, false) expectUser(user, newUserID, "Sam", 6, false, false, false, false)
changeGroupTest2("Members", false, true)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, !user.Perms.ViewTopic, "Members shouldn't be able to access the reports forum")
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum")
err = user.ChangeGroup(common.Config.DefaultGroup) err = user.ChangeGroup(common.Config.DefaultGroup)
expectNilErr(t, err) expectNilErr(t, err)
@ -371,6 +315,20 @@ func userStoreTest(t *testing.T, newUserID int) {
expectNilErr(t, err) expectNilErr(t, err)
expect(t, !common.Users.Exists(newUserID+1), fmt.Sprintf("UID #%d should no longer exist", newUserID+1)) expect(t, !common.Users.Exists(newUserID+1), fmt.Sprintf("UID #%d should no longer exist", newUserID+1))
// MySQL utf8mb4 username test
uid, err = common.Users.Create("😀😀😀", "😀😀😀", "sam@localhost.loc", awaitingActivation, false)
expectNilErr(t, err)
expect(t, uid == newUserID+2, fmt.Sprintf("The UID of the new user should be %d", newUserID+2))
expect(t, common.Users.Exists(newUserID+2), fmt.Sprintf("UID #%d should exist", newUserID+2))
user, err = common.Users.Get(newUserID + 2)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID+1)
expectUser(user, newUserID+2, "😀😀😀", 5, false, false, false, false)
err = user.Delete()
expectNilErr(t, err)
expect(t, !common.Users.Exists(newUserID+2), fmt.Sprintf("UID #%d should no longer exist", newUserID+2))
// TODO: Add unicode login tests somewhere? Probably with the rest of the auth tests // TODO: Add unicode login tests somewhere? Probably with the rest of the auth tests
// TODO: Add tests for the Cache* methods // TODO: Add tests for the Cache* methods