github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/trusts/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/identity/v3/trusts"
     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 TestCreateTrust(t *testing.T) {
    15  	th.SetupHTTP()
    16  	defer th.TeardownHTTP()
    17  	HandleCreateTrust(t)
    18  
    19  	expiresAt := time.Date(2019, 12, 1, 14, 0, 0, 0, time.UTC)
    20  	result, err := trusts.Create(context.TODO(), client.ServiceClient(), trusts.CreateOpts{
    21  		ExpiresAt:         &expiresAt,
    22  		AllowRedelegation: true,
    23  		ProjectID:         "9b71012f5a4a4aef9193f1995fe159b2",
    24  		Roles: []trusts.Role{
    25  			{
    26  				Name: "member",
    27  			},
    28  		},
    29  		TrusteeUserID: "ecb37e88cc86431c99d0332208cb6fbf",
    30  		TrustorUserID: "959ed913a32c4ec88c041c98e61cbbc3",
    31  	}).Extract()
    32  	th.AssertNoErr(t, err)
    33  
    34  	th.AssertDeepEquals(t, CreatedTrust, *result)
    35  }
    36  
    37  func TestCreateTrustNoExpire(t *testing.T) {
    38  	th.SetupHTTP()
    39  	defer th.TeardownHTTP()
    40  	HandleCreateTrustNoExpire(t)
    41  
    42  	result, err := trusts.Create(context.TODO(), client.ServiceClient(), trusts.CreateOpts{
    43  		AllowRedelegation: true,
    44  		ProjectID:         "9b71012f5a4a4aef9193f1995fe159b2",
    45  		Roles: []trusts.Role{
    46  			{
    47  				Name: "member",
    48  			},
    49  		},
    50  		TrusteeUserID: "ecb37e88cc86431c99d0332208cb6fbf",
    51  		TrustorUserID: "959ed913a32c4ec88c041c98e61cbbc3",
    52  	}).Extract()
    53  	th.AssertNoErr(t, err)
    54  
    55  	th.AssertDeepEquals(t, CreatedTrustNoExpire, *result)
    56  }
    57  
    58  func TestDeleteTrust(t *testing.T) {
    59  	th.SetupHTTP()
    60  	defer th.TeardownHTTP()
    61  	HandleDeleteTrust(t)
    62  
    63  	res := trusts.Delete(context.TODO(), client.ServiceClient(), "3422b7c113894f5d90665e1a79655e23")
    64  	th.AssertNoErr(t, res.Err)
    65  }
    66  
    67  func TestGetTrust(t *testing.T) {
    68  	th.SetupHTTP()
    69  	defer th.TeardownHTTP()
    70  	HandleGetTrustSuccessfully(t)
    71  
    72  	res := trusts.Get(context.TODO(), client.ServiceClient(), "987fe8")
    73  	th.AssertNoErr(t, res.Err)
    74  }
    75  
    76  func TestListTrusts(t *testing.T) {
    77  	th.SetupHTTP()
    78  	defer th.TeardownHTTP()
    79  	HandleListTrustsSuccessfully(t)
    80  
    81  	count := 0
    82  	err := trusts.List(client.ServiceClient(), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
    83  		count++
    84  
    85  		actual, err := trusts.ExtractTrusts(page)
    86  		th.AssertNoErr(t, err)
    87  
    88  		th.CheckDeepEquals(t, ExpectedTrustsSlice, actual)
    89  
    90  		return true, nil
    91  	})
    92  	th.AssertNoErr(t, err)
    93  	th.CheckEquals(t, count, 1)
    94  }
    95  
    96  func TestListTrustsAllPages(t *testing.T) {
    97  	th.SetupHTTP()
    98  	defer th.TeardownHTTP()
    99  	HandleListTrustsSuccessfully(t)
   100  
   101  	allPages, err := trusts.List(client.ServiceClient(), nil).AllPages(context.TODO())
   102  	th.AssertNoErr(t, err)
   103  	actual, err := trusts.ExtractTrusts(allPages)
   104  	th.AssertNoErr(t, err)
   105  	th.CheckDeepEquals(t, ExpectedTrustsSlice, actual)
   106  }
   107  
   108  func TestListTrustsFiltered(t *testing.T) {
   109  	th.SetupHTTP()
   110  	defer th.TeardownHTTP()
   111  	HandleListTrustsSuccessfully(t)
   112  	trustsListOpts := trusts.ListOpts{
   113  		TrustorUserID: "86c0d5",
   114  	}
   115  	allPages, err := trusts.List(client.ServiceClient(), trustsListOpts).AllPages(context.TODO())
   116  	th.AssertNoErr(t, err)
   117  	actual, err := trusts.ExtractTrusts(allPages)
   118  	th.AssertNoErr(t, err)
   119  	th.CheckDeepEquals(t, ExpectedTrustsSlice, actual)
   120  }
   121  
   122  func TestListTrustRoles(t *testing.T) {
   123  	th.SetupHTTP()
   124  	defer th.TeardownHTTP()
   125  	HandleListTrustRolesSuccessfully(t)
   126  
   127  	count := 0
   128  	err := trusts.ListRoles(client.ServiceClient(), "987fe8").EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
   129  		count++
   130  
   131  		actual, err := trusts.ExtractRoles(page)
   132  		th.AssertNoErr(t, err)
   133  
   134  		th.CheckDeepEquals(t, ExpectedTrustRolesSlice, actual)
   135  
   136  		return true, nil
   137  	})
   138  	th.AssertNoErr(t, err)
   139  	th.CheckEquals(t, count, 1)
   140  }
   141  
   142  func TestListTrustRolesAllPages(t *testing.T) {
   143  	th.SetupHTTP()
   144  	defer th.TeardownHTTP()
   145  	HandleListTrustRolesSuccessfully(t)
   146  
   147  	allPages, err := trusts.ListRoles(client.ServiceClient(), "987fe8").AllPages(context.TODO())
   148  	th.AssertNoErr(t, err)
   149  	actual, err := trusts.ExtractRoles(allPages)
   150  	th.AssertNoErr(t, err)
   151  	th.CheckDeepEquals(t, ExpectedTrustRolesSlice, actual)
   152  }
   153  
   154  func TestGetTrustRole(t *testing.T) {
   155  	th.SetupHTTP()
   156  	defer th.TeardownHTTP()
   157  	HandleGetTrustRoleSuccessfully(t)
   158  
   159  	role, err := trusts.GetRole(context.TODO(), client.ServiceClient(), "987fe8", "c1648e").Extract()
   160  	th.AssertNoErr(t, err)
   161  
   162  	th.AssertEquals(t, FirstRole, *role)
   163  }
   164  
   165  func TestCheckTrustRole(t *testing.T) {
   166  	th.SetupHTTP()
   167  	defer th.TeardownHTTP()
   168  	HandleCheckTrustRoleSuccessfully(t)
   169  
   170  	err := trusts.CheckRole(context.TODO(), client.ServiceClient(), "987fe8", "c1648e").ExtractErr()
   171  	th.AssertNoErr(t, err)
   172  }