blob: a6d16f3de187ab8030c91920c5a33d8275369348 [file] [log] [blame]
Ash Wilsonaa197a92014-10-03 11:38:08 -04001package tokens
2
3import (
Ash Wilsonaa197a92014-10-03 11:38:08 -04004 "testing"
Ash Wilsonaa197a92014-10-03 11:38:08 -04005
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
7 th "github.com/gophercloud/gophercloud/testhelper"
8 "github.com/gophercloud/gophercloud/testhelper/client"
Ash Wilsonaa197a92014-10-03 11:38:08 -04009)
10
Ash Wilsonc72e3622014-10-10 14:44:19 -040011func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) CreateResult {
Ash Wilsonaa197a92014-10-03 11:38:08 -040012 th.SetupHTTP()
13 defer th.TeardownHTTP()
Ash Wilsonc72e3622014-10-10 14:44:19 -040014 HandleTokenPost(t, requestJSON)
jrperritt29ae6b32016-04-13 12:59:37 -050015
jrperritt64d0ef02016-04-13 13:10:04 -050016 return Create(client.ServiceClient(), options)
Ash Wilsonaa197a92014-10-03 11:38:08 -040017}
18
Ash Wilsonc72e3622014-10-10 14:44:19 -040019func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) {
Ash Wilsonaa197a92014-10-03 11:38:08 -040020 th.SetupHTTP()
21 defer th.TeardownHTTP()
Ash Wilsonc72e3622014-10-10 14:44:19 -040022 HandleTokenPost(t, "")
Ash Wilsonaa197a92014-10-03 11:38:08 -040023
jrperritt64d0ef02016-04-13 13:10:04 -050024 actualErr := Create(client.ServiceClient(), options).Err
jrperritt29ae6b32016-04-13 12:59:37 -050025 th.CheckDeepEquals(t, expectedErr, actualErr)
Ash Wilsonaa197a92014-10-03 11:38:08 -040026}
27
Ash Wilsonaa197a92014-10-03 11:38:08 -040028func TestCreateWithPassword(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -050029 options := gophercloud.AuthOptions{
30 Username: "me",
31 Password: "swordfish",
32 }
Ash Wilsonaa197a92014-10-03 11:38:08 -040033
Ash Wilsonc72e3622014-10-10 14:44:19 -040034 IsSuccessful(t, tokenPost(t, options, `
Ash Wilsonaa197a92014-10-03 11:38:08 -040035 {
36 "auth": {
37 "passwordCredentials": {
38 "username": "me",
39 "password": "swordfish"
40 }
41 }
42 }
43 `))
44}
45
Ash Wilson29f23172014-10-03 11:45:06 -040046func TestCreateTokenWithTenantID(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -050047 options := gophercloud.AuthOptions{
48 Username: "me",
49 Password: "opensesame",
50 TenantID: "fc394f2ab2df4114bde39905f800dc57",
51 }
Ash Wilson29f23172014-10-03 11:45:06 -040052
Ash Wilsonc72e3622014-10-10 14:44:19 -040053 IsSuccessful(t, tokenPost(t, options, `
Ash Wilson29f23172014-10-03 11:45:06 -040054 {
55 "auth": {
56 "tenantId": "fc394f2ab2df4114bde39905f800dc57",
57 "passwordCredentials": {
58 "username": "me",
59 "password": "opensesame"
60 }
61 }
62 }
63 `))
64}
65
66func TestCreateTokenWithTenantName(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -050067 options := gophercloud.AuthOptions{
68 Username: "me",
69 Password: "opensesame",
70 TenantName: "demo",
71 }
Ash Wilson29f23172014-10-03 11:45:06 -040072
Ash Wilsonc72e3622014-10-10 14:44:19 -040073 IsSuccessful(t, tokenPost(t, options, `
Ash Wilson29f23172014-10-03 11:45:06 -040074 {
75 "auth": {
76 "tenantName": "demo",
77 "passwordCredentials": {
78 "username": "me",
79 "password": "opensesame"
80 }
81 }
82 }
83 `))
84}
Ash Wilson27d29e22014-10-03 11:57:14 -040085
Ash Wilson27d29e22014-10-03 11:57:14 -040086func TestRequireUsername(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -050087 options := gophercloud.AuthOptions{
88 Password: "thing",
89 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060090
jrperritt29ae6b32016-04-13 12:59:37 -050091 tokenPostErr(t, options, gophercloud.ErrMissingInput{Argument: "Username"})
Ash Wilson27d29e22014-10-03 11:57:14 -040092}
93
jrperritt29ae6b32016-04-13 12:59:37 -050094func tokenGet(t *testing.T, tokenId string) GetResult {
hzlouchao04543602015-11-30 18:44:15 +080095 th.SetupHTTP()
96 defer th.TeardownHTTP()
jrperritt29ae6b32016-04-13 12:59:37 -050097 HandleTokenGet(t, tokenId)
98 return Get(client.ServiceClient(), tokenId)
hzlouchao04543602015-11-30 18:44:15 +080099}
100
101func TestGetWithToken(t *testing.T) {
102 GetIsSuccessful(t, tokenGet(t, "db22caf43c934e6c829087c41ff8d8d6"))
103}