2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-21 17:51:49 +01:00
|
|
|
describe APIController, type: :controller do
|
2023-09-20 09:28:03 +02:00
|
|
|
describe 'authenticate_from_token' do
|
2019-02-26 16:57:04 +01:00
|
|
|
let(:procedure) { create(:procedure) }
|
|
|
|
let(:admin) { procedure.administrateurs.first }
|
2018-08-24 15:53:57 +02:00
|
|
|
|
2023-09-20 09:28:03 +02:00
|
|
|
subject do
|
|
|
|
controller.send(:authenticate_from_token)
|
|
|
|
assigns(:api_token)
|
|
|
|
end
|
2018-08-24 15:53:57 +02:00
|
|
|
|
2018-09-26 15:39:45 +02:00
|
|
|
context 'when the admin has not any token' do
|
|
|
|
context 'and the token is not given' do
|
2023-09-20 09:28:03 +02:00
|
|
|
it { is_expected.to be nil }
|
2018-09-26 15:39:45 +02:00
|
|
|
end
|
2015-12-21 17:51:49 +01:00
|
|
|
end
|
2018-08-24 15:53:57 +02:00
|
|
|
|
2018-09-26 15:39:45 +02:00
|
|
|
context 'when the admin has a token' do
|
2023-09-20 09:28:03 +02:00
|
|
|
let(:token_bearer_couple) { APIToken.generate(admin) }
|
|
|
|
let(:token) { token_bearer_couple[0] }
|
|
|
|
let(:bearer) { token_bearer_couple[1] }
|
2018-08-24 15:53:57 +02:00
|
|
|
|
2018-09-26 15:39:45 +02:00
|
|
|
context 'and the token is given by params' do
|
2023-09-20 09:28:03 +02:00
|
|
|
before { controller.params[:token] = bearer }
|
2018-08-24 15:53:57 +02:00
|
|
|
|
2023-09-20 09:28:03 +02:00
|
|
|
it { is_expected.to eq(token) }
|
2018-09-26 15:39:45 +02:00
|
|
|
end
|
2018-08-24 15:53:57 +02:00
|
|
|
|
2018-09-26 15:39:45 +02:00
|
|
|
context 'and the token is given by header' do
|
|
|
|
before do
|
2023-09-20 09:28:03 +02:00
|
|
|
valid_headers = { 'Authorization' => "Bearer token=#{bearer}" }
|
2018-09-26 15:39:45 +02:00
|
|
|
request.headers.merge!(valid_headers)
|
|
|
|
end
|
2018-08-24 15:53:57 +02:00
|
|
|
|
2023-09-20 09:28:03 +02:00
|
|
|
it { is_expected.to eq(token) }
|
2018-08-24 15:53:57 +02:00
|
|
|
end
|
|
|
|
|
2018-09-26 15:39:45 +02:00
|
|
|
context 'and the token is not given' do
|
2023-09-20 09:28:03 +02:00
|
|
|
it { is_expected.to be nil }
|
2018-09-26 15:39:45 +02:00
|
|
|
end
|
2015-12-21 17:51:49 +01:00
|
|
|
end
|
|
|
|
end
|
2023-12-21 14:00:07 +01:00
|
|
|
|
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(APIController) { def fake_action = render(plain: 'Hello, World!') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
routes.draw { get 'fake_action' => 'api#fake_action' }
|
|
|
|
valid_headers = { 'Authorization' => "Bearer token=#{bearer}" }
|
|
|
|
request.headers.merge!(valid_headers)
|
|
|
|
request.remote_ip = remote_ip
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #index' do
|
|
|
|
subject { get :fake_action }
|
|
|
|
|
2024-01-17 09:31:21 +01:00
|
|
|
context 'when no authorized networks are defined and the token is not expired' do
|
2023-12-21 14:00:07 +01:00
|
|
|
it { is_expected.to have_http_status(:ok) }
|
|
|
|
end
|
|
|
|
|
2024-01-17 09:31:21 +01:00
|
|
|
context 'when the token is expired' do
|
|
|
|
before do
|
|
|
|
token.update!(expires_at: 1.day.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to have_http_status(:unauthorized) }
|
|
|
|
end
|
|
|
|
|
2023-12-21 14:00:07 +01: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' }
|
|
|
|
|
|
|
|
it { is_expected.to have_http_status(:forbidden) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|