From 21886b7e01ada2a8c114351d0b70373c38b3423f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B4natas=20Santos?= Date: Thu, 14 Sep 2023 16:18:02 -0300 Subject: [PATCH] Fix `users.ListAuthFactor()`` return type (#260) --- pkg/users/client.go | 16 ++++++++---- pkg/users/client_test.go | 54 +++++++++++++++++++++------------------- pkg/users/users.go | 3 +-- pkg/users/users_test.go | 26 ++++++++++--------- 4 files changed, 55 insertions(+), 44 deletions(-) diff --git a/pkg/users/client.go b/pkg/users/client.go index 2d5474b8..6731eea8 100644 --- a/pkg/users/client.go +++ b/pkg/users/client.go @@ -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, @@ -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, @@ -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) @@ -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) diff --git a/pkg/users/client_test.go b/pkg/users/client_test.go index fdb8b90e..1203c124 100644 --- a/pkg/users/client_test.go +++ b/pkg/users/client_test.go @@ -1444,7 +1444,7 @@ func TestListAuthFactor(t *testing.T) { scenario string client *Client options ListAuthFactorsOpts - expected []mfa.Factor + expected ListAuthFactorsResponse err bool }{ { @@ -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", + }, }, }, }, @@ -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", + }, }, }) } diff --git a/pkg/users/users.go b/pkg/users/users.go index fe5de109..ba46e3a2 100644 --- a/pkg/users/users.go +++ b/pkg/users/users.go @@ -3,7 +3,6 @@ package users import ( "context" - "github.com/workos/workos-go/v2/pkg/mfa" "net/http" ) @@ -190,6 +189,6 @@ func EnrollAuthFactor( func ListAuthFactors( ctx context.Context, opts ListAuthFactorsOpts, -) ([]mfa.Factor, error) { +) (ListAuthFactorsResponse, error) { return DefaultClient.ListAuthFactors(ctx, opts) } diff --git a/pkg/users/users_test.go b/pkg/users/users_test.go index 86b16182..e4c49e9c 100644 --- a/pkg/users/users_test.go +++ b/pkg/users/users_test.go @@ -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", + }, }, }