-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce instance organization permissions list operation
Introduce a new method on the instance service client.Instances().ListOrganizationPermissions() which you can use in order to fetch a list of the available organization permissions of your instance
- Loading branch information
1 parent
17b024b
commit 1f0aa97
Showing
4 changed files
with
148 additions
and
42 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
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,42 @@ | ||
package clerk | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type OrganizationPermission struct { | ||
Object string `json:"object"` | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Key string `json:"key"` | ||
Description string `json:"description"` | ||
Type string `json:"type"` | ||
CreatedAt int64 `json:"created_at"` | ||
UpdatedAt int64 `json:"updated_at"` | ||
} | ||
|
||
type OrganizationPermissionsResponse struct { | ||
Data []OrganizationPermission `json:"data"` | ||
TotalCount int64 `json:"total_count"` | ||
} | ||
|
||
type ListInstanceOrganizationPermissionsParams struct { | ||
Limit *int `json:"limit,omitempty"` | ||
Offset *int `json:"offset,omitempty"` | ||
} | ||
|
||
func (s *InstanceService) ListOrganizationPermissions(params ListInstanceOrganizationPermissionsParams) (*OrganizationPermissionsResponse, error) { | ||
req, _ := s.client.NewRequest(http.MethodGet, OrganizationPermissionsUrl) | ||
|
||
paginationParams := PaginationParams{Limit: params.Limit, Offset: params.Offset} | ||
query := req.URL.Query() | ||
addPaginationParams(query, paginationParams) | ||
req.URL.RawQuery = query.Encode() | ||
|
||
response := &OrganizationPermissionsResponse{} | ||
_, err := s.client.Do(req, response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return response, 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,75 @@ | ||
package clerk | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestInstanceService_List_happyPathWithParameters(t *testing.T) { | ||
client, mux, _, teardown := setup("token") | ||
defer teardown() | ||
|
||
expectedResponse := fmt.Sprintf(`{ | ||
"data": [%s], | ||
"total_count": 1 | ||
}`, dummyOrganizationPermissionsJson) | ||
|
||
mux.HandleFunc("/organization_permissions", func(w http.ResponseWriter, req *http.Request) { | ||
testHttpMethod(t, req, http.MethodGet) | ||
testHeader(t, req, "Authorization", "Bearer token") | ||
|
||
actualQuery := req.URL.Query() | ||
expectedQuery := url.Values(map[string][]string{ | ||
"limit": {"3"}, | ||
"offset": {"2"}, | ||
}) | ||
assert.Equal(t, expectedQuery, actualQuery) | ||
fmt.Fprint(w, expectedResponse) | ||
}) | ||
|
||
want := &OrganizationPermissionsResponse{} | ||
_ = json.Unmarshal([]byte(expectedResponse), want) | ||
|
||
got, _ := client.Instances().ListOrganizationPermissions(ListInstanceOrganizationPermissionsParams{ | ||
Limit: intToPtr(3), | ||
Offset: intToPtr(2), | ||
}) | ||
if len(got.Data) != len(want.Data) { | ||
t.Errorf("Was expecting %d organization permissions to be returned, instead got %d", len(want.Data), len(got.Data)) | ||
} | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("Response = %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestInstanceService_List_invalidServer(t *testing.T) { | ||
client, _ := NewClient("token") | ||
|
||
orgPermissions, err := client.Instances().ListOrganizationPermissions(ListInstanceOrganizationPermissionsParams{}) | ||
if err == nil { | ||
t.Errorf("Expected error to be returned") | ||
} | ||
if orgPermissions != nil { | ||
t.Errorf("Was not expecting any organization permissions to be returned, instead got %v", orgPermissions) | ||
} | ||
} | ||
|
||
const dummyOrganizationPermissionID = "perm_1mebQggrD3xO5JfuHk7clQ94ysA" | ||
|
||
const dummyOrganizationPermissionsJson = `{ | ||
"object": "permission", | ||
"id": "perm_1mebQggrD3xO5JfuHk7clQ94ysA", | ||
"name": "Manage organization", | ||
"key": "org:sys_profile:manage", | ||
"description": "Permission to manage an organization.", | ||
"type": "system", | ||
"created_at": 1610783813, | ||
"updated_at": 1610783813 | ||
}` |
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