Test: /api/v1/circles/api/v1/circles/:id/accountsのテストを更新 (#130)

* Test: `/api/v1/circles`のテストを更新

* Add: `/api/v1/circles/accounts`のテストも追加
This commit is contained in:
KMY(雪あすか) 2023-10-17 08:50:00 +09:00 committed by GitHub
parent 04bb467892
commit 1b76a51975
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 358 additions and 155 deletions

View file

@ -0,0 +1,165 @@
# 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/circles/:id/accounts' do
subject do
get "/api/v1/circles/#{circle.id}/accounts", headers: headers, params: params
end
let(:params) { { limit: 0 } }
let(:circle) { Fabricate(:circle, 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| account.follow!(user.account) }
circle.accounts << accounts
end
it_behaves_like 'forbidden for wrong scope', 'write write:lists'
it 'returns the accounts in the requested circle', :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/circles/:id/accounts' do
subject do
post "/api/v1/circles/#{circle.id}/accounts", headers: headers, params: params
end
let(:circle) { Fabricate(:circle, 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
bob.follow!(user.account)
end
it 'adds account to the circle', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(circle.accounts).to include(bob)
end
end
context 'when the added account is not followed' do
it 'does not add the account to the circle', :aggregate_failures do
subject
expect(response).to have_http_status(404)
expect(circle.accounts).to_not include(bob)
end
end
context 'when the circle is not owned by the requesting user' do
let(:circle) { Fabricate(:circle) }
before do
bob.follow!(user.account)
end
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
context 'when account is already in the circle' do
before do
bob.follow!(user.account)
circle.accounts << bob
end
it 'returns http unprocessable entity' do
subject
expect(response).to have_http_status(422)
end
end
end
describe 'DELETE /api/v1/circles/:id/accounts' do
subject do
delete "/api/v1/circles/#{circle.id}/accounts", headers: headers, params: params
end
context 'when the circle is owned by the requesting user' do
let(:circle) { Fabricate(:circle, account: user.account) }
let(:bob) { Fabricate(:account, username: 'bob') }
let(:peter) { Fabricate(:account, username: 'peter') }
let(:params) { { account_ids: [bob.id] } }
before do
bob.follow!(user.account)
peter.follow!(user.account)
circle.accounts << [bob, peter]
end
it 'removes the specified account from the circle', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(circle.accounts).to_not include(bob)
end
it 'does not remove any other account from the circle' do
subject
expect(circle.accounts).to include(peter)
end
context 'when the specified account is not in the circle' do
let(:params) { { account_ids: [0] } }
it 'does not remove any account from the circle', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(circle.accounts).to contain_exactly(bob, peter)
end
end
end
context 'when the circle is not owned by the requesting user' do
let(:circle) { Fabricate(:circle) }
let(:params) { {} }
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
end
end

View file

@ -0,0 +1,193 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Circles' 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/circles' do
subject do
get '/api/v1/circles', headers: headers
end
let!(:circles) do
[
Fabricate(:circle, account: user.account, title: 'first circle'),
Fabricate(:circle, account: user.account, title: 'second circle'),
Fabricate(:circle, account: user.account, title: 'third circle'),
Fabricate(:circle, account: user.account, title: 'fourth circle'),
]
end
let(:expected_response) do
circles.map do |circle|
{
id: circle.id.to_s,
title: circle.title,
}
end
end
before do
Fabricate(:circle)
end
it_behaves_like 'forbidden for wrong scope', 'write write:lists'
it 'returns the expected circles', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json).to match_array(expected_response)
end
end
describe 'GET /api/v1/circles/:id' do
subject do
get "/api/v1/circles/#{circle.id}", headers: headers
end
let(:circle) { Fabricate(:circle, account: user.account) }
it_behaves_like 'forbidden for wrong scope', 'write write:lists'
it 'returns the requested circle correctly', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json).to eq({
id: circle.id.to_s,
title: circle.title,
})
end
context 'when the circle belongs to a different user' do
let(:circle) { Fabricate(:circle) }
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
context 'when the circle does not exist' do
it 'returns http not found' do
get '/api/v1/circles/-1', headers: headers
expect(response).to have_http_status(404)
end
end
end
describe 'POST /api/v1/circles' do
subject do
post '/api/v1/circles', headers: headers, params: params
end
let(:params) { { title: 'my circle' } }
it_behaves_like 'forbidden for wrong scope', 'read read:lists'
it 'returns the new circle', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json).to match(a_hash_including(title: 'my circle'))
expect(Circle.where(account: user.account).count).to eq(1)
end
context 'when a title is not given' do
let(:params) { { title: '' } }
it 'returns http unprocessable entity' do
subject
expect(response).to have_http_status(422)
end
end
end
describe 'PUT /api/v1/circles/:id' do
subject do
put "/api/v1/circles/#{circle.id}", headers: headers, params: params
end
let(:circle) { Fabricate(:circle, account: user.account, title: 'my circle') }
let(:params) { { title: 'circle' } }
it_behaves_like 'forbidden for wrong scope', 'read read:lists'
it 'returns the updated circle', :aggregate_failures do
subject
expect(response).to have_http_status(200)
circle.reload
expect(body_as_json).to eq({
id: circle.id.to_s,
title: circle.title,
})
end
it 'updates the circle title' do
expect { subject }.to change { circle.reload.title }.from('my circle').to('circle')
end
context 'when the circle does not exist' do
it 'returns http not found' do
put '/api/v1/circles/-1', headers: headers, params: params
expect(response).to have_http_status(404)
end
end
context 'when the circle belongs to another user' do
let(:circle) { Fabricate(:circle) }
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
end
describe 'DELETE /api/v1/circles/:id' do
subject do
delete "/api/v1/circles/#{circle.id}", headers: headers
end
let(:circle) { Fabricate(:circle, account: user.account) }
it_behaves_like 'forbidden for wrong scope', 'read read:lists'
it 'deletes the circle', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(Circle.where(id: circle.id)).to_not exist
end
context 'when the circle does not exist' do
it 'returns http not found' do
delete '/api/v1/circles/-1', headers: headers
expect(response).to have_http_status(404)
end
end
context 'when the circle belongs to another user' do
let(:circle) { Fabricate(:circle) }
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
end
end