Skip to content

Commit

Permalink
Fix users.ListAuthFactor()` return type (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatascastro12 authored Sep 14, 2023
1 parent 6ed0f4c commit 21886b7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
16 changes: 11 additions & 5 deletions pkg/users/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ type ListAuthFactorsOpts struct {
User string
}

type ListAuthFactorsResponse struct {
Data []mfa.Factor `json:"data"`

ListMetadata common.ListMetadata `json:"listMetadata"`
}

func NewClient(apiKey string) *Client {
return &Client{
APIKey: apiKey,
Expand Down Expand Up @@ -1024,7 +1030,7 @@ func (c *Client) EnrollAuthFactor(ctx context.Context, opts EnrollAuthFactorOpts
}

// ListAuthFactors lists the available authentication factors for the user.
func (c *Client) ListAuthFactors(ctx context.Context, opts ListAuthFactorsOpts) ([]mfa.Factor, error) {
func (c *Client) ListAuthFactors(ctx context.Context, opts ListAuthFactorsOpts) (ListAuthFactorsResponse, error) {
endpoint := fmt.Sprintf(
"%s/users/%s/auth/factors",
c.Endpoint,
Expand All @@ -1037,7 +1043,7 @@ func (c *Client) ListAuthFactors(ctx context.Context, opts ListAuthFactorsOpts)
nil,
)
if err != nil {
return []mfa.Factor{}, err
return ListAuthFactorsResponse{}, err
}
req = req.WithContext(ctx)
req.Header.Set("User-Agent", "workos-go/"+workos.Version)
Expand All @@ -1046,15 +1052,15 @@ func (c *Client) ListAuthFactors(ctx context.Context, opts ListAuthFactorsOpts)

res, err := c.HTTPClient.Do(req)
if err != nil {
return []mfa.Factor{}, err
return ListAuthFactorsResponse{}, err
}
defer res.Body.Close()

if err = workos_errors.TryGetHTTPError(res); err != nil {
return []mfa.Factor{}, err
return ListAuthFactorsResponse{}, err
}

var body []mfa.Factor
var body ListAuthFactorsResponse
dec := json.NewDecoder(res.Body)
err = dec.Decode(&body)

Expand Down
54 changes: 29 additions & 25 deletions pkg/users/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ func TestListAuthFactor(t *testing.T) {
scenario string
client *Client
options ListAuthFactorsOpts
expected []mfa.Factor
expected ListAuthFactorsResponse
err bool
}{
{
Expand All @@ -1458,18 +1458,20 @@ func TestListAuthFactor(t *testing.T) {
options: ListAuthFactorsOpts{
User: "user_01E3JC5F5Z1YJNPGVYWV9SX6GH",
},
expected: []mfa.Factor{
{
ID: "auth_factor_test123",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
},
{
ID: "auth_factor_test234",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
expected: ListAuthFactorsResponse{
Data: []mfa.Factor{
{
ID: "auth_factor_test123",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
},
{
ID: "auth_factor_test234",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
},
},
},
},
Expand Down Expand Up @@ -1506,18 +1508,20 @@ func listAuthFactorsTestHandler(w http.ResponseWriter, r *http.Request) {
var err error

if r.URL.Path == "/users/user_01E3JC5F5Z1YJNPGVYWV9SX6GH/auth/factors" {
body, err = json.Marshal([]mfa.Factor{
{
ID: "auth_factor_test123",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
},
{
ID: "auth_factor_test234",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
body, err = json.Marshal(ListAuthFactorsResponse{
Data: []mfa.Factor{
{
ID: "auth_factor_test123",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
},
{
ID: "auth_factor_test234",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
},
},
})
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package users

import (
"context"
"github.com/workos/workos-go/v2/pkg/mfa"
"net/http"
)

Expand Down Expand Up @@ -190,6 +189,6 @@ func EnrollAuthFactor(
func ListAuthFactors(
ctx context.Context,
opts ListAuthFactorsOpts,
) ([]mfa.Factor, error) {
) (ListAuthFactorsResponse, error) {
return DefaultClient.ListAuthFactors(ctx, opts)
}
26 changes: 14 additions & 12 deletions pkg/users/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,18 +508,20 @@ func TestUsersListAuthFactors(t *testing.T) {

SetAPIKey("test")

expectedResponse := []mfa.Factor{
{
ID: "auth_factor_test123",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
},
{
ID: "auth_factor_test234",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
expectedResponse := ListAuthFactorsResponse{
Data: []mfa.Factor{
{
ID: "auth_factor_test123",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
},
{
ID: "auth_factor_test234",
CreatedAt: "2022-02-17T22:39:26.616Z",
UpdatedAt: "2022-02-17T22:39:26.616Z",
Type: "generic_otp",
},
},
}

Expand Down

0 comments on commit 21886b7

Please sign in to comment.