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