blob: 4cd8301ef40dfcb70c49b9ac087d31b626a48e63 [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"
9 "github.com/gophercloud/gophercloud/openstack"
10 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens"
Ash Wilson8018c392014-10-03 14:35:56 -040011)
12
Joe Topjian918f5732016-08-15 08:47:08 -060013func TestTokenAuthenticate(t *testing.T) {
14 client, err := clients.NewIdentityV2UnauthenticatedClient()
15 if err != nil {
16 t.Fatalf("Unable to obtain an identity client: %v", err)
Ash Wilson8018c392014-10-03 14:35:56 -040017 }
hzlouchao04543602015-11-30 18:44:15 +080018
Joe Topjian918f5732016-08-15 08:47:08 -060019 authOptions, err := openstack.AuthOptionsFromEnv()
20 if err != nil {
21 t.Fatalf("Unable to obtain authentication options: %v", err)
22 }
hzlouchao04543602015-11-30 18:44:15 +080023
Joe Topjian918f5732016-08-15 08:47:08 -060024 result := tokens.Create(client, authOptions)
25 token, err := result.ExtractToken()
26 if err != nil {
27 t.Fatalf("Unable to extract token: %v", err)
28 }
hzlouchao04543602015-11-30 18:44:15 +080029
Joe Topjian918f5732016-08-15 08:47:08 -060030 PrintToken(t, token)
31
32 catalog, err := result.ExtractServiceCatalog()
33 if err != nil {
34 t.Fatalf("Unable to extract service catalog: %v", err)
35 }
36
37 for _, entry := range catalog.Entries {
38 PrintCatalogEntry(t, &entry)
39 }
40}
41
42func TestTokenValidate(t *testing.T) {
43 client, err := clients.NewIdentityV2Client()
44 if err != nil {
45 t.Fatalf("Unable to obtain an identity client: %v", err)
46 }
47
48 authOptions, err := openstack.AuthOptionsFromEnv()
49 if err != nil {
50 t.Fatalf("Unable to obtain authentication options: %v", err)
51 }
52
53 result := tokens.Create(client, authOptions)
54 token, err := result.ExtractToken()
55 if err != nil {
56 t.Fatalf("Unable to extract token: %v", err)
57 }
58
59 PrintToken(t, token)
60
61 getResult := tokens.Get(client, token.ID)
hzlouchao04543602015-11-30 18:44:15 +080062 user, err := getResult.ExtractUser()
Joe Topjian918f5732016-08-15 08:47:08 -060063 if err != nil {
64 t.Fatalf("Unable to extract user: %v", err)
65 }
hzlouchao04543602015-11-30 18:44:15 +080066
Joe Topjian918f5732016-08-15 08:47:08 -060067 PrintTokenUser(t, user)
Ash Wilson8018c392014-10-03 14:35:56 -040068}