Skip to content

Commit

Permalink
feat: Add API endpoints to assign and remove a permission from an org…
Browse files Browse the repository at this point in the history
… role
  • Loading branch information
mzhong9723 committed Nov 16, 2023
1 parent cd0f8d1 commit fc02a38
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
22 changes: 22 additions & 0 deletions clerk/instance_organization_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,25 @@ func (s *InstanceService) DeleteOrganizationRole(orgRoleID string) (*DeleteRespo
}
return &deleteResponse, nil
}

func (s *InstanceService) AssignOrganizationRolePermission(orgRoleID, orgPermissionID string) (*Role, error) {
req, _ := s.client.NewRequest(http.MethodPost, fmt.Sprintf("%s/%s/permissions/%s", OrganizationRolesUrl, orgRoleID, orgPermissionID))

var orgRole Role
_, err := s.client.Do(req, &orgRole)
if err != nil {
return nil, err
}
return &orgRole, nil
}

func (s *InstanceService) RemoveOrganizationRolePermission(orgRoleID, orgPermissionID string) (*Role, error) {
req, _ := s.client.NewRequest(http.MethodDelete, fmt.Sprintf("%s/%s/permissions/%s", OrganizationRolesUrl, orgRoleID, orgPermissionID))

var orgRole Role
_, err := s.client.Do(req, &orgRole)
if err != nil {
return nil, err
}
return &orgRole, nil
}
54 changes: 54 additions & 0 deletions clerk/instance_organization_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,61 @@ func TestOrganizationRolesService_Delete(t *testing.T) {
}
}

func TestOrganizationRolesService_AssignPermission(t *testing.T) {
client, mux, _, teardown := setup("token")
defer teardown()

expectedResponse := dummyOrgRoleJson
mux.HandleFunc(fmt.Sprintf("/organization_roles/%s/permissions/%s", dummyOrgRoleID, dummyOrgPermissionID), func(w http.ResponseWriter, req *http.Request) {
testHttpMethod(t, req, "POST")
testHeader(t, req, "Authorization", "Bearer token")
fmt.Fprint(w, expectedResponse)
})

got, err := client.Instances().AssignOrganizationRolePermission(dummyOrgRoleID, dummyOrgPermissionID)
if err != nil {
t.Fatal(err)
}

var want Role
err = json.Unmarshal([]byte(expectedResponse), &want)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("Response = %v, want %v", got, &want)
}
}

func TestOrganizationRolesService_RemovePermission(t *testing.T) {
client, mux, _, teardown := setup("token")
defer teardown()

expectedResponse := dummyOrgRoleJson
mux.HandleFunc(fmt.Sprintf("/organization_roles/%s/permissions/%s", dummyOrgRoleID, dummyOrgPermissionID), func(w http.ResponseWriter, req *http.Request) {
testHttpMethod(t, req, "DELETE")
testHeader(t, req, "Authorization", "Bearer token")
fmt.Fprint(w, expectedResponse)
})
got, err := client.Instances().RemoveOrganizationRolePermission(dummyOrgRoleID, dummyOrgPermissionID)
if err != nil {
t.Fatal(err)
}

var want Role
err = json.Unmarshal([]byte(expectedResponse), &want)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("Response = %v, want %v", got, &want)
}
}

const dummyOrgRoleID = "role_1mebQggrD3xO5JfuHk7clQ94ysA"
const dummyOrgPermissionID = "perm_1mebQggrD3xO5JfuHk7clQ94ysA"

const dummyOrgRoleJson = `{
"object": "role",
Expand Down

0 comments on commit fc02a38

Please sign in to comment.