2023-02-22 00:55:31 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-27 14:55:23 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe ActivityPub::Adapter do
|
2023-05-03 08:32:30 +00:00
|
|
|
before do
|
|
|
|
test_object_class = Class.new(ActiveModelSerializers::Model) do
|
|
|
|
attributes :foo
|
|
|
|
end
|
|
|
|
stub_const('TestObject', test_object_class)
|
2019-03-27 14:55:23 +00:00
|
|
|
|
2023-05-03 08:32:30 +00:00
|
|
|
test_with_basic_context_serializer = Class.new(ActivityPub::Serializer) do
|
|
|
|
attributes :foo
|
|
|
|
end
|
|
|
|
stub_const('TestWithBasicContextSerializer', test_with_basic_context_serializer)
|
2019-03-27 14:55:23 +00:00
|
|
|
|
2023-05-03 08:32:30 +00:00
|
|
|
test_with_named_context_serializer = Class.new(ActivityPub::Serializer) do
|
|
|
|
context :security
|
|
|
|
attributes :foo
|
|
|
|
end
|
|
|
|
stub_const('TestWithNamedContextSerializer', test_with_named_context_serializer)
|
2019-03-27 14:55:23 +00:00
|
|
|
|
2023-05-03 08:32:30 +00:00
|
|
|
test_with_nested_named_context_serializer = Class.new(ActivityPub::Serializer) do
|
|
|
|
attributes :foo
|
2019-03-27 14:55:23 +00:00
|
|
|
|
2023-05-03 08:32:30 +00:00
|
|
|
has_one :virtual_object, key: :baz, serializer: TestWithNamedContextSerializer
|
2019-03-27 14:55:23 +00:00
|
|
|
|
2023-05-03 08:32:30 +00:00
|
|
|
def virtual_object
|
|
|
|
object
|
|
|
|
end
|
2019-03-27 14:55:23 +00:00
|
|
|
end
|
2023-05-03 08:32:30 +00:00
|
|
|
stub_const('TestWithNestedNamedContextSerializer', test_with_nested_named_context_serializer)
|
2019-03-27 14:55:23 +00:00
|
|
|
|
2023-05-03 08:32:30 +00:00
|
|
|
test_with_context_extension_serializer = Class.new(ActivityPub::Serializer) do
|
|
|
|
context_extensions :sensitive
|
|
|
|
attributes :foo
|
|
|
|
end
|
|
|
|
stub_const('TestWithContextExtensionSerializer', test_with_context_extension_serializer)
|
2019-03-27 14:55:23 +00:00
|
|
|
|
2023-05-03 08:32:30 +00:00
|
|
|
test_with_nested_context_extension_serializer = Class.new(ActivityPub::Serializer) do
|
|
|
|
context_extensions :manually_approves_followers
|
|
|
|
attributes :foo
|
2019-03-27 14:55:23 +00:00
|
|
|
|
2023-05-03 08:32:30 +00:00
|
|
|
has_one :virtual_object, key: :baz, serializer: TestWithContextExtensionSerializer
|
2019-03-27 14:55:23 +00:00
|
|
|
|
2023-05-03 08:32:30 +00:00
|
|
|
def virtual_object
|
|
|
|
object
|
|
|
|
end
|
2019-03-27 14:55:23 +00:00
|
|
|
end
|
2023-05-03 08:32:30 +00:00
|
|
|
stub_const('TestWithNestedContextExtensionSerializer', test_with_nested_context_extension_serializer)
|
2019-03-27 14:55:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#serializable_hash' do
|
|
|
|
subject { ActiveModelSerializers::SerializableResource.new(TestObject.new(foo: 'bar'), serializer: serializer_class, adapter: described_class).as_json }
|
|
|
|
|
2023-02-20 04:24:14 +00:00
|
|
|
let(:serializer_class) {}
|
|
|
|
|
2019-03-27 14:55:23 +00:00
|
|
|
context 'when serializer defines no context' do
|
|
|
|
let(:serializer_class) { TestWithBasicContextSerializer }
|
|
|
|
|
|
|
|
it 'renders a basic @context' do
|
|
|
|
expect(subject).to include({ '@context' => 'https://www.w3.org/ns/activitystreams' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when serializer defines a named context' do
|
|
|
|
let(:serializer_class) { TestWithNamedContextSerializer }
|
|
|
|
|
|
|
|
it 'renders a @context with both items' do
|
|
|
|
expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'] })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when serializer has children that define a named context' do
|
|
|
|
let(:serializer_class) { TestWithNestedNamedContextSerializer }
|
|
|
|
|
|
|
|
it 'renders a @context with both items' do
|
|
|
|
expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'] })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when serializer defines context extensions' do
|
|
|
|
let(:serializer_class) { TestWithContextExtensionSerializer }
|
|
|
|
|
|
|
|
it 'renders a @context with the extension' do
|
|
|
|
expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', { 'sensitive' => 'as:sensitive' }] })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when serializer has children that define context extensions' do
|
|
|
|
let(:serializer_class) { TestWithNestedContextExtensionSerializer }
|
|
|
|
|
|
|
|
it 'renders a @context with both extensions' do
|
|
|
|
expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', { 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', 'sensitive' => 'as:sensitive' }] })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|