github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v2/tenants/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/tenants" 9 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 10 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 11 "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 12 ) 13 14 func TestListTenants(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 HandleListTenantsSuccessfully(t) 18 19 count := 0 20 err := tenants.List(client.ServiceClient(), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 21 count++ 22 23 actual, err := tenants.ExtractTenants(page) 24 th.AssertNoErr(t, err) 25 26 th.CheckDeepEquals(t, ExpectedTenantSlice, actual) 27 28 return true, nil 29 }) 30 th.AssertNoErr(t, err) 31 th.CheckEquals(t, count, 1) 32 } 33 34 func TestCreateTenant(t *testing.T) { 35 th.SetupHTTP() 36 defer th.TeardownHTTP() 37 38 mockCreateTenantResponse(t) 39 40 opts := tenants.CreateOpts{ 41 Name: "new_tenant", 42 Description: "This is new tenant", 43 Enabled: gophercloud.Enabled, 44 } 45 46 tenant, err := tenants.Create(context.TODO(), client.ServiceClient(), opts).Extract() 47 48 th.AssertNoErr(t, err) 49 50 expected := &tenants.Tenant{ 51 Name: "new_tenant", 52 Description: "This is new tenant", 53 Enabled: true, 54 ID: "5c62ef576dc7444cbb73b1fe84b97648", 55 } 56 57 th.AssertDeepEquals(t, expected, tenant) 58 } 59 60 func TestDeleteTenant(t *testing.T) { 61 th.SetupHTTP() 62 defer th.TeardownHTTP() 63 64 mockDeleteTenantResponse(t) 65 66 err := tenants.Delete(context.TODO(), client.ServiceClient(), "2466f69cd4714d89a548a68ed97ffcd4").ExtractErr() 67 th.AssertNoErr(t, err) 68 } 69 70 func TestUpdateTenant(t *testing.T) { 71 th.SetupHTTP() 72 defer th.TeardownHTTP() 73 74 mockUpdateTenantResponse(t) 75 76 id := "5c62ef576dc7444cbb73b1fe84b97648" 77 description := "This is new name" 78 opts := tenants.UpdateOpts{ 79 Name: "new_name", 80 Description: &description, 81 Enabled: gophercloud.Enabled, 82 } 83 84 tenant, err := tenants.Update(context.TODO(), client.ServiceClient(), id, opts).Extract() 85 86 th.AssertNoErr(t, err) 87 88 expected := &tenants.Tenant{ 89 Name: "new_name", 90 ID: id, 91 Description: "This is new name", 92 Enabled: true, 93 } 94 95 th.AssertDeepEquals(t, expected, tenant) 96 } 97 98 func TestGetTenant(t *testing.T) { 99 th.SetupHTTP() 100 defer th.TeardownHTTP() 101 102 mockGetTenantResponse(t) 103 104 tenant, err := tenants.Get(context.TODO(), client.ServiceClient(), "5c62ef576dc7444cbb73b1fe84b97648").Extract() 105 th.AssertNoErr(t, err) 106 107 expected := &tenants.Tenant{ 108 Name: "new_tenant", 109 ID: "5c62ef576dc7444cbb73b1fe84b97648", 110 Description: "This is new tenant", 111 Enabled: true, 112 } 113 114 th.AssertDeepEquals(t, expected, tenant) 115 }