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