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 | } |