Skip to content

Commit b320c9e

Browse files
authored
Prefer to_json over JSON.generate when simple strings in stub request (mastodon#38258)
1 parent f3035a8 commit b320c9e

25 files changed

Lines changed: 177 additions & 138 deletions

spec/lib/activitypub/activity/announce_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
context 'when sender is followed by a local account' do
3636
before do
3737
Fabricate(:account).follow!(sender)
38-
stub_request(:get, 'https://example.com/actor/hello-world').to_return(body: JSON.generate(unknown_object_json), headers: { 'Content-Type': 'application/activity+json' })
38+
stub_request(:get, 'https://example.com/actor/hello-world').to_return(body: unknown_object_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
3939
subject.perform
4040
end
4141

@@ -120,7 +120,7 @@
120120
let(:object_json) { 'https://example.com/actor/hello-world' }
121121

122122
before do
123-
stub_request(:get, 'https://example.com/actor/hello-world').to_return(body: JSON.generate(unknown_object_json), headers: { 'Content-Type': 'application/activity+json' })
123+
stub_request(:get, 'https://example.com/actor/hello-world').to_return(body: unknown_object_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
124124
end
125125

126126
context 'when the relay is enabled' do

spec/lib/activitypub/activity/create_spec.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,8 +1028,8 @@ def activity_for_object(json)
10281028
)
10291029
end
10301030

1031-
before do
1032-
stub_request(:get, approval_uri).to_return(headers: { 'Content-Type': 'application/activity+json' }, body: JSON.generate({
1031+
let(:quote_authorization_json) do
1032+
{
10331033
'@context': [
10341034
'https://www.w3.org/ns/activitystreams',
10351035
{
@@ -1054,7 +1054,11 @@ def activity_for_object(json)
10541054
attributedTo: ActivityPub::TagManager.instance.uri_for(quoted_status.account),
10551055
interactingObject: object_json[:id],
10561056
interactionTarget: ActivityPub::TagManager.instance.uri_for(quoted_status),
1057-
}))
1057+
}
1058+
end
1059+
1060+
before do
1061+
stub_request(:get, approval_uri).to_return(headers: { 'Content-Type': 'application/activity+json' }, body: quote_authorization_json.to_json)
10581062
end
10591063

10601064
it 'creates a status with a verified quote' do
@@ -1084,8 +1088,8 @@ def activity_for_object(json)
10841088
)
10851089
end
10861090

1087-
before do
1088-
stub_request(:get, approval_uri).to_return(headers: { 'Content-Type': 'application/activity+json' }, body: JSON.generate({
1091+
let(:quote_authorization_json) do
1092+
{
10891093
'@context': [
10901094
'https://www.w3.org/ns/activitystreams',
10911095
{
@@ -1110,7 +1114,11 @@ def activity_for_object(json)
11101114
attributedTo: ActivityPub::TagManager.instance.uri_for(quoted_status.account),
11111115
interactingObject: object_json[:id],
11121116
interactionTarget: ActivityPub::TagManager.instance.uri_for(quoted_status),
1113-
}))
1117+
}
1118+
end
1119+
1120+
before do
1121+
stub_request(:get, approval_uri).to_return(headers: { 'Content-Type': 'application/activity+json' }, body: quote_authorization_json.to_json)
11141122
end
11151123

11161124
it 'creates a status without the verified quote' do
@@ -1217,7 +1225,7 @@ def activity_for_object(json)
12171225
before do
12181226
stub_request(:get, object_json[:id])
12191227
.with(headers: { Authorization: "Bearer #{token}" })
1220-
.to_return(body: JSON.generate(object_json), headers: { 'Content-Type': 'application/activity+json' })
1228+
.to_return(body: object_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
12211229

12221230
subject.perform
12231231
end

spec/lib/activitypub/activity/quote_request_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686

8787
context 'when trying to quote a quotable local status' do
8888
before do
89-
stub_request(:get, 'https://example.com/unknown-status').to_return(status: 200, body: JSON.generate(status_json), headers: { 'Content-Type': 'application/activity+json' })
89+
stub_request(:get, 'https://example.com/unknown-status').to_return(status: 200, body: status_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
9090
quoted_post.update(quote_approval_policy: InteractionPolicy::POLICY_FLAGS[:public] << 16)
9191
end
9292

spec/lib/activitypub/activity_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
before do
9090
sender.update(uri: ActivityPub::TagManager.instance.uri_for(sender))
9191

92-
stub_request(:get, approval_uri).to_return(headers: { 'Content-Type': 'application/activity+json' }, body: JSON.generate(approval_payload))
92+
stub_request(:get, approval_uri).to_return(headers: { 'Content-Type': 'application/activity+json' }, body: approval_payload.to_json)
9393
end
9494

9595
context 'when getting them in order' do

spec/lib/activitypub/dereferencer_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
let(:uri) { nil }
1313

1414
before do
15-
stub_request(:get, 'https://example.com/foo').to_return(body: JSON.generate(object), headers: { 'Content-Type' => 'application/activity+json' })
15+
stub_request(:get, 'https://example.com/foo').to_return(body: object.to_json, headers: { 'Content-Type' => 'application/activity+json' })
1616
end
1717

1818
context 'with a URI' do

spec/lib/activitypub/forwarder_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454

5555
it 'correctly forwards to expected remote followers' do
5656
expect { subject.forward! }
57-
.to enqueue_sidekiq_job(ActivityPub::LowPriorityDeliveryWorker).with(JSON.generate(payload), anything, eve.preferred_inbox_url)
58-
.and enqueue_sidekiq_job(ActivityPub::LowPriorityDeliveryWorker).with(JSON.generate(payload), anything, mallory.preferred_inbox_url)
57+
.to enqueue_sidekiq_job(ActivityPub::LowPriorityDeliveryWorker).with(payload.to_json, anything, eve.preferred_inbox_url)
58+
.and enqueue_sidekiq_job(ActivityPub::LowPriorityDeliveryWorker).with(payload.to_json, anything, mallory.preferred_inbox_url)
5959
end
6060
end
6161
end

spec/lib/mastodon/cli/domains_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
end
8888

8989
def json_summary
90-
JSON.generate('host.example': { activity: {} })
90+
{ 'host.example': { activity: {} } }.to_json
9191
end
9292
end
9393
end

spec/lib/webhooks/payload_renderer_spec.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,34 @@
33
require 'rails_helper'
44

55
RSpec.describe Webhooks::PayloadRenderer do
6-
subject(:renderer) { described_class.new(json) }
6+
subject(:renderer) { described_class.new(payload.to_json) }
77

88
let(:event) { Webhooks::EventPresenter.new(type, object) }
99
let(:payload) { ActiveModelSerializers::SerializableResource.new(event, serializer: REST::Admin::WebhookEventSerializer, scope: nil, scope_name: :current_user).as_json }
10-
let(:json) { JSON.generate(payload) }
1110

1211
describe '#render' do
12+
subject { renderer.render(template) }
13+
1314
context 'when event is account.approved' do
1415
let(:type) { 'account.approved' }
15-
let(:object) { Fabricate(:account, display_name: 'Foo"') }
16+
let(:object) { Fabricate(:account, display_name: 'Foo"', username: 'foofoobarbar') }
17+
18+
context 'with event-related variables' do
19+
let(:template) { 'foo={{event}}' }
1620

17-
it 'renders event-related variables into template' do
18-
expect(renderer.render('foo={{event}}')).to eq 'foo=account.approved'
21+
it { is_expected.to eq('foo=account.approved') }
1922
end
2023

21-
it 'renders event-specific variables into template' do
22-
expect(renderer.render('foo={{object.username}}')).to eq "foo=#{object.username}"
24+
context 'with event-specific variables' do
25+
let(:template) { 'foo={{object.username}}' }
26+
27+
it { is_expected.to eq('foo=foofoobarbar') }
2328
end
2429

25-
it 'escapes values for use in JSON' do
26-
expect(renderer.render('foo={{object.account.display_name}}')).to eq 'foo=Foo\\"'
30+
context 'with values needing JSON escape' do
31+
let(:template) { 'foo={{object.account.display_name}}' }
32+
33+
it { is_expected.to eq('foo=Foo\\"') }
2734
end
2835
end
2936
end

spec/requests/api/v1/donation_campaigns_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
end
9292

9393
before do
94-
stub_request(:get, "#{api_url}?platform=web&seed=#{seed}&locale=en").to_return(body: JSON.generate(campaign_json), status: 200)
94+
stub_request(:get, "#{api_url}?platform=web&seed=#{seed}&locale=en").to_return(body: campaign_json.to_json, status: 200)
9595
end
9696

9797
it 'returns the expected campaign' do

spec/services/activitypub/fetch_featured_collection_service_spec.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@
7575

7676
shared_examples 'sets pinned posts' do
7777
before do
78-
stub_request(:get, 'https://example.com/account/pinned/known').to_return(status: 200, body: JSON.generate(status_json_pinned_known), headers: { 'Content-Type': 'application/activity+json' })
79-
stub_request(:get, 'https://example.com/account/pinned/unknown-inlined').to_return(status: 200, body: JSON.generate(status_json_pinned_unknown_inlined), headers: { 'Content-Type': 'application/activity+json' })
78+
stub_request(:get, 'https://example.com/account/pinned/known').to_return(status: 200, body: status_json_pinned_known.to_json, headers: { 'Content-Type': 'application/activity+json' })
79+
stub_request(:get, 'https://example.com/account/pinned/unknown-inlined').to_return(status: 200, body: status_json_pinned_unknown_inlined.to_json, headers: { 'Content-Type': 'application/activity+json' })
8080
stub_request(:get, 'https://example.com/account/pinned/unknown-unreachable').to_return(status: 404)
81-
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: JSON.generate(status_json_pinned_unknown_reachable), headers: { 'Content-Type': 'application/activity+json' })
82-
stub_request(:get, 'https://example.com/account/collections/featured').to_return(status: 200, body: JSON.generate(featured_with_null), headers: { 'Content-Type': 'application/activity+json' })
81+
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: status_json_pinned_unknown_reachable.to_json, headers: { 'Content-Type': 'application/activity+json' })
82+
stub_request(:get, 'https://example.com/account/collections/featured').to_return(status: 200, body: featured_with_null.to_json, headers: { 'Content-Type': 'application/activity+json' })
8383

8484
subject
8585
end
@@ -101,7 +101,7 @@
101101
let(:collection_or_uri) { actor.featured_collection_url }
102102

103103
before do
104-
stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: JSON.generate(payload), headers: { 'Content-Type': 'application/activity+json' })
104+
stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: payload.to_json, headers: { 'Content-Type': 'application/activity+json' })
105105
end
106106

107107
it_behaves_like 'sets pinned posts'
@@ -122,7 +122,7 @@
122122

123123
context 'when the endpoint is a Collection' do
124124
before do
125-
stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: JSON.generate(payload), headers: { 'Content-Type': 'application/activity+json' })
125+
stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: payload.to_json, headers: { 'Content-Type': 'application/activity+json' })
126126
end
127127

128128
it_behaves_like 'sets pinned posts'
@@ -139,7 +139,7 @@
139139
end
140140

141141
before do
142-
stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: JSON.generate(payload), headers: { 'Content-Type': 'application/activity+json' })
142+
stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: payload.to_json, headers: { 'Content-Type': 'application/activity+json' })
143143
end
144144

145145
it_behaves_like 'sets pinned posts'
@@ -148,11 +148,12 @@
148148
let(:items) { 'https://example.com/account/pinned/unknown-reachable' }
149149

150150
before do
151-
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: JSON.generate(status_json_pinned_unknown_reachable), headers: { 'Content-Type': 'application/activity+json' })
152-
subject
151+
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: status_json_pinned_unknown_reachable.to_json, headers: { 'Content-Type': 'application/activity+json' })
153152
end
154153

155154
it 'sets expected posts as pinned posts' do
155+
subject
156+
156157
expect(actor.pinned_statuses.pluck(:uri)).to contain_exactly(
157158
'https://example.com/account/pinned/unknown-reachable'
158159
)
@@ -175,7 +176,7 @@
175176
end
176177

177178
before do
178-
stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: JSON.generate(payload), headers: { 'Content-Type': 'application/activity+json' })
179+
stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: payload.to_json, headers: { 'Content-Type': 'application/activity+json' })
179180
end
180181

181182
it_behaves_like 'sets pinned posts'
@@ -184,11 +185,12 @@
184185
let(:items) { 'https://example.com/account/pinned/unknown-reachable' }
185186

186187
before do
187-
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: JSON.generate(status_json_pinned_unknown_reachable), headers: { 'Content-Type': 'application/activity+json' })
188-
subject
188+
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: status_json_pinned_unknown_reachable.to_json, headers: { 'Content-Type': 'application/activity+json' })
189189
end
190190

191191
it 'sets expected posts as pinned posts' do
192+
subject
193+
192194
expect(actor.pinned_statuses.pluck(:uri)).to contain_exactly(
193195
'https://example.com/account/pinned/unknown-reachable'
194196
)

0 commit comments

Comments
 (0)