OpsUpdatePaliSynonymsTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. use App\Services\OpenSearchService;
  3. beforeEach(function () {
  4. config(['mint.app.ops_token' => 'secret-ops-token']);
  5. });
  6. function updateSynonyms(?string $token, string $version = '20260731')
  7. {
  8. $headers = $token === null ? [] : ['Authorization' => 'Bearer '.$token];
  9. return test()->putJson("/api/ops/update-pali-synonyms/{$version}", [], $headers);
  10. }
  11. it('rejects a request without a token', function () {
  12. updateSynonyms(null)->assertForbidden();
  13. });
  14. it('rejects a request with a wrong token', function () {
  15. updateSynonyms('wrong-token')->assertForbidden();
  16. });
  17. it('rejects any token when APP_OPS_TOKEN is not configured', function () {
  18. config(['mint.app.ops_token' => '']);
  19. updateSynonyms('')->assertForbidden();
  20. });
  21. it('updates the synonyms path with a valid token', function () {
  22. $this->mock(OpenSearchService::class)
  23. ->shouldReceive('updatePaliSynonymsPath')
  24. ->once()
  25. ->with('20260731')
  26. ->andReturn(['path' => 'analysis/pali-synonyms-20260731.txt', 'settings' => ['acknowledged' => true]]);
  27. updateSynonyms('secret-ops-token')
  28. ->assertOk()
  29. ->assertJson([
  30. 'ok' => true,
  31. 'data' => [
  32. 'version' => '20260731',
  33. 'synonyms_path' => 'analysis/pali-synonyms-20260731.txt',
  34. ],
  35. ]);
  36. });
  37. it('returns 500 when OpenSearch rejects the update', function () {
  38. $this->mock(OpenSearchService::class)
  39. ->shouldReceive('updatePaliSynonymsPath')
  40. ->once()
  41. ->andThrow(new Exception('Index [wikipali] does not exist.'));
  42. updateSynonyms('secret-ops-token')
  43. ->assertStatus(500)
  44. ->assertJson(['ok' => false]);
  45. });
  46. it('does not match a version containing path separators', function () {
  47. updateSynonyms('secret-ops-token', '..%2Fetc')->assertNotFound();
  48. });