jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 1 | package testing |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "net/url" |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 5 | "reflect" |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 6 | "testing" |
| 7 | |
Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame] | 8 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame] | 9 | th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper" |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 10 | ) |
| 11 | |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 12 | func TestMaybeString(t *testing.T) { |
| 13 | testString := "" |
| 14 | var expected *string |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 15 | actual := gophercloud.MaybeString(testString) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 16 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 17 | |
| 18 | testString = "carol" |
| 19 | expected = &testString |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 20 | actual = gophercloud.MaybeString(testString) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 21 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 22 | } |
| 23 | |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 24 | func TestMaybeInt(t *testing.T) { |
| 25 | testInt := 0 |
| 26 | var expected *int |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 27 | actual := gophercloud.MaybeInt(testInt) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 28 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 29 | |
| 30 | testInt = 4 |
| 31 | expected = &testInt |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 32 | actual = gophercloud.MaybeInt(testInt) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 33 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 36 | func TestBuildQueryString(t *testing.T) { |
Jon Perritt | e43f3de | 2015-02-12 11:45:34 -0700 | [diff] [blame] | 37 | type testVar string |
Joe Topjian | 12f19e5 | 2016-11-05 14:35:08 -0600 | [diff] [blame] | 38 | iFalse := false |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 39 | opts := struct { |
Jon Perritt | e43f3de | 2015-02-12 11:45:34 -0700 | [diff] [blame] | 40 | J int `q:"j"` |
| 41 | R string `q:"r,required"` |
| 42 | C bool `q:"c"` |
| 43 | S []string `q:"s"` |
| 44 | TS []testVar `q:"ts"` |
| 45 | TI []int `q:"ti"` |
Joe Topjian | 12f19e5 | 2016-11-05 14:35:08 -0600 | [diff] [blame] | 46 | F *bool `q:"f"` |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 47 | }{ |
Jon Perritt | e43f3de | 2015-02-12 11:45:34 -0700 | [diff] [blame] | 48 | J: 2, |
| 49 | R: "red", |
| 50 | C: true, |
| 51 | S: []string{"one", "two", "three"}, |
| 52 | TS: []testVar{"a", "b"}, |
| 53 | TI: []int{1, 2}, |
Joe Topjian | 12f19e5 | 2016-11-05 14:35:08 -0600 | [diff] [blame] | 54 | F: &iFalse, |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 55 | } |
Joe Topjian | 12f19e5 | 2016-11-05 14:35:08 -0600 | [diff] [blame] | 56 | expected := &url.URL{RawQuery: "c=true&f=false&j=2&r=red&s=one&s=two&s=three&ti=1&ti=2&ts=a&ts=b"} |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 57 | actual, err := gophercloud.BuildQueryString(&opts) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 58 | if err != nil { |
| 59 | t.Errorf("Error building query string: %v", err) |
| 60 | } |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 61 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 62 | |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 63 | opts = struct { |
Jon Perritt | e43f3de | 2015-02-12 11:45:34 -0700 | [diff] [blame] | 64 | J int `q:"j"` |
| 65 | R string `q:"r,required"` |
| 66 | C bool `q:"c"` |
| 67 | S []string `q:"s"` |
| 68 | TS []testVar `q:"ts"` |
| 69 | TI []int `q:"ti"` |
Joe Topjian | 12f19e5 | 2016-11-05 14:35:08 -0600 | [diff] [blame] | 70 | F *bool `q:"f"` |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 71 | }{ |
| 72 | J: 2, |
| 73 | C: true, |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 74 | } |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 75 | _, err = gophercloud.BuildQueryString(&opts) |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 76 | if err == nil { |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 77 | t.Errorf("Expected error: 'Required field not set'") |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 78 | } |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 79 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 80 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 81 | _, err = gophercloud.BuildQueryString(map[string]interface{}{"Number": 4}) |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 82 | if err == nil { |
| 83 | t.Errorf("Expected error: 'Options type is not a struct'") |
| 84 | } |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 85 | } |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 86 | |
| 87 | func TestBuildHeaders(t *testing.T) { |
| 88 | testStruct := struct { |
| 89 | Accept string `h:"Accept"` |
Jon Perritt | dfeb33b | 2014-10-06 16:28:58 -0500 | [diff] [blame] | 90 | Num int `h:"Number,required"` |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 91 | Style bool `h:"Style"` |
| 92 | }{ |
| 93 | Accept: "application/json", |
| 94 | Num: 4, |
| 95 | Style: true, |
| 96 | } |
| 97 | expected := map[string]string{"Accept": "application/json", "Number": "4", "Style": "true"} |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 98 | actual, err := gophercloud.BuildHeaders(&testStruct) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 99 | th.CheckNoErr(t, err) |
| 100 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | dfeb33b | 2014-10-06 16:28:58 -0500 | [diff] [blame] | 101 | |
| 102 | testStruct.Num = 0 |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 103 | _, err = gophercloud.BuildHeaders(&testStruct) |
Jon Perritt | dfeb33b | 2014-10-06 16:28:58 -0500 | [diff] [blame] | 104 | if err == nil { |
| 105 | t.Errorf("Expected error: 'Required header not set'") |
| 106 | } |
| 107 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 108 | _, err = gophercloud.BuildHeaders(map[string]interface{}{"Number": 4}) |
Jon Perritt | dfeb33b | 2014-10-06 16:28:58 -0500 | [diff] [blame] | 109 | if err == nil { |
| 110 | t.Errorf("Expected error: 'Options type is not a struct'") |
| 111 | } |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 114 | func TestQueriesAreEscaped(t *testing.T) { |
| 115 | type foo struct { |
| 116 | Name string `q:"something"` |
| 117 | Shape string `q:"else"` |
| 118 | } |
| 119 | |
| 120 | expected := &url.URL{RawQuery: "else=Triangl+e&something=blah%2B%3F%21%21foo"} |
| 121 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 122 | actual, err := gophercloud.BuildQueryString(foo{Name: "blah+?!!foo", Shape: "Triangl e"}) |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 123 | th.AssertNoErr(t, err) |
| 124 | |
| 125 | th.AssertDeepEquals(t, expected, actual) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 126 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 127 | |
| 128 | func TestBuildRequestBody(t *testing.T) { |
| 129 | type PasswordCredentials struct { |
| 130 | Username string `json:"username" required:"true"` |
| 131 | Password string `json:"password" required:"true"` |
| 132 | } |
| 133 | |
| 134 | type TokenCredentials struct { |
| 135 | ID string `json:"id,omitempty" required:"true"` |
| 136 | } |
| 137 | |
| 138 | type orFields struct { |
| 139 | Filler int `json:"filler,omitempty"` |
| 140 | F1 int `json:"f1,omitempty" or:"F2"` |
| 141 | F2 int `json:"f2,omitempty" or:"F1"` |
| 142 | } |
| 143 | |
| 144 | // AuthOptions wraps a gophercloud AuthOptions in order to adhere to the AuthOptionsBuilder |
| 145 | // interface. |
| 146 | type AuthOptions struct { |
Joe Topjian | fb5c0e8 | 2017-06-10 20:52:58 -0600 | [diff] [blame^] | 147 | PasswordCredentials *PasswordCredentials `json:"passwordCredentials,omitempty" xor:"TokenCredentials"` |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 148 | |
| 149 | // The TenantID and TenantName fields are optional for the Identity V2 API. |
| 150 | // Some providers allow you to specify a TenantName instead of the TenantId. |
| 151 | // Some require both. Your provider's authentication policies will determine |
| 152 | // how these fields influence authentication. |
| 153 | TenantID string `json:"tenantId,omitempty"` |
| 154 | TenantName string `json:"tenantName,omitempty"` |
| 155 | |
| 156 | // TokenCredentials allows users to authenticate (possibly as another user) with an |
| 157 | // authentication token ID. |
Joe Topjian | fb5c0e8 | 2017-06-10 20:52:58 -0600 | [diff] [blame^] | 158 | TokenCredentials *TokenCredentials `json:"token,omitempty" xor:"PasswordCredentials"` |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 159 | |
Joe Topjian | fb5c0e8 | 2017-06-10 20:52:58 -0600 | [diff] [blame^] | 160 | OrFields *orFields `json:"or_fields,omitempty"` |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | var successCases = []struct { |
| 164 | opts AuthOptions |
| 165 | expected map[string]interface{} |
| 166 | }{ |
| 167 | { |
| 168 | AuthOptions{ |
Joe Topjian | fb5c0e8 | 2017-06-10 20:52:58 -0600 | [diff] [blame^] | 169 | PasswordCredentials: &PasswordCredentials{ |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 170 | Username: "me", |
| 171 | Password: "swordfish", |
| 172 | }, |
| 173 | }, |
| 174 | map[string]interface{}{ |
| 175 | "auth": map[string]interface{}{ |
| 176 | "passwordCredentials": map[string]interface{}{ |
| 177 | "password": "swordfish", |
| 178 | "username": "me", |
| 179 | }, |
| 180 | }, |
| 181 | }, |
| 182 | }, |
| 183 | { |
| 184 | AuthOptions{ |
Joe Topjian | fb5c0e8 | 2017-06-10 20:52:58 -0600 | [diff] [blame^] | 185 | TokenCredentials: &TokenCredentials{ |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 186 | ID: "1234567", |
| 187 | }, |
| 188 | }, |
| 189 | map[string]interface{}{ |
| 190 | "auth": map[string]interface{}{ |
| 191 | "token": map[string]interface{}{ |
| 192 | "id": "1234567", |
| 193 | }, |
| 194 | }, |
| 195 | }, |
| 196 | }, |
| 197 | } |
| 198 | |
| 199 | for _, successCase := range successCases { |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 200 | actual, err := gophercloud.BuildRequestBody(successCase.opts, "auth") |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 201 | th.AssertNoErr(t, err) |
| 202 | th.AssertDeepEquals(t, successCase.expected, actual) |
| 203 | } |
| 204 | |
| 205 | var failCases = []struct { |
| 206 | opts AuthOptions |
| 207 | expected error |
| 208 | }{ |
| 209 | { |
| 210 | AuthOptions{ |
| 211 | TenantID: "987654321", |
| 212 | TenantName: "me", |
| 213 | }, |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 214 | gophercloud.ErrMissingInput{}, |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 215 | }, |
| 216 | { |
| 217 | AuthOptions{ |
Joe Topjian | fb5c0e8 | 2017-06-10 20:52:58 -0600 | [diff] [blame^] | 218 | TokenCredentials: &TokenCredentials{ |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 219 | ID: "1234567", |
| 220 | }, |
Joe Topjian | fb5c0e8 | 2017-06-10 20:52:58 -0600 | [diff] [blame^] | 221 | PasswordCredentials: &PasswordCredentials{ |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 222 | Username: "me", |
| 223 | Password: "swordfish", |
| 224 | }, |
| 225 | }, |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 226 | gophercloud.ErrMissingInput{}, |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 227 | }, |
| 228 | { |
| 229 | AuthOptions{ |
Joe Topjian | fb5c0e8 | 2017-06-10 20:52:58 -0600 | [diff] [blame^] | 230 | PasswordCredentials: &PasswordCredentials{ |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 231 | Password: "swordfish", |
| 232 | }, |
| 233 | }, |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 234 | gophercloud.ErrMissingInput{}, |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 235 | }, |
| 236 | { |
| 237 | AuthOptions{ |
Joe Topjian | fb5c0e8 | 2017-06-10 20:52:58 -0600 | [diff] [blame^] | 238 | PasswordCredentials: &PasswordCredentials{ |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 239 | Username: "me", |
| 240 | Password: "swordfish", |
| 241 | }, |
Joe Topjian | fb5c0e8 | 2017-06-10 20:52:58 -0600 | [diff] [blame^] | 242 | OrFields: &orFields{ |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 243 | Filler: 2, |
| 244 | }, |
| 245 | }, |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 246 | gophercloud.ErrMissingInput{}, |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 247 | }, |
| 248 | } |
| 249 | |
| 250 | for _, failCase := range failCases { |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 251 | _, err := gophercloud.BuildRequestBody(failCase.opts, "auth") |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 252 | th.AssertDeepEquals(t, reflect.TypeOf(failCase.expected), reflect.TypeOf(err)) |
| 253 | } |
| 254 | } |