github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v3/users/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/groups"
     7  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/projects"
     8  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/users"
     9  	"github.com/huaweicloud/golangsdk/pagination"
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  	"github.com/huaweicloud/golangsdk/testhelper/client"
    12  )
    13  
    14  func TestListUsers(t *testing.T) {
    15  	th.SetupHTTP()
    16  	defer th.TeardownHTTP()
    17  	HandleListUsersSuccessfully(t)
    18  
    19  	count := 0
    20  	err := users.List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
    21  		count++
    22  
    23  		actual, err := users.ExtractUsers(page)
    24  		th.AssertNoErr(t, err)
    25  
    26  		th.CheckDeepEquals(t, ExpectedUsersSlice, actual)
    27  
    28  		return true, nil
    29  	})
    30  	th.AssertNoErr(t, err)
    31  	th.CheckEquals(t, count, 1)
    32  }
    33  
    34  func TestListUsersAllPages(t *testing.T) {
    35  	th.SetupHTTP()
    36  	defer th.TeardownHTTP()
    37  	HandleListUsersSuccessfully(t)
    38  
    39  	allPages, err := users.List(client.ServiceClient(), nil).AllPages()
    40  	th.AssertNoErr(t, err)
    41  	actual, err := users.ExtractUsers(allPages)
    42  	th.AssertNoErr(t, err)
    43  	th.CheckDeepEquals(t, ExpectedUsersSlice, actual)
    44  }
    45  
    46  func TestGetUser(t *testing.T) {
    47  	th.SetupHTTP()
    48  	defer th.TeardownHTTP()
    49  	HandleGetUserSuccessfully(t)
    50  
    51  	actual, err := users.Get(client.ServiceClient(), "9fe1d3").Extract()
    52  	th.AssertNoErr(t, err)
    53  	th.CheckDeepEquals(t, SecondUser, *actual)
    54  }
    55  
    56  func TestCreateUser(t *testing.T) {
    57  	th.SetupHTTP()
    58  	defer th.TeardownHTTP()
    59  	HandleCreateUserSuccessfully(t)
    60  
    61  	iTrue := true
    62  	createOpts := users.CreateOpts{
    63  		Name:             "jsmith",
    64  		DomainID:         "1789d1",
    65  		Enabled:          &iTrue,
    66  		Password:         "secretsecret",
    67  		DefaultProjectID: "263fd9",
    68  	}
    69  
    70  	actual, err := users.Create(client.ServiceClient(), createOpts).Extract()
    71  	th.AssertNoErr(t, err)
    72  	th.CheckDeepEquals(t, SecondUser, *actual)
    73  }
    74  
    75  func TestCreateNoOptionsUser(t *testing.T) {
    76  	th.SetupHTTP()
    77  	defer th.TeardownHTTP()
    78  	HandleCreateNoOptionsUserSuccessfully(t)
    79  
    80  	iTrue := true
    81  	createOpts := users.CreateOpts{
    82  		Name:             "jsmith",
    83  		DomainID:         "1789d1",
    84  		Enabled:          &iTrue,
    85  		Password:         "secretsecret",
    86  		DefaultProjectID: "263fd9",
    87  	}
    88  
    89  	actual, err := users.Create(client.ServiceClient(), createOpts).Extract()
    90  	th.AssertNoErr(t, err)
    91  	th.CheckDeepEquals(t, SecondUserNoOptions, *actual)
    92  }
    93  
    94  func TestUpdateUser(t *testing.T) {
    95  	th.SetupHTTP()
    96  	defer th.TeardownHTTP()
    97  	HandleUpdateUserSuccessfully(t)
    98  
    99  	iFalse := false
   100  	updateOpts := users.UpdateOpts{
   101  		Enabled: &iFalse,
   102  	}
   103  
   104  	actual, err := users.Update(client.ServiceClient(), "9fe1d3", updateOpts).Extract()
   105  	th.AssertNoErr(t, err)
   106  	th.CheckDeepEquals(t, SecondUserUpdated, *actual)
   107  }
   108  
   109  func TestDeleteUser(t *testing.T) {
   110  	th.SetupHTTP()
   111  	defer th.TeardownHTTP()
   112  	HandleDeleteUserSuccessfully(t)
   113  
   114  	res := users.Delete(client.ServiceClient(), "9fe1d3")
   115  	th.AssertNoErr(t, res.Err)
   116  }
   117  
   118  func TestListUserGroups(t *testing.T) {
   119  	th.SetupHTTP()
   120  	defer th.TeardownHTTP()
   121  	HandleListUserGroupsSuccessfully(t)
   122  	allPages, err := users.ListGroups(client.ServiceClient(), "9fe1d3").AllPages()
   123  	th.AssertNoErr(t, err)
   124  	actual, err := groups.ExtractGroups(allPages)
   125  	th.AssertNoErr(t, err)
   126  	th.CheckDeepEquals(t, ExpectedGroupsSlice, actual)
   127  }
   128  
   129  func TestListUserProjects(t *testing.T) {
   130  	th.SetupHTTP()
   131  	defer th.TeardownHTTP()
   132  	HandleListUserProjectsSuccessfully(t)
   133  	allPages, err := users.ListProjects(client.ServiceClient(), "9fe1d3").AllPages()
   134  	th.AssertNoErr(t, err)
   135  	actual, err := projects.ExtractProjects(allPages)
   136  	th.AssertNoErr(t, err)
   137  	th.CheckDeepEquals(t, ExpectedProjectsSlice, actual)
   138  }
   139  
   140  func TestListInGroup(t *testing.T) {
   141  	th.SetupHTTP()
   142  	defer th.TeardownHTTP()
   143  	HandleListInGroupSuccessfully(t)
   144  
   145  	iTrue := true
   146  	listOpts := users.ListOpts{
   147  		Enabled: &iTrue,
   148  	}
   149  
   150  	allPages, err := users.ListInGroup(client.ServiceClient(), "ea167b", listOpts).AllPages()
   151  	th.AssertNoErr(t, err)
   152  	actual, err := users.ExtractUsers(allPages)
   153  	th.AssertNoErr(t, err)
   154  	th.CheckDeepEquals(t, ExpectedUsersSlice, actual)
   155  }