|
| 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 |
0 commit comments