2017-08-09 05:52:15 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe JsonLdHelper do
|
|
|
|
describe '#equals_or_includes?' do
|
|
|
|
it 'returns true when value equals' do
|
|
|
|
expect(helper.equals_or_includes?('foo', 'foo')).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when value does not equal' do
|
|
|
|
expect(helper.equals_or_includes?('foo', 'bar')).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when value is included' do
|
|
|
|
expect(helper.equals_or_includes?(%w(foo baz), 'foo')).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when value is not included' do
|
|
|
|
expect(helper.equals_or_includes?(%w(foo baz), 'bar')).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#first_of_value' do
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'when value.is_a?(Array)' do
|
2019-01-08 14:18:27 +11:00
|
|
|
it 'returns value.first' do
|
|
|
|
value = ['a']
|
|
|
|
expect(helper.first_of_value(value)).to be 'a'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'with !value.is_a?(Array)' do
|
2019-01-08 14:18:27 +11:00
|
|
|
it 'returns value' do
|
|
|
|
value = 'a'
|
|
|
|
expect(helper.first_of_value(value)).to be 'a'
|
|
|
|
end
|
|
|
|
end
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#supported_context?' do
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'when json is present and in an activitypub tagmanager context' do
|
2019-01-08 14:18:27 +11:00
|
|
|
it 'returns true' do
|
|
|
|
json = { '@context' => ActivityPub::TagManager::CONTEXT }.as_json
|
|
|
|
expect(helper.supported_context?(json)).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'when not in activitypub tagmanager context' do
|
2019-01-08 14:18:27 +11:00
|
|
|
it 'returns false' do
|
|
|
|
json = nil
|
|
|
|
expect(helper.supported_context?(json)).to be false
|
|
|
|
end
|
|
|
|
end
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#fetch_resource' do
|
2017-10-04 10:13:48 +11:00
|
|
|
context 'when the second argument is false' do
|
|
|
|
it 'returns resource even if the retrieved ID and the given URI does not match' do
|
2024-02-16 21:56:12 +11:00
|
|
|
stub_request(:get, 'https://bob.test/').to_return(body: '{"id": "https://alice.test/"}', headers: { 'Content-Type': 'application/activity+json' })
|
|
|
|
stub_request(:get, 'https://alice.test/').to_return(body: '{"id": "https://alice.test/"}', headers: { 'Content-Type': 'application/activity+json' })
|
2017-10-04 10:13:48 +11:00
|
|
|
|
2018-04-25 22:12:28 +10:00
|
|
|
expect(fetch_resource('https://bob.test/', false)).to eq({ 'id' => 'https://alice.test/' })
|
2017-10-04 10:13:48 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil if the object identified by the given URI and the object identified by the retrieved ID does not match' do
|
2024-02-16 21:56:12 +11:00
|
|
|
stub_request(:get, 'https://mallory.test/').to_return(body: '{"id": "https://marvin.test/"}', headers: { 'Content-Type': 'application/activity+json' })
|
|
|
|
stub_request(:get, 'https://marvin.test/').to_return(body: '{"id": "https://alice.test/"}', headers: { 'Content-Type': 'application/activity+json' })
|
2017-10-04 10:13:48 +11:00
|
|
|
|
2023-02-20 16:14:50 +11:00
|
|
|
expect(fetch_resource('https://mallory.test/', false)).to be_nil
|
2017-10-04 10:13:48 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the second argument is true' do
|
|
|
|
it 'returns nil if the retrieved ID and the given URI does not match' do
|
2024-02-16 21:56:12 +11:00
|
|
|
stub_request(:get, 'https://mallory.test/').to_return(body: '{"id": "https://alice.test/"}', headers: { 'Content-Type': 'application/activity+json' })
|
2023-02-20 16:14:50 +11:00
|
|
|
expect(fetch_resource('https://mallory.test/', true)).to be_nil
|
2017-10-04 10:13:48 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#fetch_resource_without_id_validation' do
|
|
|
|
it 'returns nil if the status code is not 200' do
|
2024-02-16 21:56:12 +11:00
|
|
|
stub_request(:get, 'https://host.test/').to_return(status: 400, body: '{}', headers: { 'Content-Type': 'application/activity+json' })
|
2023-02-20 16:14:50 +11:00
|
|
|
expect(fetch_resource_without_id_validation('https://host.test/')).to be_nil
|
2017-10-04 10:13:48 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns hash' do
|
2024-02-16 21:56:12 +11:00
|
|
|
stub_request(:get, 'https://host.test/').to_return(status: 200, body: '{}', headers: { 'Content-Type': 'application/activity+json' })
|
2018-04-25 22:12:28 +10:00
|
|
|
expect(fetch_resource_without_id_validation('https://host.test/')).to eq({})
|
2017-10-04 10:13:48 +11:00
|
|
|
end
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|
2022-02-04 00:09:04 +11:00
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'with compaction and forwarding' do
|
2022-02-04 00:09:04 +11:00
|
|
|
let(:json) do
|
|
|
|
{
|
|
|
|
'@context' => [
|
|
|
|
'https://www.w3.org/ns/activitystreams',
|
|
|
|
'https://w3id.org/security/v1',
|
|
|
|
{
|
|
|
|
'obsolete' => 'http://ostatus.org#',
|
|
|
|
'convo' => 'obsolete:conversation',
|
|
|
|
'new' => 'https://obscure-unreleased-test.joinmastodon.org/#',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
'type' => 'Create',
|
|
|
|
'to' => ['https://www.w3.org/ns/activitystreams#Public'],
|
|
|
|
'object' => {
|
|
|
|
'id' => 'https://example.com/status',
|
|
|
|
'type' => 'Note',
|
|
|
|
'inReplyTo' => nil,
|
|
|
|
'convo' => 'https://example.com/conversation',
|
|
|
|
'tag' => [
|
|
|
|
{
|
|
|
|
'type' => 'Mention',
|
|
|
|
'href' => ['foo'],
|
2023-02-19 08:54:30 +11:00
|
|
|
},
|
2022-02-04 00:09:04 +11:00
|
|
|
],
|
|
|
|
},
|
|
|
|
'signature' => {
|
|
|
|
'type' => 'RsaSignature2017',
|
|
|
|
'created' => '2022-02-02T12:00:00Z',
|
|
|
|
'creator' => 'https://example.com/actor#main-key',
|
|
|
|
'signatureValue' => 'some-sig',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#compact' do
|
|
|
|
it 'properly compacts JSON-LD with alternative context definitions' do
|
|
|
|
expect(compact(json).dig('object', 'conversation')).to eq 'https://example.com/conversation'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'compacts single-item arrays' do
|
|
|
|
expect(compact(json).dig('object', 'tag', 'href')).to eq 'foo'
|
|
|
|
end
|
|
|
|
|
2023-12-19 03:47:22 +11:00
|
|
|
it 'compacts the activitystreams Public collection' do
|
2022-02-04 00:09:04 +11:00
|
|
|
expect(compact(json)['to']).to eq 'as:Public'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'properly copies signature' do
|
|
|
|
expect(compact(json)['signature']).to eq json['signature']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'patch_for_forwarding!' do
|
|
|
|
it 'properly patches incompatibilities' do
|
|
|
|
json['object'].delete('convo')
|
|
|
|
compacted = compact(json)
|
|
|
|
patch_for_forwarding!(json, compacted)
|
|
|
|
expect(compacted['to']).to eq ['https://www.w3.org/ns/activitystreams#Public']
|
|
|
|
expect(compacted.dig('object', 'tag', 0, 'href')).to eq ['foo']
|
2023-02-20 16:14:50 +11:00
|
|
|
expect(safe_for_forwarding?(json, compacted)).to be true
|
2022-02-04 00:09:04 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'safe_for_forwarding?' do
|
|
|
|
it 'deems a safe compacting as such' do
|
|
|
|
json['object'].delete('convo')
|
|
|
|
compacted = compact(json)
|
2023-10-20 01:55:06 +11:00
|
|
|
patch_for_forwarding!(json, compacted)
|
2022-02-04 00:09:04 +11:00
|
|
|
expect(compacted['to']).to eq ['https://www.w3.org/ns/activitystreams#Public']
|
2023-02-20 16:14:50 +11:00
|
|
|
expect(safe_for_forwarding?(json, compacted)).to be true
|
2022-02-04 00:09:04 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'deems an unsafe compacting as such' do
|
|
|
|
compacted = compact(json)
|
2023-10-20 01:55:06 +11:00
|
|
|
patch_for_forwarding!(json, compacted)
|
2022-02-04 00:09:04 +11:00
|
|
|
expect(compacted['to']).to eq ['https://www.w3.org/ns/activitystreams#Public']
|
2023-02-20 16:14:50 +11:00
|
|
|
expect(safe_for_forwarding?(json, compacted)).to be false
|
2022-02-04 00:09:04 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|