| 1 |
package api_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"net/http" |
| 8 |
"net/http/httptest" |
| 9 |
"net/url" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/CenturyLinkCloud/clc-sdk/api" |
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
) |
| 17 |
|
| 18 |
func TestEnvConfig(t *testing.T) { |
| 19 |
assert := assert.New(t) |
| 20 |
|
| 21 |
os.Setenv("CLC_USERNAME", "user") |
| 22 |
os.Setenv("CLC_PASSWORD", "pass") |
| 23 |
os.Setenv("CLC_ALIAS", "alias") |
| 24 |
os.Setenv("CLC_USER_AGENT", "clc-sdk") |
| 25 |
|
| 26 |
c, err := api.EnvConfig() |
| 27 |
|
| 28 |
assert.Nil(err) |
| 29 |
assert.Equal("user", c.User.Username) |
| 30 |
assert.Equal("pass", c.User.Password) |
| 31 |
assert.Equal("alias", c.Alias) |
| 32 |
assert.Equal("clc-sdk", c.UserAgent) |
| 33 |
} |
| 34 |
|
| 35 |
func clearEnvVars() { |
| 36 |
os.Setenv("CLC_USERNAME", "") |
| 37 |
os.Setenv("CLC_PASSWORD", "") |
| 38 |
os.Setenv("CLC_ALIAS", "") |
| 39 |
os.Setenv("CLC_BASE_URL", "") |
| 40 |
os.Setenv("CLC_USER_AGENT", "") |
| 41 |
} |
| 42 |
|
| 43 |
func TestInvalidEnvConfig(t *testing.T) { |
| 44 |
assert := assert.New(t) |
| 45 |
|
| 46 |
clearEnvVars() |
| 47 |
os.Setenv("CLC_USERNAME", "user") |
| 48 |
os.Setenv("CLC_PASSWORD", "") |
| 49 |
|
| 50 |
_, err := api.EnvConfig() |
| 51 |
|
| 52 |
assert.NotNil(err) |
| 53 |
} |
| 54 |
|
| 55 |
func TestNewConfigWithNoUrl(t *testing.T) { |
| 56 |
assert := assert.New(t) |
| 57 |
|
| 58 |
clearEnvVars() |
| 59 |
c, err := api.NewConfig("user", "pass") |
| 60 |
|
| 61 |
u, _ := url.Parse("https://api.ctl.io/v2") |
| 62 |
|
| 63 |
assert.Nil(err) |
| 64 |
assert.Equal("user", c.User.Username) |
| 65 |
assert.Equal("pass", c.User.Password) |
| 66 |
assert.Equal("", c.Alias) |
| 67 |
assert.Equal(u, c.BaseURL) |
| 68 |
} |
| 69 |
|
| 70 |
func TestNewConfigWithUrl(t *testing.T) { |
| 71 |
assert := assert.New(t) |
| 72 |
|
| 73 |
clearEnvVars() |
| 74 |
|
| 75 |
alt := "https://api.other.io/v2" |
| 76 |
os.Setenv("CLC_BASE_URL", alt) |
| 77 |
|
| 78 |
c, err := api.NewConfig("user", "pass") |
| 79 |
|
| 80 |
u, _ := url.Parse(alt) |
| 81 |
|
| 82 |
assert.Nil(err) |
| 83 |
assert.Equal("user", c.User.Username) |
| 84 |
assert.Equal("pass", c.User.Password) |
| 85 |
assert.Equal("", c.Alias) |
| 86 |
assert.Equal(u, c.BaseURL) |
| 87 |
} |
| 88 |
|
| 89 |
func TestFileConfig(t *testing.T) { |
| 90 |
assert := assert.New(t) |
| 91 |
|
| 92 |
file, err := ioutil.TempFile("", "tmp") |
| 93 |
assert.Nil(err) |
| 94 |
|
| 95 |
clearEnvVars() |
| 96 |
os.Setenv("CLC_ALIAS", "alias") |
| 97 |
os.Setenv("CLC_USER_AGENT", "some-sdk-client") |
| 98 |
conf, err := api.NewConfig("user", "pass") |
| 99 |
|
| 100 |
assert.Nil(err) |
| 101 |
b, _ := json.Marshal(conf) |
| 102 |
|
| 103 |
assert.Nil(ioutil.WriteFile(file.Name(), b, 755)) |
| 104 |
|
| 105 |
c, err := api.FileConfig(file.Name()) |
| 106 |
|
| 107 |
assert.Nil(err) |
| 108 |
assert.Equal("user", c.User.Username) |
| 109 |
assert.Equal("pass", c.User.Password) |
| 110 |
assert.Equal("alias", c.Alias) |
| 111 |
assert.Equal("some-sdk-client", c.UserAgent) |
| 112 |
|
| 113 |
file.Close() |
| 114 |
os.Remove(file.Name()) |
| 115 |
} |
| 116 |
|
| 117 |
func TestNewClient(t *testing.T) { |
| 118 |
assert := assert.New(t) |
| 119 |
|
| 120 |
config := api.Config{ |
| 121 |
User: api.User{Username: "user", Password: "password"}, |
| 122 |
} |
| 123 |
client := api.New(config) |
| 124 |
|
| 125 |
assert.NotNil(client) |
| 126 |
assert.Equal(config.User, client.Config().User) |
| 127 |
} |
| 128 |
|
| 129 |
func TestAuth(t *testing.T) { |
| 130 |
assert := assert.New(t) |
| 131 |
|
| 132 |
actualUser := &api.User{} |
| 133 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 134 |
if r.Method != "POST" { |
| 135 |
http.Error(w, "no", http.StatusMethodNotAllowed) |
| 136 |
return |
| 137 |
} |
| 138 |
|
| 139 |
json.NewDecoder(r.Body).Decode(actualUser) |
| 140 |
|
| 141 |
fmt.Fprintf(w, `{"userName":"[email protected]","accountAlias":"ALIAS","locationAlias":"DC1","roles":["AccountAdmin","ServerAdmin"],"bearerToken":"[LONG TOKEN VALUE]"}`) |
| 142 |
})) |
| 143 |
defer ts.Close() |
| 144 |
|
| 145 |
config := genConfig(ts) |
| 146 |
client := api.New(config) |
| 147 |
err := client.Auth() |
| 148 |
|
| 149 |
assert.Nil(err) |
| 150 |
assert.Equal(config.User.Username, actualUser.Username) |
| 151 |
assert.Equal(config.User.Password, actualUser.Password) |
| 152 |
assert.Equal("ALIAS", client.Config().Alias) |
| 153 |
} |
| 154 |
|
| 155 |
func TestAuthAlaias(t *testing.T) { |
| 156 |
assert := assert.New(t) |
| 157 |
|
| 158 |
actualUser := &api.User{} |
| 159 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 160 |
if r.Method != "POST" { |
| 161 |
http.Error(w, "no", http.StatusMethodNotAllowed) |
| 162 |
return |
| 163 |
} |
| 164 |
|
| 165 |
json.NewDecoder(r.Body).Decode(actualUser) |
| 166 |
|
| 167 |
fmt.Fprintf(w, `{"userName":"[email protected]","accountAlias":"ALIAS","locationAlias":"DC1","roles":["AccountAdmin","ServerAdmin"],"bearerToken":"[LONG TOKEN VALUE]"}`) |
| 168 |
})) |
| 169 |
defer ts.Close() |
| 170 |
|
| 171 |
config := genConfig(ts) |
| 172 |
|
| 173 |
config.Alias = "ABCD" |
| 174 |
client := api.New(config) |
| 175 |
err := client.Auth() |
| 176 |
|
| 177 |
assert.Nil(err) |
| 178 |
assert.Equal("ABCD", client.Config().Alias) |
| 179 |
} |
| 180 |
|
| 181 |
func TestDoWithAuth(t *testing.T) { |
| 182 |
assert := assert.New(t) |
| 183 |
|
| 184 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 185 |
if r.Method == "POST" && strings.HasSuffix(r.URL.RequestURI(), "login") { |
| 186 |
fmt.Fprintf(w, `{"userName":"[email protected]","accountAlias":"ALIAS","locationAlias":"DC1","roles":["AccountAdmin","ServerAdmin"],"bearerToken":"[LONG TOKEN VALUE]"}`) |
| 187 |
} |
| 188 |
})) |
| 189 |
defer ts.Close() |
| 190 |
|
| 191 |
config := genConfig(ts) |
| 192 |
client := api.New(config) |
| 193 |
err := client.DoWithAuth("GET", ts.URL, nil, nil) |
| 194 |
|
| 195 |
assert.Nil(err) |
| 196 |
assert.Equal("ALIAS", client.Config().Alias) |
| 197 |
} |
| 198 |
|
| 199 |
func TestAuth_SerializationErr(t *testing.T) { |
| 200 |
assert := assert.New(t) |
| 201 |
|
| 202 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 203 |
http.Error(w, "nope", http.StatusInternalServerError) |
| 204 |
})) |
| 205 |
defer ts.Close() |
| 206 |
|
| 207 |
config := genConfig(ts) |
| 208 |
client := api.New(config) |
| 209 |
err := client.Auth() |
| 210 |
|
| 211 |
assert.NotNil(err) |
| 212 |
} |
| 213 |
|
| 214 |
func TestGet(t *testing.T) { |
| 215 |
assert := assert.New(t) |
| 216 |
|
| 217 |
status := "ok" |
| 218 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 219 |
if r.Header.Get("Accept") != "application/json" { |
| 220 |
http.Error(w, "accept missing", http.StatusBadRequest) |
| 221 |
return |
| 222 |
} |
| 223 |
|
| 224 |
if r.Header.Get("Content-Type") != "" { |
| 225 |
http.Error(w, "content-type should not be present", http.StatusBadRequest) |
| 226 |
return |
| 227 |
} |
| 228 |
|
| 229 |
if r.Header.Get("User-Agent") != "sdk-client" { |
| 230 |
http.Error(w, "user-agent mismatch", http.StatusBadRequest) |
| 231 |
return |
| 232 |
} |
| 233 |
|
| 234 |
if r.Method != "GET" { |
| 235 |
http.Error(w, "no", http.StatusMethodNotAllowed) |
| 236 |
return |
| 237 |
} |
| 238 |
|
| 239 |
fmt.Fprintf(w, `{"status": "%s"}`, status) |
| 240 |
})) |
| 241 |
defer ts.Close() |
| 242 |
|
| 243 |
client := api.New(mockConfig()) |
| 244 |
client.Token = api.Token{Token: "valid"} |
| 245 |
|
| 246 |
resp := &Response{} |
| 247 |
err := client.Get(ts.URL, resp) |
| 248 |
|
| 249 |
assert.Nil(err) |
| 250 |
assert.Equal(status, resp.Status) |
| 251 |
} |
| 252 |
|
| 253 |
func TestPost(t *testing.T) { |
| 254 |
assert := assert.New(t) |
| 255 |
|
| 256 |
status := "ok" |
| 257 |
actualReq := &Request{} |
| 258 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 259 |
if r.Header.Get("Accept") != "application/json" { |
| 260 |
http.Error(w, "accept missing", http.StatusBadRequest) |
| 261 |
return |
| 262 |
} |
| 263 |
|
| 264 |
if r.Header.Get("Content-Type") != "application/json" { |
| 265 |
http.Error(w, "content-type missing", http.StatusBadRequest) |
| 266 |
return |
| 267 |
} |
| 268 |
if r.Method != "POST" { |
| 269 |
http.Error(w, "no", http.StatusMethodNotAllowed) |
| 270 |
return |
| 271 |
} |
| 272 |
|
| 273 |
json.NewDecoder(r.Body).Decode(actualReq) |
| 274 |
|
| 275 |
fmt.Fprintf(w, `{"status": "%s"}`, status) |
| 276 |
})) |
| 277 |
defer ts.Close() |
| 278 |
|
| 279 |
client := api.New(mockConfig()) |
| 280 |
client.Token = api.Token{Token: "valid"} |
| 281 |
|
| 282 |
req := &Request{Status: "do stuff"} |
| 283 |
resp := &Response{} |
| 284 |
err := client.Post(ts.URL, req, resp) |
| 285 |
|
| 286 |
assert.Nil(err) |
| 287 |
assert.Equal(req, actualReq) |
| 288 |
assert.Equal(status, resp.Status) |
| 289 |
} |
| 290 |
|
| 291 |
func TestPut(t *testing.T) { |
| 292 |
assert := assert.New(t) |
| 293 |
|
| 294 |
status := "ok" |
| 295 |
actualReq := &Request{} |
| 296 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 297 |
if r.Header.Get("Accept") != "application/json" { |
| 298 |
http.Error(w, "accept missing", http.StatusBadRequest) |
| 299 |
return |
| 300 |
} |
| 301 |
|
| 302 |
if r.Header.Get("Content-Type") != "application/json" { |
| 303 |
http.Error(w, "content-type missing", http.StatusBadRequest) |
| 304 |
return |
| 305 |
} |
| 306 |
|
| 307 |
if r.Header.Get("User-Agent") != "sdk-client" { |
| 308 |
http.Error(w, "user-agent mismatch", http.StatusBadRequest) |
| 309 |
return |
| 310 |
} |
| 311 |
|
| 312 |
if r.Method != "PUT" { |
| 313 |
http.Error(w, "no", http.StatusMethodNotAllowed) |
| 314 |
return |
| 315 |
} |
| 316 |
|
| 317 |
json.NewDecoder(r.Body).Decode(actualReq) |
| 318 |
|
| 319 |
fmt.Fprintf(w, `{"status": "%s"}`, status) |
| 320 |
})) |
| 321 |
defer ts.Close() |
| 322 |
|
| 323 |
client := api.New(mockConfig()) |
| 324 |
client.Token = api.Token{Token: "valid"} |
| 325 |
|
| 326 |
req := &Request{Status: "do stuff"} |
| 327 |
resp := &Response{} |
| 328 |
err := client.Put(ts.URL, req, resp) |
| 329 |
|
| 330 |
assert.Nil(err) |
| 331 |
assert.Equal(req, actualReq) |
| 332 |
assert.Equal(status, resp.Status) |
| 333 |
} |
| 334 |
|
| 335 |
func TestPatch(t *testing.T) { |
| 336 |
assert := assert.New(t) |
| 337 |
|
| 338 |
status := "ok" |
| 339 |
actualReq := &Request{} |
| 340 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 341 |
if r.Header.Get("Accept") != "application/json" { |
| 342 |
http.Error(w, "accept missing", http.StatusBadRequest) |
| 343 |
return |
| 344 |
} |
| 345 |
|
| 346 |
if r.Header.Get("Content-Type") != "application/json" { |
| 347 |
http.Error(w, "content-type missing", http.StatusBadRequest) |
| 348 |
return |
| 349 |
} |
| 350 |
|
| 351 |
if r.Header.Get("User-Agent") != "sdk-client" { |
| 352 |
http.Error(w, "user-agent mismatch", http.StatusBadRequest) |
| 353 |
return |
| 354 |
} |
| 355 |
|
| 356 |
if r.Method != "PATCH" { |
| 357 |
http.Error(w, "no", http.StatusMethodNotAllowed) |
| 358 |
return |
| 359 |
} |
| 360 |
|
| 361 |
json.NewDecoder(r.Body).Decode(actualReq) |
| 362 |
|
| 363 |
fmt.Fprintf(w, `{"status": "%s"}`, status) |
| 364 |
})) |
| 365 |
defer ts.Close() |
| 366 |
|
| 367 |
client := api.New(mockConfig()) |
| 368 |
client.Token = api.Token{Token: "valid"} |
| 369 |
|
| 370 |
req := &Request{Status: "do stuff"} |
| 371 |
resp := &Response{} |
| 372 |
err := client.Patch(ts.URL, req, resp) |
| 373 |
|
| 374 |
assert.Nil(err) |
| 375 |
assert.Equal(req, actualReq) |
| 376 |
assert.Equal(status, resp.Status) |
| 377 |
} |
| 378 |
|
| 379 |
func TestDelete(t *testing.T) { |
| 380 |
assert := assert.New(t) |
| 381 |
|
| 382 |
status := "ok" |
| 383 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 384 |
if r.Header.Get("Accept") != "application/json" { |
| 385 |
http.Error(w, "accept missing", http.StatusBadRequest) |
| 386 |
return |
| 387 |
} |
| 388 |
|
| 389 |
if r.Header.Get("Content-Type") != "" { |
| 390 |
http.Error(w, "content-type should not be present", http.StatusBadRequest) |
| 391 |
return |
| 392 |
} |
| 393 |
|
| 394 |
if r.Header.Get("User-Agent") != "sdk-client" { |
| 395 |
http.Error(w, "user-agent mismatch", http.StatusBadRequest) |
| 396 |
return |
| 397 |
} |
| 398 |
|
| 399 |
if r.Method != "DELETE" { |
| 400 |
http.Error(w, "no", http.StatusMethodNotAllowed) |
| 401 |
return |
| 402 |
} |
| 403 |
|
| 404 |
fmt.Fprintf(w, `{"status": "%s"}`, status) |
| 405 |
})) |
| 406 |
defer ts.Close() |
| 407 |
|
| 408 |
client := api.New(mockConfig()) |
| 409 |
client.Token = api.Token{Token: "valid"} |
| 410 |
|
| 411 |
resp := &Response{} |
| 412 |
err := client.Delete(ts.URL, resp) |
| 413 |
|
| 414 |
assert.Nil(err) |
| 415 |
assert.Equal(status, resp.Status) |
| 416 |
} |
| 417 |
|
| 418 |
func genConfig(ts *httptest.Server) api.Config { |
| 419 |
u, _ := url.Parse(ts.URL) |
| 420 |
config := api.Config{ |
| 421 |
User: api.User{ |
| 422 |
Username: "user.name", |
| 423 |
Password: "password", |
| 424 |
}, |
| 425 |
BaseURL: u, |
| 426 |
} |
| 427 |
return config |
| 428 |
} |
| 429 |
|
| 430 |
func mockConfig() api.Config { |
| 431 |
return api.Config{ |
| 432 |
User: api.User{ |
| 433 |
Username: "user.name", |
| 434 |
Password: "password", |
| 435 |
}, |
| 436 |
Alias: "t3bk", |
| 437 |
UserAgent: "sdk-client", |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
type Response struct { |
| 442 |
Status string `json:"status"` |
| 443 |
} |
| 444 |
|
| 445 |
type Request struct { |
| 446 |
Status string `json:"status"` |
| 447 |
} |