blob: 82a317a157e99dac554e0203eb96f68eb513c2c4 [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
Joe Topjian918f5732016-08-15 08:47:08 -06008 "github.com/gophercloud/gophercloud/acceptance/clients"
Joe Topjian66a046c2017-01-19 22:07:26 -07009 "github.com/gophercloud/gophercloud/acceptance/tools"
Joe Topjian918f5732016-08-15 08:47:08 -060010 "github.com/gophercloud/gophercloud/openstack"
11 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens"
Ash Wilson8018c392014-10-03 14:35:56 -040012)
13
Joe Topjian918f5732016-08-15 08:47:08 -060014func 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 Wilson8018c392014-10-03 14:35:56 -040018 }
hzlouchao04543602015-11-30 18:44:15 +080019
Joe Topjian918f5732016-08-15 08:47:08 -060020 authOptions, err := openstack.AuthOptionsFromEnv()
21 if err != nil {
22 t.Fatalf("Unable to obtain authentication options: %v", err)
23 }
hzlouchao04543602015-11-30 18:44:15 +080024
Joe Topjian918f5732016-08-15 08:47:08 -060025 result := tokens.Create(client, authOptions)
26 token, err := result.ExtractToken()
27 if err != nil {
28 t.Fatalf("Unable to extract token: %v", err)
29 }
hzlouchao04543602015-11-30 18:44:15 +080030
Joe Topjian66a046c2017-01-19 22:07:26 -070031 tools.PrintResource(t, token)
Joe Topjian918f5732016-08-15 08:47:08 -060032
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 Topjian66a046c2017-01-19 22:07:26 -070039 tools.PrintResource(t, entry)
Joe Topjian918f5732016-08-15 08:47:08 -060040 }
41}
42
43func 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 Topjian66a046c2017-01-19 22:07:26 -070060 tools.PrintResource(t, token)
Joe Topjian918f5732016-08-15 08:47:08 -060061
62 getResult := tokens.Get(client, token.ID)
hzlouchao04543602015-11-30 18:44:15 +080063 user, err := getResult.ExtractUser()
Joe Topjian918f5732016-08-15 08:47:08 -060064 if err != nil {
65 t.Fatalf("Unable to extract user: %v", err)
66 }
hzlouchao04543602015-11-30 18:44:15 +080067
Joe Topjian66a046c2017-01-19 22:07:26 -070068 tools.PrintResource(t, user)
Ash Wilson8018c392014-10-03 14:35:56 -040069}