From 1930cc6a114f9169636577a4063a896400478d1e Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Mon, 11 Nov 2019 14:33:30 +0000 Subject: [PATCH] MM-19790: Remove randomness in oauth test AuthData. (#12988) The ID field of Gitlab OAuth data was generated as a random number between 1-1000. Unfortunately, this gave a 1 in 1 million chance of two users generated this way colliding and meaning if you had two test users in a test case they silently ended up actually being the exact same user. We don't actually need to generate this randomly, so instead change to having the test cases explicitly set the ID number so the behaviour is as expected and completely deterministic. As you'd expect, with odds of exactly a million to 1, it just might (and in this case, it did indeed) happen. --- app/user_test.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/app/user_test.go b/app/user_test.go index 078f03ff56..f0316bfcb9 100644 --- a/app/user_test.go +++ b/app/user_test.go @@ -8,7 +8,6 @@ import ( "encoding/json" "image" "image/color" - "math/rand" "strings" "testing" "time" @@ -74,8 +73,7 @@ func TestCreateOAuthUser(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - r := rand.New(rand.NewSource(time.Now().UnixNano())) - glUser := oauthgitlab.GitLabUser{Id: int64(r.Intn(1000)) + 1, Username: "o" + model.NewId(), Email: model.NewId() + "@simulator.amazonses.com", Name: "Joram Wilander"} + glUser := oauthgitlab.GitLabUser{Id: 42, Username: "o" + model.NewId(), Email: model.NewId() + "@simulator.amazonses.com", Name: "Joram Wilander"} json := glUser.ToJson() user, err := th.App.CreateOAuthUser(model.USER_AUTH_SERVICE_GITLAB, strings.NewReader(json), th.BasicTeam.Id) @@ -253,8 +251,8 @@ func TestUpdateOAuthUserAttrs(t *testing.T) { var user, user2 *model.User var gitlabUserObj oauthgitlab.GitLabUser - user, gitlabUserObj = createGitlabUser(t, th.App, username, email) - user2, _ = createGitlabUser(t, th.App, username2, email2) + user, gitlabUserObj = createGitlabUser(t, th.App, 1, username, email) + user2, _ = createGitlabUser(t, th.App, 2, username2, email2) t.Run("UpdateUsername", func(t *testing.T) { t.Run("NoExistingUserWithSameUsername", func(t *testing.T) { @@ -443,9 +441,8 @@ func getGitlabUserPayload(gitlabUser oauthgitlab.GitLabUser, t *testing.T) []byt return payload } -func createGitlabUser(t *testing.T, a *App, username string, email string) (*model.User, oauthgitlab.GitLabUser) { - r := rand.New(rand.NewSource(time.Now().UnixNano())) - gitlabUserObj := oauthgitlab.GitLabUser{Id: int64(r.Intn(1000)) + 1, Username: username, Login: "user1", Email: email, Name: "Test User"} +func createGitlabUser(t *testing.T, a *App, id int64, username string, email string) (*model.User, oauthgitlab.GitLabUser) { + gitlabUserObj := oauthgitlab.GitLabUser{Id: id, Username: username, Login: "user1", Email: email, Name: "Test User"} gitlabUser := getGitlabUserPayload(gitlabUserObj, t) var user *model.User