Skip to content

Commit

Permalink
Add support enpoint for revoke token by its id (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
TruePack authored and ozerovandrei committed Sep 3, 2019
1 parent 15c9483 commit 2cf3ce9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
7 changes: 7 additions & 0 deletions selvpcclient/resell/v2/tokens/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@ Example of creating a domain-scoped token
log.Fatal(err)
}
fmt.Println(token.ID)
Example of deleting a token
_, err = tokens.Delete(context, resellClient, token.ID)
if err != nil {
log.Fatal(err)
}
*/
package tokens
13 changes: 13 additions & 0 deletions selvpcclient/resell/v2/tokens/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ func Create(ctx context.Context, client *selvpcclient.ServiceClient, createOpts

return result.Token, responseResult, nil
}

// Delete a user owned Identity token by its id.
func Delete(ctx context.Context, client *selvpcclient.ServiceClient, id string) (*selvpcclient.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, resourceURL, id}, "/")
responseResult, err := client.DoRequest(ctx, http.MethodDelete, url, nil)
if err != nil {
return nil, err
}
if responseResult.Err != nil {
err = responseResult.Err
}
return responseResult, err
}
66 changes: 66 additions & 0 deletions selvpcclient/resell/v2/tokens/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,69 @@ func TestCreateTokenUnmarshalError(t *testing.T) {
t.Fatal("expected error from the Create method")
}
}

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

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/tokens/dAFaSLSbK06iJ-iiq9x19A",
Method: http.MethodDelete,
Status: http.StatusNoContent,
CallFlag: &endpointCalled,
})

ctx := context.Background()
_, err := tokens.Delete(ctx, testEnv.Client, "dAFaSLSbK06iJ-iiq9x19A")
if err != nil {
t.Fatal(err)
}
if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
}

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

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/tokens/dAFaSLSbK06iJ-iiq9x19A",
Method: http.MethodDelete,
Status: http.StatusBadRequest,
CallFlag: &endpointCalled,
})

ctx := context.Background()
httpResponse, err := tokens.Delete(ctx, testEnv.Client, "dAFaSLSbK06iJ-iiq9x19A")

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

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

ctx := context.Background()
_, err := tokens.Delete(ctx, testEnv.Client, "dAFaSLSbK06iJ-iiq9x19A")

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

0 comments on commit 2cf3ce9

Please sign in to comment.