Test: /api/v1/circles
、/api/v1/circles/:id/accounts
のテストを更新 (#130)
* Test: `/api/v1/circles`のテストを更新 * Add: `/api/v1/circles/accounts`のテストも追加
This commit is contained in:
parent
04bb467892
commit
1b76a51975
4 changed files with 358 additions and 155 deletions
|
@ -1,92 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
describe Api::V1::Circles::AccountsController do
|
||||
render_views
|
||||
|
||||
let(:user) { Fabricate(:user) }
|
||||
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
|
||||
let(:circle) { Fabricate(:circle, account: user.account) }
|
||||
let(:follow) { Fabricate(:follow, target_account: user.account) }
|
||||
|
||||
before do
|
||||
circle.accounts << follow.account
|
||||
allow(controller).to receive(:doorkeeper_token) { token }
|
||||
end
|
||||
|
||||
describe 'GET #index' do
|
||||
let(:scopes) { 'read:lists' }
|
||||
|
||||
it 'returns http success' do
|
||||
get :show, params: { circle_id: circle.id }
|
||||
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST #create' do
|
||||
let(:scopes) { 'write:lists' }
|
||||
let(:bob) { Fabricate(:account, username: 'bob') }
|
||||
|
||||
context 'when the added account is followed' do
|
||||
before do
|
||||
bob.follow!(user.account)
|
||||
post :create, params: { circle_id: circle.id, account_ids: [bob.id] }
|
||||
end
|
||||
|
||||
it 'returns http success' do
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
|
||||
it 'adds account to the circle' do
|
||||
expect(circle.accounts.include?(bob)).to be true
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the added account has been sent a follow request' do
|
||||
before do
|
||||
bob.follow_requests.create!(target_account: user.account)
|
||||
post :create, params: { circle_id: circle.id, account_ids: [bob.id] }
|
||||
end
|
||||
|
||||
it 'returns http success' do
|
||||
expect(response).to have_http_status(404)
|
||||
end
|
||||
|
||||
it 'adds account to the circle' do
|
||||
expect(circle.accounts.include?(bob)).to be false
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the added account is not followed' do
|
||||
before do
|
||||
post :create, params: { circle_id: circle.id, account_ids: [bob.id] }
|
||||
end
|
||||
|
||||
it 'returns http not found' do
|
||||
expect(response).to have_http_status(404)
|
||||
end
|
||||
|
||||
it 'does not add the account to the circle' do
|
||||
expect(circle.accounts.include?(bob)).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE #destroy' do
|
||||
let(:scopes) { 'write:lists' }
|
||||
|
||||
before do
|
||||
delete :destroy, params: { circle_id: circle.id, account_ids: [circle.accounts.first.id] }
|
||||
end
|
||||
|
||||
it 'returns http success' do
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
|
||||
it 'removes account from the circle' do
|
||||
expect(circle.accounts.count).to eq 0
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,63 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
describe Api::V1::CirclesController do
|
||||
render_views
|
||||
|
||||
let(:user) { Fabricate(:user) }
|
||||
let(:circle) { Fabricate(:circle, account: user.account) }
|
||||
|
||||
before do
|
||||
allow(controller).to receive(:doorkeeper_token) { token }
|
||||
end
|
||||
|
||||
context 'with a user context' do
|
||||
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:lists') }
|
||||
|
||||
describe 'GET #show' do
|
||||
it 'returns http success' do
|
||||
get :show, params: { id: circle.id }
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET #index' do
|
||||
it 'returns http success' do
|
||||
circle_id = circle.id.to_s
|
||||
Fabricate(:circle)
|
||||
get :index
|
||||
expect(response).to have_http_status(200)
|
||||
|
||||
circle_ids = body_as_json.pluck(:id)
|
||||
expect(circle_ids.size).to eq 1
|
||||
expect(circle_ids).to include circle_id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'with the wrong user context' do
|
||||
let(:other_user) { Fabricate(:user) }
|
||||
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: other_user.id, scopes: 'read') }
|
||||
|
||||
describe 'GET #show' do
|
||||
it 'returns http not found' do
|
||||
get :show, params: { id: circle.id }
|
||||
expect(response).to have_http_status(404)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'without a user context' do
|
||||
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: nil, scopes: 'read') }
|
||||
|
||||
describe 'GET #show' do
|
||||
it 'returns http unprocessable entity' do
|
||||
get :show, params: { id: circle.id }
|
||||
|
||||
expect(response).to have_http_status(422)
|
||||
expect(response.headers['Link']).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
165
spec/requests/api/v1/circles/accounts_spec.rb
Normal file
165
spec/requests/api/v1/circles/accounts_spec.rb
Normal 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
|
193
spec/requests/api/v1/circles_spec.rb
Normal file
193
spec/requests/api/v1/circles_spec.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue