GroupShareV3Test.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. use App\Models\Share;
  3. use Illuminate\Foundation\Testing\RefreshDatabase;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Str;
  6. uses(RefreshDatabase::class);
  7. /**
  8. * v3 团队文档:共享给团队的资源。
  9. *
  10. * 端点:
  11. * GET /api/v3/groups/{group}/shares 列表(成员)
  12. * DELETE /api/v3/groups/{group}/shares/{share} 移出团队(团队 owner / manager,或资源拥有者)
  13. *
  14. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  15. */
  16. function shareToTeam(string $team, string $resId, int $resType = 2, int $power = 20): Share
  17. {
  18. $share = new Share;
  19. $share->forceFill([
  20. 'id' => random_int(1, PHP_INT_MAX),
  21. 'res_id' => $resId,
  22. 'res_type' => $resType,
  23. 'cooperator_id' => $team,
  24. 'cooperator_type' => 1,
  25. 'power' => $power,
  26. 'create_time' => time() * 1000,
  27. 'modify_time' => time() * 1000,
  28. ])->save();
  29. return $share;
  30. }
  31. it('lists what is shared with the team', function () {
  32. [$team, $users] = makeTeam();
  33. $author = makeStudio('author');
  34. $channel = makeChannel($author, 'shared channel');
  35. shareToTeam($team, $channel, 2, 20);
  36. shareToTeam($team, (string) Str::uuid(), 3, 10); // 资源已被删掉的文章
  37. $data = $this->getJson("/api/v3/groups/{$team}/shares", authHeader($users['reader']))
  38. ->assertOk()
  39. ->assertJsonPath('meta.total', 2)
  40. ->json('data');
  41. $byType = collect($data)->keyBy('resource.type');
  42. expect($byType['channel'])->toMatchArray(['role' => 'editor']);
  43. expect($byType['channel']['resource'])->toMatchArray([
  44. 'type' => 'channel', 'id' => $channel, 'title' => 'shared channel', 'owner_id' => $author,
  45. ]);
  46. expect($byType['channel']['resource']['owner']['id'])->toBe($author);
  47. expect($byType['article']['resource']['owner'])->toBeNull();
  48. expect($byType['article']['role'])->toBe('reader');
  49. expect($byType['article']['resource']['title'])->toBeNull();
  50. expect($byType['article']['id'])->toBeString();
  51. });
  52. it('hides the team documents from outsiders', function () {
  53. [$team, $users] = makeTeam();
  54. $this->getJson("/api/v3/groups/{$team}/shares", authHeader($users['outsider']))->assertForbidden();
  55. $this->getJson("/api/v3/groups/{$team}/shares")->assertUnauthorized();
  56. });
  57. it('does not query per share', function () {
  58. [$team, $users] = makeTeam();
  59. $countQueriesFor = function (int $extra) use ($team, $users): int {
  60. for ($i = 0; $i < $extra; $i++) {
  61. shareToTeam($team, makeChannel(makeStudio('a-'.Str::random(8))));
  62. }
  63. DB::flushQueryLog();
  64. DB::enableQueryLog();
  65. $this->getJson("/api/v3/groups/{$team}/shares", authHeader($users['reader']))->assertOk();
  66. $count = count(DB::getQueryLog());
  67. DB::disableQueryLog();
  68. return $count;
  69. };
  70. expect($countQueriesFor(6))->toBe($countQueriesFor(1));
  71. });
  72. it('lets team managers remove a share', function (string $actor) {
  73. [$team, $users] = makeTeam();
  74. $share = shareToTeam($team, makeChannel(makeStudio('author')));
  75. $this->deleteJson("/api/v3/groups/{$team}/shares/{$share->id}", [], authHeader($users[$actor]))
  76. ->assertNoContent();
  77. $this->assertModelMissing($share);
  78. })->with(['owner', 'manager']);
  79. it('lets the resource owner take it back even without managing the team', function () {
  80. [$team, $users] = makeTeam();
  81. $share = shareToTeam($team, makeChannel($users['reader']));
  82. $this->deleteJson("/api/v3/groups/{$team}/shares/{$share->id}", [], authHeader($users['reader']))
  83. ->assertNoContent();
  84. $this->assertModelMissing($share);
  85. });
  86. it('does not let editors remove someone else\'s share', function () {
  87. [$team, $users] = makeTeam();
  88. $share = shareToTeam($team, makeChannel(makeStudio('author')));
  89. $this->deleteJson("/api/v3/groups/{$team}/shares/{$share->id}", [], authHeader($users['editor']))
  90. ->assertForbidden();
  91. expect(Share::find($share->id))->not->toBeNull();
  92. });
  93. it('returns 404 for a share of another team', function () {
  94. [$team, $users] = makeTeam();
  95. [$otherTeam] = makeTeam();
  96. $share = shareToTeam($otherTeam, makeChannel(makeStudio('author')));
  97. $this->deleteJson("/api/v3/groups/{$team}/shares/{$share->id}", [], authHeader($users['owner']))
  98. ->assertNotFound();
  99. });