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 | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
| 23 | testhelper.TestMethod(t, r, "POST") |
| 24 | testhelper.TestHeader(t, r, "Content-Type", "application/json") |
| 25 | testhelper.TestHeader(t, r, "Accept", "application/json") |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame^] | 26 | testhelper.TestJSONRequest(t, r, requestJSON) |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 27 | |
| 28 | fmt.Fprintf(w, `{}`) |
| 29 | }) |
| 30 | |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame^] | 31 | _, err := Create(&client, scope) |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 32 | if err != nil { |
| 33 | t.Errorf("Create returned an error: %v", err) |
| 34 | } |
| 35 | } |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame^] | 36 | |
| 37 | func TestCreateUserIDAndPassword(t *testing.T) { |
| 38 | authTokenPost(t, gophercloud.AuthOptions{UserID: "me", Password: "squirrel!"}, nil, ` |
| 39 | { |
| 40 | "auth": { |
| 41 | "identity": { |
| 42 | "methods": ["password"], |
| 43 | "password": { |
| 44 | "user": { "id": "me", "password": "squirrel!" } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | `) |
| 50 | } |
| 51 | |
| 52 | func TestCreateUsernameDomainIDPassword(t *testing.T) { |
| 53 | authTokenPost(t, gophercloud.AuthOptions{Username: "fakey", Password: "notpassword", DomainID: "abc123"}, nil, ` |
| 54 | { |
| 55 | "auth": { |
| 56 | "identity": { |
| 57 | "methods": ["password"], |
| 58 | "password": { |
| 59 | "user": { |
| 60 | "domain": { |
| 61 | "id": "abc123" |
| 62 | }, |
| 63 | "name": "fakey", |
| 64 | "password": "notpassword" |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | `) |
| 71 | } |