Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 4 | "testing" |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud" |
| 7 | th "github.com/gophercloud/gophercloud/testhelper" |
| 8 | "github.com/gophercloud/gophercloud/testhelper/client" |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 9 | ) |
| 10 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 11 | func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) CreateResult { |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 12 | th.SetupHTTP() |
| 13 | defer th.TeardownHTTP() |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 14 | HandleTokenPost(t, requestJSON) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 15 | |
jrperritt | 64d0ef0 | 2016-04-13 13:10:04 -0500 | [diff] [blame] | 16 | return Create(client.ServiceClient(), options) |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 17 | } |
| 18 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 19 | func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) { |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 20 | th.SetupHTTP() |
| 21 | defer th.TeardownHTTP() |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 22 | HandleTokenPost(t, "") |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 23 | |
jrperritt | 64d0ef0 | 2016-04-13 13:10:04 -0500 | [diff] [blame] | 24 | actualErr := Create(client.ServiceClient(), options).Err |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 25 | th.CheckDeepEquals(t, expectedErr, actualErr) |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 26 | } |
| 27 | |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 28 | func TestCreateWithPassword(t *testing.T) { |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 29 | options := gophercloud.AuthOptions{ |
| 30 | Username: "me", |
| 31 | Password: "swordfish", |
| 32 | } |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 33 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 34 | IsSuccessful(t, tokenPost(t, options, ` |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 35 | { |
| 36 | "auth": { |
| 37 | "passwordCredentials": { |
| 38 | "username": "me", |
| 39 | "password": "swordfish" |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | `)) |
| 44 | } |
| 45 | |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 46 | func TestCreateTokenWithTenantID(t *testing.T) { |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 47 | options := gophercloud.AuthOptions{ |
| 48 | Username: "me", |
| 49 | Password: "opensesame", |
| 50 | TenantID: "fc394f2ab2df4114bde39905f800dc57", |
| 51 | } |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 52 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 53 | IsSuccessful(t, tokenPost(t, options, ` |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 54 | { |
| 55 | "auth": { |
| 56 | "tenantId": "fc394f2ab2df4114bde39905f800dc57", |
| 57 | "passwordCredentials": { |
| 58 | "username": "me", |
| 59 | "password": "opensesame" |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | `)) |
| 64 | } |
| 65 | |
| 66 | func TestCreateTokenWithTenantName(t *testing.T) { |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 67 | options := gophercloud.AuthOptions{ |
| 68 | Username: "me", |
| 69 | Password: "opensesame", |
| 70 | TenantName: "demo", |
| 71 | } |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 72 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 73 | IsSuccessful(t, tokenPost(t, options, ` |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 74 | { |
| 75 | "auth": { |
| 76 | "tenantName": "demo", |
| 77 | "passwordCredentials": { |
| 78 | "username": "me", |
| 79 | "password": "opensesame" |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | `)) |
| 84 | } |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 85 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 86 | func TestRequireUsername(t *testing.T) { |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 87 | options := gophercloud.AuthOptions{ |
| 88 | Password: "thing", |
| 89 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 90 | |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 91 | tokenPostErr(t, options, gophercloud.ErrMissingInput{Argument: "Username"}) |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 92 | } |
| 93 | |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 94 | func tokenGet(t *testing.T, tokenId string) GetResult { |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 95 | th.SetupHTTP() |
| 96 | defer th.TeardownHTTP() |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 97 | HandleTokenGet(t, tokenId) |
| 98 | return Get(client.ServiceClient(), tokenId) |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func TestGetWithToken(t *testing.T) { |
| 102 | GetIsSuccessful(t, tokenGet(t, "db22caf43c934e6c829087c41ff8d8d6")) |
| 103 | } |