-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from symbiosis-cloud/feature/add-api-keys
added API key management
- Loading branch information
Showing
6 changed files
with
208 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package symbiosis | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type ApiKey struct { | ||
ID string `json:"id"` | ||
Token string `json:"token"` | ||
SubjectID string `json:"subjectId"` | ||
Role UserRole `json:"role"` | ||
Description string `json:"description"` | ||
LastUsedAt time.Time `json:"lastUsedAt"` | ||
} | ||
|
||
type ApiKeyService interface { | ||
Create(input ApiKeyInput) (*ApiKey, error) | ||
List() (ApiKeyCollection, error) | ||
Delete(id string) error | ||
} | ||
|
||
type ApiKeyInput struct { | ||
Role UserRole `json:"role"` | ||
Description string `json:"description"` | ||
} | ||
|
||
type ApiKeyCollection []*ApiKey | ||
|
||
type ApiKeyServiceClient struct { | ||
client *Client | ||
} | ||
|
||
func (n *ApiKeyServiceClient) Create(input ApiKeyInput) (*ApiKey, error) { | ||
var apiKey *ApiKey | ||
|
||
err := n.client.Call( | ||
"/rest/v1/api-keys", | ||
"Post", | ||
&apiKey, | ||
WithBody(input), | ||
) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return apiKey, nil | ||
} | ||
|
||
func (n *ApiKeyServiceClient) List() (ApiKeyCollection, error) { | ||
var apiKeys ApiKeyCollection | ||
|
||
err := n.client. | ||
Call("/rest/v1/api-keys", | ||
"Get", | ||
&apiKeys) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return apiKeys, nil | ||
} | ||
|
||
func (n *ApiKeyServiceClient) Delete(id string) error { | ||
err := n.client. | ||
Call(fmt.Sprintf("/rest/v1/api-keys/%s", id), | ||
"Delete", | ||
nil) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package symbiosis | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const apiKeyJSON = ` | ||
[ | ||
{ | ||
"id": "jyduxxxxxxxbdt", | ||
"token": "a0jyduhjjaykagxbdt****************", | ||
"subjectId": "xxxxxxx-040e-401f-a4e5-xxxxxxxxxxx", | ||
"role": "ADMIN", | ||
"lastUsedAt": "2022-06-01T18:36:37.646083Z" | ||
}, | ||
{ | ||
"id": "pdxxxxxxxxxxxe", | ||
"token": "a0pdxxhusgrasbfxxe****************", | ||
"subjectId": "xxxxxxx-1f16-4e98-893b-xxxxxxxxxxx", | ||
"role": "ADMIN", | ||
"lastUsedAt": null | ||
} | ||
] | ||
` | ||
|
||
func TestCreateApiKey(t *testing.T) { | ||
c := getMocketClient() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
fakeURL := "/rest/v1/api-keys" | ||
|
||
responder := httpmock.NewStringResponder(200, `{ | ||
"id": "pdxxxxxxxxxxxe", | ||
"token": "a0pdxxhusgrasbfxxe****************", | ||
"subjectId": "xxxxxxx-1f16-4e98-893b-xxxxxxxxxxx", | ||
"role": "MEMBER", | ||
"lastUsedAt": null | ||
}`) | ||
httpmock.RegisterResponder("POST", fakeURL, responder) | ||
|
||
apiKey, err := c.ApiKeys.Create(ApiKeyInput{ | ||
Role: ROLE_MEMBER, | ||
Description: "hello world", | ||
}) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, apiKey.ID, "pdxxxxxxxxxxxe") | ||
assert.Equal(t, apiKey.Role, ROLE_MEMBER) | ||
|
||
responder = httpmock.NewErrorResponder(assert.AnError) | ||
httpmock.RegisterResponder("POST", fakeURL, responder) | ||
|
||
_, err = c.ApiKeys.Create(ApiKeyInput{ | ||
Role: ROLE_MEMBER, | ||
Description: "hello world", | ||
}) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestListApiKeys(t *testing.T) { | ||
c := getMocketClient() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
fakeURL := "/rest/v1/api-keys" | ||
|
||
var fakeApiKeys ApiKeyCollection | ||
json.Unmarshal([]byte(apiKeyJSON), &fakeApiKeys) | ||
|
||
responder := httpmock.NewStringResponder(200, apiKeyJSON) | ||
httpmock.RegisterResponder("GET", fakeURL, responder) | ||
|
||
apiKeys, err := c.ApiKeys.List() | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, fakeApiKeys, apiKeys) | ||
|
||
responder = httpmock.NewErrorResponder(assert.AnError) | ||
httpmock.RegisterResponder("GET", fakeURL, responder) | ||
|
||
_, err = c.ApiKeys.List() | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestDeleteApiKey(t *testing.T) { | ||
c := getMocketClient() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
fakeURL := "/rest/v1/api-keys/test123" | ||
|
||
responder := httpmock.NewStringResponder(200, "") | ||
httpmock.RegisterResponder("DELETE", fakeURL, responder) | ||
|
||
err := c.ApiKeys.Delete("test123") | ||
|
||
assert.Nil(t, err) | ||
|
||
responder = httpmock.NewErrorResponder(assert.AnError) | ||
httpmock.RegisterResponder("DELETE", fakeURL, responder) | ||
|
||
err = c.ApiKeys.Delete("test123") | ||
assert.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,10 @@ package symbiosis | |
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
const teamMemberJSON = ` | ||
|
@@ -17,7 +18,7 @@ const teamMemberJSON = ` | |
const teamMembersJSON = `[ ` + teamMemberJSON + ` ]` | ||
|
||
func TestGetValidRoles(t *testing.T) { | ||
validRoles := map[string]bool{RoleOwner: true, RoleAdmin: true, RoleMember: true} | ||
validRoles := map[UserRole]bool{ROLE_OWNER: true, ROLE_ADMIN: true, ROLE_MEMBER: true} | ||
|
||
assert.Equal(t, validRoles, GetValidRoles()) | ||
} | ||
|
@@ -82,15 +83,15 @@ func TestInviteMembers(t *testing.T) { | |
responder := httpmock.NewStringResponder(200, teamMembersJSON) | ||
httpmock.RegisterResponder("POST", fakeURL, responder) | ||
|
||
teamMembers, err := c.Team.InviteMembers([]string{"[email protected]"}, RoleAdmin) | ||
teamMembers, err := c.Team.InviteMembers([]string{"[email protected]"}, ROLE_ADMIN) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, fakeTeamMembers, teamMembers) | ||
|
||
responder = httpmock.NewErrorResponder(assert.AnError) | ||
httpmock.RegisterResponder("POST", fakeURL, responder) | ||
|
||
_, err = c.Team.InviteMembers([]string{"[email protected]"}, RoleAdmin) | ||
_, err = c.Team.InviteMembers([]string{"[email protected]"}, ROLE_ADMIN) | ||
assert.Error(t, err) | ||
|
||
// test invalid role | ||
|
@@ -127,14 +128,14 @@ func TestChangeRole(t *testing.T) { | |
responder := httpmock.NewStringResponder(200, "false") | ||
httpmock.RegisterResponder("PUT", fakeURL, responder) | ||
|
||
err := c.Team.ChangeRole("[email protected]", RoleAdmin) | ||
err := c.Team.ChangeRole("[email protected]", ROLE_ADMIN) | ||
|
||
assert.Nil(t, err) | ||
|
||
responder = httpmock.NewErrorResponder(assert.AnError) | ||
httpmock.RegisterResponder("PUT", fakeURL, responder) | ||
|
||
err = c.Team.ChangeRole("[email protected]", RoleAdmin) | ||
err = c.Team.ChangeRole("[email protected]", ROLE_ADMIN) | ||
assert.Error(t, err) | ||
|
||
// test invalid role | ||
|