Skip to content

Commit

Permalink
update user
Browse files Browse the repository at this point in the history
  • Loading branch information
tdeshong committed Mar 21, 2024
1 parent 245fbe9 commit b49a97a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
57 changes: 56 additions & 1 deletion user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package passage
import (
"context"
"fmt"
"net/http"
)

const UserIDDoesNotExist string = "passage User with ID \"%v\" does not exist"
const (
UserIDDoesNotExist string = "passage User with ID \"%v\" does not exist"
IdentifierDoesNotExist string = "passage User with Identifier \"%v\" does not exist"
)

// GetUser gets a user using their userID
// returns user on success, error on failure
Expand Down Expand Up @@ -39,6 +43,57 @@ func (a *App) GetUser(userID string) (*User, error) {
}
}

// GetUserByIdentifier gets a user using their identifier
// returns user on success, error on failure
func (a *App) GetUserByIdentifier(identifier string) (*User, error) {
var errorText string
message := "failed to get Passage User By Identifier"
limit := 1
res, err := a.client.ListPaginatedUsersWithResponse(
context.Background(),
a.ID,
&ListPaginatedUsersParams{
Limit: &limit,
Identifier: &identifier,
},
)

if err != nil {
return nil, Error{Message: fmt.Sprintf("network error:failed to get Passage User by Identifier. message: %s, err: %+v", message, err)}
}

if res.JSON200 != nil {
users := res.JSON200.Users
if len(users) == 0 {
message = fmt.Sprintf(IdentifierDoesNotExist, identifier)
return nil, Error{
Message: message,
StatusCode: http.StatusNotFound,
StatusText: http.StatusText(http.StatusNotFound),
ErrorText: "User not found",
}
}

return a.GetUser(users[0].ID)
}

switch {
case res.JSON401 != nil:
errorText = res.JSON401.Error
case res.JSON404 != nil:
errorText = res.JSON404.Error
case res.JSON500 != nil:
errorText = res.JSON500.Error
}

return nil, Error{
Message: message,
StatusCode: res.StatusCode(),
StatusText: res.Status(),
ErrorText: errorText,
}
}

// ActivateUser activates a user using their userID
// returns user on success, error on failure
func (a *App) ActivateUser(userID string) (*User, error) {
Expand Down
39 changes: 39 additions & 0 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,45 @@ func TestGetUserInfo(t *testing.T) {
require.Nil(t, err)
assert.Equal(t, PassageUserID, user.ID)
}

func TestGetUserInfoByIdentifier(t *testing.T) {
psg, err := passage.New(PassageAppID, &passage.Config{
APIKey: PassageApiKey,
})
require.Nil(t, err)

createUserBody := passage.CreateUserBody{
Email: RandomEmail,
}

user, err := psg.CreateUser(createUserBody)
require.Nil(t, err)
assert.Equal(t, RandomEmail, user.Email)

userByIdentifier, err := psg.GetUserByIdentifier(RandomEmail)
require.Nil(t, err)

userById, err := psg.GetUser(user.ID)
require.Nil(t, err)

assert.Equal(t, user.ID, userById.ID)

assert.Equal(t, userById, userByIdentifier)
}

func TestGetUserInfoByIdentifierError(t *testing.T) {
psg, err := passage.New(PassageAppID, &passage.Config{
APIKey: PassageApiKey,
})
require.Nil(t, err)

_, err = psg.GetUserByIdentifier("error@passage.id")
require.NotNil(t, err)

expectedMessage := "passage User with Identifier error@passage.id does not exist"
assert.Contains(t, err.Error(), expectedMessage)
}

func TestActivateUser(t *testing.T) {
psg, err := passage.New(PassageAppID, &passage.Config{
APIKey: PassageApiKey, // An API_KEY environment variable is required for testing.
Expand Down

0 comments on commit b49a97a

Please sign in to comment.