blob: ad1f615fe63c90509177ce8ece80bbc6a10fec15 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Ash Wilsonaa197a92014-10-03 11:38:08 -04002
3import (
Ash Wilsonaa197a92014-10-03 11:38:08 -04004 "testing"
Ash Wilsonaa197a92014-10-03 11:38:08 -04005
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +02006 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02007 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/identity/v2/tokens"
8 th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper"
9 "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper/client"
Ash Wilsonaa197a92014-10-03 11:38:08 -040010)
11
jrperritt3d966162016-06-06 14:08:54 -050012func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) tokens.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)
jrperritt29ae6b32016-04-13 12:59:37 -050016
jrperritt3d966162016-06-06 14:08:54 -050017 return tokens.Create(client.ServiceClient(), 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
jrperritt3d966162016-06-06 14:08:54 -050025 actualErr := tokens.Create(client.ServiceClient(), options).Err
jrperritt29ae6b32016-04-13 12:59:37 -050026 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) {
jrperritt29ae6b32016-04-13 12:59:37 -050030 options := gophercloud.AuthOptions{
31 Username: "me",
32 Password: "swordfish",
33 }
Ash Wilsonaa197a92014-10-03 11:38:08 -040034
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) {
jrperritt29ae6b32016-04-13 12:59:37 -050048 options := gophercloud.AuthOptions{
49 Username: "me",
50 Password: "opensesame",
51 TenantID: "fc394f2ab2df4114bde39905f800dc57",
52 }
Ash Wilson29f23172014-10-03 11:45:06 -040053
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) {
jrperritt29ae6b32016-04-13 12:59:37 -050068 options := gophercloud.AuthOptions{
69 Username: "me",
70 Password: "opensesame",
71 TenantName: "demo",
72 }
Ash Wilson29f23172014-10-03 11:45:06 -040073
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
Ash Wilson27d29e22014-10-03 11:57:14 -040087func TestRequireUsername(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -050088 options := gophercloud.AuthOptions{
89 Password: "thing",
90 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060091
jrperritt29ae6b32016-04-13 12:59:37 -050092 tokenPostErr(t, options, gophercloud.ErrMissingInput{Argument: "Username"})
Ash Wilson27d29e22014-10-03 11:57:14 -040093}
94
jrperritt3d966162016-06-06 14:08:54 -050095func tokenGet(t *testing.T, tokenId string) tokens.GetResult {
hzlouchao04543602015-11-30 18:44:15 +080096 th.SetupHTTP()
97 defer th.TeardownHTTP()
jrperritt29ae6b32016-04-13 12:59:37 -050098 HandleTokenGet(t, tokenId)
jrperritt3d966162016-06-06 14:08:54 -050099 return tokens.Get(client.ServiceClient(), tokenId)
hzlouchao04543602015-11-30 18:44:15 +0800100}
101
102func TestGetWithToken(t *testing.T) {
103 GetIsSuccessful(t, tokenGet(t, "db22caf43c934e6c829087c41ff8d8d6"))
104}