Skip to content

Commit

Permalink
Fix up RevokeAPIKey tests and finish implementing RequestTestSpec.Sta…
Browse files Browse the repository at this point in the history
…tusCode
  • Loading branch information
telyn committed Dec 3, 2018
1 parent 1f834e0 commit 823cafd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/requests/brain/revoke_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ func RevokeAPIKey(client lib.Client, id int) (err error) {
apiKey := brain.APIKey{
ExpiresAt: "00:00:00",
}
r.MarshalAndRun(spec, &apiKey)
_, _, err = r.MarshalAndRun(apiKey, nil)
return
}
29 changes: 12 additions & 17 deletions lib/requests/brain/revoke_api_key_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
package brain
package brain_test

import (
"fmt"
"testing"

"github.com/BytemarkHosting/bytemark-client/lib"
"github.com/BytemarkHosting/bytemark-client/lib/brain"
brainRequests "github.com/BytemarkHosting/bytemark-client/lib/requests/brain"
"github.com/BytemarkHosting/bytemark-client/lib/testutil"
"github.com/BytemarkHosting/bytemark-client/lib/testutil/assert"
)

func TestRevokeAPIKey(t *testing.T) {
tests := []struct {
name string
id int
requestExpected map[string]interface{}
statusCode int
shouldErr error
shouldErr bool
}{
{
id: 9,
name: "success",
id: 9,
requestExpected: map[string]interface{}{
"id": 9,
"expires_at": "00:00:00",
},
statusCode: 200,
shouldErr: false,
}, {
id: 25,
name: "error 500",
id: 25,
requestExpected: map[string]interface{}{
"id": 25,
"expires_at": "00:00:00",
},
statusCode: 500,
Expand All @@ -39,19 +40,13 @@ func TestRevokeAPIKey(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
rts := testutil.RequestTestSpec{
Method: "PUT",
URL: fmt.Sprintf("/api_keys/%d", test.id),
Endpoint: lib.BrainEndpoint,
Response: brain.APIKey{
ID: test.id,
Label: "jeff's cool api key for arctic exploration",
APIKey: "fake-api-key-whatever",
ExpiresAt: "2018-11-12T00:00:00Z",
},
Method: "PUT",
URL: fmt.Sprintf("/api_keys/%d", test.id),
Endpoint: lib.BrainEndpoint,
StatusCode: test.statusCode,
AssertRequest: assert.BodyUnmarshalEqual(test.requestExpected),
}
rts.Run(t, test.Name, true, func(client lib.Client) {
rts.Run(t, test.name, true, func(client lib.Client) {
err := brainRequests.RevokeAPIKey(client, test.id)
if err != nil && !test.shouldErr {
t.Errorf("Unexpected error: %v", err)
Expand Down
7 changes: 6 additions & 1 deletion lib/testutil/request_test_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ type RequestTestSpec struct {
// to use a raw string (i.e. if you don't want to use JSON) cast it to
// encoding/json.RawMessage - this will be reproduced verbatim
Response interface{}
// StatusCode is the status code that will be returned
// StatusCode is the status code that will be returned. If unset, will
// default to whatever http.ResponseWriter.Write defaults to.
// Only used if MuxHandlers is nil
StatusCode int
// AssertRequest is an optional func which will get called to check the
// request object further - for example to check the URL has particular
Expand All @@ -62,6 +64,9 @@ func (rts *RequestTestSpec) handlerFunc(t *testing.T, testName string, auth bool
if rts.AssertRequest != nil {
rts.AssertRequest(t, testName, r)
}
if rts.StatusCode != 0 {
wr.WriteHeader(rts.StatusCode)
}
WriteJSON(t, wr, rts.Response)
}
}
Expand Down

0 comments on commit 823cafd

Please sign in to comment.