@@ -3,11 +3,13 @@ package dbaas
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
+ "fmt"
6
7
"net/http"
7
8
"testing"
8
9
9
10
"github.com/jarcoal/httpmock"
10
11
"github.com/stretchr/testify/assert"
12
+ "github.com/stretchr/testify/require"
11
13
)
12
14
13
15
const aclID = "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4"
@@ -16,7 +18,7 @@ const testACLNotFoundResponse = `{
16
18
"error": {
17
19
"code": 404,
18
20
"title": "Not Found",
19
- "message": "acl 123 not found."
21
+ "message": "acl %s not found."
20
22
}
21
23
}`
22
24
@@ -119,7 +121,7 @@ func TestACLs(t *testing.T) {
119
121
testClient := SetupTestClient ()
120
122
defer httpmock .DeactivateAndReset ()
121
123
122
- httpmock .RegisterResponder ("GET" , testClient .Endpoint + "/acls" ,
124
+ httpmock .RegisterResponder ("GET" , testClient .Endpoint + ACLsURI ,
123
125
httpmock .NewStringResponder (200 , testACLsResponse ))
124
126
125
127
expected := []ACL {
@@ -153,50 +155,49 @@ func TestACLs(t *testing.T) {
153
155
154
156
actual , err := testClient .ACLs (context .Background (), nil )
155
157
156
- if assert .NoError (t , err ) {
157
- assert .Equal (t , expected , actual )
158
- }
158
+ require .NoError (t , err )
159
+ assert .Equal (t , expected , actual )
159
160
}
160
161
161
162
func TestACL (t * testing.T ) {
162
163
httpmock .Activate ()
163
164
testClient := SetupTestClient ()
164
165
defer httpmock .DeactivateAndReset ()
165
166
166
- httpmock .RegisterResponder ("GET" , testClient .Endpoint + "/acls /"+ aclID ,
167
+ httpmock .RegisterResponder ("GET" , testClient .Endpoint + ACLsURI + " /"+ aclID ,
167
168
httpmock .NewStringResponder (200 , testACLResponse ))
168
169
169
170
actual , err := testClient .ACL (context .Background (), aclID )
170
171
171
- if assert .NoError (t , err ) {
172
- assert .Equal (t , ACLExpected , actual )
173
- }
172
+ require .NoError (t , err )
173
+ assert .Equal (t , ACLExpected , actual )
174
174
}
175
175
176
176
func TestACLNotFound (t * testing.T ) {
177
177
httpmock .Activate ()
178
178
testClient := SetupTestClient ()
179
179
defer httpmock .DeactivateAndReset ()
180
180
181
- httpmock .RegisterResponder ("GET" , testClient .Endpoint + "/acls/123" ,
182
- httpmock .NewStringResponder (404 , testACLNotFoundResponse ))
181
+ notFoundResponse := fmt .Sprintf (testACLNotFoundResponse , NotFoundEntityID )
182
+ httpmock .RegisterResponder ("GET" , testClient .Endpoint + ACLsURI + "/" + NotFoundEntityID ,
183
+ httpmock .NewStringResponder (404 , notFoundResponse ))
183
184
184
185
expected := & DBaaSAPIError {}
185
186
expected .APIError .Code = 404
186
187
expected .APIError .Title = ErrorNotFoundTitle
187
- expected .APIError .Message = "acl 123 not found."
188
+ expected .APIError .Message = fmt . Sprintf ( "acl %s not found." , NotFoundEntityID )
188
189
189
- _ , err := testClient .ACL (context .Background (), "123" )
190
+ _ , err := testClient .ACL (context .Background (), NotFoundEntityID )
190
191
191
- assert .ErrorAs (t , err , & expected )
192
+ require .ErrorAs (t , err , & expected )
192
193
}
193
194
194
195
func TestCreateACL (t * testing.T ) {
195
196
httpmock .Activate ()
196
197
testClient := SetupTestClient ()
197
198
defer httpmock .DeactivateAndReset ()
198
199
199
- httpmock .RegisterResponder ("POST" , testClient .Endpoint + "/acls" ,
200
+ httpmock .RegisterResponder ("POST" , testClient .Endpoint + ACLsURI ,
200
201
func (req * http.Request ) (* http.Response , error ) {
201
202
if err := json .NewDecoder (req .Body ).Decode (& ACLCreateOpts {}); err != nil {
202
203
return httpmock .NewStringResponse (400 , "" ), err
@@ -228,17 +229,16 @@ func TestCreateACL(t *testing.T) {
228
229
229
230
ACLCreateExpected := ACLExpected
230
231
ACLCreateExpected .Status = StatusPendingCreate
231
- if assert .NoError (t , err ) {
232
- assert .Equal (t , ACLCreateExpected , actual )
233
- }
232
+ require .NoError (t , err )
233
+ assert .Equal (t , ACLCreateExpected , actual )
234
234
}
235
235
236
236
func TestCreateACLInvalidDatastoreID (t * testing.T ) {
237
237
httpmock .Activate ()
238
238
testClient := SetupTestClient ()
239
239
defer httpmock .DeactivateAndReset ()
240
240
241
- httpmock .RegisterResponder ("POST" , testClient .Endpoint + "/acls" ,
241
+ httpmock .RegisterResponder ("POST" , testClient .Endpoint + ACLsURI ,
242
242
httpmock .NewStringResponder (400 , testCreateACLInvalidDatastoreIDResponse ))
243
243
244
244
expected := & DBaaSAPIError {}
@@ -258,15 +258,15 @@ func TestCreateACLInvalidDatastoreID(t *testing.T) {
258
258
259
259
_ , err := testClient .CreateACL (context .Background (), createACLOpts )
260
260
261
- assert .ErrorAs (t , err , & expected )
261
+ require .ErrorAs (t , err , & expected )
262
262
}
263
263
264
264
func TestUpdateACL (t * testing.T ) {
265
265
httpmock .Activate ()
266
266
testClient := SetupTestClient ()
267
267
defer httpmock .DeactivateAndReset ()
268
268
269
- httpmock .RegisterResponder ("PUT" , testClient .Endpoint + "/acls /"+ aclID ,
269
+ httpmock .RegisterResponder ("PUT" , testClient .Endpoint + ACLsURI + " /"+ aclID ,
270
270
func (req * http.Request ) (* http.Response , error ) {
271
271
if err := json .NewDecoder (req .Body ).Decode (& ACLUpdateOpts {}); err != nil {
272
272
return httpmock .NewStringResponse (400 , "" ), err
@@ -294,17 +294,16 @@ func TestUpdateACL(t *testing.T) {
294
294
295
295
ACLUpdateExpexted := ACLExpected
296
296
ACLUpdateExpexted .Status = StatusPendingUpdate
297
- if assert .NoError (t , err ) {
298
- assert .Equal (t , ACLUpdateExpexted , actual )
299
- }
297
+ require .NoError (t , err )
298
+ assert .Equal (t , ACLUpdateExpexted , actual )
300
299
}
301
300
302
301
func TestUpdateACLInvalidResponse (t * testing.T ) {
303
302
httpmock .Activate ()
304
303
testClient := SetupTestClient ()
305
304
defer httpmock .DeactivateAndReset ()
306
305
307
- httpmock .RegisterResponder ("PUT" , testClient .Endpoint + "/acls /"+ aclID ,
306
+ httpmock .RegisterResponder ("PUT" , testClient .Endpoint + ACLsURI + " /"+ aclID ,
308
307
httpmock .NewStringResponder (400 , testUpdateACLInvalidResponse ))
309
308
310
309
expected := & DBaaSAPIError {}
@@ -320,5 +319,5 @@ func TestUpdateACLInvalidResponse(t *testing.T) {
320
319
321
320
_ , err := testClient .UpdateACL (context .Background (), aclID , updateACLOpts )
322
321
323
- assert .ErrorAs (t , err , & expected )
322
+ require .ErrorAs (t , err , & expected )
324
323
}
0 commit comments