uuid_services.user_services.test 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * @file
  4. * Test the UUID User Services integration.
  5. */
  6. /**
  7. * Test the UUID User Services integration.
  8. */
  9. class UuidUserServicesTest extends ServicesWebTestCase {
  10. /**
  11. * The endpoint configuration.
  12. *
  13. * @var object
  14. */
  15. protected $endpoint = NULL;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public static function getInfo() {
  20. return array(
  21. 'name' => 'UUID User Services tests',
  22. 'description' => 'Test the user services resource UUID methods and actions.',
  23. 'group' => 'UUID',
  24. );
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function setUp() {
  30. parent::setUp(
  31. 'ctools',
  32. 'services',
  33. 'rest_server',
  34. 'uuid_services'
  35. );
  36. $this->endpoint = $this->saveNewEndpoint();
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function saveNewEndpoint() {
  42. $edit = $this->populateEndpointFAPI();
  43. $endpoint = new stdClass();
  44. $endpoint->disabled = FALSE;
  45. $endpoint->api_version = 3;
  46. $endpoint->name = $edit['name'];
  47. $endpoint->server = $edit['server'];
  48. $endpoint->path = $edit['path'];
  49. $endpoint->authentication = array(
  50. 'services' => 'services',
  51. );
  52. $endpoint->server_settings = array(
  53. 'formatters' => array(
  54. 'json' => TRUE,
  55. 'bencode' => TRUE,
  56. 'rss' => TRUE,
  57. 'plist' => TRUE,
  58. 'xmlplist' => TRUE,
  59. 'php' => TRUE,
  60. 'yaml' => TRUE,
  61. 'jsonp' => FALSE,
  62. 'xml' => FALSE,
  63. ),
  64. 'parsers' => array(
  65. 'application/x-yaml' => TRUE,
  66. 'application/json' => TRUE,
  67. 'application/vnd.php.serialized' => TRUE,
  68. 'application/plist' => TRUE,
  69. 'application/plist+xml' => TRUE,
  70. 'application/x-www-form-urlencoded' => TRUE,
  71. 'multipart/form-data' => TRUE,
  72. ),
  73. );
  74. $endpoint->resources = array(
  75. 'user' => array(
  76. 'operations' => array(
  77. 'create' => array(
  78. 'enabled' => 1,
  79. ),
  80. 'retrieve' => array(
  81. 'enabled' => 1,
  82. ),
  83. 'update' => array(
  84. 'enabled' => 1,
  85. ),
  86. 'delete' => array(
  87. 'enabled' => 1,
  88. ),
  89. 'index' => array(
  90. 'enabled' => 1,
  91. ),
  92. ),
  93. ),
  94. );
  95. $endpoint->debug = 1;
  96. $endpoint->export_type = FALSE;
  97. services_endpoint_save($endpoint);
  98. $endpoint = services_endpoint_load($endpoint->name);
  99. $this->assertTrue($endpoint->name == $edit['name'], 'Endpoint successfully created');
  100. return $endpoint;
  101. }
  102. /**
  103. * Tests user Retrieve.
  104. */
  105. public function testUserRetrieve() {
  106. $admin_user = $this->drupalCreateUser(array(
  107. 'administer services',
  108. 'administer users',
  109. ));
  110. $this->drupalLogin($admin_user);
  111. $other_user = $this->drupalCreateUser();
  112. // Verify user is found.
  113. $response = $this->servicesGet($this->endpoint->path . '/user/' . $other_user->uuid);
  114. $this->assertTrue($other_user->uuid == $response['body']->uuid,
  115. 'Successfully received User info');
  116. }
  117. /**
  118. * Tests user Update his own account.
  119. */
  120. public function testUserUpdate() {
  121. $admin_user = $this->drupalCreateUser(array(
  122. 'administer services',
  123. 'administer users',
  124. 'administer permissions',
  125. ));
  126. $this->drupalLogin($admin_user);
  127. $other_user = $this->drupalCreateUser();
  128. $update = array(
  129. 'uuid' => $other_user->uuid,
  130. 'roles' => array(
  131. '2' => 'authenticated user',
  132. '3' => 'administrator',
  133. ),
  134. );
  135. $this->servicesPut($this->endpoint->path . '/user/' . $other_user->uuid, $update);
  136. $user_after_update = user_load($other_user->uid, TRUE);
  137. $this->assertTrue(in_array('administrator', $user_after_update->roles), 'Administrator role successfully added');
  138. }
  139. /**
  140. * Tests user Update another account fail with no permissions.
  141. */
  142. public function testUserUpdatePermFail() {
  143. $user = $this->drupalCreateUser();
  144. $this->drupalLogin($user);
  145. $other_user = $this->drupalCreateUser();
  146. $update = array(
  147. 'uuid' => $other_user->uuid,
  148. 'name' => 'test_edit',
  149. 'roles' => array(
  150. '2' => 'authenticated user',
  151. '3' => 'administrator',
  152. ),
  153. );
  154. $response = $this->servicesPut($this->endpoint->path . '/user/' . $other_user->uuid, $update);
  155. $user_after_update = user_load($other_user->uid, TRUE);
  156. $this->assertNotEqual($update['name'], $user_after_update->name, 'User name was not updated without the needed permissions');
  157. $this->assertFalse(in_array('administrator', $user_after_update->roles), 'Administrator role was not added without the needed permissions');
  158. $this->assertTrue($response['code'] == 403,
  159. 'Updating the user failed without the needed permissions');
  160. }
  161. /**
  162. * Tests user Update his own account fail with no permissions.
  163. */
  164. public function testUserOwnUpdatePermFail() {
  165. $user = $this->drupalCreateUser([
  166. 'access user profiles',
  167. ]);
  168. $this->drupalLogin($user);
  169. $user = user_load($user->uid, TRUE);
  170. $update = array(
  171. 'uuid' => $user->uuid,
  172. 'roles' => array(
  173. '2' => 'authenticated user',
  174. '3' => 'administrator',
  175. ),
  176. );
  177. $this->servicesPut($this->endpoint->path . '/user/' . $user->uuid, $update);
  178. $user_after_update = user_load($user->uid, TRUE);
  179. $this->assertFalse(in_array('administrator', $user_after_update->roles), 'Administrator role was not added without the needed permissions');
  180. $this->assertEqual($user->roles, $user_after_update->roles, 'Existing roles persist after update.');
  181. }
  182. /**
  183. * Tests user Delete.
  184. */
  185. public function testUserDelete() {
  186. $admin_user = $this->drupalCreateUser(array(
  187. 'administer services',
  188. 'administer users',
  189. ));
  190. $this->drupalLogin($admin_user);
  191. $other_user = $this->drupalCreateUser();
  192. $this->servicesDelete($this->endpoint->path . '/user/' . $other_user->uuid);
  193. $user_after_update = user_load($other_user->uid, TRUE);
  194. $this->assertTrue(empty($user_after_update), 'User was deleted');
  195. }
  196. /**
  197. * Tests user Delete fail with no permissions.
  198. */
  199. public function testUserDeletePermFail() {
  200. $user = $this->drupalCreateUser();
  201. $this->drupalLogin($user);
  202. $other_user = $this->drupalCreateUser();
  203. $response = $this->servicesDelete($this->endpoint->path . '/user/' . $other_user->uuid);
  204. $user_after_update = user_load($other_user->uid, TRUE);
  205. $this->assertTrue(!empty($user_after_update), 'User was not deleted without the needed permissions');
  206. $this->assertTrue($response['code'] == 403,
  207. 'Deleting the user failed without the needed permissions');
  208. }
  209. }