Skip to content

Commit

Permalink
Merge 55acf29 into 882c0e3
Browse files Browse the repository at this point in the history
  • Loading branch information
ozerovandrei committed Apr 20, 2018
2 parents 882c0e3 + 55acf29 commit dde1993
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
12 changes: 12 additions & 0 deletions selvpcclient/resell/v2/roles/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,17 @@ Example of creating several roles
for _, myRole := range allRoles {
fmt.Println(myRole)
}
Example of deleting a single role
deleteOpts := roles.RoleOpt{
ProjectID: "49338ac045f448e294b25d013f890317",
UserID: "763eecfaeb0c8e9b76ab12a82eb4c11",
}
_, err := roles.Delete(ctx, resellClient, deleteOpts)
if err != nil {
log.Fatal(err)
}
*/
package roles
13 changes: 13 additions & 0 deletions selvpcclient/resell/v2/roles/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,16 @@ func CreateBulk(ctx context.Context, client *selvpcclient.ServiceClient, createO

return result.Roles, responseResult, nil
}

// Delete requests a deletion of the single role for the specified project and user.
func Delete(ctx context.Context, client *selvpcclient.ServiceClient, deleteOpts RoleOpt) (*selvpcclient.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, resourceURL, "projects", deleteOpts.ProjectID, "users", deleteOpts.UserID}, "/")
responseResult, err := client.DoRequest(ctx, "DELETE", url, nil)
if err != nil {
return nil, err
}
if responseResult.Err != nil {
return responseResult, responseResult.Err
}
return responseResult, nil
}
4 changes: 2 additions & 2 deletions selvpcclient/resell/v2/roles/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ const TestListUserResponseRaw = `
}
`

// TestCreateRoleOpts represent options for the Create request.
var TestCreateRoleOpts = roles.RoleOpt{
// TestRoleOpt represent options for the Create and Delete requests.
var TestRoleOpt = roles.RoleOpt{
ProjectID: "49338ac045f448e294b25d013f890317",
UserID: "763eecfaeb0c8e9b76ab12a82eb4c11",
}
Expand Down
69 changes: 65 additions & 4 deletions selvpcclient/resell/v2/roles/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func TestCreateRole(t *testing.T) {
TestCreateRoleResponseRaw, http.MethodPost, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateRoleOpts
createOpts := TestRoleOpt
actual, _, err := roles.Create(ctx, testEnv.Client, createOpts)
if err != nil {
t.Fatal(err)
Expand All @@ -296,7 +296,7 @@ func TestCreateRoleHTTPError(t *testing.T) {
TestCreateRoleResponseRaw, http.MethodPost, http.StatusBadGateway, &endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateRoleOpts
createOpts := TestRoleOpt
role, httpResponse, err := roles.Create(ctx, testEnv.Client, createOpts)

if !endpointCalled {
Expand All @@ -321,7 +321,7 @@ func TestCreateRoleTimeoutError(t *testing.T) {
testEnv.NewTestResellV2Client()

ctx := context.Background()
createOpts := TestCreateRoleOpts
createOpts := TestRoleOpt
role, _, err := roles.Create(ctx, testEnv.Client, createOpts)

if role != nil {
Expand All @@ -343,7 +343,7 @@ func TestCreateRoleUnmarshalError(t *testing.T) {
TestSingleRoleInvalidResponseRaw, http.MethodPost, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateRoleOpts
createOpts := TestRoleOpt
role, _, err := roles.Create(ctx, testEnv.Client, createOpts)

if !endpointCalled {
Expand Down Expand Up @@ -460,3 +460,64 @@ func TestCreateRolesBulkUnmarshalError(t *testing.T) {
t.Fatal("expected error from the CreateBulk method")
}
}

func TestDeleteRole(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux,
"/resell/v2/roles/projects/49338ac045f448e294b25d013f890317/users/763eecfaeb0c8e9b76ab12a82eb4c11",
"", http.MethodDelete, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
deleteOpts := TestRoleOpt
_, err := roles.Delete(ctx, testEnv.Client, deleteOpts)
if err != nil {
t.Fatal(err)
}
if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
}

func TestDeleteRoleHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux,
"/resell/v2/roles/projects/49338ac045f448e294b25d013f890317/users/763eecfaeb0c8e9b76ab12a82eb4c11",
"", http.MethodDelete, http.StatusBadGateway, &endpointCalled, t)

ctx := context.Background()
deleteOpts := TestRoleOpt
httpResponse, err := roles.Delete(ctx, testEnv.Client, deleteOpts)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if err == nil {
t.Fatal("expected error from the Delete method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadRequest, httpResponse.StatusCode)
}
}

func TestDeleteRoleTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
deleteOpts := TestRoleOpt
_, err := roles.Delete(ctx, testEnv.Client, deleteOpts)

if err == nil {
t.Fatal("expected error from the Delete method")
}
}

0 comments on commit dde1993

Please sign in to comment.