blob: 95ee7e660cd552babb2e10c1573fe783bde7d849 [file] [log] [blame]
jrperritt95b74c82015-07-28 20:39:27 -06001// +build acceptance
2
3package v2
4
5import (
6 "os"
7 "testing"
8
9 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/acceptance/tools"
11 "github.com/rackspace/gophercloud/rackspace"
12 "github.com/rackspace/gophercloud/rackspace/identity/v2/tokens"
13 th "github.com/rackspace/gophercloud/testhelper"
14)
15
16func rackspaceAuthOptions(t *testing.T) gophercloud.AuthOptions {
17 // Obtain credentials from the environment.
18 options, err := rackspace.AuthOptionsFromEnv()
19 th.AssertNoErr(t, err)
20 options = tools.OnlyRS(options)
21
22 if options.Username == "" {
23 t.Fatal("Please provide a Rackspace username as RS_USERNAME.")
24 }
25 if options.APIKey == "" {
26 t.Fatal("Please provide a Rackspace API key as RS_API_KEY.")
27 }
28
29 return options
30}
31
32func createClient(t *testing.T, auth bool) *gophercloud.ServiceClient {
33 ao := rackspaceAuthOptions(t)
34
35 provider, err := rackspace.NewClient(ao.IdentityEndpoint)
36 th.AssertNoErr(t, err)
37
38 if auth {
39 err = rackspace.Authenticate(provider, ao)
40 th.AssertNoErr(t, err)
41 }
42
43 return rackspace.NewIdentityV2(provider)
44}
45
46func TestTokenAuth(t *testing.T) {
47 authedClient := createClient(t, true)
48 token := authedClient.TokenID
49
50 tenantID := os.Getenv("RS_TENANT_ID")
51 if tenantID == "" {
52 t.Skip("You must set RS_TENANT_ID environment variable to run this test")
53 }
54
55 authOpts := tokens.AuthOptions{}
56 authOpts.TenantID = tenantID
57 authOpts.Token = token
58
59 _, err := tokens.Create(authedClient, authOpts).ExtractToken()
60 th.AssertNoErr(t, err)
61}