forked from selectel/craas-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests_test.go
More file actions
162 lines (146 loc) · 4.36 KB
/
requests_test.go
File metadata and controls
162 lines (146 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package testing
import (
"context"
"fmt"
"net/http"
"reflect"
"testing"
"github.com/selectel/craas-go/pkg/testutils"
"github.com/selectel/craas-go/pkg/v2/client"
tokenV2 "github.com/selectel/craas-go/pkg/v2/token"
)
const apiV2 = "/api/v2"
func TestCreateToken(t *testing.T) {
endpointCalled := false
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/api/v2/tokens",
RawResponse: testCreateTokenResponseRaw,
Method: http.MethodPost,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})
ctx := context.Background()
testClient, err := client.NewCRaaSClientV2(testutils.TokenID, testEnv.Server.URL+apiV2)
if err != nil {
t.Errorf("got error %s", err)
}
actual, response, err := tokenV2.Create(ctx, testClient, nil, nil)
if err != nil {
t.Fatal(err)
}
if response == nil {
t.Fatal("expected an HTTP response from the POST method")
}
if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if !reflect.DeepEqual(expectedCreateTokenResponse, actual) {
t.Fatalf("expected %#v, but got %#v", expectedCreateTokenResponse, actual)
}
}
func TestGetListToken(t *testing.T) {
endpointCalled := false
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/api/v2/tokens",
RawResponse: testListTokensResponseRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})
ctx := context.Background()
testClient, err := client.NewCRaaSClientV2(testutils.TokenID, testEnv.Server.URL+apiV2)
if err != nil {
t.Errorf("got error %s", err)
}
dig := new(int)
*dig = 1
opts := tokenV2.Opts{
Limit: dig,
}
actual, response, err := tokenV2.List(ctx, testClient, opts)
if err != nil {
t.Fatal(err)
}
if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if response == nil {
t.Fatal("expected an HTTP response from the GET method")
}
if !reflect.DeepEqual(expectedListTokenResponse, actual) {
t.Fatalf("expected %#v, but got %#v", expectedListTokenResponse, actual)
}
}
func TestDeleteToken(t *testing.T) {
endpointCalled := false
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: fmt.Sprintf("/api/v2/tokens/%s", testTokenID),
RawResponse: testCreateTokenResponseRaw,
Method: http.MethodDelete,
Status: http.StatusNoContent,
CallFlag: &endpointCalled,
})
ctx := context.Background()
testClient, err := client.NewCRaaSClientV2(testutils.TokenID, testEnv.Server.URL+apiV2)
if err != nil {
t.Errorf("got error %s", err)
}
actual, err := tokenV2.Delete(ctx, testClient, testTokenID)
if err != nil {
t.Fatal(err)
}
if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if actual.StatusCode != http.StatusNoContent {
t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusNoContent, actual.StatusCode)
}
}
func TestPatchToken(t *testing.T) {
endpointCalled := false
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: fmt.Sprintf("/api/v2/tokens/%s", testTokenID),
RawResponse: testPatchTokenResponseRaw,
Method: http.MethodPatch,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})
ctx := context.Background()
testClient, err := client.NewCRaaSClientV2(testutils.TokenID, testEnv.Server.URL+apiV2)
if err != nil {
t.Errorf("got error %s", err)
}
scope := tokenV2.Scope{
ModeRW: true,
RegistryIDs: []string{"888af692-c646-4b76-a234-81ca9b5bcafe", "6303699d-c2cd-40b1-8428-9dcd6cc3d00d"},
AllRegistries: false,
}
exp := tokenV2.Expiration{
ExpiresAt: expiresAt,
}
actual, response, err := tokenV2.Patch(ctx, testClient, testTokenID, "token", scope, exp)
if err != nil {
t.Fatal(err)
}
if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if response.StatusCode != http.StatusOK {
t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusOK, response.StatusCode)
}
if !reflect.DeepEqual(expectedPatchTokenResponse, actual) {
t.Fatalf("expected %#v, but got %#v", expectedPatchTokenResponse, actual)
}
}