jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 1 | package testing |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
jrperritt | 8ce5e28 | 2016-04-13 14:31:01 -0500 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame^] | 7 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/identity/v2/users" |
| 8 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
| 9 | th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper" |
| 10 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper/client" |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | func TestList(t *testing.T) { |
| 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
| 16 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 17 | MockListUserResponse(t) |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 18 | |
| 19 | count := 0 |
| 20 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 21 | err := users.List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 22 | count++ |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 23 | actual, err := users.ExtractUsers(page) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 24 | th.AssertNoErr(t, err) |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 25 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 26 | expected := []users.User{ |
| 27 | { |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 28 | ID: "u1000", |
| 29 | Name: "John Smith", |
| 30 | Username: "jqsmith", |
| 31 | Email: "john.smith@example.org", |
| 32 | Enabled: true, |
| 33 | TenantID: "12345", |
| 34 | }, |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 35 | { |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 36 | ID: "u1001", |
| 37 | Name: "Jane Smith", |
| 38 | Username: "jqsmith", |
| 39 | Email: "jane.smith@example.org", |
| 40 | Enabled: true, |
| 41 | TenantID: "12345", |
| 42 | }, |
| 43 | } |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 44 | th.CheckDeepEquals(t, expected, actual) |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 45 | return true, nil |
| 46 | }) |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 47 | th.AssertNoErr(t, err) |
| 48 | th.AssertEquals(t, 1, count) |
| 49 | } |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 50 | |
| 51 | func TestCreateUser(t *testing.T) { |
| 52 | th.SetupHTTP() |
| 53 | defer th.TeardownHTTP() |
| 54 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 55 | mockCreateUserResponse(t) |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 56 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 57 | opts := users.CreateOpts{ |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 58 | Name: "new_user", |
| 59 | TenantID: "12345", |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 60 | Enabled: gophercloud.Disabled, |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 61 | Email: "new_user@foo.com", |
| 62 | } |
| 63 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 64 | user, err := users.Create(client.ServiceClient(), opts).Extract() |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 65 | |
| 66 | th.AssertNoErr(t, err) |
| 67 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 68 | expected := &users.User{ |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 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 | } |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 78 | |
| 79 | func TestGetUser(t *testing.T) { |
| 80 | th.SetupHTTP() |
| 81 | defer th.TeardownHTTP() |
| 82 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 83 | mockGetUserResponse(t) |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 84 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 85 | user, err := users.Get(client.ServiceClient(), "new_user").Extract() |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 86 | th.AssertNoErr(t, err) |
| 87 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 88 | expected := &users.User{ |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 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 | } |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 98 | |
| 99 | func TestUpdateUser(t *testing.T) { |
| 100 | th.SetupHTTP() |
| 101 | defer th.TeardownHTTP() |
| 102 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 103 | mockUpdateUserResponse(t) |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 104 | |
| 105 | id := "c39e3de9be2d4c779f1dfd6abacc176d" |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 106 | opts := users.UpdateOpts{ |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 107 | Name: "new_name", |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 108 | Enabled: gophercloud.Enabled, |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 109 | Email: "new_email@foo.com", |
| 110 | } |
| 111 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 112 | user, err := users.Update(client.ServiceClient(), id, opts).Extract() |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 113 | |
| 114 | th.AssertNoErr(t, err) |
| 115 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 116 | expected := &users.User{ |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 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 | } |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 126 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 127 | func TestDeleteUser(t *testing.T) { |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 128 | th.SetupHTTP() |
| 129 | defer th.TeardownHTTP() |
| 130 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 131 | mockDeleteUserResponse(t) |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 132 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 133 | res := users.Delete(client.ServiceClient(), "c39e3de9be2d4c779f1dfd6abacc176d") |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 134 | th.AssertNoErr(t, res.Err) |
| 135 | } |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 136 | |
| 137 | func TestListingUserRoles(t *testing.T) { |
| 138 | th.SetupHTTP() |
| 139 | defer th.TeardownHTTP() |
| 140 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 141 | mockListRolesResponse(t) |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 142 | |
| 143 | tenantID := "1d8b6120dcc640fda4fc9194ffc80273" |
| 144 | userID := "c39e3de9be2d4c779f1dfd6abacc176d" |
| 145 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 146 | err := users.ListRoles(client.ServiceClient(), tenantID, userID).EachPage(func(page pagination.Page) (bool, error) { |
| 147 | actual, err := users.ExtractRoles(page) |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 148 | th.AssertNoErr(t, err) |
| 149 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 150 | expected := []users.Role{ |
| 151 | {ID: "9fe2ff9ee4384b1894a90878d3e92bab", Name: "foo_role"}, |
| 152 | {ID: "1ea3d56793574b668e85960fbf651e13", Name: "admin"}, |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | th.CheckDeepEquals(t, expected, actual) |
| 156 | |
| 157 | return true, nil |
| 158 | }) |
| 159 | |
| 160 | th.AssertNoErr(t, err) |
| 161 | } |