You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.8 KiB

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