Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/testhelper" |
| 10 | ) |
| 11 | |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 12 | // authTokenPost verifies that providing certain AuthOptions and Scope results in an expected JSON structure. |
| 13 | func authTokenPost(t *testing.T, options gophercloud.AuthOptions, scope *Scope, requestJSON string) { |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 14 | setup() |
| 15 | defer teardown() |
| 16 | |
| 17 | client := gophercloud.ServiceClient{ |
| 18 | Endpoint: endpoint(), |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 19 | Options: options, |
Ash Wilson | 053fcb0 | 2014-08-29 08:04:35 -0400 | [diff] [blame] | 20 | TokenID: "12345abcdef", |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
| 24 | testhelper.TestMethod(t, r, "POST") |
| 25 | testhelper.TestHeader(t, r, "Content-Type", "application/json") |
| 26 | testhelper.TestHeader(t, r, "Accept", "application/json") |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 27 | testhelper.TestJSONRequest(t, r, requestJSON) |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 28 | |
| 29 | fmt.Fprintf(w, `{}`) |
| 30 | }) |
| 31 | |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 32 | _, err := Create(&client, scope) |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 33 | if err != nil { |
| 34 | t.Errorf("Create returned an error: %v", err) |
| 35 | } |
| 36 | } |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 37 | |
| 38 | func TestCreateUserIDAndPassword(t *testing.T) { |
| 39 | authTokenPost(t, gophercloud.AuthOptions{UserID: "me", Password: "squirrel!"}, nil, ` |
| 40 | { |
| 41 | "auth": { |
| 42 | "identity": { |
| 43 | "methods": ["password"], |
| 44 | "password": { |
| 45 | "user": { "id": "me", "password": "squirrel!" } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | `) |
| 51 | } |
| 52 | |
| 53 | func TestCreateUsernameDomainIDPassword(t *testing.T) { |
| 54 | authTokenPost(t, gophercloud.AuthOptions{Username: "fakey", Password: "notpassword", DomainID: "abc123"}, nil, ` |
| 55 | { |
| 56 | "auth": { |
| 57 | "identity": { |
| 58 | "methods": ["password"], |
| 59 | "password": { |
| 60 | "user": { |
| 61 | "domain": { |
| 62 | "id": "abc123" |
| 63 | }, |
| 64 | "name": "fakey", |
| 65 | "password": "notpassword" |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | `) |
| 72 | } |
Ash Wilson | d8da9e4 | 2014-08-29 08:01:06 -0400 | [diff] [blame] | 73 | |
| 74 | func TestCreateUsernameDomainNamePassword(t *testing.T) { |
| 75 | authTokenPost(t, gophercloud.AuthOptions{Username: "frank", Password: "swordfish", DomainName: "spork.net"}, nil, ` |
| 76 | { |
| 77 | "auth": { |
| 78 | "identity": { |
| 79 | "methods": ["password"], |
| 80 | "password": { |
| 81 | "user": { |
| 82 | "domain": { |
| 83 | "name": "spork.net" |
| 84 | }, |
| 85 | "name": "frank", |
| 86 | "password": "swordfish" |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | `) |
| 93 | } |
Ash Wilson | 053fcb0 | 2014-08-29 08:04:35 -0400 | [diff] [blame] | 94 | |
| 95 | func TestCreateTokenID(t *testing.T) { |
| 96 | authTokenPost(t, gophercloud.AuthOptions{}, nil, ` |
| 97 | { |
| 98 | "auth": { |
| 99 | "identity": { |
| 100 | "methods": ["token"], |
| 101 | "token": { |
| 102 | "id": "12345abcdef" |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | `) |
| 108 | } |
Ash Wilson | 1fde616 | 2014-08-29 08:13:06 -0400 | [diff] [blame^] | 109 | |
| 110 | func TestCreateProjectIDScope(t *testing.T) { |
| 111 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 112 | scope := &Scope{ProjectID: "123456"} |
| 113 | authTokenPost(t, options, scope, ` |
| 114 | { |
| 115 | "auth": { |
| 116 | "identity": { |
| 117 | "methods": ["password"], |
| 118 | "password": { |
| 119 | "user": { |
| 120 | "id": "fenris", |
| 121 | "password": "g0t0h311" |
| 122 | } |
| 123 | } |
| 124 | }, |
| 125 | "scope": { |
| 126 | "project": { |
| 127 | "id": "123456" |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | `) |
| 133 | } |
| 134 | |
| 135 | func TestCreateDomainIDScope(t *testing.T) { |
| 136 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 137 | scope := &Scope{DomainID: "1000"} |
| 138 | authTokenPost(t, options, scope, ` |
| 139 | { |
| 140 | "auth": { |
| 141 | "identity": { |
| 142 | "methods": ["password"], |
| 143 | "password": { |
| 144 | "user": { |
| 145 | "id": "fenris", |
| 146 | "password": "g0t0h311" |
| 147 | } |
| 148 | } |
| 149 | }, |
| 150 | "scope": { |
| 151 | "domain": { |
| 152 | "id": "1000" |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | `) |
| 158 | } |
| 159 | |
| 160 | func TestCreateProjectNameAndDomainIDScope(t *testing.T) { |
| 161 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 162 | scope := &Scope{ProjectName: "world-domination", DomainID: "1000"} |
| 163 | authTokenPost(t, options, scope, ` |
| 164 | { |
| 165 | "auth": { |
| 166 | "identity": { |
| 167 | "methods": ["password"], |
| 168 | "password": { |
| 169 | "user": { |
| 170 | "id": "fenris", |
| 171 | "password": "g0t0h311" |
| 172 | } |
| 173 | } |
| 174 | }, |
| 175 | "scope": { |
| 176 | "project": { |
| 177 | "domain": { |
| 178 | "id": "1000" |
| 179 | }, |
| 180 | "name": "world-domination" |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | `) |
| 186 | } |
| 187 | |
| 188 | func TestCreateProjectNameAndDomainNameScope(t *testing.T) { |
| 189 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 190 | scope := &Scope{ProjectName: "world-domination", DomainName: "evil-plans"} |
| 191 | authTokenPost(t, options, scope, ` |
| 192 | { |
| 193 | "auth": { |
| 194 | "identity": { |
| 195 | "methods": ["password"], |
| 196 | "password": { |
| 197 | "user": { |
| 198 | "id": "fenris", |
| 199 | "password": "g0t0h311" |
| 200 | } |
| 201 | } |
| 202 | }, |
| 203 | "scope": { |
| 204 | "project": { |
| 205 | "domain": { |
| 206 | "name": "evil-plans" |
| 207 | }, |
| 208 | "name": "world-domination" |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | `) |
| 214 | } |