1
0
mirror of https://github.com/mastodon/mastodon synced 2024-12-18 16:48:22 +09:00

Rewrite /api/v1/statuses tests as request specs (#27891)

This commit is contained in:
Claire 2023-11-17 12:36:04 +01:00 committed by GitHub
parent 9257737675
commit 297839c10c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,24 +2,26 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Api::V1::StatusesController do describe '/api/v1/statuses' do
render_views
let(:user) { Fabricate(:user) }
let(:app) { Fabricate(:application, name: 'Test app', website: 'http://testapp.com') }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, application: app, scopes: scopes) }
context 'with an oauth token' do context 'with an oauth token' do
before do let(:user) { Fabricate(:user) }
allow(controller).to receive(:doorkeeper_token) { token } let(:client_app) { Fabricate(:application, name: 'Test app', website: 'http://testapp.com') }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, application: client_app, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'GET /api/v1/statuses/:id' do
subject do
get "/api/v1/statuses/#{status.id}", headers: headers
end end
describe 'GET #show' do
let(:scopes) { 'read:statuses' } let(:scopes) { 'read:statuses' }
let(:status) { Fabricate(:status, account: user.account) } let(:status) { Fabricate(:status, account: user.account) }
it_behaves_like 'forbidden for wrong scope', 'write write:statuses'
it 'returns http success' do it 'returns http success' do
get :show, params: { id: status.id } subject
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
@ -31,11 +33,10 @@ RSpec.describe Api::V1::StatusesController do
end end
it 'returns filter information', :aggregate_failures do it 'returns filter information', :aggregate_failures do
get :show, params: { id: status.id } subject
json = body_as_json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json[:filtered][0]).to include({ expect(body_as_json[:filtered][0]).to include({
filter: a_hash_including({ filter: a_hash_including({
id: user.account.custom_filters.first.id.to_s, id: user.account.custom_filters.first.id.to_s,
title: 'filter1', title: 'filter1',
@ -55,11 +56,10 @@ RSpec.describe Api::V1::StatusesController do
end end
it 'returns filter information', :aggregate_failures do it 'returns filter information', :aggregate_failures do
get :show, params: { id: status.id } subject
json = body_as_json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json[:filtered][0]).to include({ expect(body_as_json[:filtered][0]).to include({
filter: a_hash_including({ filter: a_hash_including({
id: user.account.custom_filters.first.id.to_s, id: user.account.custom_filters.first.id.to_s,
title: 'filter1', title: 'filter1',
@ -78,11 +78,10 @@ RSpec.describe Api::V1::StatusesController do
end end
it 'returns filter information', :aggregate_failures do it 'returns filter information', :aggregate_failures do
get :show, params: { id: status.id } subject
json = body_as_json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json[:reblog][:filtered][0]).to include({ expect(body_as_json[:reblog][:filtered][0]).to include({
filter: a_hash_including({ filter: a_hash_including({
id: user.account.custom_filters.first.id.to_s, id: user.account.custom_filters.first.id.to_s,
title: 'filter1', title: 'filter1',
@ -94,7 +93,7 @@ RSpec.describe Api::V1::StatusesController do
end end
end end
describe 'GET #context' do describe 'GET /api/v1/statuses/:id/context' do
let(:scopes) { 'read:statuses' } let(:scopes) { 'read:statuses' }
let(:status) { Fabricate(:status, account: user.account) } let(:status) { Fabricate(:status, account: user.account) }
@ -103,20 +102,26 @@ RSpec.describe Api::V1::StatusesController do
end end
it 'returns http success' do it 'returns http success' do
get :context, params: { id: status.id } get "/api/v1/statuses/#{status.id}/context", headers: headers
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
end end
describe 'POST #create' do describe 'POST /api/v1/statuses' do
let(:scopes) { 'write:statuses' } subject do
post '/api/v1/statuses', headers: headers, params: params
context 'with a basic status body' do
before do
post :create, params: { status: 'Hello world' }
end end
let(:scopes) { 'write:statuses' }
let(:params) { { status: 'Hello world' } }
it_behaves_like 'forbidden for wrong scope', 'read read:statuses'
context 'with a basic status body' do
it 'returns rate limit headers', :aggregate_failures do it 'returns rate limit headers', :aggregate_failures do
subject
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
expect(response.headers['X-RateLimit-Remaining']).to eq (RateLimiter::FAMILIES[:statuses][:limit] - 1).to_s expect(response.headers['X-RateLimit-Remaining']).to eq (RateLimiter::FAMILIES[:statuses][:limit] - 1).to_s
@ -127,22 +132,22 @@ RSpec.describe Api::V1::StatusesController do
let!(:alice) { Fabricate(:account, username: 'alice') } let!(:alice) { Fabricate(:account, username: 'alice') }
let!(:bob) { Fabricate(:account, username: 'bob') } let!(:bob) { Fabricate(:account, username: 'bob') }
before do let(:params) { { status: '@alice hm, @bob is really annoying lately', allowed_mentions: [alice.id] } }
post :create, params: { status: '@alice hm, @bob is really annoying lately', allowed_mentions: [alice.id] }
end
it 'returns serialized extra accounts in body', :aggregate_failures do it 'returns serialized extra accounts in body', :aggregate_failures do
subject
expect(response).to have_http_status(422) expect(response).to have_http_status(422)
expect(body_as_json[:unexpected_accounts].map { |a| a.slice(:id, :acct) }).to eq [{ id: bob.id.to_s, acct: bob.acct }] expect(body_as_json[:unexpected_accounts].map { |a| a.slice(:id, :acct) }).to eq [{ id: bob.id.to_s, acct: bob.acct }]
end end
end end
context 'with missing parameters' do context 'with missing parameters' do
before do let(:params) { {} }
post :create, params: {}
end
it 'returns rate limit headers', :aggregate_failures do it 'returns rate limit headers', :aggregate_failures do
subject
expect(response).to have_http_status(422) expect(response).to have_http_status(422)
expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
end end
@ -152,10 +157,11 @@ RSpec.describe Api::V1::StatusesController do
before do before do
rate_limiter = RateLimiter.new(user.account, family: :statuses) rate_limiter = RateLimiter.new(user.account, family: :statuses)
300.times { rate_limiter.record! } 300.times { rate_limiter.record! }
post :create, params: { status: 'Hello world' }
end end
it 'returns rate limit headers', :aggregate_failures do it 'returns rate limit headers', :aggregate_failures do
subject
expect(response).to have_http_status(429) expect(response).to have_http_status(429)
expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
expect(response.headers['X-RateLimit-Remaining']).to eq '0' expect(response.headers['X-RateLimit-Remaining']).to eq '0'
@ -163,29 +169,37 @@ RSpec.describe Api::V1::StatusesController do
end end
end end
describe 'DELETE #destroy' do describe 'DELETE /api/v1/statuses/:id' do
subject do
delete "/api/v1/statuses/#{status.id}", headers: headers
end
let(:scopes) { 'write:statuses' } let(:scopes) { 'write:statuses' }
let(:status) { Fabricate(:status, account: user.account) } let(:status) { Fabricate(:status, account: user.account) }
before do it_behaves_like 'forbidden for wrong scope', 'read read:statuses'
post :destroy, params: { id: status.id }
end
it 'removes the status', :aggregate_failures do it 'removes the status', :aggregate_failures do
subject
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(Status.find_by(id: status.id)).to be_nil expect(Status.find_by(id: status.id)).to be_nil
end end
end end
describe 'PUT #update' do describe 'PUT /api/v1/statuses/:id' do
subject do
put "/api/v1/statuses/#{status.id}", headers: headers, params: { status: 'I am updated' }
end
let(:scopes) { 'write:statuses' } let(:scopes) { 'write:statuses' }
let(:status) { Fabricate(:status, account: user.account) } let(:status) { Fabricate(:status, account: user.account) }
before do it_behaves_like 'forbidden for wrong scope', 'read read:statuses'
put :update, params: { id: status.id, status: 'I am updated' }
end
it 'updates the status', :aggregate_failures do it 'updates the status', :aggregate_failures do
subject
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(status.reload.text).to eq 'I am updated' expect(status.reload.text).to eq 'I am updated'
end end
@ -193,49 +207,49 @@ RSpec.describe Api::V1::StatusesController do
end end
context 'without an oauth token' do context 'without an oauth token' do
before do
allow(controller).to receive(:doorkeeper_token).and_return(nil)
end
context 'with a private status' do context 'with a private status' do
let(:status) { Fabricate(:status, account: user.account, visibility: :private) } let(:status) { Fabricate(:status, visibility: :private) }
describe 'GET #show' do describe 'GET /api/v1/statuses/:id' do
it 'returns http unauthorized' do it 'returns http unauthorized' do
get :show, params: { id: status.id } get "/api/v1/statuses/#{status.id}"
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
end end
describe 'GET #context' do describe 'GET /api/v1/statuses/:id/context' do
before do before do
Fabricate(:status, account: user.account, thread: status) Fabricate(:status, thread: status)
end end
it 'returns http unauthorized' do it 'returns http unauthorized' do
get :context, params: { id: status.id } get "/api/v1/statuses/#{status.id}/context"
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
end end
end end
context 'with a public status' do context 'with a public status' do
let(:status) { Fabricate(:status, account: user.account, visibility: :public) } let(:status) { Fabricate(:status, visibility: :public) }
describe 'GET #show' do describe 'GET /api/v1/statuses/:id' do
it 'returns http success' do it 'returns http success' do
get :show, params: { id: status.id } get "/api/v1/statuses/#{status.id}"
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
end end
describe 'GET #context' do describe 'GET /api/v1/statuses/:id/context' do
before do before do
Fabricate(:status, account: user.account, thread: status) Fabricate(:status, thread: status)
end end
it 'returns http success' do it 'returns http success' do
get :context, params: { id: status.id } get "/api/v1/statuses/#{status.id}/context"
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
end end