2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-12-21 14:00:07 +01:00
|
|
|
describe API::V2::BaseController, type: :controller do
|
2024-01-17 09:31:21 +01:00
|
|
|
describe 'ensure_authorized_network and token_is_not_expired' do
|
2024-06-01 22:50:48 +02:00
|
|
|
let(:admin) { administrateurs(:default_admin) }
|
2023-12-21 14:00:07 +01:00
|
|
|
let(:token_bearer_couple) { APIToken.generate(admin) }
|
|
|
|
let(:token) { token_bearer_couple[0] }
|
|
|
|
let(:bearer) { token_bearer_couple[1] }
|
|
|
|
let(:remote_ip) { '0.0.0.0' }
|
|
|
|
|
|
|
|
controller(API::V2::BaseController) { def fake_action = render(plain: 'Hello, World!') }
|
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
describe 'with token' do
|
|
|
|
before do
|
|
|
|
routes.draw { get 'fake_action' => 'api/v2/base#fake_action' }
|
|
|
|
valid_headers = { 'Authorization' => "Bearer token=#{bearer}" }
|
|
|
|
request.headers.merge!(valid_headers)
|
|
|
|
request.remote_ip = remote_ip
|
|
|
|
end
|
2023-12-21 14:00:07 +01:00
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
describe 'GET #index' do
|
|
|
|
subject { get :fake_action }
|
2023-12-21 14:00:07 +01:00
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
context 'when no authorized networks are defined and the token is not expired' do
|
|
|
|
it { is_expected.to have_http_status(:ok) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the token is expired' do
|
|
|
|
before do
|
|
|
|
token.update!(expires_at: 1.day.ago)
|
|
|
|
end
|
2024-01-17 09:31:21 +01:00
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
it { is_expected.to have_http_status(:unauthorized) }
|
2024-01-17 09:31:21 +01:00
|
|
|
end
|
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
context 'when this is precisely the day the token expires' do
|
|
|
|
before do
|
|
|
|
token.update!(expires_at: Time.zone.today)
|
|
|
|
end
|
2024-01-17 09:31:21 +01:00
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
it { is_expected.to have_http_status(:ok) }
|
2024-01-17 09:31:21 +01:00
|
|
|
end
|
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
context 'when a single authorized network is defined' do
|
|
|
|
before do
|
|
|
|
token.update!(authorized_networks: [IPAddr.new('192.168.1.0/24')])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and the request comes from it' do
|
|
|
|
let(:remote_ip) { '192.168.1.23' }
|
|
|
|
|
|
|
|
it { is_expected.to have_http_status(:ok) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and the request does not come from it' do
|
|
|
|
let(:remote_ip) { '192.168.2.2' }
|
2023-12-21 14:00:07 +01:00
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
it { is_expected.to have_http_status(:forbidden) }
|
|
|
|
end
|
2023-12-21 14:00:07 +01:00
|
|
|
end
|
2024-09-13 14:27:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with admin' do
|
|
|
|
before do
|
|
|
|
routes.draw { get 'fake_action' => 'api/v2/base#fake_action' }
|
|
|
|
sign_in(admin.user)
|
|
|
|
end
|
2023-12-21 14:00:07 +01:00
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
describe 'GET #index' do
|
|
|
|
subject { get :fake_action }
|
2023-12-21 14:00:07 +01:00
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
context 'when admin is logged in' do
|
2023-12-21 14:00:07 +01:00
|
|
|
it { is_expected.to have_http_status(:ok) }
|
|
|
|
end
|
2024-09-13 14:27:59 +02:00
|
|
|
end
|
|
|
|
end
|
2023-12-21 14:00:07 +01:00
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
describe 'without token or admin' do
|
|
|
|
before do
|
|
|
|
routes.draw { get 'fake_action' => 'api/v2/base#fake_action' }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #index' do
|
|
|
|
let(:params) { {} }
|
|
|
|
subject { get :fake_action, params: }
|
2023-12-21 14:00:07 +01:00
|
|
|
|
2024-09-13 14:27:59 +02:00
|
|
|
context 'without token and not logged in' do
|
2023-12-21 14:00:07 +01:00
|
|
|
it { is_expected.to have_http_status(:forbidden) }
|
|
|
|
end
|
2024-09-13 14:27:59 +02:00
|
|
|
|
|
|
|
context 'with queryId' do
|
|
|
|
let(:params) { { queryId: '123' } }
|
|
|
|
it { is_expected.to have_http_status(:ok) }
|
|
|
|
end
|
2023-12-21 14:00:07 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|