Skip to content

Commit

Permalink
Convert more API specs from controller->request style (mastodon#29004)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski authored Mar 1, 2024
1 parent a25014d commit 18945f6
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

require 'rails_helper'

describe Api::V1::Accounts::StatusesController do
render_views
describe 'API V1 Accounts Statuses' 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}" } }

let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:statuses') }

before do
allow(controller).to receive(:doorkeeper_token) { token }
end

describe 'GET #index' do
describe 'GET /api/v1/accounts/:account_id/statuses' do
it 'returns expected headers', :aggregate_failures do
Fabricate(:status, account: user.account)
get :index, params: { account_id: user.account.id, limit: 1 }
get "/api/v1/accounts/#{user.account.id}/statuses", params: { limit: 1 }, headers: headers

expect(response).to have_http_status(200)
expect(links_from_header.size)
Expand All @@ -24,7 +20,7 @@

context 'with only media' do
it 'returns http success' do
get :index, params: { account_id: user.account.id, only_media: true }
get "/api/v1/accounts/#{user.account.id}/statuses", params: { only_media: true }, headers: headers

expect(response).to have_http_status(200)
end
Expand All @@ -36,7 +32,7 @@

before do
Fabricate(:status, account: user.account, thread: Fabricate(:status)) # Reply to another user
get :index, params: { account_id: user.account.id, exclude_replies: true }
get "/api/v1/accounts/#{user.account.id}/statuses", params: { exclude_replies: true }, headers: headers
end

it 'returns posts along with self replies', :aggregate_failures do
Expand All @@ -57,7 +53,7 @@
end

it 'returns http success and includes a header link' do
get :index, params: { account_id: user.account.id, pinned: true }
get "/api/v1/accounts/#{user.account.id}/statuses", params: { pinned: true }, headers: headers

expect(response).to have_http_status(200)
expect(links_from_header.size)
Expand All @@ -79,7 +75,7 @@
end

it 'returns http success and header pagination links to prev and next' do
get :index, params: { account_id: user.account.id, pinned: true }
get "/api/v1/accounts/#{user.account.id}/statuses", params: { pinned: true }, headers: headers

expect(response).to have_http_status(200)
expect(links_from_header.size)
Expand Down Expand Up @@ -109,15 +105,19 @@
end

it 'returns http success' do
get :index, params: { account_id: account.id, pinned: true }
get "/api/v1/accounts/#{account.id}/statuses", params: { pinned: true }, headers: headers

expect(response).to have_http_status(200)
end

context 'when user does not follow account' do
it 'lists the public status only' do
get :index, params: { account_id: account.id, pinned: true }
json = body_as_json
expect(json.map { |item| item[:id].to_i }).to eq [status.id]
get "/api/v1/accounts/#{account.id}/statuses", params: { pinned: true }, headers: headers

expect(body_as_json)
.to contain_exactly(
a_hash_including(id: status.id.to_s)
)
end
end

Expand All @@ -127,9 +127,13 @@
end

it 'lists both the public and the private statuses' do
get :index, params: { account_id: account.id, pinned: true }
json = body_as_json
expect(json.map { |item| item[:id].to_i }).to contain_exactly(status.id, private_status.id)
get "/api/v1/accounts/#{account.id}/statuses", params: { pinned: true }, headers: headers

expect(body_as_json)
.to contain_exactly(
a_hash_including(id: status.id.to_s),
a_hash_including(id: private_status.id.to_s)
)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,26 @@

require 'rails_helper'

describe Api::V1::Admin::Trends::StatusesController do
render_views

describe 'API V1 Admin Trends Statuses' 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(:account) { Fabricate(:account) }
let(:status) { Fabricate(:status) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }

before do
allow(controller).to receive(:doorkeeper_token) { token }
end

describe 'GET #index' do
describe 'GET /api/v1/admin/trends/statuses' do
it 'returns http success' do
get :index, params: { account_id: account.id, limit: 2 }
get '/api/v1/admin/trends/statuses', params: { account_id: account.id, limit: 2 }, headers: headers

expect(response).to have_http_status(200)
end
end

describe 'POST #approve' do
describe 'POST /api/v1/admin/trends/statuses/:id/approve' do
before do
post :approve, params: { id: status.id }
post "/api/v1/admin/trends/statuses/#{status.id}/approve", headers: headers
end

it_behaves_like 'forbidden for wrong scope', 'write:statuses'
Expand All @@ -37,9 +32,9 @@
end
end

describe 'POST #reject' do
describe 'POST /api/v1/admin/trends/statuses/:id/unapprove' do
before do
post :reject, params: { id: status.id }
post "/api/v1/admin/trends/statuses/#{status.id}/reject", headers: headers
end

it_behaves_like 'forbidden for wrong scope', 'write:statuses'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,26 @@

require 'rails_helper'

describe Api::V1::Admin::Trends::TagsController do
render_views

describe 'API V1 Admin Trends Tags' 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(:account) { Fabricate(:account) }
let(:tag) { Fabricate(:tag) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }

before do
allow(controller).to receive(:doorkeeper_token) { token }
end

describe 'GET #index' do
describe 'GET /api/v1/admin/trends/tags' do
it 'returns http success' do
get :index, params: { account_id: account.id, limit: 2 }
get '/api/v1/admin/trends/tags', params: { account_id: account.id, limit: 2 }, headers: headers

expect(response).to have_http_status(200)
end
end

describe 'POST #approve' do
describe 'POST /api/v1/admin/trends/tags/:id/approve' do
before do
post :approve, params: { id: tag.id }
post "/api/v1/admin/trends/tags/#{tag.id}/approve", headers: headers
end

it_behaves_like 'forbidden for wrong scope', 'write:statuses'
Expand All @@ -37,9 +32,9 @@
end
end

describe 'POST #reject' do
describe 'POST /api/v1/admin/trends/tags/:id/reject' do
before do
post :reject, params: { id: tag.id }
post "/api/v1/admin/trends/tags/#{tag.id}/reject", headers: headers
end

it_behaves_like 'forbidden for wrong scope', 'write:statuses'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@

require 'rails_helper'

RSpec.describe Api::V1::Announcements::ReactionsController do
render_views

RSpec.describe 'API V1 Announcements Reactions' do
let(:user) { Fabricate(:user) }
let(:scopes) { 'write:favourites' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }

let!(:announcement) { Fabricate(:announcement) }

describe 'PUT #update' do
describe 'PUT /api/v1/announcements/:announcement_id/reactions/:id' do
context 'without token' do
it 'returns http unauthorized' do
put :update, params: { announcement_id: announcement.id, id: '😂' }
put "/api/v1/announcements/#{announcement.id}/reactions/#{escaped_emoji}"

expect(response).to have_http_status 401
end
end

context 'with token' do
before do
allow(controller).to receive(:doorkeeper_token) { token }
put :update, params: { announcement_id: announcement.id, id: '😂' }
put "/api/v1/announcements/#{announcement.id}/reactions/#{escaped_emoji}", headers: headers
end

it 'creates reaction', :aggregate_failures do
Expand All @@ -32,22 +31,21 @@
end
end

describe 'DELETE #destroy' do
describe 'DELETE /api/v1/announcements/:announcement_id/reactions/:id' do
before do
announcement.announcement_reactions.create!(account: user.account, name: '😂')
end

context 'without token' do
it 'returns http unauthorized' do
delete :destroy, params: { announcement_id: announcement.id, id: '😂' }
delete "/api/v1/announcements/#{announcement.id}/reactions/#{escaped_emoji}"
expect(response).to have_http_status 401
end
end

context 'with token' do
before do
allow(controller).to receive(:doorkeeper_token) { token }
delete :destroy, params: { announcement_id: announcement.id, id: '😂' }
delete "/api/v1/announcements/#{announcement.id}/reactions/#{escaped_emoji}", headers: headers
end

it 'creates reaction', :aggregate_failures do
Expand All @@ -56,4 +54,8 @@
end
end
end

def escaped_emoji
CGI.escape('😂')
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@

require 'rails_helper'

RSpec.describe Api::V1::AnnouncementsController do
render_views

let(:user) { Fabricate(:user) }
let(:scopes) { 'read' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
RSpec.describe 'API V1 Announcements' do
let(:user) { Fabricate(:user) }
let(:scopes) { 'read' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }

let!(:announcement) { Fabricate(:announcement) }

describe 'GET #index' do
describe 'GET /api/v1/announcements' do
context 'without token' do
it 'returns http unprocessable entity' do
get :index
get '/api/v1/announcements'

expect(response).to have_http_status 422
end
end

context 'with token' do
before do
allow(controller).to receive(:doorkeeper_token) { token }
get :index
get '/api/v1/announcements', headers: headers
end

it 'returns http success' do
Expand All @@ -31,10 +30,11 @@
end
end

describe 'POST #dismiss' do
describe 'POST /api/v1/announcements/:id/dismiss' do
context 'without token' do
it 'returns http unauthorized' do
post :dismiss, params: { id: announcement.id }
post "/api/v1/announcements/#{announcement.id}/dismiss"

expect(response).to have_http_status 401
end
end
Expand All @@ -43,8 +43,7 @@
let(:scopes) { 'write:accounts' }

before do
allow(controller).to receive(:doorkeeper_token) { token }
post :dismiss, params: { id: announcement.id }
post "/api/v1/announcements/#{announcement.id}/dismiss", headers: headers
end

it 'dismisses announcement', :aggregate_failures do
Expand Down
Loading

0 comments on commit 18945f6

Please sign in to comment.