blob: 2f02825a47aa2fbcecaef68738745fbeabd86626 [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
6 "github.com/rackspace/gophercloud"
Ash Wilsonaa197a92014-10-03 11:38:08 -04007 th "github.com/rackspace/gophercloud/testhelper"
Ash Wilsonc72e3622014-10-10 14:44:19 -04008 "github.com/rackspace/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
Ash Wilsonaa197a92014-10-03 11:38:08 -040025 th.CheckEquals(t, expectedErr, actualErr)
26}
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 }
Ash Wilsonc72e3622014-10-10 14:44:19 -0400130
Ash Wilson27d29e22014-10-03 11:57:14 -0400131 tokenPostErr(t, options, ErrUsernameRequired)
132}
133
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400134func TestRequirePassword(t *testing.T) {
Ash Wilsonc72e3622014-10-10 14:44:19 -0400135 options := gophercloud.AuthOptions{
136 Username: "me",
Ash Wilson27d29e22014-10-03 11:57:14 -0400137 }
Ash Wilsonc72e3622014-10-10 14:44:19 -0400138
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400139 tokenPostErr(t, options, ErrPasswordRequired)
Ash Wilson27d29e22014-10-03 11:57:14 -0400140}