Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add username to get account #265

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions app/services/account_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,37 @@ import (
"github.com/pkg/errors"
)

func AccountGetter(store data.AccountStore, accountID int) (*models.Account, error) {
account, err := store.Find(accountID)
if err != nil {
return nil, errors.Wrap(err, "Find")
type AccountGetterParams struct {
AccountID *int
Username *string
}

func AccountGetter(store data.AccountStore, params AccountGetterParams) (*models.Account, error) {
Comment on lines +9 to +14
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't seen structs used for params, so let me know if this should be changed

var account *models.Account

if params.AccountID != nil {
ac, err := store.Find(*params.AccountID)
if err != nil {
return nil, errors.Wrap(err, "Find")
}

account = ac
}

if params.Username != nil && params.AccountID == nil {
ac, err := store.FindByUsername(*params.Username)
if err != nil {
return nil, errors.Wrap(err, "FindByUsername")
}

account = ac
}

if account == nil {
return nil, FieldErrors{{"account", ErrNotFound}}
}

oauthAccounts, err := store.GetOauthAccounts(accountID)
oauthAccounts, err := store.GetOauthAccounts(account.ID)
if err != nil {
return nil, errors.Wrap(err, "GetOauthAccounts")
}
Expand Down
34 changes: 31 additions & 3 deletions app/services/account_getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,29 @@ import (

func TestAccountGetter(t *testing.T) {

t.Run("get username", func(t *testing.T) {
accountStore := mock.NewAccountStore()
acc, err := accountStore.Create("[email protected]", []byte("password"))
require.NoError(t, err)

accountID := acc.ID

account, err := services.AccountGetter(accountStore, services.AccountGetterParams{
Username: &acc.Username,
})
require.NoError(t, err)

require.Equal(t, accountID, account.ID)
})

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

accountID := 9999

account, err := services.AccountGetter(accountStore, services.AccountGetterParams{
AccountID: &accountID,
})

require.NotNil(t, err)
require.Nil(t, account)
Expand All @@ -24,7 +44,11 @@ func TestAccountGetter(t *testing.T) {
acc, err := accountStore.Create("[email protected]", []byte("password"))
require.NoError(t, err)

account, err := services.AccountGetter(accountStore, acc.ID)
accountID := acc.ID

account, err := services.AccountGetter(accountStore, services.AccountGetterParams{
AccountID: &accountID,
})
require.NoError(t, err)

require.Equal(t, 0, len(account.OauthAccounts))
Expand All @@ -41,7 +65,11 @@ func TestAccountGetter(t *testing.T) {
err = accountStore.AddOauthAccount(acc.ID, "trial", "ID2", "email2", "TOKEN2")
require.NoError(t, err)

account, err := services.AccountGetter(accountStore, acc.ID)
accountID := acc.ID

account, err := services.AccountGetter(accountStore, services.AccountGetterParams{
AccountID: &accountID,
})
require.NoError(t, err)

oAccounts := account.OauthAccounts
Expand Down
4 changes: 3 additions & 1 deletion app/services/totp_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ var ErrExistingTOTPSecret = errors.New("a OTP secret has already been establishe

// TOTPCreator handles the creation and storage of new OTP tokens
func TOTPCreator(accountStore data.AccountStore, totpCache data.TOTPCache, accountID int, audience *route.Domain) (*otp.Key, error) {
account, err := AccountGetter(accountStore, accountID)
account, err := AccountGetter(accountStore, AccountGetterParams{
AccountID: &accountID,
})
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion app/services/totp_setter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func TOTPSetter(accountStore data.AccountStore, totpCache data.TOTPCache, cfg *a
return FieldErrors{{"otp", ErrInvalidOrExpired}}
}

account, err := AccountGetter(accountStore, accountID)
account, err := AccountGetter(accountStore, AccountGetterParams{
AccountID: &accountID,
})
if err != nil {
return err
}
Expand Down
21 changes: 16 additions & 5 deletions server/handlers/get_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@ import (

func GetAccount(app *app.App) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
WriteNotFound(w, "account")
var paramID *int
var paramUsername *string

idOrUsername := mux.Vars(r)["id"]
if idOrUsername == "" {
return
}

account, err := services.AccountGetter(app.AccountStore, id)
id, err := strconv.Atoi(idOrUsername)
if err != nil {
paramUsername = &idOrUsername
} else {
paramID = &id
}

account, err := services.AccountGetter(app.AccountStore, services.AccountGetterParams{
AccountID: paramID,
Username: paramUsername,
})
if err != nil {
if _, ok := err.(services.FieldErrors); ok {
WriteNotFound(w, "account")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you say more about this change? I'm not sure how it's related.

Copy link
Author

@ahme-dev ahme-dev Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've commited it by mistake, re-adding now. Let me know if there's anything else

return
}

Expand Down
23 changes: 23 additions & 0 deletions server/handlers/get_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers_test
import (
"fmt"
"net/http"
"net/url"
"sort"
"testing"
"time"
Expand Down Expand Up @@ -52,6 +53,28 @@ func TestGetAccount(t *testing.T) {

assertGetAccountResponse(t, res, account, oauthAccounts)
})

t.Run("valid account username", func(t *testing.T) {
account, err := app.AccountStore.Create("[email protected]", []byte("bar"))
require.NoError(t, err)

err = app.AccountStore.AddOauthAccount(account.ID, "test", "ID1", "email", "TOKEN1")
require.NoError(t, err)

err = app.AccountStore.AddOauthAccount(account.ID, "trial", "ID2", "email", "TOKEN2")
require.NoError(t, err)

oauthAccounts, err := app.AccountStore.GetOauthAccounts(account.ID)
require.NoError(t, err)

username := url.QueryEscape(account.Username)
fmt.Println(username)
res, err := client.Get(fmt.Sprintf("/accounts/%v", username))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)

assertGetAccountResponse(t, res, account, oauthAccounts)
})
}

func assertGetAccountResponse(t *testing.T, res *http.Response, acc *models.Account, oAccs []*models.OauthAccount) {
Expand Down
4 changes: 3 additions & 1 deletion server/handlers/get_oauth_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ func GetOauthAccounts(app *app.App) http.HandlerFunc {
return
}

account, err := services.AccountGetter(app.AccountStore, accountID)
account, err := services.AccountGetter(app.AccountStore, services.AccountGetterParams{
AccountID: &accountID,
})
if err != nil {
WriteErrors(w, err)
return
Expand Down
2 changes: 1 addition & 1 deletion server/private_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func PrivateRoutes(app *app.App) []*route.HandledRoute {
SecuredWith(authentication).
Handle(handlers.PostAccountsImport(app)),

route.Get("/accounts/{id:[0-9]+}").
route.Get("/accounts/{id}").
SecuredWith(authentication).
Handle(handlers.GetAccount(app)),

Expand Down