blob: d25c2d7fd09cbd89999a3ab52de9934f5365d574 [file] [log] [blame]
Ash Wilsonaa197a92014-10-03 11:38:08 -04001package tokens
2
3import (
Jon Perrittdb0ae142016-03-13 00:33:41 -06004 "reflect"
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)
Jon Perrittdb0ae142016-03-13 00:33:41 -060016 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
Jon Perrittdb0ae142016-03-13 00:33:41 -060024 actualErr := Create(client.ServiceClient(), options).Err
25 th.CheckDeepEquals(t, reflect.TypeOf(expectedErr), reflect.TypeOf(actualErr))
26}
27
28func TestCreateWithToken(t *testing.T) {
29 options := gophercloud.AuthOptions{
30 TokenID: "cbc36478b0bd8e67e89469c7749d4127",
31 }
32
33 IsSuccessful(t, tokenPost(t, options, `
34 {
35 "auth": {
36 "token": {
37 "id": "cbc36478b0bd8e67e89469c7749d4127"
38 }
39 }
40 }
41 `))
Ash Wilsonaa197a92014-10-03 11:38:08 -040042}
43
Ash Wilsonaa197a92014-10-03 11:38:08 -040044func TestCreateWithPassword(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060045 options := gophercloud.AuthOptions{}
46 options.Username = "me"
47 options.Password = "swordfish"
Ash Wilsonaa197a92014-10-03 11:38:08 -040048
Ash Wilsonc72e3622014-10-10 14:44:19 -040049 IsSuccessful(t, tokenPost(t, options, `
Ash Wilsonaa197a92014-10-03 11:38:08 -040050 {
51 "auth": {
52 "passwordCredentials": {
53 "username": "me",
54 "password": "swordfish"
55 }
56 }
57 }
58 `))
59}
60
Ash Wilson29f23172014-10-03 11:45:06 -040061func TestCreateTokenWithTenantID(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060062 options := gophercloud.AuthOptions{}
63 options.Username = "me"
64 options.Password = "opensesame"
65 options.TenantID = "fc394f2ab2df4114bde39905f800dc57"
Ash Wilson29f23172014-10-03 11:45:06 -040066
Ash Wilsonc72e3622014-10-10 14:44:19 -040067 IsSuccessful(t, tokenPost(t, options, `
Ash Wilson29f23172014-10-03 11:45:06 -040068 {
69 "auth": {
70 "tenantId": "fc394f2ab2df4114bde39905f800dc57",
71 "passwordCredentials": {
72 "username": "me",
73 "password": "opensesame"
74 }
75 }
76 }
77 `))
78}
79
80func TestCreateTokenWithTenantName(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060081 options := gophercloud.AuthOptions{}
82 options.Username = "me"
83 options.Password = "opensesame"
84 options.TenantName = "demo"
Ash Wilson29f23172014-10-03 11:45:06 -040085
Ash Wilsonc72e3622014-10-10 14:44:19 -040086 IsSuccessful(t, tokenPost(t, options, `
Ash Wilson29f23172014-10-03 11:45:06 -040087 {
88 "auth": {
89 "tenantName": "demo",
90 "passwordCredentials": {
91 "username": "me",
92 "password": "opensesame"
93 }
94 }
95 }
96 `))
97}
Ash Wilson27d29e22014-10-03 11:57:14 -040098
Ash Wilson27d29e22014-10-03 11:57:14 -040099func TestRequireUsername(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600100 options := gophercloud.AuthOptions{}
101 options.Password = "thing"
102
Jon Perritta3302e12016-03-07 03:48:59 -0600103 expected := gophercloud.ErrMissingInput{}
Jon Perritta3302e12016-03-07 03:48:59 -0600104 expected.Argument = "tokens.AuthOptions.Username/tokens.AuthOptions.TokenID"
105 expected.Info = "You must provide either username/password or tenantID/token values."
106 tokenPostErr(t, options, expected)
Ash Wilson27d29e22014-10-03 11:57:14 -0400107}
108
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400109func TestRequirePassword(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600110 options := gophercloud.AuthOptions{}
111 options.Username = "me"
112
Jon Perritta3302e12016-03-07 03:48:59 -0600113 expected := gophercloud.ErrMissingInput{}
Jon Perritta3302e12016-03-07 03:48:59 -0600114 expected.Argument = "tokens.AuthOptions.Password"
115 tokenPostErr(t, options, expected)
Ash Wilson27d29e22014-10-03 11:57:14 -0400116}
hzlouchao04543602015-11-30 18:44:15 +0800117
Jon Perritta3302e12016-03-07 03:48:59 -0600118func tokenGet(t *testing.T, tokenID string) GetResult {
hzlouchao04543602015-11-30 18:44:15 +0800119 th.SetupHTTP()
120 defer th.TeardownHTTP()
Jon Perritta3302e12016-03-07 03:48:59 -0600121 HandleTokenGet(t, tokenID)
122 return Get(client.ServiceClient(), tokenID)
hzlouchao04543602015-11-30 18:44:15 +0800123}
124
125func TestGetWithToken(t *testing.T) {
126 GetIsSuccessful(t, tokenGet(t, "db22caf43c934e6c829087c41ff8d8d6"))
127}