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