blob: c2f7e51e93557d7bf5a65715dd5299b5344c2d4a [file] [log] [blame]
Jamie Hannafordc21ffb92014-10-30 11:46:31 +01001// +build acceptance identity
Ash Wilson8018c392014-10-03 14:35:56 -04002
3package v2
4
5import (
6 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 tokens2 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens"
9 th "github.com/gophercloud/gophercloud/testhelper"
Ash Wilson8018c392014-10-03 14:35:56 -040010)
11
hzlouchao04543602015-11-30 18:44:15 +080012func TestAuthenticateAndValidate(t *testing.T) {
13 // 1. TestAuthenticate
Ash Wilsonf4aee1e2014-10-03 15:25:13 -040014 ao := v2AuthOptions(t)
15 service := unauthenticatedClient(t)
Ash Wilson8018c392014-10-03 14:35:56 -040016
17 // Authenticated!
Ash Wilsond9682572014-10-20 11:52:12 -040018 result := tokens2.Create(service, tokens2.WrapOptions(ao))
Ash Wilson8018c392014-10-03 14:35:56 -040019
20 // Extract and print the token.
21 token, err := result.ExtractToken()
22 th.AssertNoErr(t, err)
23
24 t.Logf("Acquired token: [%s]", token.ID)
25 t.Logf("The token will expire at: [%s]", token.ExpiresAt.String())
26 t.Logf("The token is valid for tenant: [%#v]", token.Tenant)
27
28 // Extract and print the service catalog.
29 catalog, err := result.ExtractServiceCatalog()
30 th.AssertNoErr(t, err)
31
32 t.Logf("Acquired service catalog listing [%d] services", len(catalog.Entries))
33 for i, entry := range catalog.Entries {
Ash Wilsonf4aee1e2014-10-03 15:25:13 -040034 t.Logf("[%02d]: name=[%s], type=[%s]", i, entry.Name, entry.Type)
Ash Wilson8018c392014-10-03 14:35:56 -040035 for _, endpoint := range entry.Endpoints {
36 t.Logf(" - region=[%s] publicURL=[%s]", endpoint.Region, endpoint.PublicURL)
37 }
38 }
hzlouchao04543602015-11-30 18:44:15 +080039
40 // 2. TestValidate
41 client := authenticatedClient(t)
42
43 // Validate Token!
44 getResult := tokens2.Get(client, token.ID)
45
46 // Extract and print the user.
47 user, err := getResult.ExtractUser()
48 th.AssertNoErr(t, err)
49
50 t.Logf("Acquired User: [%s]", user.Name)
51 t.Logf("The User id: [%s]", user.ID)
52 t.Logf("The User username: [%s]", user.UserName)
53 t.Logf("The User roles: [%#v]", user.Roles)
Ash Wilson8018c392014-10-03 14:35:56 -040054}