Merge commit 'c91c0175db1cc8b954a977d29472886234ce9586' into glitch-soc/merge-upstream

Conflicts:
- `spec/controllers/api/v1/timelines/tag_controller_spec.rb`:
  Glitch-soc had a few extra lines in this file to account for a different
  default setting. This file got replaced by
  `spec/requests/api/v1/timelines/tag_spec.rb`, into which the glitch-soc
  additions were moved too.

Additional changes:
- `spec/requests/api/v1/statuses/sources_spec.rb`:
  Add glitch-soc-only attribute `content_type`.
This commit is contained in:
Claire
2023-10-17 21:35:07 +02:00
89 changed files with 1738 additions and 2169 deletions

View File

@@ -51,14 +51,9 @@ RSpec.describe 'Account actions' do
it_behaves_like 'a successful notification delivery'
it_behaves_like 'a successful logged action', :disable, :user
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'disables the target account' do
expect { subject }.to change { target_account.reload.user_disabled? }.from(false).to(true)
expect(response).to have_http_status(200)
end
end
@@ -70,14 +65,9 @@ RSpec.describe 'Account actions' do
it_behaves_like 'a successful notification delivery'
it_behaves_like 'a successful logged action', :sensitive, :account
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'marks the target account as sensitive' do
expect { subject }.to change { target_account.reload.sensitized? }.from(false).to(true)
expect(response).to have_http_status(200)
end
end
@@ -89,14 +79,9 @@ RSpec.describe 'Account actions' do
it_behaves_like 'a successful notification delivery'
it_behaves_like 'a successful logged action', :silence, :account
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'marks the target account as silenced' do
expect { subject }.to change { target_account.reload.silenced? }.from(false).to(true)
expect(response).to have_http_status(200)
end
end
@@ -108,14 +93,9 @@ RSpec.describe 'Account actions' do
it_behaves_like 'a successful notification delivery'
it_behaves_like 'a successful logged action', :suspend, :account
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'marks the target account as suspended' do
expect { subject }.to change { target_account.reload.suspended? }.from(false).to(true)
expect(response).to have_http_status(200)
end
end

View File

@@ -0,0 +1,401 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Accounts' do
let(:role) { UserRole.find_by(name: 'Admin') }
let(:user) { Fabricate(:user, role: role) }
let(:scopes) { 'admin:read:accounts admin:write:accounts' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'GET /api/v1/admin/accounts' do
subject do
get '/api/v1/admin/accounts', headers: headers, params: params
end
shared_examples 'a successful request' do
it 'returns the correct accounts', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json.pluck(:id)).to match_array(expected_results.map { |a| a.id.to_s })
end
end
let!(:remote_account) { Fabricate(:account, domain: 'example.org') }
let!(:suspended_account) { Fabricate(:account, suspended: true) }
let!(:disabled_account) { Fabricate(:user, disabled: true).account }
let!(:pending_account) { Fabricate(:user, approved: false).account }
let!(:admin_account) { user.account }
let(:params) { {} }
it_behaves_like 'forbidden for wrong scope', 'read read:accounts admin:write admin:write:accounts'
it_behaves_like 'forbidden for wrong role', ''
context 'when requesting active local staff accounts' do
let(:expected_results) { [admin_account] }
let(:params) { { active: 'true', local: 'true', staff: 'true' } }
it_behaves_like 'a successful request'
end
context 'when requesting remote accounts from a specified domain' do
let(:expected_results) { [remote_account] }
let(:params) { { by_domain: 'example.org', remote: 'true' } }
before do
Fabricate(:account, domain: 'foo.bar')
end
it_behaves_like 'a successful request'
end
context 'when requesting suspended accounts' do
let(:expected_results) { [suspended_account] }
let(:params) { { suspended: 'true' } }
before do
Fabricate(:account, domain: 'foo.bar', suspended: true)
end
it_behaves_like 'a successful request'
end
context 'when requesting disabled accounts' do
let(:expected_results) { [disabled_account] }
let(:params) { { disabled: 'true' } }
it_behaves_like 'a successful request'
end
context 'when requesting pending accounts' do
let(:expected_results) { [pending_account] }
let(:params) { { pending: 'true' } }
before do
pending_account.user.update(approved: false)
end
it_behaves_like 'a successful request'
end
context 'when no parameter is given' do
let(:expected_results) { [disabled_account, pending_account, admin_account] }
it_behaves_like 'a successful request'
end
context 'with limit param' do
let(:params) { { limit: 2 } }
it 'returns only the requested number of accounts', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json.size).to eq(params[:limit])
end
end
end
describe 'GET /api/v1/admin/accounts/:id' do
subject do
get "/api/v1/admin/accounts/#{account.id}", headers: headers
end
let(:account) { Fabricate(:account) }
it_behaves_like 'forbidden for wrong scope', 'read read:accounts admin:write admin:write:accounts'
it_behaves_like 'forbidden for wrong role', ''
it 'returns the requested account successfully', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json).to match(
a_hash_including(id: account.id.to_s, username: account.username, email: account.user.email)
)
end
context 'when the account is not found' do
it 'returns http not found' do
get '/api/v1/admin/accounts/-1', headers: headers
expect(response).to have_http_status(404)
end
end
end
describe 'POST /api/v1/admin/accounts/:id/approve' do
subject do
post "/api/v1/admin/accounts/#{account.id}/approve", headers: headers
end
let(:account) { Fabricate(:account) }
context 'when the account is pending' do
before do
account.user.update(approved: false)
end
it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
it_behaves_like 'forbidden for wrong role', ''
it 'approves the user successfully', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(account.reload.user_approved?).to be(true)
end
it 'logs action', :aggregate_failures do
subject
log_item = Admin::ActionLog.last
expect(log_item).to be_present
expect(log_item.action).to eq :approve
expect(log_item.account_id).to eq user.account_id
expect(log_item.target_id).to eq account.user.id
end
end
context 'when the account is already approved' do
it 'returns http forbidden' do
subject
expect(response).to have_http_status(403)
end
end
context 'when the account is not found' do
it 'returns http not found' do
post '/api/v1/admin/accounts/-1/approve', headers: headers
expect(response).to have_http_status(404)
end
end
end
describe 'POST /api/v1/admin/accounts/:id/reject' do
subject do
post "/api/v1/admin/accounts/#{account.id}/reject", headers: headers
end
let(:account) { Fabricate(:account) }
context 'when the account is pending' do
before do
account.user.update(approved: false)
end
it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
it_behaves_like 'forbidden for wrong role', ''
it 'removes the user successfully', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(User.where(id: account.user.id)).to_not exist
end
it 'logs action', :aggregate_failures do
subject
log_item = Admin::ActionLog.last
expect(log_item).to be_present
expect(log_item.action).to eq :reject
expect(log_item.account_id).to eq user.account_id
expect(log_item.target_id).to eq account.user.id
end
end
context 'when account is already approved' do
it 'returns http forbidden' do
subject
expect(response).to have_http_status(403)
end
end
context 'when the account is not found' do
it 'returns http not found' do
post '/api/v1/admin/accounts/-1/reject', headers: headers
expect(response).to have_http_status(404)
end
end
end
describe 'POST /api/v1/admin/accounts/:id/enable' do
subject do
post "/api/v1/admin/accounts/#{account.id}/enable", headers: headers
end
let(:account) { Fabricate(:account) }
before do
account.user.update(disabled: true)
end
it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
it_behaves_like 'forbidden for wrong role', ''
it 'enables the user successfully', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(account.reload.user_disabled?).to be false
end
context 'when the account is not found' do
it 'returns http not found' do
post '/api/v1/admin/accounts/-1/enable', headers: headers
expect(response).to have_http_status(404)
end
end
end
describe 'POST /api/v1/admin/accounts/:id/unsuspend' do
subject do
post "/api/v1/admin/accounts/#{account.id}/unsuspend", headers: headers
end
let(:account) { Fabricate(:account) }
context 'when the account is suspended' do
before do
account.suspend!
end
it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
it_behaves_like 'forbidden for wrong role', ''
it 'unsuspends the account successfully', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(account.reload.suspended?).to be false
end
end
context 'when the account is not suspended' do
it 'returns http forbidden' do
subject
expect(response).to have_http_status(403)
end
end
context 'when the account is not found' do
it 'returns http not found' do
post '/api/v1/admin/accounts/-1/unsuspend', headers: headers
expect(response).to have_http_status(404)
end
end
end
describe 'POST /api/v1/admin/accounts/:id/unsensitive' do
subject do
post "/api/v1/admin/accounts/#{account.id}/unsensitive", headers: headers
end
let(:account) { Fabricate(:account) }
before do
account.update(sensitized_at: 10.days.ago)
end
it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
it_behaves_like 'forbidden for wrong role', ''
it 'unsensitizes the account successfully', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(account.reload.sensitized?).to be false
end
context 'when the account is not found' do
it 'returns http not found' do
post '/api/v1/admin/accounts/-1/unsensitive', headers: headers
expect(response).to have_http_status(404)
end
end
end
describe 'POST /api/v1/admin/accounts/:id/unsilence' do
subject do
post "/api/v1/admin/accounts/#{account.id}/unsilence", headers: headers
end
let(:account) { Fabricate(:account) }
before do
account.update(silenced_at: 3.days.ago)
end
it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
it_behaves_like 'forbidden for wrong role', ''
it 'unsilences the account successfully', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(account.reload.silenced?).to be false
end
context 'when the account is not found' do
it 'returns http not found' do
post '/api/v1/admin/accounts/-1/unsilence', headers: headers
expect(response).to have_http_status(404)
end
end
end
describe 'DELETE /api/v1/admin/accounts/:id' do
subject do
delete "/api/v1/admin/accounts/#{account.id}", headers: headers
end
let(:account) { Fabricate(:account) }
context 'when account is suspended' do
before do
account.suspend!
end
it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
it_behaves_like 'forbidden for wrong role', ''
it 'deletes the account successfully', :aggregate_failures do
allow(Admin::AccountDeletionWorker).to receive(:perform_async)
subject
expect(response).to have_http_status(200)
expect(Admin::AccountDeletionWorker).to have_received(:perform_async).with(account.id).once
end
end
context 'when account is not suspended' do
it 'returns http forbidden' do
subject
expect(response).to have_http_status(403)
end
end
context 'when the account is not found' do
it 'returns http not found' do
delete '/api/v1/admin/accounts/-1', headers: headers
expect(response).to have_http_status(404)
end
end
end
end

View File

@@ -92,15 +92,10 @@ RSpec.describe 'Canonical Email Blocks' do
it_behaves_like 'forbidden for wrong role', 'Moderator'
context 'when the requested canonical email block exists' do
it 'returns http success' do
it 'returns the requested canonical email block data correctly', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the requested canonical email block data correctly' do
subject
json = body_as_json
expect(json[:id]).to eq(canonical_email_block.id.to_s)
@@ -142,29 +137,19 @@ RSpec.describe 'Canonical Email Blocks' do
context 'when there is a matching canonical email block' do
let!(:canonical_email_block) { CanonicalEmailBlock.create(params) }
it 'returns http success' do
it 'returns the expected canonical email hash', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the expected canonical email hash' do
subject
expect(body_as_json[0][:canonical_email_hash]).to eq(canonical_email_block.canonical_email_hash)
end
end
context 'when there is no matching canonical email block' do
it 'returns http success' do
it 'returns an empty list', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns an empty list' do
subject
expect(body_as_json).to be_empty
end
end
@@ -183,15 +168,10 @@ RSpec.describe 'Canonical Email Blocks' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
it 'returns the canonical_email_hash correctly', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the canonical_email_hash correctly' do
subject
expect(body_as_json[:canonical_email_hash]).to eq(canonical_email_block.canonical_email_hash)
end
@@ -208,15 +188,10 @@ RSpec.describe 'Canonical Email Blocks' do
context 'when the canonical_email_hash param is provided instead of email' do
let(:params) { { canonical_email_hash: 'dd501ce4e6b08698f19df96f2f15737e48a75660b1fa79b6ff58ea25ee4851a4' } }
it 'returns http success' do
it 'returns the correct canonical_email_hash', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the correct canonical_email_hash' do
subject
expect(body_as_json[:canonical_email_hash]).to eq(params[:canonical_email_hash])
end
end
@@ -224,15 +199,10 @@ RSpec.describe 'Canonical Email Blocks' do
context 'when both email and canonical_email_hash params are provided' do
let(:params) { { email: 'example@email.com', canonical_email_hash: 'dd501ce4e6b08698f19df96f2f15737e48a75660b1fa79b6ff58ea25ee4851a4' } }
it 'returns http success' do
it 'ignores the canonical_email_hash param', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'ignores the canonical_email_hash param' do
subject
expect(body_as_json[:canonical_email_hash]).to eq(canonical_email_block.canonical_email_hash)
end
end
@@ -262,15 +232,10 @@ RSpec.describe 'Canonical Email Blocks' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
it 'deletes the canonical email block', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'deletes the canonical email block' do
subject
expect(CanonicalEmailBlock.find_by(id: canonical_email_block.id)).to be_nil
end

View File

@@ -75,15 +75,10 @@ RSpec.describe 'Domain Allows' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
it 'returns the expected allowed domain name', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the expected allowed domain name' do
subject
expect(body_as_json[:domain]).to eq domain_allow.domain
end
@@ -108,21 +103,11 @@ RSpec.describe 'Domain Allows' do
it_behaves_like 'forbidden for wrong role', 'Moderator'
context 'with a valid domain name' do
it 'returns http success' do
it 'returns the expected domain name', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the expected domain name' do
subject
expect(body_as_json[:domain]).to eq 'foo.bar.com'
end
it 'creates a domain allow' do
subject
expect(DomainAllow.find_by(domain: 'foo.bar.com')).to be_present
end
end
@@ -171,15 +156,10 @@ RSpec.describe 'Domain Allows' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
it 'deletes the allowed domain', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'deletes the allowed domain' do
subject
expect(DomainAllow.find_by(id: domain_allow.id)).to be_nil
end

View File

@@ -89,15 +89,10 @@ RSpec.describe 'Domain Blocks' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
it 'returns the expected domain block content', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the expected domain block content' do
subject
expect(body_as_json).to eq(
{
id: domain_block.id.to_s,
@@ -133,27 +128,18 @@ RSpec.describe 'Domain Blocks' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'returns expected domain name and severity' do
it 'returns expected domain name and severity', :aggregate_failures do
subject
body = body_as_json
expect(response).to have_http_status(200)
expect(body).to match a_hash_including(
{
domain: 'foo.bar.com',
severity: 'silence',
}
)
end
it 'creates a domain block' do
subject
expect(DomainBlock.find_by(domain: 'foo.bar.com')).to be_present
end
@@ -163,15 +149,10 @@ RSpec.describe 'Domain Blocks' do
Fabricate(:domain_block, domain: 'bar.com', severity: :suspend)
end
it 'returns http unprocessable entity' do
it 'returns existing domain block in error', :aggregate_failures do
subject
expect(response).to have_http_status(422)
end
it 'returns existing domain block in error' do
subject
expect(body_as_json[:existing_domain_block][:domain]).to eq('bar.com')
end
end
@@ -199,15 +180,10 @@ RSpec.describe 'Domain Blocks' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
it 'returns the updated domain block', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the updated domain block' do
subject
expect(body_as_json).to match a_hash_including(
{
id: domain_block.id.to_s,
@@ -241,15 +217,10 @@ RSpec.describe 'Domain Blocks' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
it 'deletes the domain block', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'deletes the domain block' do
subject
expect(DomainBlock.find_by(id: domain_block.id)).to be_nil
end

View File

@@ -93,15 +93,10 @@ RSpec.describe 'Email Domain Blocks' do
it_behaves_like 'forbidden for wrong role', 'Moderator'
context 'when email domain block exists' do
it 'returns http success' do
it 'returns the correct blocked domain', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the correct blocked domain' do
subject
expect(body_as_json[:domain]).to eq(email_domain_block.domain)
end
end
@@ -126,15 +121,10 @@ RSpec.describe 'Email Domain Blocks' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
it 'returns the correct blocked email domain', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the correct blocked email domain' do
subject
expect(body_as_json[:domain]).to eq(params[:domain])
end
@@ -182,21 +172,11 @@ RSpec.describe 'Email Domain Blocks' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
it 'deletes email domain block', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns an empty body' do
subject
expect(body_as_json).to be_empty
end
it 'deletes email domain block' do
subject
expect(EmailDomainBlock.find_by(id: email_domain_block.id)).to be_nil
end

View File

@@ -84,15 +84,10 @@ RSpec.describe 'IP Blocks' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
it 'returns the correct ip block', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the correct ip block' do
subject
json = body_as_json
expect(json[:ip]).to eq("#{ip_block.ip}/#{ip_block.ip.prefix}")
@@ -119,15 +114,10 @@ RSpec.describe 'IP Blocks' do
it_behaves_like 'forbidden for wrong role', ''
it_behaves_like 'forbidden for wrong role', 'Moderator'
it 'returns http success' do
it 'returns the correct ip block', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the correct ip block' do
subject
json = body_as_json
expect(json[:ip]).to eq("#{params[:ip]}/32")
@@ -186,15 +176,10 @@ RSpec.describe 'IP Blocks' do
let!(:ip_block) { IpBlock.create(ip: '185.200.13.3', severity: 'no_access', comment: 'Spam', expires_in: 48.hours) }
let(:params) { { severity: 'sign_up_requires_approval', comment: 'Decreasing severity' } }
it 'returns http success' do
it 'returns the correct ip block', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the correct ip block' do
subject
expect(body_as_json).to match(hash_including({
ip: "#{ip_block.ip}/#{ip_block.ip.prefix}",
severity: 'sign_up_requires_approval',
@@ -226,21 +211,11 @@ RSpec.describe 'IP Blocks' do
let!(:ip_block) { IpBlock.create(ip: '185.200.13.3', severity: 'no_access') }
it 'returns http success' do
it 'deletes the ip block', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns an empty body' do
subject
expect(body_as_json).to be_empty
end
it 'deletes the ip block' do
subject
expect(IpBlock.find_by(id: ip_block.id)).to be_nil
end

View File

@@ -122,15 +122,10 @@ RSpec.describe 'Reports' do
it_behaves_like 'forbidden for wrong scope', 'write:statuses'
it_behaves_like 'forbidden for wrong role', ''
it 'returns http success' do
it 'returns the requested report content', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the requested report content' do
subject
expect(body_as_json).to include(
{
id: report.id.to_s,
@@ -155,18 +150,10 @@ RSpec.describe 'Reports' do
let!(:report) { Fabricate(:report, category: :other) }
let(:params) { { category: 'spam' } }
it 'returns http success' do
subject
it 'updates the report category', :aggregate_failures do
expect { subject }.to change { report.reload.category }.from('other').to('spam')
expect(response).to have_http_status(200)
end
it 'updates the report category' do
expect { subject }.to change { report.reload.category }.from('other').to('spam')
end
it 'returns the updated report content' do
subject
report.reload
@@ -196,14 +183,9 @@ RSpec.describe 'Reports' do
it_behaves_like 'forbidden for wrong scope', 'write:statuses'
it_behaves_like 'forbidden for wrong role', ''
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'marks report as resolved' do
it 'marks report as resolved', :aggregate_failures do
expect { subject }.to change { report.reload.unresolved? }.from(true).to(false)
expect(response).to have_http_status(200)
end
end
@@ -217,14 +199,9 @@ RSpec.describe 'Reports' do
it_behaves_like 'forbidden for wrong scope', 'write:statuses'
it_behaves_like 'forbidden for wrong role', ''
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'marks report as unresolved' do
it 'marks report as unresolved', :aggregate_failures do
expect { subject }.to change { report.reload.unresolved? }.from(false).to(true)
expect(response).to have_http_status(200)
end
end
@@ -238,14 +215,9 @@ RSpec.describe 'Reports' do
it_behaves_like 'forbidden for wrong scope', 'write:statuses'
it_behaves_like 'forbidden for wrong role', ''
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'assigns report to the requesting user' do
it 'assigns report to the requesting user', :aggregate_failures do
expect { subject }.to change { report.reload.assigned_account_id }.from(nil).to(user.account.id)
expect(response).to have_http_status(200)
end
end
@@ -259,14 +231,9 @@ RSpec.describe 'Reports' do
it_behaves_like 'forbidden for wrong scope', 'write:statuses'
it_behaves_like 'forbidden for wrong role', ''
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'unassigns report from assignee' do
it 'unassigns report from assignee', :aggregate_failures do
expect { subject }.to change { report.reload.assigned_account_id }.from(user.account.id).to(nil)
expect(response).to have_http_status(200)
end
end
end

View File

@@ -0,0 +1,129 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'Links' do
let(:role) { UserRole.find_by(name: 'Admin') }
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(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'GET /api/v1/admin/trends/links' do
subject do
get '/api/v1/admin/trends/links', headers: headers
end
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
end
describe 'POST /api/v1/admin/trends/links/:id/approve' do
subject do
post "/api/v1/admin/trends/links/#{preview_card.id}/approve", headers: headers
end
let(:preview_card) { Fabricate(:preview_card, trendable: false) }
it_behaves_like 'forbidden for wrong scope', 'read write'
it_behaves_like 'forbidden for wrong role', ''
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'sets the link as trendable' do
expect { subject }.to change { preview_card.reload.trendable }.from(false).to(true)
end
it 'returns the link data' do
subject
expect(body_as_json).to match(
a_hash_including(
url: preview_card.url,
title: preview_card.title,
description: preview_card.description,
type: 'link',
requires_review: false
)
)
end
context 'when the link does not exist' do
it 'returns http not found' do
post '/api/v1/admin/trends/links/-1/approve', headers: headers
expect(response).to have_http_status(404)
end
end
context 'without an authorization header' do
let(:headers) { {} }
it 'returns http forbidden' do
subject
expect(response).to have_http_status(403)
end
end
end
describe 'POST /api/v1/admin/trends/links/:id/reject' do
subject do
post "/api/v1/admin/trends/links/#{preview_card.id}/reject", headers: headers
end
let(:preview_card) { Fabricate(:preview_card, trendable: false) }
it_behaves_like 'forbidden for wrong scope', 'read write'
it_behaves_like 'forbidden for wrong role', ''
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'does not set the link as trendable' do
expect { subject }.to_not(change { preview_card.reload.trendable })
end
it 'returns the link data' do
subject
expect(body_as_json).to match(
a_hash_including(
url: preview_card.url,
title: preview_card.title,
description: preview_card.description,
type: 'link',
requires_review: false
)
)
end
context 'when the link does not exist' do
it 'returns http not found' do
post '/api/v1/admin/trends/links/-1/reject', headers: headers
expect(response).to have_http_status(404)
end
end
context 'without an authorization header' do
let(:headers) { {} }
it 'returns http forbidden' do
subject
expect(response).to have_http_status(403)
end
end
end
end

View File

@@ -12,14 +12,10 @@ describe 'Credentials' do
let(:token) { Fabricate(:accessible_access_token, scopes: 'read', application: Fabricate(:application)) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
it 'returns http success' do
it 'returns the app information correctly', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the app information correctly' do
subject
expect(body_as_json).to match(
a_hash_including(

View File

@@ -23,20 +23,11 @@ RSpec.describe 'Apps' do
end
context 'with valid params' do
it 'returns http success' do
it 'creates an OAuth app', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'creates an OAuth app' do
subject
expect(Doorkeeper::Application.find_by(name: client_name)).to be_present
end
it 'returns client ID and client secret' do
subject
body = body_as_json
@@ -58,15 +49,10 @@ RSpec.describe 'Apps' do
context 'with many duplicate scopes' do
let(:scopes) { (%w(read) * 40).join(' ') }
it 'returns http success' do
it 'only saves the scope once', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'only saves the scope once' do
subject
expect(Doorkeeper::Application.find_by(name: client_name).scopes.to_s).to eq 'read'
end
end

View File

@@ -0,0 +1,80 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Blocks' do
let(:user) { Fabricate(:user) }
let(:scopes) { 'read:blocks' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'GET /api/v1/blocks' do
subject do
get '/api/v1/blocks', headers: headers, params: params
end
let!(:blocks) { Fabricate.times(3, :block, account: user.account) }
let(:params) { {} }
let(:expected_response) do
blocks.map { |block| a_hash_including(id: block.target_account.id.to_s, username: block.target_account.username) }
end
it_behaves_like 'forbidden for wrong scope', 'write write:blocks'
it 'returns the blocked accounts', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json).to match_array(expected_response)
end
context 'with limit param' do
let(:params) { { limit: 2 } }
it 'returns only the requested number of blocked accounts' do
subject
expect(body_as_json.size).to eq(params[:limit])
end
it 'sets the correct pagination header for the prev path' do
subject
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_blocks_url(limit: params[:limit], since_id: blocks.last.id))
end
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_blocks_url(limit: params[:limit], max_id: blocks[1].id))
end
end
context 'with max_id param' do
let(:params) { { max_id: blocks[1].id } }
it 'queries the blocks in range according to max_id', :aggregate_failures do
subject
response_body = body_as_json
expect(response_body.size).to be 1
expect(response_body[0][:id]).to eq(blocks[0].target_account.id.to_s)
end
end
context 'with since_id param' do
let(:params) { { since_id: blocks[1].id } }
it 'queries the blocks in range according to since_id', :aggregate_failures do
subject
response_body = body_as_json
expect(response_body.size).to be 1
expect(response_body[0][:id]).to eq(blocks[2].target_account.id.to_s)
end
end
end
end

View File

@@ -22,15 +22,10 @@ RSpec.describe 'Domain blocks' do
it_behaves_like 'forbidden for wrong scope', 'write:blocks'
it 'returns http success' do
it 'returns the domains blocked by the requesting user', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the domains blocked by the requesting user' do
subject
expect(body_as_json).to match_array(blocked_domains)
end
@@ -54,15 +49,10 @@ RSpec.describe 'Domain blocks' do
it_behaves_like 'forbidden for wrong scope', 'read read:blocks'
it 'returns http success' do
it 'creates a domain block', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'creates a domain block' do
subject
expect(user.account.domain_blocking?(params[:domain])).to be(true)
end
@@ -100,15 +90,10 @@ RSpec.describe 'Domain blocks' do
it_behaves_like 'forbidden for wrong scope', 'read read:blocks'
it 'returns http success' do
it 'deletes the specified domain block', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'deletes the specified domain block' do
subject
expect(user.account.domain_blocking?('example.com')).to be(false)
end

View File

@@ -0,0 +1,71 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Favourites' do
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:scopes) { 'read:favourites' }
let(:headers) { { Authorization: "Bearer #{token.token}" } }
describe 'GET /api/v1/favourites' do
subject do
get '/api/v1/favourites', headers: headers, params: params
end
let(:params) { {} }
let!(:favourites) { Fabricate.times(3, :favourite, account: user.account) }
let(:expected_response) do
favourites.map do |favourite|
a_hash_including(id: favourite.status.id.to_s, account: a_hash_including(id: favourite.status.account.id.to_s))
end
end
it_behaves_like 'forbidden for wrong scope', 'write'
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'returns the favourites' do
subject
expect(body_as_json).to match_array(expected_response)
end
context 'with limit param' do
let(:params) { { limit: 2 } }
it 'returns only the requested number of favourites' do
subject
expect(body_as_json.size).to eq(params[:limit])
end
it 'sets the correct pagination header for the prev path' do
subject
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_favourites_url(limit: params[:limit], min_id: favourites.last.id))
end
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_favourites_url(limit: params[:limit], max_id: favourites[1].id))
end
end
context 'without an authorization header' do
let(:headers) { {} }
it 'returns http unauthorized' do
subject
expect(response).to have_http_status(401)
end
end
end
end

View File

@@ -32,15 +32,10 @@ RSpec.describe 'Follow requests' do
it_behaves_like 'forbidden for wrong scope', 'write write:follows'
it 'returns http success' do
it 'returns the expected content from accounts requesting to follow', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the expected content from accounts requesting to follow' do
subject
expect(body_as_json).to match_array(expected_response)
end
@@ -68,19 +63,9 @@ RSpec.describe 'Follow requests' do
it_behaves_like 'forbidden for wrong scope', 'read read:follows'
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'allows the requesting follower to follow' do
it 'allows the requesting follower to follow', :aggregate_failures do
expect { subject }.to change { follower.following?(user.account) }.from(false).to(true)
end
it 'returns JSON with followed_by set to true' do
subject
expect(response).to have_http_status(200)
expect(body_as_json[:followed_by]).to be true
end
end
@@ -98,21 +83,11 @@ RSpec.describe 'Follow requests' do
it_behaves_like 'forbidden for wrong scope', 'read read:follows'
it 'returns http success' do
it 'removes the follow request', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'removes the follow request' do
subject
expect(FollowRequest.where(target_account: user.account, account: follower)).to_not exist
end
it 'returns JSON with followed_by set to false' do
subject
expect(body_as_json[:followed_by]).to be false
end
end

View File

@@ -0,0 +1,65 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Followed tags' do
let(:user) { Fabricate(:user) }
let(:scopes) { 'read:follows' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'GET /api/v1/followed_tags' do
subject do
get '/api/v1/followed_tags', headers: headers, params: params
end
let!(:tag_follows) { Fabricate.times(5, :tag_follow, account: user.account) }
let(:params) { {} }
let(:expected_response) do
tag_follows.map do |tag_follow|
a_hash_including(name: tag_follow.tag.name, following: true)
end
end
before do
Fabricate(:tag_follow)
end
it_behaves_like 'forbidden for wrong scope', 'write write:follows'
it 'returns http success' do
subject
expect(response).to have_http_status(:success)
end
it 'returns the followed tags correctly' do
subject
expect(body_as_json).to match_array(expected_response)
end
context 'with limit param' do
let(:params) { { limit: 3 } }
it 'returns only the requested number of follow tags' do
subject
expect(body_as_json.size).to eq(params[:limit])
end
it 'sets the correct pagination header for the prev path' do
subject
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_followed_tags_url(limit: params[:limit], since_id: tag_follows.last.id))
end
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))
end
end
end
end

View File

@@ -0,0 +1,178 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Accounts' do
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:scopes) { 'read:lists write:lists' }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'GET /api/v1/lists/:id/accounts' do
subject do
get "/api/v1/lists/#{list.id}/accounts", headers: headers, params: params
end
let(:params) { { limit: 0 } }
let(:list) { Fabricate(:list, account: user.account) }
let(:accounts) { Fabricate.times(3, :account) }
let(:expected_response) do
accounts.map do |account|
a_hash_including(id: account.id.to_s, username: account.username, acct: account.acct)
end
end
before do
accounts.each { |account| user.account.follow!(account) }
list.accounts << accounts
end
it_behaves_like 'forbidden for wrong scope', 'write write:lists'
it 'returns the accounts in the requested list', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json).to match_array(expected_response)
end
context 'with limit param' do
let(:params) { { limit: 1 } }
it 'returns only the requested number of accounts' do
subject
expect(body_as_json.size).to eq(params[:limit])
end
end
end
describe 'POST /api/v1/lists/:id/accounts' do
subject do
post "/api/v1/lists/#{list.id}/accounts", headers: headers, params: params
end
let(:list) { Fabricate(:list, account: user.account) }
let(:bob) { Fabricate(:account, username: 'bob') }
let(:params) { { account_ids: [bob.id] } }
it_behaves_like 'forbidden for wrong scope', 'read read:lists'
context 'when the added account is followed' do
before do
user.account.follow!(bob)
end
it 'adds account to the list', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(list.accounts).to include(bob)
end
end
context 'when the added account has been sent a follow request' do
before do
user.account.follow_requests.create!(target_account: bob)
end
it 'adds account to the list', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(list.accounts).to include(bob)
end
end
context 'when the added account is not followed' do
it 'does not add the account to the list', :aggregate_failures do
subject
expect(response).to have_http_status(404)
expect(list.accounts).to_not include(bob)
end
end
context 'when the list is not owned by the requesting user' do
let(:list) { Fabricate(:list) }
before do
user.account.follow!(bob)
end
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
context 'when account is already in the list' do
before do
user.account.follow!(bob)
list.accounts << bob
end
it 'returns http unprocessable entity' do
subject
expect(response).to have_http_status(422)
end
end
end
describe 'DELETE /api/v1/lists/:id/accounts' do
subject do
delete "/api/v1/lists/#{list.id}/accounts", headers: headers, params: params
end
context 'when the list is owned by the requesting user' do
let(:list) { Fabricate(:list, account: user.account) }
let(:bob) { Fabricate(:account, username: 'bob') }
let(:peter) { Fabricate(:account, username: 'peter') }
let(:params) { { account_ids: [bob.id] } }
before do
user.account.follow!(bob)
user.account.follow!(peter)
list.accounts << [bob, peter]
end
it 'removes the specified account from the list', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(list.accounts).to_not include(bob)
end
it 'does not remove any other account from the list' do
subject
expect(list.accounts).to include(peter)
end
context 'when the specified account is not in the list' do
let(:params) { { account_ids: [0] } }
it 'does not remove any account from the list', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(list.accounts).to contain_exactly(bob, peter)
end
end
end
context 'when the list is not owned by the requesting user' do
let(:list) { Fabricate(:list) }
let(:params) { {} }
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
end
end

View File

@@ -39,15 +39,10 @@ RSpec.describe 'Lists' do
it_behaves_like 'forbidden for wrong scope', 'write write:lists'
it 'returns http success' do
it 'returns the expected lists', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the expected lists' do
subject
expect(body_as_json).to match_array(expected_response)
end
end
@@ -61,15 +56,10 @@ RSpec.describe 'Lists' do
it_behaves_like 'forbidden for wrong scope', 'write write:lists'
it 'returns http success' do
it 'returns the requested list correctly', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the requested list correctly' do
subject
expect(body_as_json).to eq({
id: list.id.to_s,
title: list.title,
@@ -106,21 +96,11 @@ RSpec.describe 'Lists' do
it_behaves_like 'forbidden for wrong scope', 'read read:lists'
it 'returns http success' do
it 'returns the new list', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the new list' do
subject
expect(body_as_json).to match(a_hash_including(title: 'my list', replies_policy: 'none', exclusive: true))
end
it 'creates a list' do
subject
expect(List.where(account: user.account).count).to eq(1)
end
@@ -155,15 +135,10 @@ RSpec.describe 'Lists' do
it_behaves_like 'forbidden for wrong scope', 'read read:lists'
it 'returns http success' do
it 'returns the updated list', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the updated list' do
subject
list.reload
expect(body_as_json).to eq({
@@ -214,15 +189,10 @@ RSpec.describe 'Lists' do
it_behaves_like 'forbidden for wrong scope', 'read read:lists'
it 'returns http success' do
it 'deletes the list', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'deletes the list' do
subject
expect(List.where(id: list.id)).to_not exist
end

View File

@@ -0,0 +1,89 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Reports' do
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:scopes) { 'write:reports' }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'POST /api/v1/reports' do
subject do
post '/api/v1/reports', headers: headers, params: params
end
let!(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
let(:status) { Fabricate(:status) }
let(:target_account) { status.account }
let(:category) { 'other' }
let(:forward) { nil }
let(:rule_ids) { nil }
let(:params) do
{
status_ids: [status.id],
account_id: target_account.id,
comment: 'reasons',
category: category,
rule_ids: rule_ids,
forward: forward,
}
end
it_behaves_like 'forbidden for wrong scope', 'read read:reports'
it 'creates a report', :aggregate_failures do
perform_enqueued_jobs do
subject
expect(response).to have_http_status(200)
expect(body_as_json).to match(
a_hash_including(
status_ids: [status.id.to_s],
category: category,
comment: 'reasons'
)
)
expect(target_account.targeted_reports).to_not be_empty
expect(target_account.targeted_reports.first.comment).to eq 'reasons'
expect(ActionMailer::Base.deliveries.first.to).to eq([admin.email])
end
end
context 'when a status does not belong to the reported account' do
let(:target_account) { Fabricate(:account) }
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
context 'when a category is chosen' do
let(:category) { 'spam' }
it 'saves category' do
subject
expect(target_account.targeted_reports.first.spam?).to be true
end
end
context 'when violated rules are chosen' do
let(:rule) { Fabricate(:rule) }
let(:category) { 'violation' }
let(:rule_ids) { [rule.id] }
it 'saves category and rule_ids' do
subject
expect(target_account.targeted_reports.first.violation?).to be true
expect(target_account.targeted_reports.first.rule_ids).to contain_exactly(rule.id)
end
end
end
end

View File

@@ -0,0 +1,74 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Sources' do
let(:user) { Fabricate(:user) }
let(:scopes) { 'read:statuses' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'GET /api/v1/statuses/:status_id/source' do
subject do
get "/api/v1/statuses/#{status.id}/source", headers: headers
end
let(:status) { Fabricate(:status) }
it_behaves_like 'forbidden for wrong scope', 'write write:statuses'
context 'with public status' do
it 'returns the source properties of the status', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json).to eq({
id: status.id.to_s,
text: status.text,
spoiler_text: status.spoiler_text,
content_type: nil,
})
end
end
context 'with private status of non-followed account' do
let(:status) { Fabricate(:status, visibility: :private) }
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
context 'with private status of followed account' do
let(:status) { Fabricate(:status, visibility: :private) }
before do
user.account.follow!(status.account)
end
it 'returns the source properties of the status', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json).to eq({
id: status.id.to_s,
text: status.text,
spoiler_text: status.spoiler_text,
content_type: nil,
})
end
end
context 'without an authorization header' do
let(:headers) { {} }
it 'returns http unauthorized' do
subject
expect(response).to have_http_status(401)
end
end
end
end

View File

@@ -17,15 +17,10 @@ RSpec.describe 'Tags' do
let!(:tag) { Fabricate(:tag) }
let(:name) { tag.name }
it 'returns http success' do
it 'returns the tag', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'returns the tag' do
subject
expect(body_as_json[:name]).to eq(name)
end
end
@@ -62,15 +57,10 @@ RSpec.describe 'Tags' do
it_behaves_like 'forbidden for wrong scope', 'read read:follows'
context 'when the tag exists' do
it 'returns http success' do
it 'creates follow', :aggregate_failures do
subject
expect(response).to have_http_status(:success)
end
it 'creates follow' do
subject
expect(TagFollow.where(tag: tag, account: user.account)).to exist
end
end
@@ -78,21 +68,11 @@ RSpec.describe 'Tags' do
context 'when the tag does not exist' do
let(:name) { 'hoge' }
it 'returns http success' do
it 'creates a new tag with the specified name', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'creates a new tag with the specified name' do
subject
expect(Tag.where(name: name)).to exist
end
it 'creates follow' do
subject
expect(TagFollow.where(tag: Tag.find_by(name: name), account: user.account)).to exist
end
end
@@ -133,15 +113,10 @@ RSpec.describe 'Tags' do
it_behaves_like 'forbidden for wrong scope', 'read read:follows'
it 'returns http success' do
it 'removes the follow', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
it 'removes the follow' do
subject
expect(TagFollow.where(tag: tag, account: user.account)).to_not exist
end

View File

@@ -0,0 +1,116 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Tag' do
let(:user) { Fabricate(:user) }
let(:scopes) { 'read:statuses' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
shared_examples 'a successful request to the tag timeline' do
it 'returns the expected statuses', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json.pluck(:id)).to match_array(expected_statuses.map { |status| status.id.to_s })
end
end
describe 'GET /api/v1/timelines/tag/:hashtag' do
subject do
get "/api/v1/timelines/tag/#{hashtag}", headers: headers, params: params
end
before do
Setting.timeline_preview = true
end
let(:account) { Fabricate(:account) }
let!(:private_status) { PostStatusService.new.call(account, visibility: :private, text: '#life could be a dream') } # rubocop:disable RSpec/LetSetup
let!(:life_status) { PostStatusService.new.call(account, text: 'tell me what is my #life without your #love') }
let!(:war_status) { PostStatusService.new.call(user.account, text: '#war, war never changes') }
let!(:love_status) { PostStatusService.new.call(account, text: 'what is #love?') }
let(:params) { {} }
let(:hashtag) { 'life' }
context 'when given only one hashtag' do
let(:expected_statuses) { [life_status] }
it_behaves_like 'a successful request to the tag timeline'
end
context 'with any param' do
let(:expected_statuses) { [life_status, love_status] }
let(:params) { { any: %(love) } }
it_behaves_like 'a successful request to the tag timeline'
end
context 'with all param' do
let(:expected_statuses) { [life_status] }
let(:params) { { all: %w(love) } }
it_behaves_like 'a successful request to the tag timeline'
end
context 'with none param' do
let(:expected_statuses) { [war_status] }
let(:hashtag) { 'war' }
let(:params) { { none: %w(life love) } }
it_behaves_like 'a successful request to the tag timeline'
end
context 'with limit param' do
let(:hashtag) { 'love' }
let(:params) { { limit: 1 } }
it 'returns only the requested number of statuses' do
subject
expect(body_as_json.size).to eq(params[:limit])
end
it 'sets the correct pagination headers', :aggregate_failures do
subject
headers = response.headers['Link']
expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_timelines_tag_url(limit: 1, min_id: love_status.id.to_s))
expect(headers.find_link(%w(rel next)).href).to eq(api_v1_timelines_tag_url(limit: 1, max_id: love_status.id.to_s))
end
end
context 'when the instance allows public preview' do
context 'when the user is not authenticated' do
let(:headers) { {} }
let(:expected_statuses) { [life_status] }
it_behaves_like 'a successful request to the tag timeline'
end
end
context 'when the instance does not allow public preview' do
before do
Form::AdminSettings.new(timeline_preview: false).save
end
context 'when the user is not authenticated' do
let(:headers) { {} }
it 'returns http unauthorized' do
subject
expect(response).to have_http_status(401)
end
end
context 'when the user is authenticated' do
let(:expected_statuses) { [life_status] }
it_behaves_like 'a successful request to the tag timeline'
end
end
end
end