blob: f95e897b2dab12f3a959a839d5382b7289dbfc50 [file] [log] [blame]
Ash Wilsoncde68122014-08-28 16:15:43 -04001package tokens
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/testhelper"
10)
11
Ash Wilson417d9222014-08-29 07:58:35 -040012// authTokenPost verifies that providing certain AuthOptions and Scope results in an expected JSON structure.
13func authTokenPost(t *testing.T, options gophercloud.AuthOptions, scope *Scope, requestJSON string) {
Ash Wilsoncde68122014-08-28 16:15:43 -040014 setup()
15 defer teardown()
16
17 client := gophercloud.ServiceClient{
18 Endpoint: endpoint(),
Ash Wilson417d9222014-08-29 07:58:35 -040019 Options: options,
Ash Wilson053fcb02014-08-29 08:04:35 -040020 TokenID: "12345abcdef",
Ash Wilsoncde68122014-08-28 16:15:43 -040021 }
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 Wilson417d9222014-08-29 07:58:35 -040027 testhelper.TestJSONRequest(t, r, requestJSON)
Ash Wilsoncde68122014-08-28 16:15:43 -040028
29 fmt.Fprintf(w, `{}`)
30 })
31
Ash Wilson417d9222014-08-29 07:58:35 -040032 _, err := Create(&client, scope)
Ash Wilsoncde68122014-08-28 16:15:43 -040033 if err != nil {
34 t.Errorf("Create returned an error: %v", err)
35 }
36}
Ash Wilson417d9222014-08-29 07:58:35 -040037
38func 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
53func 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 Wilsond8da9e42014-08-29 08:01:06 -040073
74func 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 Wilson053fcb02014-08-29 08:04:35 -040094
95func 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}