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

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/identity/v3/applicationcredentials"
     8  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     9  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    11  )
    12  
    13  func TestListApplicationCredentials(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  	HandleListApplicationCredentialsSuccessfully(t)
    17  
    18  	count := 0
    19  	err := applicationcredentials.List(client.ServiceClient(), userID, nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
    20  		count++
    21  
    22  		actual, err := applicationcredentials.ExtractApplicationCredentials(page)
    23  		th.AssertNoErr(t, err)
    24  
    25  		th.CheckDeepEquals(t, ExpectedApplicationCredentialsSlice, actual)
    26  
    27  		return true, nil
    28  	})
    29  	th.AssertNoErr(t, err)
    30  	th.CheckEquals(t, count, 1)
    31  }
    32  
    33  func TestListApplicationCredentialsAllPages(t *testing.T) {
    34  	th.SetupHTTP()
    35  	defer th.TeardownHTTP()
    36  	HandleListApplicationCredentialsSuccessfully(t)
    37  
    38  	allPages, err := applicationcredentials.List(client.ServiceClient(), userID, nil).AllPages(context.TODO())
    39  	th.AssertNoErr(t, err)
    40  	actual, err := applicationcredentials.ExtractApplicationCredentials(allPages)
    41  	th.AssertNoErr(t, err)
    42  	th.CheckDeepEquals(t, ExpectedApplicationCredentialsSlice, actual)
    43  	th.AssertDeepEquals(t, ExpectedApplicationCredentialsSlice[0].Roles, []applicationcredentials.Role{{ID: "31f87923ae4a4d119aa0b85dcdbeed13", Name: "compute_viewer"}})
    44  	th.AssertDeepEquals(t, ExpectedApplicationCredentialsSlice[1].Roles, []applicationcredentials.Role{{ID: "31f87923ae4a4d119aa0b85dcdbeed13", Name: "compute_viewer"}, {ID: "4494bc5bea1a4105ad7fbba6a7eb9ef4", Name: "network_viewer"}})
    45  }
    46  
    47  func TestGetApplicationCredential(t *testing.T) {
    48  	th.SetupHTTP()
    49  	defer th.TeardownHTTP()
    50  	HandleGetApplicationCredentialSuccessfully(t)
    51  
    52  	actual, err := applicationcredentials.Get(context.TODO(), client.ServiceClient(), userID, applicationCredentialID).Extract()
    53  	th.AssertNoErr(t, err)
    54  	th.CheckDeepEquals(t, ApplicationCredential, *actual)
    55  }
    56  
    57  func TestCreateApplicationCredential(t *testing.T) {
    58  	th.SetupHTTP()
    59  	defer th.TeardownHTTP()
    60  	HandleCreateApplicationCredentialSuccessfully(t)
    61  
    62  	createOpts := applicationcredentials.CreateOpts{
    63  		Name:   "test",
    64  		Secret: "mysecret",
    65  		Roles: []applicationcredentials.Role{
    66  			{ID: "31f87923ae4a4d119aa0b85dcdbeed13"},
    67  		},
    68  		AccessRules: []applicationcredentials.AccessRule{
    69  			{
    70  				Path:    "/v2.0/metrics",
    71  				Method:  "GET",
    72  				Service: "monitoring",
    73  			},
    74  		},
    75  	}
    76  
    77  	ApplicationCredentialResponse := ApplicationCredential
    78  	ApplicationCredentialResponse.Secret = "mysecret"
    79  
    80  	actual, err := applicationcredentials.Create(context.TODO(), client.ServiceClient(), userID, createOpts).Extract()
    81  	th.AssertNoErr(t, err)
    82  	th.CheckDeepEquals(t, ApplicationCredentialResponse, *actual)
    83  }
    84  
    85  func TestCreateNoSecretApplicationCredential(t *testing.T) {
    86  	th.SetupHTTP()
    87  	defer th.TeardownHTTP()
    88  	HandleCreateNoSecretApplicationCredentialSuccessfully(t)
    89  
    90  	createOpts := applicationcredentials.CreateOpts{
    91  		Name: "test1",
    92  		Roles: []applicationcredentials.Role{
    93  			{ID: "31f87923ae4a4d119aa0b85dcdbeed13"},
    94  		},
    95  	}
    96  
    97  	actual, err := applicationcredentials.Create(context.TODO(), client.ServiceClient(), userID, createOpts).Extract()
    98  	th.AssertNoErr(t, err)
    99  	th.CheckDeepEquals(t, ApplicationCredentialNoSecretResponse, *actual)
   100  }
   101  
   102  func TestCreateUnrestrictedApplicationCredential(t *testing.T) {
   103  	th.SetupHTTP()
   104  	defer th.TeardownHTTP()
   105  	HandleCreateUnrestrictedApplicationCredentialSuccessfully(t)
   106  
   107  	createOpts := applicationcredentials.CreateOpts{
   108  		Name:         "test2",
   109  		Unrestricted: true,
   110  		Roles: []applicationcredentials.Role{
   111  			{ID: "31f87923ae4a4d119aa0b85dcdbeed13"},
   112  			{ID: "4494bc5bea1a4105ad7fbba6a7eb9ef4"},
   113  		},
   114  		ExpiresAt: &ApplationCredentialExpiresAt,
   115  	}
   116  
   117  	UnrestrictedApplicationCredentialResponse := UnrestrictedApplicationCredential
   118  	UnrestrictedApplicationCredentialResponse.Secret = "generated_secret"
   119  
   120  	actual, err := applicationcredentials.Create(context.TODO(), client.ServiceClient(), userID, createOpts).Extract()
   121  	th.AssertNoErr(t, err)
   122  	th.CheckDeepEquals(t, UnrestrictedApplicationCredentialResponse, *actual)
   123  }
   124  
   125  func TestDeleteApplicationCredential(t *testing.T) {
   126  	th.SetupHTTP()
   127  	defer th.TeardownHTTP()
   128  	HandleDeleteApplicationCredentialSuccessfully(t)
   129  
   130  	res := applicationcredentials.Delete(context.TODO(), client.ServiceClient(), userID, applicationCredentialID)
   131  	th.AssertNoErr(t, res.Err)
   132  }
   133  
   134  func TestListAccessRules(t *testing.T) {
   135  	th.SetupHTTP()
   136  	defer th.TeardownHTTP()
   137  	HandleListAccessRulesSuccessfully(t)
   138  
   139  	count := 0
   140  	err := applicationcredentials.ListAccessRules(client.ServiceClient(), userID).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
   141  		count++
   142  
   143  		actual, err := applicationcredentials.ExtractAccessRules(page)
   144  		th.AssertNoErr(t, err)
   145  
   146  		th.CheckDeepEquals(t, ExpectedAccessRulesSlice, actual)
   147  
   148  		return true, nil
   149  	})
   150  	th.AssertNoErr(t, err)
   151  	th.CheckEquals(t, count, 1)
   152  }
   153  
   154  func TestGetAccessRule(t *testing.T) {
   155  	th.SetupHTTP()
   156  	defer th.TeardownHTTP()
   157  	HandleGetAccessRuleSuccessfully(t)
   158  
   159  	actual, err := applicationcredentials.GetAccessRule(context.TODO(), client.ServiceClient(), userID, accessRuleID).Extract()
   160  	th.AssertNoErr(t, err)
   161  	th.CheckDeepEquals(t, AccessRule, *actual)
   162  }
   163  
   164  func TestDeleteAccessRule(t *testing.T) {
   165  	th.SetupHTTP()
   166  	defer th.TeardownHTTP()
   167  	HandleDeleteAccessRuleSuccessfully(t)
   168  
   169  	res := applicationcredentials.DeleteAccessRule(context.TODO(), client.ServiceClient(), userID, accessRuleID)
   170  	th.AssertNoErr(t, res.Err)
   171  }