Skip to content

Commit

Permalink
Remove username formatting(convert lowercase) while saving
Browse files Browse the repository at this point in the history
Resolves: #288

Add test case to cover username in mixed case

In github usernmae can be in lowercase and mixed
case. So we need to handle both usernames are
equal on our system,

Remove additional username check logic. it's not required.
  • Loading branch information
kk-r committed Aug 22, 2021
1 parent a11a299 commit 6666415
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
8 changes: 5 additions & 3 deletions api/pkg/service/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (r *request) addUser(ghUser *github.User) (*model.User, error) {
if err == gorm.ErrRecordNotFound {

user.GithubName = ghUser.GetName()
user.GithubLogin = strings.ToLower(ghUser.GetLogin())
user.GithubLogin = ghUser.GetLogin()
user.Type = model.NormalUserType
user.AvatarURL = ghUser.GetAvatarURL()

Expand All @@ -155,8 +155,10 @@ func (r *request) addUser(ghUser *github.User) (*model.User, error) {
user.GithubName = ghUser.GetName()
user.Type = model.NormalUserType
}
// For existing user, check if URL is not added
if user.AvatarURL == "" {

// For existing user, check if URL is not added or github-login is incorrect
if user.AvatarURL == "" || user.GithubLogin != ghUser.GetLogin() {
user.GithubLogin = ghUser.GetLogin()
user.AvatarURL = ghUser.GetAvatarURL()
if err = r.db.Save(&user).Error; err != nil {
r.log.Error(err)
Expand Down
60 changes: 60 additions & 0 deletions api/pkg/service/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,63 @@ func TestLogin_UserAddedByConfig(t *testing.T) {

assert.Equal(t, gock.IsDone(), true)
}

func TestLogin_UserWitMixedUSerName(t *testing.T) {
tc := testutils.Setup(t)
testutils.LoadFixtures(t, tc.FixturePath())

defer gock.Off()

gock.New("https://github.com").
Post("/login/oauth/access_token").
Reply(200).
JSON(map[string]string{
"access_token": "test-token",
})

gock.New("https://api.github.com").
Get("/user").
Reply(200).
JSON(map[string]string{
"login": "conFIG-UsER",
"name": "config-user",
"avatar_url": "http://config",
})

// Mocks the time
token.Now = testutils.Now

//get old user id
old_user := model.User{}
err := tc.DB().Where("github_login = ?", "config-user").First(&old_user).Error
assert.NoError(t, err)

authSvc := New(tc)
payload := &auth.AuthenticatePayload{Code: "test-code"}
res, err := authSvc.Authenticate(context.Background(), payload)
assert.NoError(t, err)

// validate the github login of user after login
ut := &model.User{}
err = tc.DB().Where("github_login = ?", "conFIG-UsER").First(ut).Error
assert.NoError(t, err)
// check shouldn't create no new user due to mixed user name
assert.Equal(t, old_user.ID, ut.ID)

// expected refresh jwt for user
user, refreshToken, err := tc.RefreshTokenForUser("conFIG-UsER")
assert.Equal(t, user.GithubLogin, "conFIG-UsER")
assert.NoError(t, err)

accessExpiryTime := testutils.Now().Add(tc.JWTConfig().AccessExpiresIn).Unix()
refreshExpiryTime := testutils.Now().Add(tc.JWTConfig().RefreshExpiresIn).Unix()

assert.Equal(t, tc.JWTConfig().AccessExpiresIn.String(), res.Data.Access.RefreshInterval)
assert.Equal(t, accessExpiryTime, res.Data.Access.ExpiresAt)

assert.Equal(t, refreshToken, res.Data.Refresh.Token)
assert.Equal(t, tc.JWTConfig().RefreshExpiresIn.String(), res.Data.Refresh.RefreshInterval)
assert.Equal(t, refreshExpiryTime, res.Data.Refresh.ExpiresAt)

assert.Equal(t, gock.IsDone(), true)
}

0 comments on commit 6666415

Please sign in to comment.