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 | |
| 6 | "github.com/rackspace/gophercloud" |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 7 | th "github.com/rackspace/gophercloud/testhelper" |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 8 | "github.com/rackspace/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) |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 15 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 16 | return Create(client.ServiceClient(), AuthOptions{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 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 24 | actualErr := Create(client.ServiceClient(), AuthOptions{options}).Err |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 25 | th.CheckEquals(t, expectedErr, actualErr) |
| 26 | } |
| 27 | |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 28 | func TestCreateWithPassword(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 29 | options := gophercloud.AuthOptions{ |
| 30 | Username: "me", |
| 31 | Password: "swordfish", |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 32 | } |
| 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) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 47 | options := gophercloud.AuthOptions{ |
| 48 | Username: "me", |
| 49 | Password: "opensesame", |
| 50 | TenantID: "fc394f2ab2df4114bde39905f800dc57", |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 51 | } |
| 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) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 67 | options := gophercloud.AuthOptions{ |
| 68 | Username: "me", |
| 69 | Password: "opensesame", |
| 70 | TenantName: "demo", |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 71 | } |
| 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 | |
| 86 | func TestProhibitUserID(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 87 | options := gophercloud.AuthOptions{ |
| 88 | Username: "me", |
| 89 | UserID: "1234", |
| 90 | Password: "thing", |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 91 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 92 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 93 | tokenPostErr(t, options, ErrUserIDProvided) |
| 94 | } |
| 95 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 96 | func TestProhibitAPIKey(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 97 | options := gophercloud.AuthOptions{ |
| 98 | Username: "me", |
| 99 | Password: "thing", |
| 100 | APIKey: "123412341234", |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 101 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 102 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 103 | tokenPostErr(t, options, ErrAPIKeyProvided) |
| 104 | } |
| 105 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 106 | func TestProhibitDomainID(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 107 | options := gophercloud.AuthOptions{ |
| 108 | Username: "me", |
| 109 | Password: "thing", |
| 110 | DomainID: "1234", |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 111 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 112 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 113 | tokenPostErr(t, options, ErrDomainIDProvided) |
| 114 | } |
| 115 | |
| 116 | func TestProhibitDomainName(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 117 | options := gophercloud.AuthOptions{ |
| 118 | Username: "me", |
| 119 | Password: "thing", |
| 120 | DomainName: "wat", |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 121 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 122 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 123 | tokenPostErr(t, options, ErrDomainNameProvided) |
| 124 | } |
| 125 | |
| 126 | func TestRequireUsername(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 127 | options := gophercloud.AuthOptions{ |
| 128 | Password: "thing", |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 129 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 130 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 131 | tokenPostErr(t, options, ErrUsernameRequired) |
| 132 | } |
| 133 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 134 | func TestRequirePassword(t *testing.T) { |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 135 | options := gophercloud.AuthOptions{ |
| 136 | Username: "me", |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 137 | } |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame^] | 138 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 139 | tokenPostErr(t, options, ErrPasswordRequired) |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 140 | } |