github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/groups/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/groups"
     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 TestListGroups(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  	HandleListGroupsSuccessfully(t)
    17  
    18  	count := 0
    19  	err := groups.List(client.ServiceClient(), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
    20  		count++
    21  
    22  		actual, err := groups.ExtractGroups(page)
    23  		th.AssertNoErr(t, err)
    24  
    25  		th.CheckDeepEquals(t, ExpectedGroupsSlice, actual)
    26  
    27  		return true, nil
    28  	})
    29  	th.AssertNoErr(t, err)
    30  	th.CheckEquals(t, count, 1)
    31  }
    32  
    33  func TestListGroupsAllPages(t *testing.T) {
    34  	th.SetupHTTP()
    35  	defer th.TeardownHTTP()
    36  	HandleListGroupsSuccessfully(t)
    37  
    38  	allPages, err := groups.List(client.ServiceClient(), nil).AllPages(context.TODO())
    39  	th.AssertNoErr(t, err)
    40  	actual, err := groups.ExtractGroups(allPages)
    41  	th.AssertNoErr(t, err)
    42  	th.CheckDeepEquals(t, ExpectedGroupsSlice, actual)
    43  	th.AssertEquals(t, ExpectedGroupsSlice[0].Extra["email"], "support@localhost")
    44  	th.AssertEquals(t, ExpectedGroupsSlice[1].Extra["email"], "support@example.com")
    45  }
    46  
    47  func TestListGroupsFiltersCheck(t *testing.T) {
    48  	type test struct {
    49  		filterName string
    50  		wantErr    bool
    51  	}
    52  	tests := []test{
    53  		{"foo__contains", false},
    54  		{"foo", true},
    55  		{"foo_contains", true},
    56  		{"foo__", true},
    57  		{"__foo", true},
    58  	}
    59  
    60  	var listOpts groups.ListOpts
    61  	for _, _test := range tests {
    62  		listOpts.Filters = map[string]string{_test.filterName: "bar"}
    63  		_, err := listOpts.ToGroupListQuery()
    64  
    65  		if !_test.wantErr {
    66  			th.AssertNoErr(t, err)
    67  		} else {
    68  			switch _t := err.(type) {
    69  			case nil:
    70  				t.Fatal("error expected but got a nil")
    71  			case groups.InvalidListFilter:
    72  			default:
    73  				t.Fatalf("unexpected error type: [%T]", _t)
    74  			}
    75  		}
    76  	}
    77  }
    78  
    79  func TestGetGroup(t *testing.T) {
    80  	th.SetupHTTP()
    81  	defer th.TeardownHTTP()
    82  	HandleGetGroupSuccessfully(t)
    83  
    84  	actual, err := groups.Get(context.TODO(), client.ServiceClient(), "9fe1d3").Extract()
    85  
    86  	th.AssertNoErr(t, err)
    87  	th.CheckDeepEquals(t, SecondGroup, *actual)
    88  	th.AssertEquals(t, SecondGroup.Extra["email"], "support@example.com")
    89  }
    90  
    91  func TestCreateGroup(t *testing.T) {
    92  	th.SetupHTTP()
    93  	defer th.TeardownHTTP()
    94  	HandleCreateGroupSuccessfully(t)
    95  
    96  	createOpts := groups.CreateOpts{
    97  		Name:        "support",
    98  		DomainID:    "1789d1",
    99  		Description: "group for support users",
   100  		Extra: map[string]any{
   101  			"email": "support@example.com",
   102  		},
   103  	}
   104  
   105  	actual, err := groups.Create(context.TODO(), client.ServiceClient(), createOpts).Extract()
   106  	th.AssertNoErr(t, err)
   107  	th.CheckDeepEquals(t, SecondGroup, *actual)
   108  }
   109  
   110  func TestUpdateGroup(t *testing.T) {
   111  	th.SetupHTTP()
   112  	defer th.TeardownHTTP()
   113  	HandleUpdateGroupSuccessfully(t)
   114  
   115  	var description = "L2 Support Team"
   116  	updateOpts := groups.UpdateOpts{
   117  		Description: &description,
   118  		Extra: map[string]any{
   119  			"email": "supportteam@example.com",
   120  		},
   121  	}
   122  
   123  	actual, err := groups.Update(context.TODO(), client.ServiceClient(), "9fe1d3", updateOpts).Extract()
   124  	th.AssertNoErr(t, err)
   125  	th.CheckDeepEquals(t, SecondGroupUpdated, *actual)
   126  }
   127  
   128  func TestDeleteGroup(t *testing.T) {
   129  	th.SetupHTTP()
   130  	defer th.TeardownHTTP()
   131  	HandleDeleteGroupSuccessfully(t)
   132  
   133  	res := groups.Delete(context.TODO(), client.ServiceClient(), "9fe1d3")
   134  	th.AssertNoErr(t, res.Err)
   135  }