Skip to content

Commit 49287ed

Browse files
authored
Merge pull request #1271 from Crown-Commercial-Service/feature/nrmi-2-get-submissions-endpoint
Feature/nrmi 2 get submissions endpoint
2 parents e9187de + 82845c2 commit 49287ed

5 files changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class V2::SubmissionsController < ActionController::API
2+
include ApiKeyAuthenticatable
3+
4+
def index
5+
@submissions = Submission.all
6+
if params[:updated_at].present?
7+
@submissions = @submissions.where(updated_at: Time.zone.parse(params[:updated_at])..Time.zone.now)
8+
end
9+
10+
render json: @submissions
11+
rescue ArgumentError
12+
render json: { error: 'updated_at must be a valid date or datetime' }, status: :bad_request
13+
end
14+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class V2::TasksController < ActionController::API
2+
include ApiKeyAuthenticatable
3+
4+
def index
5+
@tasks = Task.all
6+
if params[:updated_at].present?
7+
@tasks = @tasks.where(updated_at: Time.zone.parse(params[:updated_at])..Time.zone.now)
8+
end
9+
10+
render json: @tasks
11+
rescue ArgumentError
12+
render json: { error: 'updated_at must be a valid date or datetime' }, status: :bad_request
13+
end
14+
end

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
resources :frameworks, only: :index
8181
resources :framework_lots, only: :index
8282
resources :agreement_framework_lots, only: :index
83+
resources :tasks, only: :index
84+
resources :submissions, only: :index
8385
end
8486

8587
namespace :admin do
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe 'V2::Submissions', type: :request do
4+
let(:valid_api_key) { create(:api_key) }
5+
let(:invalid_api_key) { 'invalid_key' }
6+
7+
describe 'GET /v2/submissions' do
8+
context 'with valid API key' do
9+
before do
10+
create_list(:submission, 3)
11+
get v2_submissions_path, headers: { 'API-Key' => valid_api_key.key }
12+
end
13+
14+
it 'returns all submissions when updated_at is not provided' do
15+
expect(response).to have_http_status(:ok)
16+
expect(JSON.parse(response.body).size).to eq(3)
17+
end
18+
19+
it 'returns submissions updated since the provided date' do
20+
old_submission = create(:submission, updated_at: 3.days.ago)
21+
recent_submission = create(:submission, updated_at: 1.hour.ago)
22+
23+
get v2_submissions_path, params: { updated_at: 2.days.ago.iso8601 }, headers: { 'API-Key' => valid_api_key.key }
24+
submission_ids = JSON.parse(response.body).pluck('id')
25+
26+
expect(response).to have_http_status(:ok)
27+
expect(submission_ids).to include(recent_submission.id)
28+
expect(submission_ids).not_to include(old_submission.id)
29+
end
30+
end
31+
32+
context 'with missing API key' do
33+
before { get v2_submissions_path }
34+
35+
it 'returns unauthorized status' do
36+
expect(response).to have_http_status(:unauthorized)
37+
expect(JSON.parse(response.body)['error']).to eq('API key is missing')
38+
end
39+
end
40+
41+
context 'with invalid API key' do
42+
before { get v2_submissions_path, headers: { 'API-Key' => invalid_api_key } }
43+
44+
it 'returns unauthorized status' do
45+
expect(response).to have_http_status(:unauthorized)
46+
expect(JSON.parse(response.body)['error']).to eq('API key is invalid')
47+
end
48+
end
49+
end
50+
end

spec/requests/v2/tasks_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe 'V2::Tasks', type: :request do
4+
let(:valid_api_key) { create(:api_key) }
5+
let(:invalid_api_key) { 'invalid_key' }
6+
7+
describe 'GET /v2/tasks' do
8+
context 'with valid API key' do
9+
before do
10+
create_list(:task, 3)
11+
get v2_tasks_path, headers: { 'API-Key' => valid_api_key.key }
12+
end
13+
14+
it 'returns all tasks when updated_at is not provided' do
15+
expect(response).to have_http_status(:ok)
16+
expect(JSON.parse(response.body).size).to eq(3)
17+
end
18+
19+
it 'returns tasks updated since the provided date' do
20+
old_task = create(:task, updated_at: 3.days.ago)
21+
recent_task = create(:task, updated_at: 1.hour.ago)
22+
23+
get v2_tasks_path, params: { updated_at: 2.days.ago.iso8601 }, headers: { 'API-Key' => valid_api_key.key }
24+
task_ids = JSON.parse(response.body).pluck('id')
25+
26+
expect(response).to have_http_status(:ok)
27+
expect(task_ids).to include(recent_task.id)
28+
expect(task_ids).not_to include(old_task.id)
29+
end
30+
end
31+
32+
context 'with missing API key' do
33+
before { get v2_tasks_path }
34+
35+
it 'returns unauthorized status' do
36+
expect(response).to have_http_status(:unauthorized)
37+
expect(JSON.parse(response.body)['error']).to eq('API key is missing')
38+
end
39+
end
40+
41+
context 'with invalid API key' do
42+
before { get v2_tasks_path, headers: { 'API-Key' => invalid_api_key } }
43+
44+
it 'returns unauthorized status' do
45+
expect(response).to have_http_status(:unauthorized)
46+
expect(JSON.parse(response.body)['error']).to eq('API key is invalid')
47+
end
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)