adapter_spec.rb 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::Adapter do
  4. before do
  5. test_object_class = Class.new(ActiveModelSerializers::Model) do
  6. attributes :foo
  7. end
  8. stub_const('TestObject', test_object_class)
  9. test_with_basic_context_serializer = Class.new(ActivityPub::Serializer) do
  10. attributes :foo
  11. end
  12. stub_const('TestWithBasicContextSerializer', test_with_basic_context_serializer)
  13. test_with_named_context_serializer = Class.new(ActivityPub::Serializer) do
  14. context :security
  15. attributes :foo
  16. end
  17. stub_const('TestWithNamedContextSerializer', test_with_named_context_serializer)
  18. test_with_nested_named_context_serializer = Class.new(ActivityPub::Serializer) do
  19. attributes :foo
  20. has_one :virtual_object, key: :baz, serializer: TestWithNamedContextSerializer
  21. def virtual_object
  22. object
  23. end
  24. end
  25. stub_const('TestWithNestedNamedContextSerializer', test_with_nested_named_context_serializer)
  26. test_with_context_extension_serializer = Class.new(ActivityPub::Serializer) do
  27. context_extensions :sensitive
  28. attributes :foo
  29. end
  30. stub_const('TestWithContextExtensionSerializer', test_with_context_extension_serializer)
  31. test_with_nested_context_extension_serializer = Class.new(ActivityPub::Serializer) do
  32. context_extensions :manually_approves_followers
  33. attributes :foo
  34. has_one :virtual_object, key: :baz, serializer: TestWithContextExtensionSerializer
  35. def virtual_object
  36. object
  37. end
  38. end
  39. stub_const('TestWithNestedContextExtensionSerializer', test_with_nested_context_extension_serializer)
  40. end
  41. describe '#serializable_hash' do
  42. subject { ActiveModelSerializers::SerializableResource.new(TestObject.new(foo: 'bar'), serializer: serializer_class, adapter: described_class).as_json }
  43. let(:serializer_class) { nil }
  44. context 'when serializer defines no context' do
  45. let(:serializer_class) { TestWithBasicContextSerializer }
  46. it 'renders a basic @context' do
  47. expect(subject).to include({ '@context': 'https://www.w3.org/ns/activitystreams' })
  48. end
  49. end
  50. context 'when serializer defines a named context' do
  51. let(:serializer_class) { TestWithNamedContextSerializer }
  52. it 'renders a @context with both items' do
  53. expect(subject).to include({ '@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'] })
  54. end
  55. end
  56. context 'when serializer has children that define a named context' do
  57. let(:serializer_class) { TestWithNestedNamedContextSerializer }
  58. it 'renders a @context with both items' do
  59. expect(subject).to include({ '@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'] })
  60. end
  61. end
  62. context 'when serializer defines context extensions' do
  63. let(:serializer_class) { TestWithContextExtensionSerializer }
  64. it 'renders a @context with the extension' do
  65. expect(subject).to include({ '@context': ['https://www.w3.org/ns/activitystreams', { 'sensitive' => 'as:sensitive' }] })
  66. end
  67. end
  68. context 'when serializer has children that define context extensions' do
  69. let(:serializer_class) { TestWithNestedContextExtensionSerializer }
  70. it 'renders a @context with both extensions' do
  71. expect(subject).to include({ '@context': ['https://www.w3.org/ns/activitystreams', { 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', 'sensitive' => 'as:sensitive' }] })
  72. end
  73. end
  74. end
  75. end