Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Refactor and improve tests (mastodon#17386)
Browse files Browse the repository at this point in the history
* Change account and user fabricators to simplify and improve tests

- `Fabricate(:account)` implicitly fabricates an associated `user` if
  no `domain` attribute is given (an account with `domain: nil` is
  considered a local account, but no user record was created), unless
  `user: nil` is passed
- `Fabricate(:account, user: Fabricate(:user))` should still be possible
  but is discouraged.

* Fix and refactor tests

- avoid passing unneeded attributes to `Fabricate(:user)` or
  `Fabricate(:account)`
- avoid embedding `Fabricate(:user)` into a `Fabricate(:account)` or the other
  way around
- prefer `Fabricate(:user, account_attributes: …)` to
  `Fabricate(:user, account: Fabricate(:account, …)`
- also, some tests were using remote accounts with local user records, which is
  not representative of production code.
  • Loading branch information
ClearlyClaire authored Jan 27, 2022
1 parent 03d5934 commit e38fc31
Show file tree
Hide file tree
Showing 95 changed files with 187 additions and 185 deletions.
2 changes: 1 addition & 1 deletion spec/controllers/accounts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe AccountsController, type: :controller do
render_views

let(:account) { Fabricate(:user).account }
let(:account) { Fabricate(:account) }

shared_examples 'cachable response' do
it 'does not set cookies' do
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/admin/accounts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

describe 'GET #show' do
let(:current_user) { Fabricate(:user, admin: true) }
let(:account) { Fabricate(:account, username: 'bob') }
let(:account) { Fabricate(:account) }

it 'returns http success' do
get :show, params: { id: account.id }
Expand All @@ -73,7 +73,7 @@
subject { post :memorialize, params: { id: account.id } }

let(:current_user) { Fabricate(:user, admin: current_user_admin) }
let(:account) { Fabricate(:account, user: user) }
let(:account) { user.account }
let(:user) { Fabricate(:user, admin: target_user_admin) }

context 'when user is admin' do
Expand Down Expand Up @@ -125,7 +125,7 @@
subject { post :enable, params: { id: account.id } }

let(:current_user) { Fabricate(:user, admin: admin) }
let(:account) { Fabricate(:account, user: user) }
let(:account) { user.account }
let(:user) { Fabricate(:user, disabled: true) }

context 'when user is admin' do
Expand Down
12 changes: 5 additions & 7 deletions spec/controllers/admin/change_email_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@

describe "GET #show" do
it "returns http success" do
account = Fabricate(:account)
user = Fabricate(:user, account: account)
user = Fabricate(:user)

get :show, params: { account_id: account.id }
get :show, params: { account_id: user.account.id }

expect(response).to have_http_status(200)
end
Expand All @@ -26,12 +25,11 @@
end

it "returns http success" do
account = Fabricate(:account)
user = Fabricate(:user, account: account)
user = Fabricate(:user)

previous_email = user.email

post :update, params: { account_id: account.id, user: { unconfirmed_email: '[email protected]' } }
post :update, params: { account_id: user.account.id, user: { unconfirmed_email: '[email protected]' } }

user.reload

Expand All @@ -41,7 +39,7 @@

expect(UserMailer).to have_received(:confirmation_instructions).with(user, user.confirmation_token, { to: '[email protected]' })

expect(response).to redirect_to(admin_account_path(account.id))
expect(response).to redirect_to(admin_account_path(user.account.id))
end
end
end
10 changes: 4 additions & 6 deletions spec/controllers/admin/confirmations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

describe 'POST #create' do
it 'confirms the user' do
account = Fabricate(:account)
user = Fabricate(:user, confirmed_at: false, account: account)
post :create, params: { account_id: account.id }
user = Fabricate(:user, confirmed_at: false)
post :create, params: { account_id: user.account.id }

expect(response).to redirect_to(admin_accounts_path)
expect(user.reload).to be_confirmed
Expand All @@ -32,10 +31,9 @@
end

describe 'POST #resernd' do
subject { post :resend, params: { account_id: account.id } }
subject { post :resend, params: { account_id: user.account.id } }

let(:account) { Fabricate(:account) }
let!(:user) { Fabricate(:user, confirmed_at: confirmed_at, account: account) }
let!(:user) { Fabricate(:user, confirmed_at: confirmed_at) }

before do
allow(UserMailer).to receive(:confirmation_instructions) { double(:email, deliver_later: nil) }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/resets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Admin::ResetsController do
render_views

let(:account) { Fabricate(:account, user: Fabricate(:user)) }
let(:account) { Fabricate(:account) }
before do
sign_in Fabricate(:user, admin: true), scope: :user
end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/base_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def error
end

describe 'non-functional accounts handling' do
let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read') }

controller do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Api::V1::Accounts::CredentialsController do
render_views

let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }

context 'with an oauth token' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Api::V1::Accounts::FollowerAccountsController do
render_views

let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:accounts') }
let(:account) { Fabricate(:account) }
let(:alice) { Fabricate(:account) }
Expand Down Expand Up @@ -49,10 +49,10 @@
end

context 'when requesting user is the account owner' do
let(:user) { Fabricate(:user, account: account) }
let(:user) { account.user }

it 'returns all accounts, including muted accounts' do
user.account.mute!(bob)
account.mute!(bob)
get :index, params: { account_id: account.id, limit: 2 }

expect(body_as_json.size).to eq 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Api::V1::Accounts::FollowingAccountsController do
render_views

let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:accounts') }
let(:account) { Fabricate(:account) }
let(:alice) { Fabricate(:account) }
Expand Down Expand Up @@ -49,10 +49,10 @@
end

context 'when requesting user is the account owner' do
let(:user) { Fabricate(:user, account: account) }
let(:user) { account.user }

it 'returns all accounts, including muted accounts' do
user.account.mute!(bob)
account.mute!(bob)
get :index, params: { account_id: account.id, limit: 2 }

expect(body_as_json.size).to eq 2
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/v1/accounts/lists_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Api::V1::Accounts::ListsController do
render_views

let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:lists') }
let(:account) { Fabricate(:account) }
let(:list) { Fabricate(:list, account: user.account) }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/v1/accounts/notes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Api::V1::Accounts::NotesController do
render_views

let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'write:accounts') }
let(:account) { Fabricate(:account) }
let(:comment) { 'foo' }
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/api/v1/accounts/pins_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require 'rails_helper'

RSpec.describe Api::V1::Accounts::PinsController, type: :controller do
let(:john) { Fabricate(:user, account: Fabricate(:account, username: 'john')) }
let(:kevin) { Fabricate(:user, account: Fabricate(:account, username: 'kevin')) }
let(:john) { Fabricate(:user) }
let(:kevin) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: john.id, scopes: 'write:accounts') }

before do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
describe Api::V1::Accounts::RelationshipsController do
render_views

let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:follows') }

before do
allow(controller).to receive(:doorkeeper_token) { token }
end

describe 'GET #index' do
let(:simon) { Fabricate(:user, email: '[email protected]', account: Fabricate(:account, username: 'simon')).account }
let(:lewis) { Fabricate(:user, email: '[email protected]', account: Fabricate(:account, username: 'lewis')).account }
let(:simon) { Fabricate(:account) }
let(:lewis) { Fabricate(:account) }

before do
user.account.follow!(simon)
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/v1/accounts/search_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe Api::V1::Accounts::SearchController, type: :controller do
render_views

let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:accounts') }

before do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Api::V1::Accounts::StatusesController do
render_views

let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:statuses') }

before do
Expand Down
20 changes: 10 additions & 10 deletions spec/controllers/api/v1/accounts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe Api::V1::AccountsController, type: :controller do
render_views

let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:scopes) { '' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }

Expand Down Expand Up @@ -69,7 +69,7 @@

describe 'POST #follow' do
let(:scopes) { 'write:follows' }
let(:other_account) { Fabricate(:user, email: '[email protected]', account: Fabricate(:account, username: 'bob', locked: locked)).account }
let(:other_account) { Fabricate(:account, username: 'bob', locked: locked) }

context do
before do
Expand Down Expand Up @@ -150,7 +150,7 @@

describe 'POST #unfollow' do
let(:scopes) { 'write:follows' }
let(:other_account) { Fabricate(:user, email: '[email protected]', account: Fabricate(:account, username: 'bob')).account }
let(:other_account) { Fabricate(:account, username: 'bob') }

before do
user.account.follow!(other_account)
Expand All @@ -170,7 +170,7 @@

describe 'POST #remove_from_followers' do
let(:scopes) { 'write:follows' }
let(:other_account) { Fabricate(:user, email: '[email protected]', account: Fabricate(:account, username: 'bob')).account }
let(:other_account) { Fabricate(:account, username: 'bob') }

before do
other_account.follow!(user.account)
Expand All @@ -190,7 +190,7 @@

describe 'POST #block' do
let(:scopes) { 'write:blocks' }
let(:other_account) { Fabricate(:user, email: '[email protected]', account: Fabricate(:account, username: 'bob')).account }
let(:other_account) { Fabricate(:account, username: 'bob') }

before do
user.account.follow!(other_account)
Expand All @@ -214,7 +214,7 @@

describe 'POST #unblock' do
let(:scopes) { 'write:blocks' }
let(:other_account) { Fabricate(:user, email: '[email protected]', account: Fabricate(:account, username: 'bob')).account }
let(:other_account) { Fabricate(:account, username: 'bob') }

before do
user.account.block!(other_account)
Expand All @@ -234,7 +234,7 @@

describe 'POST #mute' do
let(:scopes) { 'write:mutes' }
let(:other_account) { Fabricate(:user, email: '[email protected]', account: Fabricate(:account, username: 'bob')).account }
let(:other_account) { Fabricate(:account, username: 'bob') }

before do
user.account.follow!(other_account)
Expand Down Expand Up @@ -262,7 +262,7 @@

describe 'POST #mute with notifications set to false' do
let(:scopes) { 'write:mutes' }
let(:other_account) { Fabricate(:user, email: '[email protected]', account: Fabricate(:account, username: 'bob')).account }
let(:other_account) { Fabricate(:account, username: 'bob') }

before do
user.account.follow!(other_account)
Expand Down Expand Up @@ -290,7 +290,7 @@

describe 'POST #mute with nonzero duration set' do
let(:scopes) { 'write:mutes' }
let(:other_account) { Fabricate(:user, email: '[email protected]', account: Fabricate(:account, username: 'bob')).account }
let(:other_account) { Fabricate(:account, username: 'bob') }

before do
user.account.follow!(other_account)
Expand Down Expand Up @@ -318,7 +318,7 @@

describe 'POST #unmute' do
let(:scopes) { 'write:mutes' }
let(:other_account) { Fabricate(:user, email: '[email protected]', account: Fabricate(:account, username: 'bob')).account }
let(:other_account) { Fabricate(:account, username: 'bob') }

before do
user.account.mute!(other_account)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
render_views

let(:role) { 'moderator' }
let(:user) { Fabricate(:user, role: role, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user, role: role) }
let(:scopes) { 'admin:read admin:write' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:account) { Fabricate(:user).account }
let(:account) { Fabricate(:account) }

before do
allow(controller).to receive(:doorkeeper_token) { token }
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/api/v1/admin/accounts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
render_views

let(:role) { 'moderator' }
let(:user) { Fabricate(:user, role: role, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user, role: role) }
let(:scopes) { 'admin:read admin:write' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:account) { Fabricate(:user).account }
let(:account) { Fabricate(:account) }

before do
allow(controller).to receive(:doorkeeper_token) { token }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/v1/admin/reports_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
render_views

let(:role) { 'moderator' }
let(:user) { Fabricate(:user, role: role, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user, role: role) }
let(:scopes) { 'admin:read admin:write' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:report) { Fabricate(:report) }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/v1/blocks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe Api::V1::BlocksController, type: :controller do
render_views

let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:scopes) { 'read:blocks' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }

Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/api/v1/conversations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
RSpec.describe Api::V1::ConversationsController, type: :controller do
render_views

let!(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let!(:user) { Fabricate(:user, account_attributes: { username: 'alice' }) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:other) { Fabricate(:user, account: Fabricate(:account, username: 'bob')) }
let(:other) { Fabricate(:user) }

before do
allow(controller).to receive(:doorkeeper_token) { token }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/v1/domain_blocks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe Api::V1::DomainBlocksController, type: :controller do
render_views

let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }

before do
Expand Down
Loading

0 comments on commit e38fc31

Please sign in to comment.