1
0

jsonld_helper_spec.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe JsonLdHelper do
  4. describe '#equals_or_includes?' do
  5. it 'returns true when value equals' do
  6. expect(helper.equals_or_includes?('foo', 'foo')).to be true
  7. end
  8. it 'returns false when value does not equal' do
  9. expect(helper.equals_or_includes?('foo', 'bar')).to be false
  10. end
  11. it 'returns true when value is included' do
  12. expect(helper.equals_or_includes?(%w(foo baz), 'foo')).to be true
  13. end
  14. it 'returns false when value is not included' do
  15. expect(helper.equals_or_includes?(%w(foo baz), 'bar')).to be false
  16. end
  17. end
  18. describe '#first_of_value' do
  19. context 'value.is_a?(Array)' do
  20. it 'returns value.first' do
  21. value = ['a']
  22. expect(helper.first_of_value(value)).to be 'a'
  23. end
  24. end
  25. context '!value.is_a?(Array)' do
  26. it 'returns value' do
  27. value = 'a'
  28. expect(helper.first_of_value(value)).to be 'a'
  29. end
  30. end
  31. end
  32. describe '#supported_context?' do
  33. context "!json.nil? && equals_or_includes?(json['@context'], ActivityPub::TagManager::CONTEXT)" do
  34. it 'returns true' do
  35. json = { '@context' => ActivityPub::TagManager::CONTEXT }.as_json
  36. expect(helper.supported_context?(json)).to be true
  37. end
  38. end
  39. context 'else' do
  40. it 'returns false' do
  41. json = nil
  42. expect(helper.supported_context?(json)).to be false
  43. end
  44. end
  45. end
  46. describe '#fetch_resource' do
  47. context 'when the second argument is false' do
  48. it 'returns resource even if the retrieved ID and the given URI does not match' do
  49. stub_request(:get, 'https://bob.test/').to_return body: '{"id": "https://alice.test/"}'
  50. stub_request(:get, 'https://alice.test/').to_return body: '{"id": "https://alice.test/"}'
  51. expect(fetch_resource('https://bob.test/', false)).to eq({ 'id' => 'https://alice.test/' })
  52. end
  53. it 'returns nil if the object identified by the given URI and the object identified by the retrieved ID does not match' do
  54. stub_request(:get, 'https://mallory.test/').to_return body: '{"id": "https://marvin.test/"}'
  55. stub_request(:get, 'https://marvin.test/').to_return body: '{"id": "https://alice.test/"}'
  56. expect(fetch_resource('https://mallory.test/', false)).to eq nil
  57. end
  58. end
  59. context 'when the second argument is true' do
  60. it 'returns nil if the retrieved ID and the given URI does not match' do
  61. stub_request(:get, 'https://mallory.test/').to_return body: '{"id": "https://alice.test/"}'
  62. expect(fetch_resource('https://mallory.test/', true)).to eq nil
  63. end
  64. end
  65. end
  66. describe '#fetch_resource_without_id_validation' do
  67. it 'returns nil if the status code is not 200' do
  68. stub_request(:get, 'https://host.test/').to_return status: 400, body: '{}'
  69. expect(fetch_resource_without_id_validation('https://host.test/')).to eq nil
  70. end
  71. it 'returns hash' do
  72. stub_request(:get, 'https://host.test/').to_return status: 200, body: '{}'
  73. expect(fetch_resource_without_id_validation('https://host.test/')).to eq({})
  74. end
  75. end
  76. end