Jamie Hannaford | c21ffb9 | 2014-10-30 11:46:31 +0100 | [diff] [blame] | 1 | // +build acceptance identity |
Ash Wilson | 8018c39 | 2014-10-03 14:35:56 -0400 | [diff] [blame] | 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
Ash Wilson | 8018c39 | 2014-10-03 14:35:56 -0400 | [diff] [blame] | 8 | tokens2 "github.com/rackspace/gophercloud/openstack/identity/v2/tokens" |
Ash Wilson | 8018c39 | 2014-10-03 14:35:56 -0400 | [diff] [blame] | 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | ) |
| 11 | |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame^] | 12 | func TestAuthenticateAndValidate(t *testing.T) { |
| 13 | // 1. TestAuthenticate |
Ash Wilson | f4aee1e | 2014-10-03 15:25:13 -0400 | [diff] [blame] | 14 | ao := v2AuthOptions(t) |
| 15 | service := unauthenticatedClient(t) |
Ash Wilson | 8018c39 | 2014-10-03 14:35:56 -0400 | [diff] [blame] | 16 | |
| 17 | // Authenticated! |
Ash Wilson | d968257 | 2014-10-20 11:52:12 -0400 | [diff] [blame] | 18 | result := tokens2.Create(service, tokens2.WrapOptions(ao)) |
Ash Wilson | 8018c39 | 2014-10-03 14:35:56 -0400 | [diff] [blame] | 19 | |
| 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 Wilson | f4aee1e | 2014-10-03 15:25:13 -0400 | [diff] [blame] | 34 | t.Logf("[%02d]: name=[%s], type=[%s]", i, entry.Name, entry.Type) |
Ash Wilson | 8018c39 | 2014-10-03 14:35:56 -0400 | [diff] [blame] | 35 | for _, endpoint := range entry.Endpoints { |
| 36 | t.Logf(" - region=[%s] publicURL=[%s]", endpoint.Region, endpoint.PublicURL) |
| 37 | } |
| 38 | } |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame^] | 39 | |
| 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 Wilson | 8018c39 | 2014-10-03 14:35:56 -0400 | [diff] [blame] | 54 | } |