blob: a86075196a6e90d687a6128d9d22b52a903ebab7 [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 Wilsoncde68122014-08-28 16:15:43 -040020 }
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 Wilson417d9222014-08-29 07:58:35 -040026 testhelper.TestJSONRequest(t, r, requestJSON)
Ash Wilsoncde68122014-08-28 16:15:43 -040027
28 fmt.Fprintf(w, `{}`)
29 })
30
Ash Wilson417d9222014-08-29 07:58:35 -040031 _, err := Create(&client, scope)
Ash Wilsoncde68122014-08-28 16:15:43 -040032 if err != nil {
33 t.Errorf("Create returned an error: %v", err)
34 }
35}
Ash Wilson417d9222014-08-29 07:58:35 -040036
37func 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
52func 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}
Ash Wilsond8da9e42014-08-29 08:01:06 -040072
73func TestCreateUsernameDomainNamePassword(t *testing.T) {
74 authTokenPost(t, gophercloud.AuthOptions{Username: "frank", Password: "swordfish", DomainName: "spork.net"}, nil, `
75 {
76 "auth": {
77 "identity": {
78 "methods": ["password"],
79 "password": {
80 "user": {
81 "domain": {
82 "name": "spork.net"
83 },
84 "name": "frank",
85 "password": "swordfish"
86 }
87 }
88 }
89 }
90 }
91 `)
92}