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

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/openstack/identity/v2/users"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	"github.com/huaweicloud/golangsdk/testhelper/client"
    11  )
    12  
    13  func TestList(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  
    17  	MockListUserResponse(t)
    18  
    19  	count := 0
    20  
    21  	err := users.List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
    22  		count++
    23  		actual, err := users.ExtractUsers(page)
    24  		th.AssertNoErr(t, err)
    25  
    26  		expected := []users.User{
    27  			{
    28  				ID:       "u1000",
    29  				Name:     "John Smith",
    30  				Username: "jqsmith",
    31  				Email:    "john.smith@example.org",
    32  				Enabled:  true,
    33  				TenantID: "12345",
    34  			},
    35  			{
    36  				ID:       "u1001",
    37  				Name:     "Jane Smith",
    38  				Username: "jqsmith",
    39  				Email:    "jane.smith@example.org",
    40  				Enabled:  true,
    41  				TenantID: "12345",
    42  			},
    43  		}
    44  		th.CheckDeepEquals(t, expected, actual)
    45  		return true, nil
    46  	})
    47  	th.AssertNoErr(t, err)
    48  	th.AssertEquals(t, 1, count)
    49  }
    50  
    51  func TestCreateUser(t *testing.T) {
    52  	th.SetupHTTP()
    53  	defer th.TeardownHTTP()
    54  
    55  	mockCreateUserResponse(t)
    56  
    57  	opts := users.CreateOpts{
    58  		Name:     "new_user",
    59  		TenantID: "12345",
    60  		Enabled:  golangsdk.Disabled,
    61  		Email:    "new_user@foo.com",
    62  	}
    63  
    64  	user, err := users.Create(client.ServiceClient(), opts).Extract()
    65  
    66  	th.AssertNoErr(t, err)
    67  
    68  	expected := &users.User{
    69  		Name:     "new_user",
    70  		ID:       "c39e3de9be2d4c779f1dfd6abacc176d",
    71  		Email:    "new_user@foo.com",
    72  		Enabled:  false,
    73  		TenantID: "12345",
    74  	}
    75  
    76  	th.AssertDeepEquals(t, expected, user)
    77  }
    78  
    79  func TestGetUser(t *testing.T) {
    80  	th.SetupHTTP()
    81  	defer th.TeardownHTTP()
    82  
    83  	mockGetUserResponse(t)
    84  
    85  	user, err := users.Get(client.ServiceClient(), "new_user").Extract()
    86  	th.AssertNoErr(t, err)
    87  
    88  	expected := &users.User{
    89  		Name:     "new_user",
    90  		ID:       "c39e3de9be2d4c779f1dfd6abacc176d",
    91  		Email:    "new_user@foo.com",
    92  		Enabled:  false,
    93  		TenantID: "12345",
    94  	}
    95  
    96  	th.AssertDeepEquals(t, expected, user)
    97  }
    98  
    99  func TestUpdateUser(t *testing.T) {
   100  	th.SetupHTTP()
   101  	defer th.TeardownHTTP()
   102  
   103  	mockUpdateUserResponse(t)
   104  
   105  	id := "c39e3de9be2d4c779f1dfd6abacc176d"
   106  	opts := users.UpdateOpts{
   107  		Name:    "new_name",
   108  		Enabled: golangsdk.Enabled,
   109  		Email:   "new_email@foo.com",
   110  	}
   111  
   112  	user, err := users.Update(client.ServiceClient(), id, opts).Extract()
   113  
   114  	th.AssertNoErr(t, err)
   115  
   116  	expected := &users.User{
   117  		Name:     "new_name",
   118  		ID:       id,
   119  		Email:    "new_email@foo.com",
   120  		Enabled:  true,
   121  		TenantID: "12345",
   122  	}
   123  
   124  	th.AssertDeepEquals(t, expected, user)
   125  }
   126  
   127  func TestDeleteUser(t *testing.T) {
   128  	th.SetupHTTP()
   129  	defer th.TeardownHTTP()
   130  
   131  	mockDeleteUserResponse(t)
   132  
   133  	res := users.Delete(client.ServiceClient(), "c39e3de9be2d4c779f1dfd6abacc176d")
   134  	th.AssertNoErr(t, res.Err)
   135  }
   136  
   137  func TestListingUserRoles(t *testing.T) {
   138  	th.SetupHTTP()
   139  	defer th.TeardownHTTP()
   140  
   141  	mockListRolesResponse(t)
   142  
   143  	tenantID := "1d8b6120dcc640fda4fc9194ffc80273"
   144  	userID := "c39e3de9be2d4c779f1dfd6abacc176d"
   145  
   146  	err := users.ListRoles(client.ServiceClient(), tenantID, userID).EachPage(func(page pagination.Page) (bool, error) {
   147  		actual, err := users.ExtractRoles(page)
   148  		th.AssertNoErr(t, err)
   149  
   150  		expected := []users.Role{
   151  			{ID: "9fe2ff9ee4384b1894a90878d3e92bab", Name: "foo_role"},
   152  			{ID: "1ea3d56793574b668e85960fbf651e13", Name: "admin"},
   153  		}
   154  
   155  		th.CheckDeepEquals(t, expected, actual)
   156  
   157  		return true, nil
   158  	})
   159  
   160  	th.AssertNoErr(t, err)
   161  }