-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmclient_test.go
71 lines (57 loc) · 1.64 KB
/
wmclient_test.go
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
package main
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"github.com/lazynomad/waste-management/restclient"
)
// Mock HTTP client that implements the interface defined in the restclient package.
type mockHTTPClient struct {
}
var (
// Used to call mockHTTPClient Do function.
// This function is used to define the test action when an HTTP call is executed.
doFunc func(req *http.Request) (*http.Response, error)
)
// Implementation method of mockHTTPClient following the interface defined in restclient package.
func (client *mockHTTPClient) Do(req *http.Request) (*http.Response, error) {
return doFunc(req)
}
// Tests the flow of actions to fetch the Auth token
func TestGetAuthToken(t *testing.T) {
testClient := getTestWmclient()
doFunc = func(*http.Request) (*http.Response, error) {
testStr := `{
"statusCode":200,
"data": {
"access_token":"test.token.sign",
"id":"1a2b3c4d5e6f"
}}`
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(testStr)),
}, nil
}
token, err := testClient.GetAuthToken()
if (err != nil) {
t.Errorf("Failed with error" + err.Error())
}
if (token != "test.token.sign") {
t.Errorf("Wrong token" + token)
}
}
// Gets a wmclient stub with dummy configs over mock HTTP client
func getTestWmclient() WMClient {
conf := config {
BaseURL: "https://test.url",
}
conf.Auth.User = "user"
conf.Auth.Pass = "pass"
conf.APIKeys.Auth = "1234567890"
conf.APIKeys.Account = "1234567890"
conf.APIKeys.Service = "1234567890"
HTTPClient := new(mockHTTPClient)
restClient := restclient.NewRestClient(HTTPClient)
return NewWmClient(conf, restClient)
}