2
0

ChannelContentAccessV3Test.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. use App\Models\Channel;
  3. use App\Services\OpenSearchService;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Illuminate\Support\Str;
  6. uses(RefreshDatabase::class);
  7. /**
  8. * 按 channel 取正文的两个 v3 接口要判 channel 的读权限:
  9. * GET /api/v3/tipitaka-reading/{channel}
  10. * GET /api/v3/search/{id}(整章正文文档 tipitaka_chapter_{book}-{para}_{channel})
  11. *
  12. * 私有 channel 只有拥有者、团队成员、被共享的人能读;公开与「公开不列出」谁都能读。
  13. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  14. */
  15. function contentChannel(string $ownerUid, int $status): string
  16. {
  17. $uid = makeChannel($ownerUid, 'content '.Str::random(6));
  18. Channel::where('uid', $uid)->update(['status' => $status]);
  19. return $uid;
  20. }
  21. /** 让 search/{id} 不真的去连 OpenSearch:被放行时返回一条最小文档 */
  22. function fakeSearchDocument(): void
  23. {
  24. test()->mock(OpenSearchService::class)
  25. ->shouldReceive('get')
  26. ->andReturn(['_id' => 'doc', '_source' => [
  27. 'id' => 'doc', 'resource_id' => 'res', 'resource_type' => 'chapter', 'language' => 'zh',
  28. 'updated_at' => '2026-01-01T00:00:00Z',
  29. 'title' => ['text' => ['zh' => '标题']], 'content' => ['text' => ['zh' => '正文']],
  30. ]]);
  31. }
  32. /* ---------------- tipitaka-reading ---------------- */
  33. it('reads a private channel only with access', function (string $who, int $status) {
  34. [$team, $users] = makeTeam();
  35. $channel = contentChannel($team, Channel::STATUS_PRIVATE);
  36. $headers = $who === 'anonymous' ? [] : authHeader($users[$who]);
  37. $this->getJson("/api/v3/tipitaka-reading/{$channel}", $headers)->assertStatus($status);
  38. })->with([
  39. 'anonymous' => ['anonymous', 403],
  40. 'outsider' => ['outsider', 403],
  41. 'team reader' => ['reader', 200],
  42. 'team owner' => ['owner', 200],
  43. ]);
  44. it('reads a private channel shared with me', function () {
  45. $me = makeStudio('me');
  46. $channel = contentChannel(makeStudio('author'), Channel::STATUS_PRIVATE);
  47. grantChannelShare($channel, $me, 10);
  48. $this->getJson("/api/v3/tipitaka-reading/{$channel}", authHeader($me))->assertOk();
  49. });
  50. it('reads public and unlisted channels without login', function (int $status) {
  51. $channel = contentChannel(makeStudio('author'), $status);
  52. $this->getJson("/api/v3/tipitaka-reading/{$channel}")->assertOk();
  53. })->with(['public' => [Channel::STATUS_PUBLIC], 'unlisted' => [Channel::STATUS_UNLISTED]]);
  54. it('returns 404 for an unknown or deleted channel', function () {
  55. $this->getJson('/api/v3/tipitaka-reading/'.Str::uuid())->assertNotFound();
  56. $owner = makeStudio('owner');
  57. $channel = contentChannel($owner, Channel::STATUS_PUBLIC);
  58. Channel::where('uid', $channel)->first()->delete();
  59. $this->getJson("/api/v3/tipitaka-reading/{$channel}")->assertNotFound();
  60. });
  61. /* ---------------- search/{id} 的整章正文 ---------------- */
  62. it('guards whole-chapter search documents by their channel', function () {
  63. fakeSearchDocument();
  64. [$team, $users] = makeTeam();
  65. $private = contentChannel($team, Channel::STATUS_PRIVATE);
  66. $doc = "tipitaka_chapter_1-2_{$private}";
  67. $this->getJson("/api/v3/search/{$doc}")->assertForbidden();
  68. $this->getJson("/api/v3/search/{$doc}", authHeader($users['outsider']))->assertForbidden();
  69. $this->getJson("/api/v3/search/{$doc}", authHeader($users['reader']))->assertOk();
  70. });
  71. it('serves whole-chapter documents of public and unlisted channels to anyone', function (int $status) {
  72. fakeSearchDocument();
  73. $channel = contentChannel(makeStudio('author'), $status);
  74. $this->getJson("/api/v3/search/tipitaka_chapter_1-2_{$channel}")->assertOk();
  75. })->with(['public' => [Channel::STATUS_PUBLIC], 'unlisted' => [Channel::STATUS_UNLISTED]]);
  76. it('leaves other search documents alone', function () {
  77. fakeSearchDocument();
  78. $this->getJson('/api/v3/search/term_'.Str::uuid())->assertOk();
  79. });