Skip to content

Commit

Permalink
Reduce .times usage in request and controller specs (mastodon#27949)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski authored and vmstan committed Dec 14, 2023
1 parent 39ae2f4 commit 9d45eeb
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion spec/controllers/admin/invites_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

describe 'POST #deactivate_all' do
it 'expires all invites, then redirects to admin_invites_path' do
invites = Fabricate.times(2, :invite, expires_at: nil)
invites = Fabricate.times(1, :invite, expires_at: nil)

post :deactivate_all

Expand Down
4 changes: 2 additions & 2 deletions spec/requests/api/v1/admin/domain_allows_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
end

context 'when there are allowed domains' do
let!(:domain_allows) { Fabricate.times(5, :domain_allow) }
let!(:domain_allows) { Fabricate.times(2, :domain_allow) }
let(:expected_response) do
domain_allows.map do |domain_allow|
{
Expand All @@ -53,7 +53,7 @@
end

context 'with limit param' do
let(:params) { { limit: 2 } }
let(:params) { { limit: 1 } }

it 'returns only the requested number of allowed domains' do
subject
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/api/v1/bookmarks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
end

let(:params) { {} }
let!(:bookmarks) { Fabricate.times(3, :bookmark, account: user.account) }
let!(:bookmarks) { Fabricate.times(2, :bookmark, account: user.account) }

let(:expected_response) do
bookmarks.map do |bookmark|
Expand All @@ -37,7 +37,7 @@
end

context 'with limit param' do
let(:params) { { limit: 2 } }
let(:params) { { limit: 1 } }

it 'paginates correctly', :aggregate_failures do
subject
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/api/v1/favourites_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
end

let(:params) { {} }
let!(:favourites) { Fabricate.times(3, :favourite, account: user.account) }
let!(:favourites) { Fabricate.times(2, :favourite, account: user.account) }

let(:expected_response) do
favourites.map do |favourite|
Expand All @@ -37,7 +37,7 @@
end

context 'with limit param' do
let(:params) { { limit: 2 } }
let(:params) { { limit: 1 } }

it 'returns only the requested number of favourites' do
subject
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/api/v1/featured_tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
end

context 'when the requesting user has no featured tag' do
before { Fabricate.times(3, :featured_tag) }
before { Fabricate(:featured_tag) }

it 'returns an empty body' do
get '/api/v1/featured_tags', headers: headers
Expand All @@ -44,7 +44,7 @@
end

context 'when the requesting user has featured tags' do
let!(:user_featured_tags) { Fabricate.times(5, :featured_tag, account: user.account) }
let!(:user_featured_tags) { Fabricate.times(1, :featured_tag, account: user.account) }

it 'returns only the featured tags belonging to the requesting user' do
get '/api/v1/featured_tags', headers: headers
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/api/v1/follow_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
get '/api/v1/follow_requests', headers: headers, params: params
end

let(:accounts) { Fabricate.times(5, :account) }
let(:accounts) { Fabricate.times(2, :account) }
let(:params) { {} }

let(:expected_response) do
Expand All @@ -40,7 +40,7 @@
end

context 'with limit param' do
let(:params) { { limit: 2 } }
let(:params) { { limit: 1 } }

it 'returns only the requested number of follow requests' do
subject
Expand Down
6 changes: 3 additions & 3 deletions spec/requests/api/v1/followed_tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
get '/api/v1/followed_tags', headers: headers, params: params
end

let!(:tag_follows) { Fabricate.times(5, :tag_follow, account: user.account) }
let!(:tag_follows) { Fabricate.times(2, :tag_follow, account: user.account) }
let(:params) { {} }

let(:expected_response) do
Expand Down Expand Up @@ -41,7 +41,7 @@
end

context 'with limit param' do
let(:params) { { limit: 3 } }
let(:params) { { limit: 1 } }

it 'returns only the requested number of follow tags' do
subject
Expand All @@ -58,7 +58,7 @@
it 'sets the correct pagination header for the next path' do
subject

expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_followed_tags_url(limit: params[:limit], max_id: tag_follows[2].id))
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_followed_tags_url(limit: params[:limit], max_id: tag_follows.last.id))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/v1/lists/accounts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

let(:params) { { limit: 0 } }
let(:list) { Fabricate(:list, account: user.account) }
let(:accounts) { Fabricate.times(3, :account) }
let(:accounts) { Fabricate.times(2, :account) }

let(:expected_response) do
accounts.map do |account|
Expand Down
12 changes: 6 additions & 6 deletions spec/requests/api/v1/mutes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
get '/api/v1/mutes', headers: headers, params: params
end

let!(:mutes) { Fabricate.times(3, :mute, account: user.account) }
let!(:mutes) { Fabricate.times(2, :mute, account: user.account) }
let(:params) { {} }

it_behaves_like 'forbidden for wrong scope', 'write write:mutes'
Expand All @@ -33,7 +33,7 @@
end

context 'with limit param' do
let(:params) { { limit: 2 } }
let(:params) { { limit: 1 } }

it 'returns only the requested number of muted accounts' do
subject
Expand All @@ -46,8 +46,8 @@

headers = response.headers['Link']

expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_mutes_url(limit: params[:limit], since_id: mutes[2].id.to_s))
expect(headers.find_link(%w(rel next)).href).to eq(api_v1_mutes_url(limit: params[:limit], max_id: mutes[1].id.to_s))
expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_mutes_url(limit: params[:limit], since_id: mutes.last.id.to_s))
expect(headers.find_link(%w(rel next)).href).to eq(api_v1_mutes_url(limit: params[:limit], max_id: mutes.last.id.to_s))
end
end

Expand All @@ -72,8 +72,8 @@

body = body_as_json

expect(body.size).to eq 2
expect(body[0][:id]).to eq mutes[2].target_account_id.to_s
expect(body.size).to eq 1
expect(body[0][:id]).to eq mutes[1].target_account_id.to_s
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/v1/notifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def body_json_types
end

before do
Fabricate.times(3, :notification, account: user.account)
Fabricate(:notification, account: user.account)
end

it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/v1/statuses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
context 'when exceeding rate limit' do
before do
rate_limiter = RateLimiter.new(user.account, family: :statuses)
300.times { rate_limiter.record! }
RateLimiter::FAMILIES[:statuses][:limit].times { rate_limiter.record! }
end

it 'returns rate limit headers', :aggregate_failures do
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/v2/filters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
get '/api/v2/filters', headers: headers
end

let!(:filters) { Fabricate.times(3, :custom_filter, account: user.account) }
let!(:filters) { Fabricate.times(2, :custom_filter, account: user.account) }

it_behaves_like 'forbidden for wrong scope', 'write write:filters'
it_behaves_like 'unauthorized for invalid token'
Expand Down

0 comments on commit 9d45eeb

Please sign in to comment.