blob: 307f3678dc09d6f6e63f39b622cfc427418eec08 [file] [log] [blame]
Ash Wilsonaa197a92014-10-03 11:38:08 -04001package tokens
2
3import (
jrperritt475668a2015-07-28 21:43:37 -06004 "fmt"
Ash Wilsonaa197a92014-10-03 11:38:08 -04005 "testing"
Ash Wilsonaa197a92014-10-03 11:38:08 -04006
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
8 th "github.com/gophercloud/gophercloud/testhelper"
9 "github.com/gophercloud/gophercloud/testhelper/client"
Ash Wilsonaa197a92014-10-03 11:38:08 -040010)
11
Ash Wilsonc72e3622014-10-10 14:44:19 -040012func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) CreateResult {
Ash Wilsonaa197a92014-10-03 11:38:08 -040013 th.SetupHTTP()
14 defer th.TeardownHTTP()
Ash Wilsonc72e3622014-10-10 14:44:19 -040015 HandleTokenPost(t, requestJSON)
Ash Wilsonaa197a92014-10-03 11:38:08 -040016
Ash Wilsonc72e3622014-10-10 14:44:19 -040017 return Create(client.ServiceClient(), AuthOptions{options})
Ash Wilsonaa197a92014-10-03 11:38:08 -040018}
19
Ash Wilsonc72e3622014-10-10 14:44:19 -040020func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) {
Ash Wilsonaa197a92014-10-03 11:38:08 -040021 th.SetupHTTP()
22 defer th.TeardownHTTP()
Ash Wilsonc72e3622014-10-10 14:44:19 -040023 HandleTokenPost(t, "")
Ash Wilsonaa197a92014-10-03 11:38:08 -040024
Ash Wilsonc72e3622014-10-10 14:44:19 -040025 actualErr := Create(client.ServiceClient(), AuthOptions{options}).Err
jrperritt475668a2015-07-28 21:43:37 -060026 th.CheckDeepEquals(t, expectedErr, actualErr)
Ash Wilsonaa197a92014-10-03 11:38:08 -040027}
28
Ash Wilsonaa197a92014-10-03 11:38:08 -040029func TestCreateWithPassword(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -040030 options := gophercloud.AuthOptions{
31 Username: "me",
32 Password: "swordfish",
Ash Wilsonaa197a92014-10-03 11:38:08 -040033 }
34
Ash Wilsonc72e3622014-10-10 14:44:19 -040035 IsSuccessful(t, tokenPost(t, options, `
Ash Wilsonaa197a92014-10-03 11:38:08 -040036 {
37 "auth": {
38 "passwordCredentials": {
39 "username": "me",
40 "password": "swordfish"
41 }
42 }
43 }
44 `))
45}
46
Ash Wilson29f23172014-10-03 11:45:06 -040047func TestCreateTokenWithTenantID(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -040048 options := gophercloud.AuthOptions{
49 Username: "me",
50 Password: "opensesame",
51 TenantID: "fc394f2ab2df4114bde39905f800dc57",
Ash Wilson29f23172014-10-03 11:45:06 -040052 }
53
Ash Wilsonc72e3622014-10-10 14:44:19 -040054 IsSuccessful(t, tokenPost(t, options, `
Ash Wilson29f23172014-10-03 11:45:06 -040055 {
56 "auth": {
57 "tenantId": "fc394f2ab2df4114bde39905f800dc57",
58 "passwordCredentials": {
59 "username": "me",
60 "password": "opensesame"
61 }
62 }
63 }
64 `))
65}
66
67func TestCreateTokenWithTenantName(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -040068 options := gophercloud.AuthOptions{
69 Username: "me",
70 Password: "opensesame",
71 TenantName: "demo",
Ash Wilson29f23172014-10-03 11:45:06 -040072 }
73
Ash Wilsonc72e3622014-10-10 14:44:19 -040074 IsSuccessful(t, tokenPost(t, options, `
Ash Wilson29f23172014-10-03 11:45:06 -040075 {
76 "auth": {
77 "tenantName": "demo",
78 "passwordCredentials": {
79 "username": "me",
80 "password": "opensesame"
81 }
82 }
83 }
84 `))
85}
Ash Wilson27d29e22014-10-03 11:57:14 -040086
87func TestProhibitUserID(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -040088 options := gophercloud.AuthOptions{
89 Username: "me",
90 UserID: "1234",
91 Password: "thing",
Ash Wilson27d29e22014-10-03 11:57:14 -040092 }
Ash Wilsonc72e3622014-10-10 14:44:19 -040093
Ash Wilson27d29e22014-10-03 11:57:14 -040094 tokenPostErr(t, options, ErrUserIDProvided)
95}
96
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040097func TestProhibitAPIKey(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -040098 options := gophercloud.AuthOptions{
99 Username: "me",
100 Password: "thing",
101 APIKey: "123412341234",
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400102 }
Ash Wilsonc72e3622014-10-10 14:44:19 -0400103
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400104 tokenPostErr(t, options, ErrAPIKeyProvided)
105}
106
Ash Wilson27d29e22014-10-03 11:57:14 -0400107func TestProhibitDomainID(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -0400108 options := gophercloud.AuthOptions{
109 Username: "me",
110 Password: "thing",
111 DomainID: "1234",
Ash Wilson27d29e22014-10-03 11:57:14 -0400112 }
Ash Wilsonc72e3622014-10-10 14:44:19 -0400113
Ash Wilson27d29e22014-10-03 11:57:14 -0400114 tokenPostErr(t, options, ErrDomainIDProvided)
115}
116
117func TestProhibitDomainName(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -0400118 options := gophercloud.AuthOptions{
119 Username: "me",
120 Password: "thing",
121 DomainName: "wat",
Ash Wilson27d29e22014-10-03 11:57:14 -0400122 }
Ash Wilsonc72e3622014-10-10 14:44:19 -0400123
Ash Wilson27d29e22014-10-03 11:57:14 -0400124 tokenPostErr(t, options, ErrDomainNameProvided)
125}
126
127func TestRequireUsername(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -0400128 options := gophercloud.AuthOptions{
129 Password: "thing",
Ash Wilson27d29e22014-10-03 11:57:14 -0400130 }
Ash Wilsonc72e3622014-10-10 14:44:19 -0400131
jrperritt475668a2015-07-28 21:43:37 -0600132 tokenPostErr(t, options, fmt.Errorf("You must provide either username/password or tenantID/token values."))
Ash Wilson27d29e22014-10-03 11:57:14 -0400133}
134
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400135func TestRequirePassword(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -0400136 options := gophercloud.AuthOptions{
137 Username: "me",
Ash Wilson27d29e22014-10-03 11:57:14 -0400138 }
Ash Wilsonc72e3622014-10-10 14:44:19 -0400139
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400140 tokenPostErr(t, options, ErrPasswordRequired)
Ash Wilson27d29e22014-10-03 11:57:14 -0400141}
hzlouchao04543602015-11-30 18:44:15 +0800142
143func tokenGet(t *testing.T, tokenId string) GetResult {
144 th.SetupHTTP()
145 defer th.TeardownHTTP()
146 HandleTokenGet(t, tokenId)
147 return Get(client.ServiceClient(), tokenId)
148}
149
150func TestGetWithToken(t *testing.T) {
151 GetIsSuccessful(t, tokenGet(t, "db22caf43c934e6c829087c41ff8d8d6"))
152}