blob: 9b3273ef1710d776887883d1dbfe2e7231da8964 [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)
Ash Wilsonaa197a92014-10-03 11:38:08 -040015
Ash Wilsonc72e3622014-10-10 14:44:19 -040016 return Create(client.ServiceClient(), AuthOptions{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
Ash Wilsonc72e3622014-10-10 14:44:19 -040024 actualErr := Create(client.ServiceClient(), AuthOptions{options}).Err
jrperritt475668a2015-07-28 21:43:37 -060025 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) {
Ash Wilsonc72e3622014-10-10 14:44:19 -040029 options := gophercloud.AuthOptions{
30 Username: "me",
31 Password: "swordfish",
Ash Wilsonaa197a92014-10-03 11:38:08 -040032 }
33
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) {
Ash Wilsonc72e3622014-10-10 14:44:19 -040047 options := gophercloud.AuthOptions{
48 Username: "me",
49 Password: "opensesame",
50 TenantID: "fc394f2ab2df4114bde39905f800dc57",
Ash Wilson29f23172014-10-03 11:45:06 -040051 }
52
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) {
Ash Wilsonc72e3622014-10-10 14:44:19 -040067 options := gophercloud.AuthOptions{
68 Username: "me",
69 Password: "opensesame",
70 TenantName: "demo",
Ash Wilson29f23172014-10-03 11:45:06 -040071 }
72
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
86func TestProhibitUserID(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -040087 options := gophercloud.AuthOptions{
88 Username: "me",
89 UserID: "1234",
90 Password: "thing",
Ash Wilson27d29e22014-10-03 11:57:14 -040091 }
Ash Wilsonc72e3622014-10-10 14:44:19 -040092
Ash Wilson27d29e22014-10-03 11:57:14 -040093 tokenPostErr(t, options, ErrUserIDProvided)
94}
95
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040096func TestProhibitAPIKey(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -040097 options := gophercloud.AuthOptions{
98 Username: "me",
99 Password: "thing",
100 APIKey: "123412341234",
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400101 }
Ash Wilsonc72e3622014-10-10 14:44:19 -0400102
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400103 tokenPostErr(t, options, ErrAPIKeyProvided)
104}
105
Ash Wilson27d29e22014-10-03 11:57:14 -0400106func TestProhibitDomainID(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -0400107 options := gophercloud.AuthOptions{
108 Username: "me",
109 Password: "thing",
110 DomainID: "1234",
Ash Wilson27d29e22014-10-03 11:57:14 -0400111 }
Ash Wilsonc72e3622014-10-10 14:44:19 -0400112
Ash Wilson27d29e22014-10-03 11:57:14 -0400113 tokenPostErr(t, options, ErrDomainIDProvided)
114}
115
116func TestProhibitDomainName(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -0400117 options := gophercloud.AuthOptions{
118 Username: "me",
119 Password: "thing",
120 DomainName: "wat",
Ash Wilson27d29e22014-10-03 11:57:14 -0400121 }
Ash Wilsonc72e3622014-10-10 14:44:19 -0400122
Ash Wilson27d29e22014-10-03 11:57:14 -0400123 tokenPostErr(t, options, ErrDomainNameProvided)
124}
125
126func TestRequireUsername(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -0400127 options := gophercloud.AuthOptions{
128 Password: "thing",
Ash Wilson27d29e22014-10-03 11:57:14 -0400129 }
Jon Perritta3302e12016-03-07 03:48:59 -0600130 expected := gophercloud.ErrMissingInput{}
131 expected.Function = "tokens.ToTokenCreateMap"
132 expected.Argument = "tokens.AuthOptions.Username/tokens.AuthOptions.TokenID"
133 expected.Info = "You must provide either username/password or tenantID/token values."
134 tokenPostErr(t, options, expected)
Ash Wilson27d29e22014-10-03 11:57:14 -0400135}
136
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400137func TestRequirePassword(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -0400138 options := gophercloud.AuthOptions{
139 Username: "me",
Ash Wilson27d29e22014-10-03 11:57:14 -0400140 }
Jon Perritta3302e12016-03-07 03:48:59 -0600141 expected := gophercloud.ErrMissingInput{}
142 expected.Function = "tokens.ToTokenCreateMap"
143 expected.Argument = "tokens.AuthOptions.Password"
144 tokenPostErr(t, options, expected)
Ash Wilson27d29e22014-10-03 11:57:14 -0400145}
hzlouchao04543602015-11-30 18:44:15 +0800146
Jon Perritta3302e12016-03-07 03:48:59 -0600147func tokenGet(t *testing.T, tokenID string) GetResult {
hzlouchao04543602015-11-30 18:44:15 +0800148 th.SetupHTTP()
149 defer th.TeardownHTTP()
Jon Perritta3302e12016-03-07 03:48:59 -0600150 HandleTokenGet(t, tokenID)
151 return Get(client.ServiceClient(), tokenID)
hzlouchao04543602015-11-30 18:44:15 +0800152}
153
154func TestGetWithToken(t *testing.T) {
155 GetIsSuccessful(t, tokenGet(t, "db22caf43c934e6c829087c41ff8d8d6"))
156}