github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v2/tokens/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/vnpaycloud-console/gophercloud/v2" 8 "github.com/vnpaycloud-console/gophercloud/v2/openstack/identity/v2/tokens" 9 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 10 "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 11 ) 12 13 func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) tokens.CreateResult { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 HandleTokenPost(t, requestJSON) 17 18 return tokens.Create(context.TODO(), client.ServiceClient(), options) 19 } 20 21 func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) { 22 th.SetupHTTP() 23 defer th.TeardownHTTP() 24 HandleTokenPost(t, "") 25 26 actualErr := tokens.Create(context.TODO(), client.ServiceClient(), options).Err 27 th.CheckDeepEquals(t, expectedErr, actualErr) 28 } 29 30 func TestCreateWithPassword(t *testing.T) { 31 options := gophercloud.AuthOptions{ 32 Username: "me", 33 Password: "swordfish", 34 } 35 36 IsSuccessful(t, tokenPost(t, options, ` 37 { 38 "auth": { 39 "passwordCredentials": { 40 "username": "me", 41 "password": "swordfish" 42 } 43 } 44 } 45 `)) 46 } 47 48 func TestCreateTokenWithTenantID(t *testing.T) { 49 options := gophercloud.AuthOptions{ 50 Username: "me", 51 Password: "opensesame", 52 TenantID: "fc394f2ab2df4114bde39905f800dc57", 53 } 54 55 IsSuccessful(t, tokenPost(t, options, ` 56 { 57 "auth": { 58 "tenantId": "fc394f2ab2df4114bde39905f800dc57", 59 "passwordCredentials": { 60 "username": "me", 61 "password": "opensesame" 62 } 63 } 64 } 65 `)) 66 } 67 68 func TestCreateTokenWithTenantName(t *testing.T) { 69 options := gophercloud.AuthOptions{ 70 Username: "me", 71 Password: "opensesame", 72 TenantName: "demo", 73 } 74 75 IsSuccessful(t, tokenPost(t, options, ` 76 { 77 "auth": { 78 "tenantName": "demo", 79 "passwordCredentials": { 80 "username": "me", 81 "password": "opensesame" 82 } 83 } 84 } 85 `)) 86 } 87 88 func TestRequireUsername(t *testing.T) { 89 options := gophercloud.AuthOptions{ 90 Password: "thing", 91 } 92 93 tokenPostErr(t, options, gophercloud.ErrMissingInput{Argument: "Username"}) 94 } 95 96 func tokenGet(t *testing.T, tokenId string) tokens.GetResult { 97 th.SetupHTTP() 98 defer th.TeardownHTTP() 99 HandleTokenGet(t, tokenId) 100 return tokens.Get(context.TODO(), client.ServiceClient(), tokenId) 101 } 102 103 func TestGetWithToken(t *testing.T) { 104 GetIsSuccessful(t, tokenGet(t, "db22caf43c934e6c829087c41ff8d8d6")) 105 }