Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
jrperritt | 475668a | 2015-07-28 21:43:37 -0600 | [diff] [blame] | 4 | "fmt" |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 5 | "testing" |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 6 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame^] | 7 | "github.com/gophercloud/gophercloud" |
| 8 | th "github.com/gophercloud/gophercloud/testhelper" |
| 9 | "github.com/gophercloud/gophercloud/testhelper/client" |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 10 | ) |
| 11 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 12 | func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) CreateResult { |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 13 | th.SetupHTTP() |
| 14 | defer th.TeardownHTTP() |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 15 | HandleTokenPost(t, requestJSON) |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 16 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 17 | return Create(client.ServiceClient(), AuthOptions{options}) |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 18 | } |
| 19 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 20 | func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) { |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 21 | th.SetupHTTP() |
| 22 | defer th.TeardownHTTP() |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 23 | HandleTokenPost(t, "") |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 24 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 25 | actualErr := Create(client.ServiceClient(), AuthOptions{options}).Err |
jrperritt | 475668a | 2015-07-28 21:43:37 -0600 | [diff] [blame] | 26 | th.CheckDeepEquals(t, expectedErr, actualErr) |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 27 | } |
| 28 | |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 29 | func TestCreateWithPassword(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 30 | options := gophercloud.AuthOptions{ |
| 31 | Username: "me", |
| 32 | Password: "swordfish", |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 33 | } |
| 34 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 35 | IsSuccessful(t, tokenPost(t, options, ` |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 36 | { |
| 37 | "auth": { |
| 38 | "passwordCredentials": { |
| 39 | "username": "me", |
| 40 | "password": "swordfish" |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | `)) |
| 45 | } |
| 46 | |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 47 | func TestCreateTokenWithTenantID(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 48 | options := gophercloud.AuthOptions{ |
| 49 | Username: "me", |
| 50 | Password: "opensesame", |
| 51 | TenantID: "fc394f2ab2df4114bde39905f800dc57", |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 54 | IsSuccessful(t, tokenPost(t, options, ` |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 55 | { |
| 56 | "auth": { |
| 57 | "tenantId": "fc394f2ab2df4114bde39905f800dc57", |
| 58 | "passwordCredentials": { |
| 59 | "username": "me", |
| 60 | "password": "opensesame" |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | `)) |
| 65 | } |
| 66 | |
| 67 | func TestCreateTokenWithTenantName(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 68 | options := gophercloud.AuthOptions{ |
| 69 | Username: "me", |
| 70 | Password: "opensesame", |
| 71 | TenantName: "demo", |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 74 | IsSuccessful(t, tokenPost(t, options, ` |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 75 | { |
| 76 | "auth": { |
| 77 | "tenantName": "demo", |
| 78 | "passwordCredentials": { |
| 79 | "username": "me", |
| 80 | "password": "opensesame" |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | `)) |
| 85 | } |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 86 | |
| 87 | func TestProhibitUserID(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 88 | options := gophercloud.AuthOptions{ |
| 89 | Username: "me", |
| 90 | UserID: "1234", |
| 91 | Password: "thing", |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 92 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 93 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 94 | tokenPostErr(t, options, ErrUserIDProvided) |
| 95 | } |
| 96 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 97 | func TestProhibitAPIKey(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 98 | options := gophercloud.AuthOptions{ |
| 99 | Username: "me", |
| 100 | Password: "thing", |
| 101 | APIKey: "123412341234", |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 102 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 103 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 104 | tokenPostErr(t, options, ErrAPIKeyProvided) |
| 105 | } |
| 106 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 107 | func TestProhibitDomainID(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 108 | options := gophercloud.AuthOptions{ |
| 109 | Username: "me", |
| 110 | Password: "thing", |
| 111 | DomainID: "1234", |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 112 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 113 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 114 | tokenPostErr(t, options, ErrDomainIDProvided) |
| 115 | } |
| 116 | |
| 117 | func TestProhibitDomainName(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 118 | options := gophercloud.AuthOptions{ |
| 119 | Username: "me", |
| 120 | Password: "thing", |
| 121 | DomainName: "wat", |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 122 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 123 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 124 | tokenPostErr(t, options, ErrDomainNameProvided) |
| 125 | } |
| 126 | |
| 127 | func TestRequireUsername(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 128 | options := gophercloud.AuthOptions{ |
| 129 | Password: "thing", |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 130 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 131 | |
jrperritt | 475668a | 2015-07-28 21:43:37 -0600 | [diff] [blame] | 132 | tokenPostErr(t, options, fmt.Errorf("You must provide either username/password or tenantID/token values.")) |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 135 | func TestRequirePassword(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 136 | options := gophercloud.AuthOptions{ |
| 137 | Username: "me", |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 138 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 139 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 140 | tokenPostErr(t, options, ErrPasswordRequired) |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 141 | } |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 142 | |
| 143 | func 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 | |
| 150 | func TestGetWithToken(t *testing.T) { |
| 151 | GetIsSuccessful(t, tokenGet(t, "db22caf43c934e6c829087c41ff8d8d6")) |
| 152 | } |