diff options
author | pluralcafe-docker <git@plural.cafe> | 2018-12-27 21:35:47 +0000 |
---|---|---|
committer | pluralcafe-docker <git@plural.cafe> | 2018-12-27 21:35:47 +0000 |
commit | 797a8429a0deb511e6d6092edad39f856231534e (patch) | |
tree | 6e44d3c2a5a662dfc4e4087fdc391b8e7bb41dba /spec | |
parent | 94894b8a6ad1247306497dc8c0c47d52a8a2f72c (diff) | |
parent | f349fe2159fb36e598263f2797f041417ef7c2da (diff) |
Merge branch 'glitch'
Diffstat (limited to 'spec')
37 files changed, 1102 insertions, 184 deletions
diff --git a/spec/controllers/admin/accounts_controller_spec.rb b/spec/controllers/admin/accounts_controller_spec.rb index dbcad3c2d..a348ab3d7 100644 --- a/spec/controllers/admin/accounts_controller_spec.rb +++ b/spec/controllers/admin/accounts_controller_spec.rb @@ -191,58 +191,6 @@ RSpec.describe Admin::AccountsController, type: :controller do end end - describe 'POST #disable' do - subject { post :disable, params: { id: account.id } } - - let(:current_user) { Fabricate(:user, admin: current_user_admin) } - let(:account) { Fabricate(:account, user: user) } - let(:user) { Fabricate(:user, disabled: false, admin: target_user_admin) } - - context 'when user is admin' do - let(:current_user_admin) { true } - - context 'when target user is admin' do - let(:target_user_admin) { true } - - it 'fails to disable account' do - is_expected.to have_http_status :forbidden - expect(user.reload).not_to be_disabled - end - end - - context 'when target user is not admin' do - let(:target_user_admin) { false } - - it 'succeeds in disabling account' do - is_expected.to redirect_to admin_account_path(account.id) - expect(user.reload).to be_disabled - end - end - end - - context 'when user is not admin' do - let(:current_user_admin) { false } - - context 'when target user is admin' do - let(:target_user_admin) { true } - - it 'fails to disable account' do - is_expected.to have_http_status :forbidden - expect(user.reload).not_to be_disabled - end - end - - context 'when target user is not admin' do - let(:target_user_admin) { false } - - it 'fails to disable account' do - is_expected.to have_http_status :forbidden - expect(user.reload).not_to be_disabled - end - end - end - end - describe 'POST #redownload' do subject { post :redownload, params: { id: account.id } } diff --git a/spec/controllers/admin/reports_controller_spec.rb b/spec/controllers/admin/reports_controller_spec.rb index bcc789c57..b428299ee 100644 --- a/spec/controllers/admin/reports_controller_spec.rb +++ b/spec/controllers/admin/reports_controller_spec.rb @@ -46,73 +46,37 @@ describe Admin::ReportsController do end end - describe 'PUT #update' do - describe 'with an unknown outcome' do - it 'rejects the change' do - report = Fabricate(:report) - put :update, params: { id: report, outcome: 'unknown' } - - expect(response).to have_http_status(404) - end - end - - describe 'with an outcome of `resolve`' do - it 'resolves the report' do - report = Fabricate(:report) - - put :update, params: { id: report, outcome: 'resolve' } - expect(response).to redirect_to(admin_reports_path) - report.reload - expect(report.action_taken_by_account).to eq user.account - expect(report.action_taken).to eq true - end - end - - describe 'with an outsome of `silence`' do - it 'silences the reported account' do - report = Fabricate(:report) - - put :update, params: { id: report, outcome: 'silence' } - expect(response).to redirect_to(admin_reports_path) - report.reload - expect(report.action_taken_by_account).to eq user.account - expect(report.action_taken).to eq true - expect(report.target_account).to be_silenced - end - end - - describe 'with an outsome of `reopen`' do - it 'reopens the report' do - report = Fabricate(:report) + describe 'POST #reopen' do + it 'reopens the report' do + report = Fabricate(:report) - put :update, params: { id: report, outcome: 'reopen' } - expect(response).to redirect_to(admin_report_path(report)) - report.reload - expect(report.action_taken_by_account).to eq nil - expect(report.action_taken).to eq false - end + put :reopen, params: { id: report } + expect(response).to redirect_to(admin_report_path(report)) + report.reload + expect(report.action_taken_by_account).to eq nil + expect(report.action_taken).to eq false end + end - describe 'with an outsome of `assign_to_self`' do - it 'reopens the report' do - report = Fabricate(:report) + describe 'POST #assign_to_self' do + it 'reopens the report' do + report = Fabricate(:report) - put :update, params: { id: report, outcome: 'assign_to_self' } - expect(response).to redirect_to(admin_report_path(report)) - report.reload - expect(report.assigned_account).to eq user.account - end + put :assign_to_self, params: { id: report } + expect(response).to redirect_to(admin_report_path(report)) + report.reload + expect(report.assigned_account).to eq user.account end + end - describe 'with an outsome of `unassign`' do - it 'reopens the report' do - report = Fabricate(:report) + describe 'POST #unassign' do + it 'reopens the report' do + report = Fabricate(:report) - put :update, params: { id: report, outcome: 'unassign' } - expect(response).to redirect_to(admin_report_path(report)) - report.reload - expect(report.assigned_account).to eq nil - end + put :unassign, params: { id: report } + expect(response).to redirect_to(admin_report_path(report)) + report.reload + expect(report.assigned_account).to eq nil end end end diff --git a/spec/controllers/admin/silences_controller_spec.rb b/spec/controllers/admin/silences_controller_spec.rb deleted file mode 100644 index 78560eb39..000000000 --- a/spec/controllers/admin/silences_controller_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'rails_helper' - -describe Admin::SilencesController do - render_views - - before do - sign_in Fabricate(:user, admin: true), scope: :user - end - - describe 'POST #create' do - it 'redirects to admin accounts page' do - account = Fabricate(:account, silenced: false) - - post :create, params: { account_id: account.id } - - account.reload - expect(account.silenced?).to eq true - expect(response).to redirect_to(admin_accounts_path) - end - end - - describe 'DELETE #destroy' do - it 'redirects to admin accounts page' do - account = Fabricate(:account, silenced: true) - - delete :destroy, params: { account_id: account.id } - - account.reload - expect(account.silenced?).to eq false - expect(response).to redirect_to(admin_accounts_path) - end - end -end diff --git a/spec/controllers/admin/suspensions_controller_spec.rb b/spec/controllers/admin/suspensions_controller_spec.rb deleted file mode 100644 index 1bc33e490..000000000 --- a/spec/controllers/admin/suspensions_controller_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -require 'rails_helper' - -describe Admin::SuspensionsController do - render_views - - before do - sign_in Fabricate(:user, admin: true), scope: :user - end - - describe 'GET #new' do - it 'returns 200' do - get :new, params: { account_id: Fabricate(:account).id, report_id: Fabricate(:report).id } - expect(response).to have_http_status(200) - end - end - - describe 'POST #create' do - it 'redirects to admin accounts page' do - account = Fabricate(:account, suspended: false) - expect(Admin::SuspensionWorker).to receive(:perform_async).with(account.id) - - post :create, params: { account_id: account.id, form_admin_suspension_confirmation: { acct: account.acct } } - - expect(response).to redirect_to(admin_accounts_path) - end - end - - describe 'DELETE #destroy' do - it 'redirects to admin accounts page' do - account = Fabricate(:account, suspended: true) - - delete :destroy, params: { account_id: account.id } - - account.reload - expect(account.suspended?).to eq false - expect(response).to redirect_to(admin_accounts_path) - end - end -end diff --git a/spec/controllers/api/v1/accounts/pins_controller_spec.rb b/spec/controllers/api/v1/accounts/pins_controller_spec.rb new file mode 100644 index 000000000..c71935df2 --- /dev/null +++ b/spec/controllers/api/v1/accounts/pins_controller_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +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(:token) { Fabricate(:accessible_access_token, resource_owner_id: john.id, scopes: 'write:accounts') } + + before do + kevin.account.followers << john.account + allow(controller).to receive(:doorkeeper_token) { token } + end + + describe 'POST #create' do + subject { post :create, params: { account_id: kevin.account.id } } + + it 'returns 200' do + expect(response).to have_http_status(200) + end + + it 'creates account_pin' do + expect do + subject + end.to change { AccountPin.where(account: john.account, target_account: kevin.account).count }.by(1) + end + end + + describe 'DELETE #destroy' do + subject { delete :destroy, params: { account_id: kevin.account.id } } + + before do + Fabricate(:account_pin, account: john.account, target_account: kevin.account) + end + + it 'returns 200' do + expect(response).to have_http_status(200) + end + + it 'destroys account_pin' do + expect do + subject + end.to change { AccountPin.where(account: john.account, target_account: kevin.account).count }.by(-1) + end + end +end diff --git a/spec/controllers/api/v1/endorsements_controller_spec.rb b/spec/controllers/api/v1/endorsements_controller_spec.rb new file mode 100644 index 000000000..ad5ff400f --- /dev/null +++ b/spec/controllers/api/v1/endorsements_controller_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Api::V1::EndorsementsController, type: :controller do + let(:user) { Fabricate(:user) } + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:accounts') } + + describe 'GET #index' do + it 'returns 200' do + allow(controller).to receive(:doorkeeper_token) { token } + get :index + + expect(response).to have_http_status(200) + end + end +end diff --git a/spec/controllers/api/v1/instances/activity_controller_spec.rb b/spec/controllers/api/v1/instances/activity_controller_spec.rb new file mode 100644 index 000000000..159792ee0 --- /dev/null +++ b/spec/controllers/api/v1/instances/activity_controller_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Api::V1::Instances::ActivityController, type: :controller do + describe 'GET #show' do + it 'returns 200' do + get :show + expect(response).to have_http_status(200) + end + + context '!Setting.activity_api_enabled' do + it 'returns 404' do + Setting.activity_api_enabled = false + + get :show + expect(response).to have_http_status(404) + end + end + end +end diff --git a/spec/controllers/api/v1/instances/peers_controller_spec.rb b/spec/controllers/api/v1/instances/peers_controller_spec.rb new file mode 100644 index 000000000..12a214a83 --- /dev/null +++ b/spec/controllers/api/v1/instances/peers_controller_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Api::V1::Instances::PeersController, type: :controller do + describe 'GET #index' do + it 'returns 200' do + get :index + expect(response).to have_http_status(200) + end + + context '!Setting.peers_api_enabled' do + it 'returns 404' do + Setting.peers_api_enabled = false + + get :index + expect(response).to have_http_status(404) + end + end + end +end diff --git a/spec/controllers/api/v1/timelines/direct_controller_spec.rb b/spec/controllers/api/v1/timelines/direct_controller_spec.rb new file mode 100644 index 000000000..a22c2cbea --- /dev/null +++ b/spec/controllers/api/v1/timelines/direct_controller_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Api::V1::Timelines::DirectController, type: :controller do + let(:user) { Fabricate(:user) } + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:statuses') } + + describe 'GET #show' do + it 'returns 200' do + allow(controller).to receive(:doorkeeper_token) { token } + get :show + + expect(response).to have_http_status(200) + end + end +end diff --git a/spec/controllers/concerns/accountable_concern_spec.rb b/spec/controllers/concerns/accountable_concern_spec.rb new file mode 100644 index 000000000..e3c06b494 --- /dev/null +++ b/spec/controllers/concerns/accountable_concern_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe AccountableConcern do + class Hoge + include AccountableConcern + attr_reader :current_account + + def initialize(current_account) + @current_account = current_account + end + end + + let(:user) { Fabricate(:user, account: Fabricate(:account)) } + let(:target) { Fabricate(:user, account: Fabricate(:account)) } + let(:hoge) { Hoge.new(user.account) } + + describe '#log_action' do + it 'creates Admin::ActionLog' do + expect do + hoge.log_action(:create, target.account) + end.to change { Admin::ActionLog.count }.by(1) + end + end +end diff --git a/spec/fabricators/account_warning_fabricator.rb b/spec/fabricators/account_warning_fabricator.rb new file mode 100644 index 000000000..db161d446 --- /dev/null +++ b/spec/fabricators/account_warning_fabricator.rb @@ -0,0 +1,5 @@ +Fabricator(:account_warning) do + account nil + target_account nil + text "MyText" +end diff --git a/spec/fabricators/account_warning_preset_fabricator.rb b/spec/fabricators/account_warning_preset_fabricator.rb new file mode 100644 index 000000000..6c0b87e7c --- /dev/null +++ b/spec/fabricators/account_warning_preset_fabricator.rb @@ -0,0 +1,3 @@ +Fabricator(:account_warning_preset) do + text "MyText" +end diff --git a/spec/fixtures/requests/windows-1251.txt b/spec/fixtures/requests/windows-1251.txt new file mode 100644 index 000000000..f573e28b2 --- /dev/null +++ b/spec/fixtures/requests/windows-1251.txt @@ -0,0 +1,17 @@ +HTTP/1.1 200 OK +server: nginx +date: Wed, 12 Dec 2018 13:14:03 GMT +content-type: text/html +content-length: 190 +accept-ranges: bytes + +<!DOCTYPE html> +<html> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=windows-1251" /> + <title> </title> +</head> +<body> + <p> </p> +</body> +</html> diff --git a/spec/mailers/admin_mailer_spec.rb b/spec/mailers/admin_mailer_spec.rb new file mode 100644 index 000000000..4a8ef7b5e --- /dev/null +++ b/spec/mailers/admin_mailer_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe AdminMailer, type: :mailer do + describe '.new_report' do + let(:sender) { Fabricate(:account, username: 'John', user: Fabricate(:user)) } + let(:recipient) { Fabricate(:account, username: 'Mike', user: Fabricate(:user, locale: :en)) } + let(:report) { Fabricate(:report, account: sender, target_account: recipient) } + let(:mail) { described_class.new_report(recipient, report) } + + it 'renders the headers' do + expect(mail.subject).to eq("New report for cb6e6126.ngrok.io (##{report.id})") + expect(mail.to).to eq [recipient.user_email] + expect(mail.from).to eq ['notifications@localhost'] + end + + it 'renders the body' do + expect(mail.body.encoded).to eq("Mike,\r\n\r\nJohn has reported Mike\r\n\r\nView: https://cb6e6126.ngrok.io/admin/reports/#{report.id}\r\n") + end + end +end diff --git a/spec/mailers/previews/user_mailer_preview.rb b/spec/mailers/previews/user_mailer_preview.rb index d9cdb9264..53c836494 100644 --- a/spec/mailers/previews/user_mailer_preview.rb +++ b/spec/mailers/previews/user_mailer_preview.rb @@ -39,4 +39,9 @@ class UserMailerPreview < ActionMailer::Preview def backup_ready UserMailer.backup_ready(User.first, Backup.first) end + + # Preview this email at http://localhost:3000/rails/mailers/user_mailer/warning + def warning + UserMailer.warning(User.first, AccountWarning.new(text: '', action: :silence)) + end end diff --git a/spec/models/account_warning_preset_spec.rb b/spec/models/account_warning_preset_spec.rb new file mode 100644 index 000000000..a859a305f --- /dev/null +++ b/spec/models/account_warning_preset_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe AccountWarningPreset, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/account_warning_spec.rb b/spec/models/account_warning_spec.rb new file mode 100644 index 000000000..5286f9177 --- /dev/null +++ b/spec/models/account_warning_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe AccountWarning, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/admin/account_action_spec.rb b/spec/models/admin/account_action_spec.rb new file mode 100644 index 000000000..8c55cf4dd --- /dev/null +++ b/spec/models/admin/account_action_spec.rb @@ -0,0 +1,4 @@ +require 'rails_helper' + +RSpec.describe Admin::AccountAction, type: :model do +end diff --git a/spec/models/custom_emoji_filter_spec.rb b/spec/models/custom_emoji_filter_spec.rb new file mode 100644 index 000000000..d859f5c5f --- /dev/null +++ b/spec/models/custom_emoji_filter_spec.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe CustomEmojiFilter do + describe '#results' do + let!(:custom_emoji_0) { Fabricate(:custom_emoji, domain: 'a') } + let!(:custom_emoji_1) { Fabricate(:custom_emoji, domain: 'b') } + let!(:custom_emoji_2) { Fabricate(:custom_emoji, domain: nil, shortcode: 'hoge') } + + subject { described_class.new(params).results } + + context 'params have values' do + context 'local' do + let(:params) { { local: true } } + + it 'returns ActiveRecord::Relation' do + expect(subject).to be_kind_of(ActiveRecord::Relation) + expect(subject).to match_array([custom_emoji_2]) + end + end + + context 'remote' do + let(:params) { { remote: true } } + + it 'returns ActiveRecord::Relation' do + expect(subject).to be_kind_of(ActiveRecord::Relation) + expect(subject).to match_array([custom_emoji_0, custom_emoji_1]) + end + end + + context 'by_domain' do + let(:params) { { by_domain: 'a' } } + + it 'returns ActiveRecord::Relation' do + expect(subject).to be_kind_of(ActiveRecord::Relation) + expect(subject).to match_array([custom_emoji_0]) + end + end + + context 'shortcode' do + let(:params) { { shortcode: 'hoge' } } + + it 'returns ActiveRecord::Relation' do + expect(subject).to be_kind_of(ActiveRecord::Relation) + expect(subject).to match_array([custom_emoji_2]) + end + end + + context 'else' do + let(:params) { { else: 'else' } } + + it 'raises RuntimeError' do + expect do + subject + end.to raise_error(RuntimeError, /Unknown filter: else/) + end + end + end + + context 'params without value' do + let(:params) { { hoge: nil } } + + it 'returns ActiveRecord::Relation' do + expect(subject).to be_kind_of(ActiveRecord::Relation) + expect(subject).to match_array([custom_emoji_0, custom_emoji_1, custom_emoji_2]) + end + end + end +end diff --git a/spec/policies/account_moderation_note_policy_spec.rb b/spec/policies/account_moderation_note_policy_spec.rb new file mode 100644 index 000000000..bb7af94e4 --- /dev/null +++ b/spec/policies/account_moderation_note_policy_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe AccountModerationNotePolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :create? do + context 'staff' do + it 'grants to create' do + expect(subject).to permit(admin, AccountModerationNotePolicy) + end + end + + context 'not staff' do + it 'denies to create' do + expect(subject).to_not permit(john, AccountModerationNotePolicy) + end + end + end + + permissions :destroy? do + let(:account_moderation_note) do + Fabricate(:account_moderation_note, + account: john, + target_account: Fabricate(:account)) + end + + context 'admin' do + it 'grants to destroy' do + expect(subject).to permit(admin, AccountModerationNotePolicy) + end + end + + context 'owner' do + it 'grants to destroy' do + expect(subject).to permit(john, account_moderation_note) + end + end + + context 'neither admin nor owner' do + let(:kevin) { Fabricate(:user).account } + + it 'denies to destroy' do + expect(subject).to_not permit(kevin, account_moderation_note) + end + end + end +end diff --git a/spec/policies/account_policy_spec.rb b/spec/policies/account_policy_spec.rb new file mode 100644 index 000000000..6648b0888 --- /dev/null +++ b/spec/policies/account_policy_spec.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe AccountPolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :index?, :show?, :unsuspend?, :unsilence?, :remove_avatar?, :remove_header? do + context 'staff' do + it 'permits' do + expect(subject).to permit(admin) + end + end + + context 'not staff' do + it 'denies' do + expect(subject).to_not permit(john) + end + end + end + + permissions :redownload?, :subscribe?, :unsubscribe? do + context 'admin' do + it 'permits' do + expect(subject).to permit(admin) + end + end + + context 'not admin' do + it 'denies' do + expect(subject).to_not permit(john) + end + end + end + + permissions :suspend?, :silence? do + let(:staff) { Fabricate(:user, admin: true).account } + + context 'staff' do + context 'record is staff' do + it 'denies' do + expect(subject).to_not permit(admin, staff) + end + end + + context 'record is not staff' do + it 'permits' do + expect(subject).to permit(admin, john) + end + end + end + + context 'not staff' do + it 'denies' do + expect(subject).to_not permit(john, Account) + end + end + end + + permissions :memorialize? do + let(:other_admin) { Fabricate(:user, admin: true).account } + + context 'admin' do + context 'record is admin' do + it 'denies' do + expect(subject).to_not permit(admin, other_admin) + end + end + + context 'record is not admin' do + it 'permits' do + expect(subject).to permit(admin, john) + end + end + end + + context 'not admin' do + it 'denies' do + expect(subject).to_not permit(john, Account) + end + end + end +end diff --git a/spec/policies/backup_policy_spec.rb b/spec/policies/backup_policy_spec.rb new file mode 100644 index 000000000..80407e12f --- /dev/null +++ b/spec/policies/backup_policy_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe BackupPolicy do + let(:subject) { described_class } + let(:john) { Fabricate(:user).account } + + permissions :create? do + context 'not user_signed_in?' do + it 'denies' do + expect(subject).to_not permit(nil, Backup) + end + end + + context 'user_signed_in?' do + context 'no backups' do + it 'permits' do + expect(subject).to permit(john, Backup) + end + end + + context 'backups are too old' do + it 'permits' do + travel(-8.days) do + Fabricate(:backup, user: john.user) + end + + expect(subject).to permit(john, Backup) + end + end + + context 'backups are newer' do + it 'denies' do + travel(-3.days) do + Fabricate(:backup, user: john.user) + end + + expect(subject).to_not permit(john, Backup) + end + end + end + end +end diff --git a/spec/policies/custom_emoji_policy_spec.rb b/spec/policies/custom_emoji_policy_spec.rb new file mode 100644 index 000000000..8def88212 --- /dev/null +++ b/spec/policies/custom_emoji_policy_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe CustomEmojiPolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :index?, :enable?, :disable? do + context 'staff' do + it 'permits' do + expect(subject).to permit(admin, CustomEmoji) + end + end + + context 'not staff' do + it 'denies' do + expect(subject).to_not permit(john, CustomEmoji) + end + end + end + + permissions :create?, :update?, :copy?, :destroy? do + context 'admin' do + it 'permits' do + expect(subject).to permit(admin, CustomEmoji) + end + end + + context 'not admin' do + it 'denies' do + expect(subject).to_not permit(john, CustomEmoji) + end + end + end +end diff --git a/spec/policies/domain_block_policy_spec.rb b/spec/policies/domain_block_policy_spec.rb new file mode 100644 index 000000000..aea50ec0f --- /dev/null +++ b/spec/policies/domain_block_policy_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe DomainBlockPolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :index?, :show?, :create?, :destroy? do + context 'admin' do + it 'permits' do + expect(subject).to permit(admin, DomainBlock) + end + end + + context 'not admin' do + it 'denies' do + expect(subject).to_not permit(john, DomainBlock) + end + end + end +end diff --git a/spec/policies/email_domain_block_policy_spec.rb b/spec/policies/email_domain_block_policy_spec.rb new file mode 100644 index 000000000..a3e825e07 --- /dev/null +++ b/spec/policies/email_domain_block_policy_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe EmailDomainBlockPolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :index?, :create?, :destroy? do + context 'admin' do + it 'permits' do + expect(subject).to permit(admin, EmailDomainBlock) + end + end + + context 'not admin' do + it 'denies' do + expect(subject).to_not permit(john, EmailDomainBlock) + end + end + end +end diff --git a/spec/policies/instance_policy_spec.rb b/spec/policies/instance_policy_spec.rb new file mode 100644 index 000000000..fbfddd72f --- /dev/null +++ b/spec/policies/instance_policy_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe InstancePolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :index?, :resubscribe? do + context 'admin' do + it 'permits' do + expect(subject).to permit(admin, Instance) + end + end + + context 'not admin' do + it 'denies' do + expect(subject).to_not permit(john, Instance) + end + end + end +end diff --git a/spec/policies/invite_policy_spec.rb b/spec/policies/invite_policy_spec.rb new file mode 100644 index 000000000..e391455be --- /dev/null +++ b/spec/policies/invite_policy_spec.rb @@ -0,0 +1,94 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe InvitePolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :index? do + context 'staff?' do + it 'permits' do + expect(subject).to permit(admin, Invite) + end + end + end + + permissions :create? do + context 'min_required_role?' do + it 'permits' do + allow_any_instance_of(described_class).to receive(:min_required_role?) { true } + expect(subject).to permit(john, Invite) + end + end + + context 'not min_required_role?' do + it 'denies' do + allow_any_instance_of(described_class).to receive(:min_required_role?) { false } + expect(subject).to_not permit(john, Invite) + end + end + end + + permissions :deactivate_all? do + context 'admin?' do + it 'permits' do + expect(subject).to permit(admin, Invite) + end + end + + context 'not admin?' do + it 'denies' do + expect(subject).to_not permit(john, Invite) + end + end + end + + permissions :destroy? do + context 'owner?' do + it 'permits' do + expect(subject).to permit(john, Fabricate(:invite, user: john.user)) + end + end + + context 'not owner?' do + context 'Setting.min_invite_role == "admin"' do + before do + Setting.min_invite_role = 'admin' + end + + context 'admin?' do + it 'permits' do + expect(subject).to permit(admin, Fabricate(:invite)) + end + end + + context 'not admin?' do + it 'denies' do + expect(subject).to_not permit(john, Fabricate(:invite)) + end + end + end + + context 'Setting.min_invite_role != "admin"' do + before do + Setting.min_invite_role = 'else' + end + + context 'staff?' do + it 'permits' do + expect(subject).to permit(admin, Fabricate(:invite)) + end + end + + context 'not staff?' do + it 'denies' do + expect(subject).to_not permit(john, Fabricate(:invite)) + end + end + end + end + end +end diff --git a/spec/policies/relay_policy_spec.rb b/spec/policies/relay_policy_spec.rb new file mode 100644 index 000000000..640f27d54 --- /dev/null +++ b/spec/policies/relay_policy_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe RelayPolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :update? do + context 'admin?' do + it 'permits' do + expect(subject).to permit(admin, Relay) + end + end + + context '!admin?' do + it 'denies' do + expect(subject).to_not permit(john, Relay) + end + end + end +end diff --git a/spec/policies/report_note_policy_spec.rb b/spec/policies/report_note_policy_spec.rb new file mode 100644 index 000000000..596d7d7a9 --- /dev/null +++ b/spec/policies/report_note_policy_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe ReportNotePolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :create? do + context 'staff?' do + it 'permits' do + expect(subject).to permit(admin, ReportNote) + end + end + + context '!staff?' do + it 'denies' do + expect(subject).to_not permit(john, ReportNote) + end + end + end + + permissions :destroy? do + context 'admin?' do + it 'permit' do + expect(subject).to permit(admin, ReportNote) + end + end + + context 'admin?' do + context 'owner?' do + it 'permit' do + report_note = Fabricate(:report_note, account: john) + expect(subject).to permit(john, report_note) + end + end + + context '!owner?' do + it 'denies' do + report_note = Fabricate(:report_note) + expect(subject).to_not permit(john, report_note) + end + end + end + end +end diff --git a/spec/policies/report_policy_spec.rb b/spec/policies/report_policy_spec.rb new file mode 100644 index 000000000..c9ae1e87a --- /dev/null +++ b/spec/policies/report_policy_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe ReportPolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :update?, :index?, :show? do + context 'staff?' do + it 'permits' do + expect(subject).to permit(admin, Report) + end + end + + context '!staff?' do + it 'denies' do + expect(subject).to_not permit(john, Report) + end + end + end +end diff --git a/spec/policies/settings_policy_spec.rb b/spec/policies/settings_policy_spec.rb new file mode 100644 index 000000000..92f1f4869 --- /dev/null +++ b/spec/policies/settings_policy_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe SettingsPolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :update?, :show? do + context 'admin?' do + it 'permits' do + expect(subject).to permit(admin, Settings) + end + end + + context '!admin?' do + it 'denies' do + expect(subject).to_not permit(john, Settings) + end + end + end +end diff --git a/spec/policies/status_policy_spec.rb b/spec/policies/status_policy_spec.rb index 837fa9cee..8bce29cad 100644 --- a/spec/policies/status_policy_spec.rb +++ b/spec/policies/status_policy_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rails_helper' require 'pundit/rspec' @@ -118,4 +120,30 @@ RSpec.describe StatusPolicy, type: :model do expect(subject).to_not permit(nil, status) end end + + permissions :favourite? do + it 'grants access when viewer is not blocked' do + follow = Fabricate(:follow) + status.account = follow.target_account + + expect(subject).to permit(follow.account, status) + end + + it 'denies when viewer is blocked' do + block = Fabricate(:block) + status.account = block.target_account + + expect(subject).to_not permit(block.account, status) + end + end + + permissions :index?, :update? do + it 'grants access if staff' do + expect(subject).to permit(admin.account) + end + + it 'denies access unless staff' do + expect(subject).to_not permit(alice) + end + end end diff --git a/spec/policies/subscription_policy_spec.rb b/spec/policies/subscription_policy_spec.rb new file mode 100644 index 000000000..21d60c15f --- /dev/null +++ b/spec/policies/subscription_policy_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe SubscriptionPolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :index? do + context 'admin?' do + it 'permits' do + expect(subject).to permit(admin, Subscription) + end + end + + context '!admin?' do + it 'denies' do + expect(subject).to_not permit(john, Subscription) + end + end + end +end diff --git a/spec/policies/tag_policy_spec.rb b/spec/policies/tag_policy_spec.rb new file mode 100644 index 000000000..c7afaa7c9 --- /dev/null +++ b/spec/policies/tag_policy_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe TagPolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :index?, :hide?, :unhide? do + context 'staff?' do + it 'permits' do + expect(subject).to permit(admin, Tag) + end + end + + context '!staff?' do + it 'denies' do + expect(subject).to_not permit(john, Tag) + end + end + end +end diff --git a/spec/policies/user_policy_spec.rb b/spec/policies/user_policy_spec.rb new file mode 100644 index 000000000..e37904f04 --- /dev/null +++ b/spec/policies/user_policy_spec.rb @@ -0,0 +1,167 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'pundit/rspec' + +RSpec.describe UserPolicy do + let(:subject) { described_class } + let(:admin) { Fabricate(:user, admin: true).account } + let(:john) { Fabricate(:user).account } + + permissions :reset_password?, :change_email? do + context 'staff?' do + context '!record.staff?' do + it 'permits' do + expect(subject).to permit(admin, john.user) + end + end + + context 'record.staff?' do + it 'denies' do + expect(subject).to_not permit(admin, admin.user) + end + end + end + + context '!staff?' do + it 'denies' do + expect(subject).to_not permit(john, User) + end + end + end + + permissions :disable_2fa? do + context 'admin?' do + context '!record.staff?' do + it 'permits' do + expect(subject).to permit(admin, john.user) + end + end + + context 'record.staff?' do + it 'denies' do + expect(subject).to_not permit(admin, admin.user) + end + end + end + + context '!admin?' do + it 'denies' do + expect(subject).to_not permit(john, User) + end + end + end + + permissions :confirm? do + context 'staff?' do + context '!record.confirmed?' do + it 'permits' do + john.user.update(confirmed_at: nil) + expect(subject).to permit(admin, john.user) + end + end + + context 'record.confirmed?' do + it 'denies' do + john.user.confirm! + expect(subject).to_not permit(admin, john.user) + end + end + end + + context '!staff?' do + it 'denies' do + expect(subject).to_not permit(john, User) + end + end + end + + permissions :enable? do + context 'staff?' do + it 'permits' do + expect(subject).to permit(admin, User) + end + end + + context '!staff?' do + it 'denies' do + expect(subject).to_not permit(john, User) + end + end + end + + permissions :disable? do + context 'staff?' do + context '!record.admin?' do + it 'permits' do + expect(subject).to permit(admin, john.user) + end + end + + context 'record.admin?' do + it 'denies' do + expect(subject).to_not permit(admin, admin.user) + end + end + end + + context '!staff?' do + it 'denies' do + expect(subject).to_not permit(john, User) + end + end + end + + permissions :promote? do + context 'admin?' do + context 'promoteable?' do + it 'permits' do + expect(subject).to permit(admin, john.user) + end + end + + context '!promoteable?' do + it 'denies' do + expect(subject).to_not permit(admin, admin.user) + end + end + end + + context '!admin?' do + it 'denies' do + expect(subject).to_not permit(john, User) + end + end + end + + permissions :demote? do + context 'admin?' do + context '!record.admin?' do + context 'demoteable?' do + it 'permits' do + john.user.update(moderator: true) + expect(subject).to permit(admin, john.user) + end + end + + context '!demoteable?' do + it 'denies' do + expect(subject).to_not permit(admin, john.user) + end + end + end + + context 'record.admin?' do + it 'denies' do + expect(subject).to_not permit(admin, admin.user) + end + end + end + + context '!admin?' do + it 'denies' do + expect(subject).to_not permit(john, User) + end + end + end +end diff --git a/spec/presenters/instance_presenter_spec.rb b/spec/presenters/instance_presenter_spec.rb index 006403925..ccc558f71 100644 --- a/spec/presenters/instance_presenter_spec.rb +++ b/spec/presenters/instance_presenter_spec.rb @@ -111,4 +111,37 @@ describe InstancePresenter do expect(instance_presenter.domain_count).to eq(345) end end + + describe '#version_number' do + it 'returns Mastodon::Version' do + expect(instance_presenter.version_number).to be(Mastodon::Version) + end + end + + describe '#source_url' do + it 'returns "https://github.com/glitch-soc/mastodon"' do + expect(instance_presenter.source_url).to eq('https://github.com/glitch-soc/mastodon') + end + end + + describe '#thumbnail' do + it 'returns SiteUpload' do + thumbnail = Fabricate(:site_upload, var: 'thumbnail') + expect(instance_presenter.thumbnail).to eq(thumbnail) + end + end + + describe '#hero' do + it 'returns SiteUpload' do + hero = Fabricate(:site_upload, var: 'hero') + expect(instance_presenter.hero).to eq(hero) + end + end + + describe '#mascot' do + it 'returns SiteUpload' do + mascot = Fabricate(:site_upload, var: 'mascot') + expect(instance_presenter.mascot).to eq(mascot) + end + end end diff --git a/spec/services/fetch_link_card_service_spec.rb b/spec/services/fetch_link_card_service_spec.rb index 88c5339db..50c60aafd 100644 --- a/spec/services/fetch_link_card_service_spec.rb +++ b/spec/services/fetch_link_card_service_spec.rb @@ -17,6 +17,8 @@ RSpec.describe FetchLinkCardService, type: :service do stub_request(:head, 'https://github.com/qbi/WannaCry').to_return(status: 404) stub_request(:head, 'http://example.com/test-').to_return(status: 200, headers: { 'Content-Type' => 'text/html' }) stub_request(:get, 'http://example.com/test-').to_return(request_fixture('idn.txt')) + stub_request(:head, 'http://example.com/windows-1251').to_return(status: 200, headers: { 'Content-Type' => 'text/html' }) + stub_request(:get, 'http://example.com/windows-1251').to_return(request_fixture('windows-1251.txt')) subject.call(status) end @@ -58,6 +60,15 @@ RSpec.describe FetchLinkCardService, type: :service do end context do + let(:status) { Fabricate(:status, text: 'Check out http://example.com/windows-1251') } + + it 'works with windows-1251' do + expect(a_request(:get, 'http://example.com/windows-1251')).to have_been_made.at_least_once + expect(status.preview_cards.first.title).to eq('сэмпл текст') + end + end + + context do let(:status) { Fabricate(:status, text: 'テストhttp://example.com/日本語') } it 'works with Japanese path string' do |