2
0

ShareStoreTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. use App\Models\AiModel;
  3. use App\Models\Article;
  4. use App\Models\Project;
  5. use App\Models\Share;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Illuminate\Support\Str;
  8. uses(RefreshDatabase::class);
  9. /**
  10. * POST /v2/share(添加协作者)的快照测试。
  11. *
  12. * 原实现不校验登录也不校验资源归属,任何人都能给任意资源签 editor(power 20),
  13. * 再凭它改别人的译文。修复只收紧「谁能调」,拥有者的请求与响应形状保持不变——
  14. * v4 的 CollaboratorAdd 还在调它。
  15. */
  16. function shareChannelPayload(string $resId, array $userIds, string $role = 'editor', int $resType = 2): array
  17. {
  18. return [
  19. 'res_id' => $resId,
  20. 'res_type' => $resType,
  21. 'user_id' => $userIds,
  22. 'user_type' => 'user',
  23. 'role' => $role,
  24. ];
  25. }
  26. function makeShareArticle(string $ownerUid): string
  27. {
  28. $uid = (string) Str::uuid();
  29. (new Article)->forceFill([
  30. 'id' => random_int(1, PHP_INT_MAX),
  31. 'uid' => $uid,
  32. 'title' => 'article',
  33. 'owner' => $ownerUid,
  34. 'owner_id' => 0,
  35. 'editor_id' => 0,
  36. 'create_time' => time() * 1000,
  37. 'modify_time' => time() * 1000,
  38. ])->save();
  39. return $uid;
  40. }
  41. function makeShareProject(string $ownerUid): string
  42. {
  43. $uid = (string) Str::uuid();
  44. (new Project)->forceFill([
  45. 'uid' => $uid,
  46. 'title' => 'project',
  47. 'owner_id' => $ownerUid,
  48. 'editor_id' => $ownerUid,
  49. ])->save();
  50. return $uid;
  51. }
  52. function makeShareAiModel(string $ownerUid): string
  53. {
  54. $uid = (string) Str::uuid();
  55. (new AiModel)->forceFill([
  56. 'id' => random_int(1, PHP_INT_MAX),
  57. 'uid' => $uid,
  58. 'name' => 'model',
  59. 'real_name' => 'model-'.Str::random(6),
  60. 'model' => 'gpt-4',
  61. 'owner_id' => $ownerUid,
  62. 'editor_id' => $ownerUid,
  63. ])->save();
  64. return $uid;
  65. }
  66. it('lets the channel owner add an editor', function () {
  67. $owner = makeStudio('owner');
  68. $channel = makeChannel($owner);
  69. $friend = makeStudio('friend');
  70. $this->postJson('/api/v2/share', shareChannelPayload($channel, [$friend]), authHeader($owner))
  71. ->assertOk()
  72. ->assertJsonPath('ok', true)
  73. ->assertJsonPath('data', 1);
  74. $row = Share::where('res_id', $channel)->where('cooperator_id', $friend)->first();
  75. expect($row)->not->toBeNull();
  76. expect((int) $row->power)->toBe(20);
  77. expect((int) $row->cooperator_type)->toBe(0);
  78. });
  79. it('maps the reader role to power 10', function () {
  80. $owner = makeStudio('owner');
  81. $channel = makeChannel($owner);
  82. $friend = makeStudio('friend');
  83. $this->postJson('/api/v2/share', shareChannelPayload($channel, [$friend], 'reader'), authHeader($owner))
  84. ->assertJsonPath('ok', true);
  85. expect((int) Share::where('res_id', $channel)->value('power'))->toBe(10);
  86. });
  87. it('lets owners share articles, projects and ai models', function (int $resType, Closure $make) {
  88. $owner = makeStudio('owner');
  89. $resId = $make($owner);
  90. $friend = makeStudio('friend');
  91. $this->postJson('/api/v2/share', shareChannelPayload($resId, [$friend], 'editor', $resType), authHeader($owner))
  92. ->assertJsonPath('ok', true);
  93. expect(Share::where('res_id', $resId)->where('cooperator_id', $friend)->exists())->toBeTrue();
  94. })->with([
  95. 'article' => [3, fn (string $owner) => makeShareArticle($owner)],
  96. 'project' => [7, fn (string $owner) => makeShareProject($owner)],
  97. 'ai model' => [8, fn (string $owner) => makeShareAiModel($owner)],
  98. ]);
  99. it('rejects anonymous requests', function () {
  100. $channel = makeChannel(makeStudio('owner'));
  101. $attacker = makeStudio('attacker');
  102. $this->postJson('/api/v2/share', shareChannelPayload($channel, [$attacker]))
  103. ->assertStatus(401)
  104. ->assertJsonPath('ok', false);
  105. expect(Share::where('res_id', $channel)->exists())->toBeFalse();
  106. });
  107. it('rejects users who do not own the resource', function () {
  108. $channel = makeChannel(makeStudio('owner'));
  109. $attacker = makeStudio('attacker');
  110. $this->postJson('/api/v2/share', shareChannelPayload($channel, [$attacker]), authHeader($attacker))
  111. ->assertStatus(403)
  112. ->assertJsonPath('ok', false);
  113. expect(Share::where('res_id', $channel)->exists())->toBeFalse();
  114. });
  115. it('rejects a wrong res_type that would hide the real owner', function () {
  116. $owner = makeStudio('owner');
  117. $article = makeShareArticle($owner);
  118. $attacker = makeStudio('attacker');
  119. // 资源是 article,attacker 却谎称它是自己名下的 channel
  120. $this->postJson('/api/v2/share', shareChannelPayload($article, [$attacker], 'editor', 2), authHeader($attacker))
  121. ->assertStatus(403);
  122. expect(Share::where('res_id', $article)->exists())->toBeFalse();
  123. });
  124. it('does not let an editor collaborator add more collaborators', function () {
  125. $owner = makeStudio('owner');
  126. $channel = makeChannel($owner);
  127. $editor = makeStudio('editor');
  128. $outsider = makeStudio('outsider');
  129. $this->postJson('/api/v2/share', shareChannelPayload($channel, [$editor]), authHeader($owner))
  130. ->assertJsonPath('ok', true);
  131. $this->postJson('/api/v2/share', shareChannelPayload($channel, [$outsider]), authHeader($editor))
  132. ->assertStatus(403);
  133. expect(Share::where('res_id', $channel)->where('cooperator_id', $outsider)->exists())->toBeFalse();
  134. });