Refactor tokens to use build tag fixtures.
diff --git a/openstack/identity/v2/tokens/fixtures.go b/openstack/identity/v2/tokens/fixtures.go
new file mode 100644
index 0000000..1cb0d05
--- /dev/null
+++ b/openstack/identity/v2/tokens/fixtures.go
@@ -0,0 +1,128 @@
+// +build fixtures
+
+package tokens
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+ "time"
+
+ "github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
+ th "github.com/rackspace/gophercloud/testhelper"
+)
+
+// ExpectedToken is the token that should be parsed from TokenCreationResponse.
+var ExpectedToken = &Token{
+ ID: "aaaabbbbccccdddd",
+ ExpiresAt: time.Date(2014, time.January, 31, 15, 30, 58, 0, time.UTC),
+ Tenant: tenants.Tenant{
+ ID: "fc394f2ab2df4114bde39905f800dc57",
+ Name: "test",
+ Description: "There are many tenants. This one is yours.",
+ Enabled: true,
+ },
+}
+
+// ExpectedServiceCatalog is the service catalog that should be parsed from TokenCreationResponse.
+var ExpectedServiceCatalog = &ServiceCatalog{
+ Entries: []CatalogEntry{
+ CatalogEntry{
+ Name: "inscrutablewalrus",
+ Type: "something",
+ Endpoints: []Endpoint{
+ Endpoint{
+ PublicURL: "http://something0:1234/v2/",
+ Region: "region0",
+ },
+ Endpoint{
+ PublicURL: "http://something1:1234/v2/",
+ Region: "region1",
+ },
+ },
+ },
+ CatalogEntry{
+ Name: "arbitrarypenguin",
+ Type: "else",
+ Endpoints: []Endpoint{
+ Endpoint{
+ PublicURL: "http://else0:4321/v3/",
+ Region: "region0",
+ },
+ },
+ },
+ },
+}
+
+// TokenCreationResponse is a JSON response that contains ExpectedToken and ExpectedServiceCatalog.
+const TokenCreationResponse = `
+{
+ "access": {
+ "token": {
+ "issued_at": "2014-01-30T15:30:58.000000Z",
+ "expires": "2014-01-31T15:30:58Z",
+ "id": "aaaabbbbccccdddd",
+ "tenant": {
+ "description": "There are many tenants. This one is yours.",
+ "enabled": true,
+ "id": "fc394f2ab2df4114bde39905f800dc57",
+ "name": "test"
+ }
+ },
+ "serviceCatalog": [
+ {
+ "endpoints": [
+ {
+ "publicURL": "http://something0:1234/v2/",
+ "region": "region0"
+ },
+ {
+ "publicURL": "http://something1:1234/v2/",
+ "region": "region1"
+ }
+ ],
+ "type": "something",
+ "name": "inscrutablewalrus"
+ },
+ {
+ "endpoints": [
+ {
+ "publicURL": "http://else0:4321/v3/",
+ "region": "region0"
+ }
+ ],
+ "type": "else",
+ "name": "arbitrarypenguin"
+ }
+ ]
+ }
+}
+`
+
+// HandleTokenPost expects a POST against a /tokens handler, ensures that the request body has been
+// constructed properly given certain auth options, and returns the result.
+func HandleTokenPost(t *testing.T, requestJSON string) {
+ th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ if requestJSON != "" {
+ th.TestJSONRequest(t, r, requestJSON)
+ }
+
+ w.WriteHeader(http.StatusOK)
+ fmt.Fprintf(w, TokenCreationResponse)
+ })
+}
+
+// IsSuccessful ensures that a CreateResult was successful and contains the correct token and
+// service catalog.
+func IsSuccessful(t *testing.T, result CreateResult) {
+ token, err := result.ExtractToken()
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, ExpectedToken, token)
+
+ serviceCatalog, err := result.ExtractServiceCatalog()
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, ExpectedServiceCatalog, serviceCatalog)
+}
diff --git a/openstack/identity/v2/tokens/requests_test.go b/openstack/identity/v2/tokens/requests_test.go
index b6476df..2f02825 100644
--- a/openstack/identity/v2/tokens/requests_test.go
+++ b/openstack/identity/v2/tokens/requests_test.go
@@ -1,155 +1,37 @@
package tokens
import (
- "fmt"
- "net/http"
"testing"
- "time"
"github.com/rackspace/gophercloud"
- "github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
)
-var expectedToken = &Token{
- ID: "aaaabbbbccccdddd",
- ExpiresAt: time.Date(2014, time.January, 31, 15, 30, 58, 0, time.UTC),
- Tenant: tenants.Tenant{
- ID: "fc394f2ab2df4114bde39905f800dc57",
- Name: "test",
- Description: "There are many tenants. This one is yours.",
- Enabled: true,
- },
-}
-
-var expectedServiceCatalog = &ServiceCatalog{
- Entries: []CatalogEntry{
- CatalogEntry{
- Name: "inscrutablewalrus",
- Type: "something",
- Endpoints: []Endpoint{
- Endpoint{
- PublicURL: "http://something0:1234/v2/",
- Region: "region0",
- },
- Endpoint{
- PublicURL: "http://something1:1234/v2/",
- Region: "region1",
- },
- },
- },
- CatalogEntry{
- Name: "arbitrarypenguin",
- Type: "else",
- Endpoints: []Endpoint{
- Endpoint{
- PublicURL: "http://else0:4321/v3/",
- Region: "region0",
- },
- },
- },
- },
-}
-
-func tokenPost(t *testing.T, options AuthOptions, requestJSON string) CreateResult {
+func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) CreateResult {
th.SetupHTTP()
defer th.TeardownHTTP()
+ HandleTokenPost(t, requestJSON)
- client := gophercloud.ServiceClient{Endpoint: th.Endpoint()}
-
- th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "Content-Type", "application/json")
- th.TestHeader(t, r, "Accept", "application/json")
- th.TestJSONRequest(t, r, requestJSON)
-
- w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, `
-{
- "access": {
- "token": {
- "issued_at": "2014-01-30T15:30:58.000000Z",
- "expires": "2014-01-31T15:30:58Z",
- "id": "aaaabbbbccccdddd",
- "tenant": {
- "description": "There are many tenants. This one is yours.",
- "enabled": true,
- "id": "fc394f2ab2df4114bde39905f800dc57",
- "name": "test"
- }
- },
- "serviceCatalog": [
- {
- "endpoints": [
- {
- "publicURL": "http://something0:1234/v2/",
- "region": "region0"
- },
- {
- "publicURL": "http://something1:1234/v2/",
- "region": "region1"
- }
- ],
- "type": "something",
- "name": "inscrutablewalrus"
- },
- {
- "endpoints": [
- {
- "publicURL": "http://else0:4321/v3/",
- "region": "region0"
- }
- ],
- "type": "else",
- "name": "arbitrarypenguin"
- }
- ]
- }
-}
- `)
- })
-
- return Create(&client, options)
+ return Create(client.ServiceClient(), AuthOptions{options})
}
-func tokenPostErr(t *testing.T, options AuthOptions, expectedErr error) {
+func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) {
th.SetupHTTP()
defer th.TeardownHTTP()
+ HandleTokenPost(t, "")
- client := gophercloud.ServiceClient{Endpoint: th.Endpoint()}
-
- th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "Content-Type", "application/json")
- th.TestHeader(t, r, "Accept", "application/json")
-
- w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, `{}`)
- })
-
- actualErr := Create(&client, options).Err
+ actualErr := Create(client.ServiceClient(), AuthOptions{options}).Err
th.CheckEquals(t, expectedErr, actualErr)
}
-func isSuccessful(t *testing.T, result CreateResult) {
- token, err := result.ExtractToken()
- th.AssertNoErr(t, err)
- th.CheckDeepEquals(t, expectedToken, token)
-
- serviceCatalog, err := result.ExtractServiceCatalog()
- th.AssertNoErr(t, err)
- th.CheckDeepEquals(t, expectedServiceCatalog, serviceCatalog)
-}
-
func TestCreateWithPassword(t *testing.T) {
- options := AuthOptions{
- gophercloud.AuthOptions{
- Username: "me",
- Password: "swordfish",
- },
+ options := gophercloud.AuthOptions{
+ Username: "me",
+ Password: "swordfish",
}
- isSuccessful(t, tokenPost(t, options, `
+ IsSuccessful(t, tokenPost(t, options, `
{
"auth": {
"passwordCredentials": {
@@ -162,15 +44,13 @@
}
func TestCreateTokenWithTenantID(t *testing.T) {
- options := AuthOptions{
- gophercloud.AuthOptions{
- Username: "me",
- Password: "opensesame",
- TenantID: "fc394f2ab2df4114bde39905f800dc57",
- },
+ options := gophercloud.AuthOptions{
+ Username: "me",
+ Password: "opensesame",
+ TenantID: "fc394f2ab2df4114bde39905f800dc57",
}
- isSuccessful(t, tokenPost(t, options, `
+ IsSuccessful(t, tokenPost(t, options, `
{
"auth": {
"tenantId": "fc394f2ab2df4114bde39905f800dc57",
@@ -184,15 +64,13 @@
}
func TestCreateTokenWithTenantName(t *testing.T) {
- options := AuthOptions{
- gophercloud.AuthOptions{
- Username: "me",
- Password: "opensesame",
- TenantName: "demo",
- },
+ options := gophercloud.AuthOptions{
+ Username: "me",
+ Password: "opensesame",
+ TenantName: "demo",
}
- isSuccessful(t, tokenPost(t, options, `
+ IsSuccessful(t, tokenPost(t, options, `
{
"auth": {
"tenantName": "demo",
@@ -206,63 +84,57 @@
}
func TestProhibitUserID(t *testing.T) {
- options := AuthOptions{
- gophercloud.AuthOptions{
- Username: "me",
- UserID: "1234",
- Password: "thing",
- },
+ options := gophercloud.AuthOptions{
+ Username: "me",
+ UserID: "1234",
+ Password: "thing",
}
+
tokenPostErr(t, options, ErrUserIDProvided)
}
func TestProhibitAPIKey(t *testing.T) {
- options := AuthOptions{
- gophercloud.AuthOptions{
- Username: "me",
- Password: "thing",
- APIKey: "123412341234",
- },
+ options := gophercloud.AuthOptions{
+ Username: "me",
+ Password: "thing",
+ APIKey: "123412341234",
}
+
tokenPostErr(t, options, ErrAPIKeyProvided)
}
func TestProhibitDomainID(t *testing.T) {
- options := AuthOptions{
- gophercloud.AuthOptions{
- Username: "me",
- Password: "thing",
- DomainID: "1234",
- },
+ options := gophercloud.AuthOptions{
+ Username: "me",
+ Password: "thing",
+ DomainID: "1234",
}
+
tokenPostErr(t, options, ErrDomainIDProvided)
}
func TestProhibitDomainName(t *testing.T) {
- options := AuthOptions{
- gophercloud.AuthOptions{
- Username: "me",
- Password: "thing",
- DomainName: "wat",
- },
+ options := gophercloud.AuthOptions{
+ Username: "me",
+ Password: "thing",
+ DomainName: "wat",
}
+
tokenPostErr(t, options, ErrDomainNameProvided)
}
func TestRequireUsername(t *testing.T) {
- options := AuthOptions{
- gophercloud.AuthOptions{
- Password: "thing",
- },
+ options := gophercloud.AuthOptions{
+ Password: "thing",
}
+
tokenPostErr(t, options, ErrUsernameRequired)
}
func TestRequirePassword(t *testing.T) {
- options := AuthOptions{
- gophercloud.AuthOptions{
- Username: "me",
- },
+ options := gophercloud.AuthOptions{
+ Username: "me",
}
+
tokenPostErr(t, options, ErrPasswordRequired)
}
diff --git a/rackspace/identity/v2/tokens/delegate_test.go b/rackspace/identity/v2/tokens/delegate_test.go
index 9f9b321..6678ff4 100644
--- a/rackspace/identity/v2/tokens/delegate_test.go
+++ b/rackspace/identity/v2/tokens/delegate_test.go
@@ -1,145 +1,20 @@
package tokens
import (
- "fmt"
- "net/http"
"testing"
- "time"
"github.com/rackspace/gophercloud"
- "github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
os "github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
)
-var expectedToken = &os.Token{
- ID: "aaaabbbbccccdddd",
- ExpiresAt: time.Date(2014, time.January, 31, 15, 30, 58, 0, time.UTC),
- Tenant: tenants.Tenant{
- ID: "fc394f2ab2df4114bde39905f800dc57",
- Name: "test",
- Description: "There are many tenants. This one is yours.",
- Enabled: true,
- },
-}
-
-var expectedServiceCatalog = &os.ServiceCatalog{
- Entries: []os.CatalogEntry{
- os.CatalogEntry{
- Name: "inscrutablewalrus",
- Type: "something",
- Endpoints: []os.Endpoint{
- os.Endpoint{
- PublicURL: "http://something0:1234/v2/",
- Region: "region0",
- },
- os.Endpoint{
- PublicURL: "http://something1:1234/v2/",
- Region: "region1",
- },
- },
- },
- os.CatalogEntry{
- Name: "arbitrarypenguin",
- Type: "else",
- Endpoints: []os.Endpoint{
- os.Endpoint{
- PublicURL: "http://else0:4321/v3/",
- Region: "region0",
- },
- },
- },
- },
-}
-
func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) os.CreateResult {
th.SetupHTTP()
defer th.TeardownHTTP()
+ os.HandleTokenPost(t, requestJSON)
- client := gophercloud.ServiceClient{Endpoint: th.Endpoint()}
-
- th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "Content-Type", "application/json")
- th.TestHeader(t, r, "Accept", "application/json")
- th.TestJSONRequest(t, r, requestJSON)
-
- w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, `
-{
- "access": {
- "token": {
- "issued_at": "2014-01-30T15:30:58.000000Z",
- "expires": "2014-01-31T15:30:58Z",
- "id": "aaaabbbbccccdddd",
- "tenant": {
- "description": "There are many tenants. This one is yours.",
- "enabled": true,
- "id": "fc394f2ab2df4114bde39905f800dc57",
- "name": "test"
- }
- },
- "serviceCatalog": [
- {
- "endpoints": [
- {
- "publicURL": "http://something0:1234/v2/",
- "region": "region0"
- },
- {
- "publicURL": "http://something1:1234/v2/",
- "region": "region1"
- }
- ],
- "type": "something",
- "name": "inscrutablewalrus"
- },
- {
- "endpoints": [
- {
- "publicURL": "http://else0:4321/v3/",
- "region": "region0"
- }
- ],
- "type": "else",
- "name": "arbitrarypenguin"
- }
- ]
- }
-}
- `)
- })
-
- return Create(&client, WrapOptions(options))
-}
-
-func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) {
- th.SetupHTTP()
- defer th.TeardownHTTP()
-
- client := gophercloud.ServiceClient{Endpoint: th.Endpoint()}
-
- th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "Content-Type", "application/json")
- th.TestHeader(t, r, "Accept", "application/json")
-
- w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, `{}`)
- })
-
- actualErr := Create(&client, WrapOptions(options)).Err
- th.CheckEquals(t, expectedErr, actualErr)
-}
-
-func isSuccessful(t *testing.T, result os.CreateResult) {
- token, err := result.ExtractToken()
- th.AssertNoErr(t, err)
- th.CheckDeepEquals(t, expectedToken, token)
-
- serviceCatalog, err := result.ExtractServiceCatalog()
- th.AssertNoErr(t, err)
- th.CheckDeepEquals(t, expectedServiceCatalog, serviceCatalog)
+ return Create(client.ServiceClient(), WrapOptions(options))
}
func TestCreateTokenWithAPIKey(t *testing.T) {
@@ -148,7 +23,7 @@
APIKey: "1234567890abcdef",
}
- isSuccessful(t, tokenPost(t, options, `
+ os.IsSuccessful(t, tokenPost(t, options, `
{
"auth": {
"RAX-KSKEY:apiKeyCredentials": {