Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 4 | "reflect" |
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) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [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 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 24 | actualErr := Create(client.ServiceClient(), options).Err |
| 25 | th.CheckDeepEquals(t, reflect.TypeOf(expectedErr), reflect.TypeOf(actualErr)) |
| 26 | } |
| 27 | |
| 28 | func 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 Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 44 | func TestCreateWithPassword(t *testing.T) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 45 | options := gophercloud.AuthOptions{} |
| 46 | options.Username = "me" |
| 47 | options.Password = "swordfish" |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 48 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 49 | IsSuccessful(t, tokenPost(t, options, ` |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 50 | { |
| 51 | "auth": { |
| 52 | "passwordCredentials": { |
| 53 | "username": "me", |
| 54 | "password": "swordfish" |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | `)) |
| 59 | } |
| 60 | |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 61 | func TestCreateTokenWithTenantID(t *testing.T) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 62 | options := gophercloud.AuthOptions{} |
| 63 | options.Username = "me" |
| 64 | options.Password = "opensesame" |
| 65 | options.TenantID = "fc394f2ab2df4114bde39905f800dc57" |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 66 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 67 | IsSuccessful(t, tokenPost(t, options, ` |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 68 | { |
| 69 | "auth": { |
| 70 | "tenantId": "fc394f2ab2df4114bde39905f800dc57", |
| 71 | "passwordCredentials": { |
| 72 | "username": "me", |
| 73 | "password": "opensesame" |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | `)) |
| 78 | } |
| 79 | |
| 80 | func TestCreateTokenWithTenantName(t *testing.T) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 81 | options := gophercloud.AuthOptions{} |
| 82 | options.Username = "me" |
| 83 | options.Password = "opensesame" |
| 84 | options.TenantName = "demo" |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 85 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 86 | IsSuccessful(t, tokenPost(t, options, ` |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 87 | { |
| 88 | "auth": { |
| 89 | "tenantName": "demo", |
| 90 | "passwordCredentials": { |
| 91 | "username": "me", |
| 92 | "password": "opensesame" |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | `)) |
| 97 | } |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 98 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 99 | func TestRequireUsername(t *testing.T) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 100 | options := gophercloud.AuthOptions{} |
| 101 | options.Password = "thing" |
| 102 | |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 103 | expected := gophercloud.ErrMissingInput{} |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 104 | 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 Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 109 | func TestRequirePassword(t *testing.T) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 110 | options := gophercloud.AuthOptions{} |
| 111 | options.Username = "me" |
| 112 | |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 113 | expected := gophercloud.ErrMissingInput{} |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 114 | expected.Argument = "tokens.AuthOptions.Password" |
| 115 | tokenPostErr(t, options, expected) |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 116 | } |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 117 | |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 118 | func tokenGet(t *testing.T, tokenID string) GetResult { |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 119 | th.SetupHTTP() |
| 120 | defer th.TeardownHTTP() |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 121 | HandleTokenGet(t, tokenID) |
| 122 | return Get(client.ServiceClient(), tokenID) |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | func TestGetWithToken(t *testing.T) { |
| 126 | GetIsSuccessful(t, tokenGet(t, "db22caf43c934e6c829087c41ff8d8d6")) |
| 127 | } |