From 76d8bf6ffeec289b9afd7d59cdee0b249a040b76 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Mon, 27 Jan 2025 17:06:24 +0300 Subject: [PATCH] feat: support username for get account --- authn/authn.go | 6 ++--- authn/internal_client.go | 6 ++--- authn/internal_client_test.go | 44 ++++++++++++++++++++++++----------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/authn/authn.go b/authn/authn.go index cc36301..a9e37fd 100644 --- a/authn/authn.go +++ b/authn/authn.go @@ -98,9 +98,9 @@ func (ac *Client) claimsFromVerifier(idToken string, verifier JWTClaimsExtractor return claims, nil } -// GetAccount gets the account with the associated id -func (ac *Client) GetAccount(id string) (*Account, error) { // Should this be a string or an int? - return ac.iclient.GetAccount(id) +// GetAccount gets the account with the associated id or username +func (ac *Client) GetAccount(idOrUsername string) (*Account, error) { + return ac.iclient.GetAccount(idOrUsername) } // Update updates the account with the associated id diff --git a/authn/internal_client.go b/authn/internal_client.go index daa1fd4..a46c00e 100644 --- a/authn/internal_client.go +++ b/authn/internal_client.go @@ -75,9 +75,9 @@ func (ic *internalClient) Key(kid string) ([]jose.JSONWebKey, error) { return jwks.Key(kid), nil } -// GetAccount gets the account details for the specified account id -func (ic *internalClient) GetAccount(id string) (*Account, error) { - resp, err := ic.doWithAuth(get, "accounts/"+id, nil) +// GetAccount gets the account details for the specified account id or username +func (ic *internalClient) GetAccount(idOrUsername string) (*Account, error) { + resp, err := ic.doWithAuth(get, "accounts/"+idOrUsername, nil) if err != nil { return nil, err } diff --git a/authn/internal_client_test.go b/authn/internal_client_test.go index 70dcec5..62a31a3 100644 --- a/authn/internal_client_test.go +++ b/authn/internal_client_test.go @@ -52,10 +52,10 @@ func testingHTTPClient(handler http.Handler) (*http.Client, func()) { // Based on information at https://keratin.github.io/authn-server/#/api?id=get-account func TestICGetAccount(t *testing.T) { type request struct { - url string - htusername string - htpassword string - id string + url string + htusername string + htpassword string + idOrUsername string } type response struct { id int @@ -71,10 +71,10 @@ func TestICGetAccount(t *testing.T) { }{ { request: request{ - url: "http://test.com", - htusername: "username", - htpassword: "password", - id: "1", + url: "http://test.com", + htusername: "username", + htpassword: "password", + idOrUsername: "1", }, response: response{ id: 1, @@ -87,10 +87,26 @@ func TestICGetAccount(t *testing.T) { }, { request: request{ - url: "http://test.com", - htusername: "username", - htpassword: "password", - id: "1", + url: "http://test.com", + htusername: "username", + htpassword: "password", + idOrUsername: "test@test.com", + }, + response: response{ + id: 1, + username: "test@test.com", + locked: true, + deleted: true, + code: http.StatusOK, + errorMsg: "", + }, + }, + { + request: request{ + url: "http://test.com", + htusername: "username", + htpassword: "password", + idOrUsername: "1", }, response: response{ code: http.StatusNotFound, @@ -106,7 +122,7 @@ func TestICGetAccount(t *testing.T) { assert.Equal(t, http.MethodGet, r.Method) assert.Equal(t, tc.request.htusername, username) assert.Equal(t, tc.request.htpassword, password) - assert.Equal(t, "/accounts/"+tc.request.id, r.URL.Path) + assert.Equal(t, "/accounts/"+tc.request.idOrUsername, r.URL.Path) w.WriteHeader(tc.response.code) //if we're mocking a good request, return the json if tc.response.code == http.StatusOK { @@ -129,7 +145,7 @@ func TestICGetAccount(t *testing.T) { } cli.client = httpClient - account, err := cli.GetAccount(tc.request.id) + account, err := cli.GetAccount(tc.request.idOrUsername) if tc.response.errorMsg == "" { //Expecting no error assert.Nil(t, err) assert.Equal(t, tc.response.id, account.ID)