2023-10-14 01:23:16 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2024-09-04 15:12:25 +10:00
|
|
|
RSpec.describe 'Links' do
|
2023-10-14 01:23:16 +11:00
|
|
|
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)
|
2024-09-20 23:13:04 +10:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-10-14 01:23:16 +11:00
|
|
|
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
|
2024-01-12 02:13:11 +11:00
|
|
|
expect { subject }
|
|
|
|
.to change_link_trendable_to_true
|
2023-10-14 01:23:16 +11:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 23:13:04 +10:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-01-12 02:13:11 +11:00
|
|
|
expects_correct_link_data
|
2023-10-14 01:23:16 +11:00
|
|
|
end
|
|
|
|
|
2024-01-12 02:13:11 +11:00
|
|
|
def change_link_trendable_to_true
|
|
|
|
change { preview_card.reload.trendable }.from(false).to(true)
|
2023-10-14 01:23:16 +11:00
|
|
|
end
|
|
|
|
|
2024-01-12 02:13:11 +11:00
|
|
|
def expects_correct_link_data
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body).to match(
|
2023-10-14 01:23:16 +11:00
|
|
|
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)
|
2024-09-20 23:13:04 +10:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-10-14 01:23:16 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without an authorization header' do
|
|
|
|
let(:headers) { {} }
|
|
|
|
|
|
|
|
it 'returns http forbidden' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(403)
|
2024-09-20 23:13:04 +10:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-10-14 01:23:16 +11:00
|
|
|
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
|
2024-01-12 02:13:11 +11:00
|
|
|
expect { subject }
|
|
|
|
.to_not change_link_trendable
|
2023-10-14 01:23:16 +11:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 23:13:04 +10:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-10-14 01:23:16 +11:00
|
|
|
end
|
|
|
|
|
2024-01-12 02:13:11 +11:00
|
|
|
def change_link_trendable
|
|
|
|
change { preview_card.reload.trendable }
|
2023-10-14 01:23:16 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the link data' do
|
|
|
|
subject
|
|
|
|
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body).to match(
|
2023-10-14 01:23:16 +11:00
|
|
|
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)
|
2024-09-20 23:13:04 +10:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-10-14 01:23:16 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without an authorization header' do
|
|
|
|
let(:headers) { {} }
|
|
|
|
|
|
|
|
it 'returns http forbidden' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(403)
|
2024-09-20 23:13:04 +10:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-10-14 01:23:16 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|