github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/applicationcredentials/testing/requests_test.go (about)

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