AdminUserControllerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\AdminPermission;
  4. use App\Models\AdminRole;
  5. use App\Models\AdminUser;
  6. use Illuminate\Support\Facades\Hash;
  7. use Tests\AdminTestCase;
  8. use Illuminate\Foundation\Testing\WithFaker;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Tests\Traits\RequestActions;
  11. class AdminUserControllerTest extends AdminTestCase
  12. {
  13. use RefreshDatabase;
  14. use RequestActions;
  15. protected $resourceName = 'admin-users';
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->login();
  20. }
  21. /**
  22. * 给用户绑定一个角色,该角色有一个权限。并为用户绑定另一个单独的权限
  23. *
  24. * @param AdminUser|null $user
  25. *
  26. * @return array [roleId, permissionId1, permissionId2]
  27. */
  28. protected function attachAuthToUser(AdminUser $user = null)
  29. {
  30. $user = $user ?? $this->user;
  31. $permission1 = factory(AdminPermission::class)->create(['slug' => 'perm1'])->id;
  32. $permission2 = factory(AdminPermission::class)->create(['slug' => 'perm2'])->id;
  33. $role = factory(AdminRole::class)->create(['slug' => 'role']);
  34. $role->permissions()->attach($permission1);
  35. $user->roles()->attach($role->id);
  36. $user->permissions()->attach($permission2);
  37. return [$role->id, $permission1, $permission2];
  38. }
  39. public function testUser()
  40. {
  41. $this->attachAuthToUser();
  42. $res = $this->get(route('admin.user'));
  43. $res->assertStatus(200)
  44. ->assertJsonFragment(['id' => $this->user->id])
  45. ->assertJsonFragment(['roles' => ['role']])
  46. ->assertJsonFragment(['permissions' => ['perm1', 'perm2']]);
  47. }
  48. public function testEditUser()
  49. {
  50. $this->attachAuthToUser();
  51. $res = $this->get(route('admin.user.edit'));
  52. $res->assertStatus(200)
  53. ->assertJsonCount(1, 'roles')
  54. ->assertJsonCount(1, 'permissions');
  55. }
  56. public function testUpdateUser()
  57. {
  58. $this->attachAuthToUser();
  59. $res = $this->put(route('admin.user.update'), [
  60. 'name' => 'new name',
  61. 'password' => '123456',
  62. 'password_confirmation' => '123456',
  63. 'username' => 'can not update',
  64. 'roles' => [],
  65. 'permissions' => [],
  66. ]);
  67. $res->assertStatus(201)
  68. ->assertSeeText('new name')
  69. // 账号没变
  70. ->assertDontSee('can not update')
  71. // 权限没变
  72. ->assertJsonFragment(['permissions' => ['perm1', 'perm2']]);
  73. // 密码变了
  74. $this->assertTrue(Hash::check('123456', $this->user->password));
  75. }
  76. public function testIndex()
  77. {
  78. factory(AdminUser::class, 20)->create();
  79. $permissions = factory(AdminPermission::class, 20)->create();
  80. $roles = factory(AdminRole::class, 10)->create();
  81. $this->user->roles()->attach($roles->take(3)->pluck('id'));
  82. $this->user->permissions()->attach($permissions->take(3)->pluck('id'));
  83. $res = $this->getResources([
  84. 'page' => 2,
  85. ]);
  86. $res->assertStatus(200)
  87. ->assertJsonCount(6, 'data') // 第二页有 6 个数据
  88. ->assertJsonCount(3, 'data.5.roles') // 第二页第 5 个数据,即 $this->user
  89. ->assertJsonCount(3, 'data.5.permissions');
  90. // 只测试权限和角色名搜索
  91. $res = $this->getResources([
  92. 'role_name' => 'nothing',
  93. ]);
  94. $res->assertStatus(200)
  95. ->assertJsonCount(0, 'data');
  96. $res = $this->getResources([
  97. 'permission_name' => 'nothing',
  98. ]);
  99. $res->assertStatus(200)
  100. ->assertJsonCount(0, 'data');
  101. $res = $this->getResources([
  102. 'role_name' => AdminRole::first()->value('name'),
  103. 'permission_name' => AdminPermission::first()->value('name'),
  104. ]);
  105. $res->assertStatus(200)
  106. ->assertJsonCount(1, 'data');
  107. }
  108. public function testStoreValidation()
  109. {
  110. // username, name, password required
  111. // roles.*, permissions.*, exists
  112. // avatar max:255
  113. $res = $this->storeResource([
  114. 'roles' => [9999],
  115. 'permissions' => [9999],
  116. 'avatar' => str_repeat('a', 256),
  117. ]);
  118. $res->assertJsonValidationErrors([
  119. 'username',
  120. 'name',
  121. 'password',
  122. 'roles.0',
  123. 'permissions.0',
  124. 'avatar',
  125. ]);
  126. // username, name max:100
  127. // password max:20
  128. $res = $this->storeResource([
  129. 'username' => str_repeat('e', 101),
  130. 'name' => str_repeat('e', 101),
  131. 'password' => str_repeat('e', 21),
  132. ]);
  133. $res->assertJsonValidationErrors(['username', 'name', 'password']);
  134. // username unique
  135. // password min:6
  136. $res = $this->storeResource([
  137. 'username' => 'admin',
  138. 'password' => str_repeat('e', 5),
  139. ]);
  140. $res->assertJsonValidationErrors(['username', 'password']);
  141. // password confirmed
  142. $res = $this->storeResource([
  143. 'password' => 'password',
  144. 'password_confirmation' => 'not match',
  145. ]);
  146. $res->assertJsonValidationErrors(['password']);
  147. }
  148. public function testStore()
  149. {
  150. $roles = factory(AdminRole::class, 5)->create();
  151. $permissions = factory(AdminPermission::class, 5)->create();
  152. $pw = '000000';
  153. $userInputs = factory(AdminUser::class)->make([
  154. 'password' => $pw,
  155. ])->toArray();
  156. $res = $this->storeResource($userInputs + [
  157. 'password_confirmation' => $pw,
  158. 'roles' => $roles->take(3)->pluck('id')->toArray(),
  159. 'permissions' => $permissions->take(-2)->pluck('id')->toArray(),
  160. ]);
  161. $res->assertStatus(201);
  162. $userId = $this->getLastInsertId('admin_users');
  163. $this->assertDatabaseHas('admin_users', [
  164. 'id' => $userId,
  165. 'username' => $userInputs['username'],
  166. 'name' => $userInputs['name'],
  167. ]);
  168. $this->assertTrue(Hash::check($pw, AdminUser::find($userId)->password));
  169. $this->assertDatabaseHas('admin_user_role', [
  170. 'user_id' => $userId,
  171. 'role_id' => $roles->take(3)->pluck('id')->first(),
  172. ]);
  173. $this->assertDatabaseHas('admin_user_permission', [
  174. 'user_id' => $userId,
  175. 'permission_id' => $permissions->take(-2)->pluck('id')->first(),
  176. ]);
  177. }
  178. public function testShow()
  179. {
  180. $this->user->roles()->attach(factory(AdminRole::class, 3)->create()->pluck('id'));
  181. $this->user->permissions()->attach(factory(AdminPermission::class, 3)->create()->pluck('id'));
  182. $res = $this->getResource($this->user->id);
  183. $res->assertStatus(200)
  184. ->assertJsonCount(3, 'roles')
  185. ->assertJsonCount(3, 'permissions');
  186. }
  187. public function testUpdate()
  188. {
  189. // 测试更新时,判断 传入的 全路径 头像,是否会替换掉数据库的相对路径
  190. $this->storage
  191. ->getDriver()
  192. ->getConfig()
  193. ->set('url', 'http://domain.com');
  194. $this->user->avatar = '/path/to/avatar/jpg';
  195. $this->user->save();
  196. $this->user->roles()
  197. ->createMany(factory(AdminRole::class, 3)->make()->toArray());
  198. $oldRoleId = $this->getLastInsertId('admin_roles');
  199. $this->user->permissions()
  200. ->createMany(factory(AdminPermission::class, 3)->make()->toArray());
  201. $oldPermissionId = $this->getLastInsertId('admin_permissions');
  202. $newRoles = factory(AdminRole::class, 3)->create()->pluck('id')->toArray();
  203. $newPerms = factory(AdminPermission::class, 3)->create()->pluck('id')->toArray();
  204. $userId = $this->user->id;
  205. $pw = 'new password';
  206. $res = $this->updateResource($userId, [
  207. 'username' => 'admin',
  208. 'name' => 'new name',
  209. 'roles' => $newRoles,
  210. 'permissions' => $newPerms,
  211. 'password' => $pw,
  212. 'password_confirmation' => $pw,
  213. 'avatar' => $this->storage->url($this->user->avatar),
  214. ]);
  215. $res->assertStatus(201);
  216. $this->assertTrue(Hash::check($pw, AdminUser::find($userId)->password));
  217. $this->assertDatabaseHas('admin_users', [
  218. 'id' => $userId,
  219. 'username' => 'admin',
  220. 'name' => 'new name',
  221. 'avatar' => $this->user->avatar,
  222. ]);
  223. // 新角色
  224. $this->assertDatabaseHas('admin_user_role', [
  225. 'user_id' => $userId,
  226. 'role_id' => $newRoles[0],
  227. ]);
  228. // 新权限
  229. $this->assertDatabaseMissing('admin_user_role', [
  230. 'user_id' => $userId,
  231. 'role_id' => $oldRoleId,
  232. ]);
  233. // 旧角色移除
  234. $this->assertDatabaseHas('admin_user_permission', [
  235. 'user_id' => $userId,
  236. 'permission_id' => $newPerms[0],
  237. ]);
  238. // 旧权限移除
  239. $this->assertDatabaseMissing('admin_user_permission', [
  240. 'user_id' => $userId,
  241. 'permission_id' => $oldPermissionId,
  242. ]);
  243. // 移除全部角色权限
  244. $res = $this->updateResource($userId, [
  245. 'roles' => [],
  246. 'permissions' => [],
  247. ]);
  248. $res->assertStatus(201);
  249. $this->assertDatabaseMissing('admin_user_role', [
  250. 'user_id' => $userId,
  251. ]);
  252. // 不填密码, 或者为空
  253. $pw = AdminUser::find($userId)->password;
  254. $res = $this->updateResource($userId, [
  255. 'password' => '',
  256. ]);
  257. $res->assertStatus(201);
  258. $this->assertTrue($pw == AdminUser::find($userId)->password);
  259. }
  260. public function testDestroy()
  261. {
  262. $this->user->roles()->createMany(factory(AdminRole::class, 1)->make()->toArray());
  263. $this->user->permissions()->createMany(factory(AdminPermission::class, 1)->make()->toArray());
  264. $userId = $this->user->id;
  265. $res = $this->destroyResource($userId);
  266. $res->assertStatus(204);
  267. $this->assertDatabaseMissing('admin_users', ['id' => $userId]);
  268. $this->assertDatabaseMissing('admin_user_role', ['user_id' => $userId]);
  269. $this->assertDatabaseMissing('admin_user_permission', ['user_id' => $userId]);
  270. }
  271. public function testEdit()
  272. {
  273. $this->user->roles()->attach($roleIds = factory(AdminRole::class, 3)->create()->pluck('id'));
  274. $this->user->permissions()->attach(factory(AdminPermission::class, 3)->create()->pluck('id'));
  275. $res = $this->editResource($this->user->id);
  276. $res->assertStatus(200)
  277. ->assertJsonFragment(['roles' => $roleIds]);
  278. }
  279. }