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

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/gophercloud/gophercloud/openstack/identity/v3/policies"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  	th "github.com/gophercloud/gophercloud/testhelper"
    10  	"github.com/gophercloud/gophercloud/testhelper/client"
    11  )
    12  
    13  func TestListPolicies(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  	HandleListPoliciesSuccessfully(t)
    17  
    18  	count := 0
    19  	err := policies.List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
    20  		count++
    21  
    22  		actual, err := policies.ExtractPolicies(page)
    23  		th.AssertNoErr(t, err)
    24  
    25  		th.CheckDeepEquals(t, ExpectedPoliciesSlice, actual)
    26  
    27  		return true, nil
    28  	})
    29  	th.AssertNoErr(t, err)
    30  	th.CheckEquals(t, count, 1)
    31  }
    32  
    33  func TestListPoliciesAllPages(t *testing.T) {
    34  	th.SetupHTTP()
    35  	defer th.TeardownHTTP()
    36  	HandleListPoliciesSuccessfully(t)
    37  
    38  	allPages, err := policies.List(client.ServiceClient(), nil).AllPages()
    39  	th.AssertNoErr(t, err)
    40  	actual, err := policies.ExtractPolicies(allPages)
    41  	th.AssertNoErr(t, err)
    42  	th.CheckDeepEquals(t, ExpectedPoliciesSlice, actual)
    43  }
    44  
    45  func TestListPoliciesWithFilter(t *testing.T) {
    46  	th.SetupHTTP()
    47  	defer th.TeardownHTTP()
    48  	HandleListPoliciesSuccessfully(t)
    49  
    50  	listOpts := policies.ListOpts{
    51  		Type: "application/json",
    52  	}
    53  	allPages, err := policies.List(client.ServiceClient(), listOpts).AllPages()
    54  	th.AssertNoErr(t, err)
    55  	actual, err := policies.ExtractPolicies(allPages)
    56  	th.AssertNoErr(t, err)
    57  	th.CheckDeepEquals(t, []policies.Policy{SecondPolicy}, actual)
    58  }
    59  
    60  func TestListPoliciesFiltersCheck(t *testing.T) {
    61  	type test struct {
    62  		filterName string
    63  		wantErr    bool
    64  	}
    65  	tests := []test{
    66  		{"foo__contains", false},
    67  		{"foo", true},
    68  		{"foo_contains", true},
    69  		{"foo__", true},
    70  		{"__foo", true},
    71  	}
    72  
    73  	var listOpts policies.ListOpts
    74  	for _, _test := range tests {
    75  		listOpts.Filters = map[string]string{_test.filterName: "bar"}
    76  		_, err := listOpts.ToPolicyListQuery()
    77  
    78  		if !_test.wantErr {
    79  			th.AssertNoErr(t, err)
    80  		} else {
    81  			switch _t := err.(type) {
    82  			case nil:
    83  				t.Fatal("error expected but got a nil")
    84  			case policies.InvalidListFilter:
    85  			default:
    86  				t.Fatalf("unexpected error type: [%T]", _t)
    87  			}
    88  		}
    89  	}
    90  }
    91  
    92  func TestCreatePolicy(t *testing.T) {
    93  	th.SetupHTTP()
    94  	defer th.TeardownHTTP()
    95  	HandleCreatePolicySuccessfully(t)
    96  
    97  	createOpts := policies.CreateOpts{
    98  		Type: "application/json",
    99  		Blob: []byte("{'bar_user': 'role:network-user'}"),
   100  		Extra: map[string]interface{}{
   101  			"description": "policy for bar_user",
   102  		},
   103  	}
   104  
   105  	actual, err := policies.Create(client.ServiceClient(), createOpts).Extract()
   106  	th.AssertNoErr(t, err)
   107  	th.CheckDeepEquals(t, SecondPolicy, *actual)
   108  }
   109  
   110  func TestCreatePolicyTypeLengthCheck(t *testing.T) {
   111  	// strGenerator generates a string of fixed length filled with '0'
   112  	strGenerator := func(length int) string {
   113  		return fmt.Sprintf(fmt.Sprintf("%%0%dd", length), 0)
   114  	}
   115  
   116  	type test struct {
   117  		length  int
   118  		wantErr bool
   119  	}
   120  
   121  	tests := []test{
   122  		{100, false},
   123  		{255, false},
   124  		{256, true},
   125  		{300, true},
   126  	}
   127  
   128  	createOpts := policies.CreateOpts{
   129  		Blob: []byte("{'bar_user': 'role:network-user'}"),
   130  	}
   131  
   132  	for _, _test := range tests {
   133  		createOpts.Type = strGenerator(_test.length)
   134  		if len(createOpts.Type) != _test.length {
   135  			t.Fatal("function strGenerator does not work properly")
   136  		}
   137  
   138  		_, err := createOpts.ToPolicyCreateMap()
   139  		if !_test.wantErr {
   140  			th.AssertNoErr(t, err)
   141  		} else {
   142  			switch _t := err.(type) {
   143  			case nil:
   144  				t.Fatal("error expected but got a nil")
   145  			case policies.StringFieldLengthExceedsLimit:
   146  			default:
   147  				t.Fatalf("unexpected error type: [%T]", _t)
   148  			}
   149  		}
   150  	}
   151  }
   152  
   153  func TestGetPolicy(t *testing.T) {
   154  	th.SetupHTTP()
   155  	defer th.TeardownHTTP()
   156  	HandleGetPolicySuccessfully(t)
   157  
   158  	id := "b49884da9d31494ea02aff38d4b4e701"
   159  	actual, err := policies.Get(client.ServiceClient(), id).Extract()
   160  
   161  	th.AssertNoErr(t, err)
   162  	th.CheckDeepEquals(t, SecondPolicy, *actual)
   163  }
   164  
   165  func TestUpdatePolicy(t *testing.T) {
   166  	th.SetupHTTP()
   167  	defer th.TeardownHTTP()
   168  	HandleUpdatePolicySuccessfully(t)
   169  
   170  	updateOpts := policies.UpdateOpts{
   171  		Extra: map[string]interface{}{
   172  			"description": "updated policy for bar_user",
   173  		},
   174  	}
   175  
   176  	id := "b49884da9d31494ea02aff38d4b4e701"
   177  	actual, err := policies.Update(client.ServiceClient(), id, updateOpts).Extract()
   178  	th.AssertNoErr(t, err)
   179  	th.CheckDeepEquals(t, SecondPolicyUpdated, *actual)
   180  }
   181  
   182  func TestUpdatePolicyTypeLengthCheck(t *testing.T) {
   183  	// strGenerator generates a string of fixed length filled with '0'
   184  	strGenerator := func(length int) string {
   185  		return fmt.Sprintf(fmt.Sprintf("%%0%dd", length), 0)
   186  	}
   187  
   188  	type test struct {
   189  		length  int
   190  		wantErr bool
   191  	}
   192  
   193  	tests := []test{
   194  		{100, false},
   195  		{255, false},
   196  		{256, true},
   197  		{300, true},
   198  	}
   199  
   200  	var updateOpts policies.UpdateOpts
   201  	for _, _test := range tests {
   202  		updateOpts.Type = strGenerator(_test.length)
   203  		if len(updateOpts.Type) != _test.length {
   204  			t.Fatal("function strGenerator does not work properly")
   205  		}
   206  
   207  		_, err := updateOpts.ToPolicyUpdateMap()
   208  		if !_test.wantErr {
   209  			th.AssertNoErr(t, err)
   210  		} else {
   211  			switch _t := err.(type) {
   212  			case nil:
   213  				t.Fatal("error expected but got a nil")
   214  			case policies.StringFieldLengthExceedsLimit:
   215  			default:
   216  				t.Fatalf("unexpected error type: [%T]", _t)
   217  			}
   218  		}
   219  	}
   220  }
   221  
   222  func TestDeletePolicy(t *testing.T) {
   223  	th.SetupHTTP()
   224  	defer th.TeardownHTTP()
   225  	HandleDeletePolicySuccessfully(t)
   226  
   227  	res := policies.Delete(client.ServiceClient(), "9fe1d3")
   228  	th.AssertNoErr(t, res.Err)
   229  }