Skip to content

Commit

Permalink
Addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosperes committed Apr 11, 2024
1 parent 1a10cea commit 5277772
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 16 deletions.
24 changes: 16 additions & 8 deletions app/models/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ func (a Account) MarshalJSON() ([]byte, error) {
formattedPasswordChangedAt = a.PasswordChangedAt.Format(time.RFC3339)
}

return json.Marshal(map[string]interface{}{
"id": a.ID,
"username": a.Username,
"oauth_accounts": a.OauthAccounts,
"last_login_at": formattedLastLogin,
"password_changed_at": formattedPasswordChangedAt,
"locked": a.Locked,
"deleted": a.DeletedAt != nil,
return json.Marshal(struct {
ID int `json:"id"`
Username string `json:"username"`
OauthAccounts []*OauthAccount `json:"oauth_accounts"`
LastLoginAt string `json:"last_login_at"`
PasswordChangedAt string `json:"password_changed_at"`
Locked bool `json:"locked"`
Deleted bool `json:"deleted"`
}{
ID: a.ID,
Username: a.Username,
OauthAccounts: a.OauthAccounts,
LastLoginAt: formattedLastLogin,
PasswordChangedAt: formattedPasswordChangedAt,
Locked: a.Locked,
Deleted: a.DeletedAt != nil,
})
}
12 changes: 8 additions & 4 deletions app/models/oauth_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ type OauthAccount struct {
}

func (o OauthAccount) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"provider": o.Provider,
"provider_account_id": o.ProviderID,
"email": o.Email,
return json.Marshal(struct {
Provider string `json:"provider"`
ProviderID string `json:"provider_account_id"`
Email string `json:"email"`
}{
Provider: o.Provider,
ProviderID: o.ProviderID,
Email: o.Email,
})
}
9 changes: 9 additions & 0 deletions app/services/account_getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import (
)

func TestAccountGetter(t *testing.T) {

t.Run("get non existing account", func(t *testing.T) {
accountStore := mock.NewAccountStore()
account, err := services.AccountGetter(accountStore, 9999)

require.NotNil(t, err)
require.Nil(t, account)
})

t.Run("returns empty map when no oauth accounts", func(t *testing.T) {
accountStore := mock.NewAccountStore()
acc, err := accountStore.Create("user@keratin.tech", []byte("password"))
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions app/services/identity_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ func updateUserInfo(accountStore data.AccountStore, accountID int, providerName
continue
}

if oAccount.Email == "" {
if oAccount.Email != providerUser.Email {
_, err = accountStore.UpdateOauthAccount(accountID, oAccount.Provider, providerUser.Email)
if err != nil {
return errors.Wrap(err, "UpdateOauthAccountEmail")
return errors.Wrap(err, "UpdateOauthAccount")
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions app/services/identity_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,25 @@ func TestIdentityReconciler(t *testing.T) {
assert.Equal(t, 1, len(oAccounts))
assert.Equal(t, email, oAccounts[0].Email)
})

t.Run("update oauth email when is outdated", func(t *testing.T) {
provider := "testProvider"
providerAccountId := "777"
email := "update-outdate-oauth-email@test.com"

account, err := store.Create(email, []byte("password"))
require.NoError(t, err)

err = store.AddOauthAccount(account.ID, provider, providerAccountId, "email@email.com", "TOKEN")
require.NoError(t, err)

found, err := services.IdentityReconciler(store, cfg, provider, &oauth.UserInfo{ID: providerAccountId, Email: email}, &oauth2.Token{}, 0)
assert.NoError(t, err)
assert.NotNil(t, found)

oAccounts, err := store.GetOauthAccounts(account.ID)
assert.NoError(t, err)
assert.Equal(t, 1, len(oAccounts))
assert.Equal(t, email, oAccounts[0].Email)
})
}
19 changes: 17 additions & 2 deletions server/handlers/get_oauth_accounts_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers_test

import (
"encoding/json"
"net/http"
"testing"

Expand Down Expand Up @@ -29,7 +30,14 @@ func TestGetOauthInfo(t *testing.T) {
})

t.Run("success", func(t *testing.T) {
expected := "{\"result\":[{\"email\":\"email\",\"provider\":\"test\",\"provider_account_id\":\"ID\"}]}"
var expected struct {
Result []struct {
Email string `json:"email"`
Provider string `json:"provider"`
ProviderAccountID string `json:"provider_account_id"`
}
}

account, err := app.AccountStore.Create("get-oauth-info@keratin.tech", []byte("password"))
require.NoError(t, err)

Expand All @@ -42,6 +50,13 @@ func TestGetOauthInfo(t *testing.T) {
require.NoError(t, err)

require.Equal(t, http.StatusOK, res.StatusCode)
require.Equal(t, []byte(expected), test.ReadBody(res))

err = json.Unmarshal(test.ReadBody(res), &expected)
require.NoError(t, err)

require.Equal(t, len(expected.Result), 1)
require.Equal(t, expected.Result[0].Email, "email")
require.Equal(t, expected.Result[0].Provider, "test")
require.Equal(t, expected.Result[0].ProviderAccountID, "ID")
})
}

0 comments on commit 5277772

Please sign in to comment.