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 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud/acceptance/clients" |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/acceptance/tools" |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/openstack" |
| 11 | "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens" |
Ash Wilson | 8018c39 | 2014-10-03 14:35:56 -0400 | [diff] [blame] | 12 | ) |
| 13 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 14 | func TestTokenAuthenticate(t *testing.T) { |
| 15 | client, err := clients.NewIdentityV2UnauthenticatedClient() |
| 16 | if err != nil { |
| 17 | t.Fatalf("Unable to obtain an identity client: %v", err) |
Ash Wilson | 8018c39 | 2014-10-03 14:35:56 -0400 | [diff] [blame] | 18 | } |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 19 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 20 | authOptions, err := openstack.AuthOptionsFromEnv() |
| 21 | if err != nil { |
| 22 | t.Fatalf("Unable to obtain authentication options: %v", err) |
| 23 | } |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 24 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 25 | result := tokens.Create(client, authOptions) |
| 26 | token, err := result.ExtractToken() |
| 27 | if err != nil { |
| 28 | t.Fatalf("Unable to extract token: %v", err) |
| 29 | } |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 30 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 31 | tools.PrintResource(t, token) |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 32 | |
| 33 | catalog, err := result.ExtractServiceCatalog() |
| 34 | if err != nil { |
| 35 | t.Fatalf("Unable to extract service catalog: %v", err) |
| 36 | } |
| 37 | |
| 38 | for _, entry := range catalog.Entries { |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 39 | tools.PrintResource(t, entry) |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestTokenValidate(t *testing.T) { |
| 44 | client, err := clients.NewIdentityV2Client() |
| 45 | if err != nil { |
| 46 | t.Fatalf("Unable to obtain an identity client: %v", err) |
| 47 | } |
| 48 | |
| 49 | authOptions, err := openstack.AuthOptionsFromEnv() |
| 50 | if err != nil { |
| 51 | t.Fatalf("Unable to obtain authentication options: %v", err) |
| 52 | } |
| 53 | |
| 54 | result := tokens.Create(client, authOptions) |
| 55 | token, err := result.ExtractToken() |
| 56 | if err != nil { |
| 57 | t.Fatalf("Unable to extract token: %v", err) |
| 58 | } |
| 59 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 60 | tools.PrintResource(t, token) |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 61 | |
| 62 | getResult := tokens.Get(client, token.ID) |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 63 | user, err := getResult.ExtractUser() |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 64 | if err != nil { |
| 65 | t.Fatalf("Unable to extract user: %v", err) |
| 66 | } |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 67 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 68 | tools.PrintResource(t, user) |
Ash Wilson | 8018c39 | 2014-10-03 14:35:56 -0400 | [diff] [blame] | 69 | } |