diff options
Diffstat (limited to 'spec/controllers')
11 files changed, 333 insertions, 68 deletions
diff --git a/spec/controllers/accounts_controller_spec.rb b/spec/controllers/accounts_controller_spec.rb index 92f888590..a8ade790c 100644 --- a/spec/controllers/accounts_controller_spec.rb +++ b/spec/controllers/accounts_controller_spec.rb @@ -4,6 +4,7 @@ RSpec.describe AccountsController, type: :controller do render_views let(:alice) { Fabricate(:account, username: 'alice') } + let(:eve) { Fabricate(:user) } describe 'GET #show' do let!(:status1) { Status.create!(account: alice, text: 'Hello world') } @@ -19,93 +20,123 @@ RSpec.describe AccountsController, type: :controller do let!(:status_pin3) { StatusPin.create!(account: alice, status: status7, created_at: 10.minutes.ago) } before do + alice.block!(eve.account) status3.media_attachments.create!(account: alice, file: fixture_file_upload('files/attachment.jpg', 'image/jpeg')) end - context 'atom' do + shared_examples 'responses' do before do - get :show, params: { username: alice.username, max_id: status4.stream_entry.id, since_id: status1.stream_entry.id }, format: 'atom' + sign_in(current_user) if defined? current_user + get :show, params: { + username: alice.username, + max_id: (max_id if defined? max_id), + since_id: (since_id if defined? since_id), + current_user: (current_user if defined? current_user), + }, format: format end it 'assigns @account' do expect(assigns(:account)).to eq alice end - it 'assigns @entries' do - entries = assigns(:entries).to_a - expect(entries.size).to eq 2 - expect(entries[0].status).to eq status3 - expect(entries[1].status).to eq status2 + it 'returns http success' do + expect(response).to have_http_status(:success) end - it 'returns http success with Atom' do - expect(response).to have_http_status(:success) + it 'returns correct format' do + expect(response.content_type).to eq content_type end end - context 'activitystreams2' do - before do - get :show, params: { username: alice.username }, format: 'json' - end + context 'atom' do + let(:format) { 'atom' } + let(:content_type) { 'application/atom+xml' } - it 'assigns @account' do - expect(assigns(:account)).to eq alice + shared_examples 'responsed streams' do + it 'assigns @entries' do + entries = assigns(:entries).to_a + expect(entries.size).to eq expected_statuses.size + entries.each.zip(expected_statuses.each) do |entry, expected_status| + expect(entry.status).to eq expected_status + end + end end - it 'returns http success with Activity Streams 2.0' do - expect(response).to have_http_status(:success) - end + include_examples 'responses' - it 'returns application/activity+json' do - expect(response.content_type).to eq 'application/activity+json' - end - end + context 'without max_id nor since_id' do + let(:expected_statuses) { [status7, status6, status5, status4, status3, status2, status1] } - context 'html without since_id nor max_id' do - before do - get :show, params: { username: alice.username } + include_examples 'responsed streams' end - it 'assigns @account' do - expect(assigns(:account)).to eq alice - end + context 'with max_id and since_id' do + let(:max_id) { status4.stream_entry.id } + let(:since_id) { status1.stream_entry.id } + let(:expected_statuses) { [status3, status2] } - it 'assigns @pinned_statuses' do - pinned_statuses = assigns(:pinned_statuses).to_a - expect(pinned_statuses.size).to eq 3 - expect(pinned_statuses[0]).to eq status7 - expect(pinned_statuses[1]).to eq status5 - expect(pinned_statuses[2]).to eq status6 + include_examples 'responsed streams' end + end - it 'returns http success' do - expect(response).to have_http_status(:success) - end + context 'activitystreams2' do + let(:format) { 'json' } + let(:content_type) { 'application/activity+json' } + + include_examples 'responses' end - context 'html with since_id and max_id' do - before do - get :show, params: { username: alice.username, max_id: status4.id, since_id: status1.id } - end + context 'html' do + let(:format) { nil } + let(:content_type) { 'text/html' } - it 'assigns @account' do - expect(assigns(:account)).to eq alice - end + shared_examples 'responsed statuses' do + it 'assigns @pinned_statuses' do + pinned_statuses = assigns(:pinned_statuses).to_a + expect(pinned_statuses.size).to eq expected_pinned_statuses.size + pinned_statuses.each.zip(expected_pinned_statuses.each) do |pinned_status, expected_pinned_status| + expect(pinned_status).to eq expected_pinned_status + end + end - it 'assigns @statuses' do - statuses = assigns(:statuses).to_a - expect(statuses.size).to eq 2 - expect(statuses[0]).to eq status3 - expect(statuses[1]).to eq status2 + it 'assigns @statuses' do + statuses = assigns(:statuses).to_a + expect(statuses.size).to eq expected_statuses.size + statuses.each.zip(expected_statuses.each) do |status, expected_status| + expect(status).to eq expected_status + end + end end - it 'assigns an empty array to @pinned_statuses' do - pinned_statuses = assigns(:pinned_statuses).to_a - expect(pinned_statuses.size).to eq 0 + include_examples 'responses' + + context 'with anonymous visitor' do + context 'without since_id nor max_id' do + let(:expected_statuses) { [status7, status6, status5, status4, status3, status2, status1] } + let(:expected_pinned_statuses) { [status7, status5, status6] } + + include_examples 'responsed statuses' + end + + context 'with since_id nor max_id' do + let(:max_id) { status4.id } + let(:since_id) { status1.id } + let(:expected_statuses) { [status3, status2] } + let(:expected_pinned_statuses) { [] } + + include_examples 'responsed statuses' + end end - it 'returns http success' do - expect(response).to have_http_status(:success) + context 'with blocked visitor' do + let(:current_user) { eve } + + context 'without since_id nor max_id' do + let(:expected_statuses) { [] } + let(:expected_pinned_statuses) { [] } + + include_examples 'responsed statuses' + end end end end diff --git a/spec/controllers/api/v1/accounts/credentials_controller_spec.rb b/spec/controllers/api/v1/accounts/credentials_controller_spec.rb index 461b8b34b..247420d08 100644 --- a/spec/controllers/api/v1/accounts/credentials_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/credentials_controller_spec.rb @@ -51,7 +51,9 @@ describe Api::V1::Accounts::CredentialsController do describe 'with invalid data' do before do - patch :update, params: { note: 'This is too long. ' * 10 } + note = 'This is too long. ' + note = note + 'a' * (Account::MAX_NOTE_LENGTH - note.length + 1) + patch :update, params: { note: note } end it 'returns http unprocessable entity' do diff --git a/spec/controllers/api/v1/accounts/relationships_controller_spec.rb b/spec/controllers/api/v1/accounts/relationships_controller_spec.rb index 431fc2194..f25b86ac1 100644 --- a/spec/controllers/api/v1/accounts/relationships_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/relationships_controller_spec.rb @@ -32,7 +32,7 @@ describe Api::V1::Accounts::RelationshipsController do json = body_as_json expect(json).to be_a Enumerable - expect(json.first[:following]).to be true + expect(json.first[:following]).to be_truthy expect(json.first[:followed_by]).to be false end end @@ -51,7 +51,7 @@ describe Api::V1::Accounts::RelationshipsController do expect(json).to be_a Enumerable expect(json.first[:id]).to eq simon.id.to_s - expect(json.first[:following]).to be true + expect(json.first[:following]).to be_truthy expect(json.first[:followed_by]).to be false expect(json.first[:muting]).to be false expect(json.first[:requested]).to be false diff --git a/spec/controllers/api/v1/accounts_controller_spec.rb b/spec/controllers/api/v1/accounts_controller_spec.rb index c770649ec..f3b879421 100644 --- a/spec/controllers/api/v1/accounts_controller_spec.rb +++ b/spec/controllers/api/v1/accounts_controller_spec.rb @@ -31,10 +31,10 @@ RSpec.describe Api::V1::AccountsController, type: :controller do expect(response).to have_http_status(:success) end - it 'returns JSON with following=true and requested=false' do + it 'returns JSON with following=truthy and requested=false' do json = body_as_json - expect(json[:following]).to be true + expect(json[:following]).to be_truthy expect(json[:requested]).to be false end @@ -50,11 +50,11 @@ RSpec.describe Api::V1::AccountsController, type: :controller do expect(response).to have_http_status(:success) end - it 'returns JSON with following=false and requested=true' do + it 'returns JSON with following=false and requested=truthy' do json = body_as_json expect(json[:following]).to be false - expect(json[:requested]).to be true + expect(json[:requested]).to be_truthy end it 'creates a follow request relation between user and target user' do @@ -137,6 +137,35 @@ RSpec.describe Api::V1::AccountsController, type: :controller do it 'creates a muting relation' do expect(user.account.muting?(other_account)).to be true end + + it 'mutes notifications' do + expect(user.account.muting_notifications?(other_account)).to be true + end + end + + describe 'POST #mute with notifications set to false' do + let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account } + + before do + user.account.follow!(other_account) + post :mute, params: {id: other_account.id, notifications: false } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'does not remove the following relation between user and target user' do + expect(user.account.following?(other_account)).to be true + end + + it 'creates a muting relation' do + expect(user.account.muting?(other_account)).to be true + end + + it 'does not mute notifications' do + expect(user.account.muting_notifications?(other_account)).to be false + end end describe 'POST #unmute' do diff --git a/spec/controllers/api/v1/lists/accounts_controller_spec.rb b/spec/controllers/api/v1/lists/accounts_controller_spec.rb new file mode 100644 index 000000000..953e5909d --- /dev/null +++ b/spec/controllers/api/v1/lists/accounts_controller_spec.rb @@ -0,0 +1,54 @@ +require 'rails_helper' + +describe Api::V1::Lists::AccountsController do + render_views + + let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) } + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read write') } + let(:list) { Fabricate(:list, account: user.account) } + + before do + follow = Fabricate(:follow, account: user.account) + list.accounts << follow.target_account + allow(controller).to receive(:doorkeeper_token) { token } + end + + describe 'GET #index' do + it 'returns http success' do + get :show, params: { list_id: list.id } + + expect(response).to have_http_status(:success) + end + end + + describe 'POST #create' do + let(:bob) { Fabricate(:account, username: 'bob') } + + before do + user.account.follow!(bob) + post :create, params: { list_id: list.id, account_ids: [bob.id] } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'adds account to the list' do + expect(list.accounts.include?(bob)).to be true + end + end + + describe 'DELETE #destroy' do + before do + delete :destroy, params: { list_id: list.id, account_ids: [list.accounts.first.id] } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'removes account from the list' do + expect(list.accounts.count).to eq 0 + end + end +end diff --git a/spec/controllers/api/v1/lists_controller_spec.rb b/spec/controllers/api/v1/lists_controller_spec.rb new file mode 100644 index 000000000..be08c221f --- /dev/null +++ b/spec/controllers/api/v1/lists_controller_spec.rb @@ -0,0 +1,68 @@ +require 'rails_helper' + +RSpec.describe Api::V1::ListsController, type: :controller do + render_views + + let!(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) } + let!(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read write') } + let!(:list) { Fabricate(:list, account: user.account) } + + before { allow(controller).to receive(:doorkeeper_token) { token } } + + describe 'GET #index' do + it 'returns http success' do + get :index + expect(response).to have_http_status(:success) + end + end + + describe 'GET #show' do + it 'returns http success' do + get :show, params: { id: list.id } + expect(response).to have_http_status(:success) + end + end + + describe 'POST #create' do + before do + post :create, params: { title: 'Foo bar' } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'creates list' do + expect(List.where(account: user.account).count).to eq 2 + expect(List.last.title).to eq 'Foo bar' + end + end + + describe 'PUT #update' do + before do + put :update, params: { id: list.id, title: 'Updated title' } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'updates the list' do + expect(list.reload.title).to eq 'Updated title' + end + end + + describe 'DELETE #destroy' do + before do + delete :destroy, params: { id: list.id } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'deletes the list' do + expect(List.find_by(id: list.id)).to be_nil + end + end +end diff --git a/spec/controllers/api/v1/mutes_controller_spec.rb b/spec/controllers/api/v1/mutes_controller_spec.rb index 3e6fa887b..7387b9d2d 100644 --- a/spec/controllers/api/v1/mutes_controller_spec.rb +++ b/spec/controllers/api/v1/mutes_controller_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Api::V1::MutesController, type: :controller do let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'follow') } before do - Fabricate(:mute, account: user.account) + Fabricate(:mute, account: user.account, hide_notifications: false) allow(controller).to receive(:doorkeeper_token) { token } end @@ -18,4 +18,24 @@ RSpec.describe Api::V1::MutesController, type: :controller do expect(response).to have_http_status(:success) end end + + describe 'GET #details' do + before do + get :details, params: { limit: 1 } + end + + let(:mutes) { JSON.parse(response.body) } + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'returns one mute' do + expect(mutes.size).to be(1) + end + + it 'returns whether the mute hides notifications' do + expect(mutes.first["hide_notifications"]).to be(false) + end + end end diff --git a/spec/controllers/api/v1/timelines/list_controller_spec.rb b/spec/controllers/api/v1/timelines/list_controller_spec.rb new file mode 100644 index 000000000..07eba955a --- /dev/null +++ b/spec/controllers/api/v1/timelines/list_controller_spec.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Api::V1::Timelines::ListController do + render_views + + let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) } + let(:list) { Fabricate(:list, account: user.account) } + + before do + allow(controller).to receive(:doorkeeper_token) { token } + end + + context 'with a user context' do + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read') } + + describe 'GET #show' do + before do + follow = Fabricate(:follow, account: user.account) + list.accounts << follow.target_account + PostStatusService.new.call(follow.target_account, 'New status for user home timeline.') + end + + it 'returns http success' do + get :show, params: { id: list.id } + expect(response).to have_http_status(:success) + end + end + end + + context 'with the wrong user context' do + let(:other_user) { Fabricate(:user, account: Fabricate(:account, username: 'bob')) } + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: other_user.id, scopes: 'read') } + + describe 'GET #show' do + it 'returns http not found' do + get :show, params: { id: list.id } + expect(response).to have_http_status(:not_found) + end + end + end + + context 'without a user context' do + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: nil, scopes: 'read') } + + describe 'GET #show' do + it 'returns http unprocessable entity' do + get :show, params: { id: list.id } + + expect(response).to have_http_status(:unprocessable_entity) + expect(response.headers['Link']).to be_nil + end + end + end +end diff --git a/spec/controllers/api/v1/timelines/tag_controller_spec.rb b/spec/controllers/api/v1/timelines/tag_controller_spec.rb index 74de1e81f..6c66ee58e 100644 --- a/spec/controllers/api/v1/timelines/tag_controller_spec.rb +++ b/spec/controllers/api/v1/timelines/tag_controller_spec.rb @@ -5,7 +5,7 @@ require 'rails_helper' describe Api::V1::Timelines::TagController do render_views - let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) } + let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) } before do allow(controller).to receive(:doorkeeper_token) { token } diff --git a/spec/controllers/settings/applications_controller_spec.rb b/spec/controllers/settings/applications_controller_spec.rb index ca66f8d23..90e6a63d5 100644 --- a/spec/controllers/settings/applications_controller_spec.rb +++ b/spec/controllers/settings/applications_controller_spec.rb @@ -2,10 +2,10 @@ require 'rails_helper' describe Settings::ApplicationsController do render_views - + let!(:user) { Fabricate(:user) } let!(:app) { Fabricate(:application, owner: user) } - + before do sign_in user, scope: :user end @@ -21,7 +21,7 @@ describe Settings::ApplicationsController do end end - + describe 'GET #show' do it 'returns http success' do get :show, params: { id: app.id } @@ -110,7 +110,7 @@ describe Settings::ApplicationsController do end end end - + describe 'PATCH #update' do context 'success' do let(:opts) { @@ -131,7 +131,7 @@ describe Settings::ApplicationsController do call_update expect(app.reload.website).to eql(opts[:website]) end - + it 'redirects back to applications page' do expect(call_update).to redirect_to(settings_applications_path) end diff --git a/spec/controllers/settings/keyword_mutes_controller_spec.rb b/spec/controllers/settings/keyword_mutes_controller_spec.rb new file mode 100644 index 000000000..a8c37a072 --- /dev/null +++ b/spec/controllers/settings/keyword_mutes_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Settings::KeywordMutesController, type: :controller do + +end |