|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Subscriptions API' do |
| 6 | + describe 'POST /api/subscriptions' do |
| 7 | + let(:path) { '/api/subscriptions' } |
| 8 | + let(:payload) do |
| 9 | + { |
| 10 | + subscription: { |
| 11 | + email: 'teacher@example.com', |
| 12 | + test_opt_in: true, |
| 13 | + privacy_policy: true |
| 14 | + } |
| 15 | + } |
| 16 | + end |
| 17 | + |
| 18 | + let(:submitter_result_success) do |
| 19 | + Subscriptions::PardotFormHandlerSubmitter::Result.new(success?: true) |
| 20 | + end |
| 21 | + let(:submitter_result_failure) do |
| 22 | + Subscriptions::PardotFormHandlerSubmitter::Result.new( |
| 23 | + success?: false, |
| 24 | + status: :service_unavailable, |
| 25 | + error_code: 'subscription_provider_unavailable', |
| 26 | + message: 'Subscription provider is currently unavailable.' |
| 27 | + ) |
| 28 | + end |
| 29 | + let(:submitter_result_rejected) do |
| 30 | + Subscriptions::PardotFormHandlerSubmitter::Result.new( |
| 31 | + success?: false, |
| 32 | + status: :bad_gateway, |
| 33 | + error_code: 'subscription_provider_rejected', |
| 34 | + message: 'Subscription provider rejected the request.' |
| 35 | + ) |
| 36 | + end |
| 37 | + let(:submitter) { instance_double(Subscriptions::PardotFormHandlerSubmitter) } |
| 38 | + |
| 39 | + before do |
| 40 | + allow(Subscriptions::PardotFormHandlerSubmitter).to receive(:new).and_return(submitter) |
| 41 | + allow(submitter).to receive(:call).and_return(submitter_result_success) |
| 42 | + end |
| 43 | + |
| 44 | + it 'returns success for a valid payload' do |
| 45 | + post(path, params: payload, as: :json) |
| 46 | + |
| 47 | + expect(response).to have_http_status(:ok) |
| 48 | + expect(response.parsed_body).to include( |
| 49 | + 'ok' => true, |
| 50 | + 'message' => 'Subscription accepted' |
| 51 | + ) |
| 52 | + expect(submitter).to have_received(:call).with( |
| 53 | + form_payload: { |
| 54 | + 'email' => 'teacher@example.com', |
| 55 | + 'test_opt_in' => true, |
| 56 | + 'privacy_policy' => true |
| 57 | + } |
| 58 | + ) |
| 59 | + end |
| 60 | + |
| 61 | + it 'returns 422 when email is missing' do |
| 62 | + post(path, params: payload.deep_merge(subscription: { email: '' }), as: :json) |
| 63 | + |
| 64 | + expect(response).to have_http_status(:unprocessable_content) |
| 65 | + expect(response.parsed_body['errors']).to include('email is required') |
| 66 | + end |
| 67 | + |
| 68 | + it 'returns 422 when email is malformed' do |
| 69 | + post(path, params: payload.deep_merge(subscription: { email: 'invalid-email' }), as: :json) |
| 70 | + |
| 71 | + expect(response).to have_http_status(:unprocessable_content) |
| 72 | + expect(response.parsed_body['errors']).to include('email is invalid') |
| 73 | + end |
| 74 | + |
| 75 | + it 'returns 422 when privacy_policy is not true' do |
| 76 | + post(path, params: payload.deep_merge(subscription: { privacy_policy: false }), as: :json) |
| 77 | + |
| 78 | + expect(response).to have_http_status(:unprocessable_content) |
| 79 | + expect(response.parsed_body['errors']).to include('privacy_policy must be true') |
| 80 | + end |
| 81 | + |
| 82 | + it 'returns 400 when subscription params are missing' do |
| 83 | + post(path, params: {}, as: :json) |
| 84 | + |
| 85 | + expect(response).to have_http_status(:bad_request) |
| 86 | + end |
| 87 | + |
| 88 | + it 'returns provider error status/message when provider submission fails' do |
| 89 | + allow(submitter).to receive(:call).and_return(submitter_result_failure) |
| 90 | + |
| 91 | + post(path, params: payload, as: :json) |
| 92 | + |
| 93 | + expect(response).to have_http_status(:service_unavailable) |
| 94 | + expect(response.parsed_body).to include( |
| 95 | + 'ok' => false, |
| 96 | + 'error_code' => 'subscription_provider_unavailable', |
| 97 | + 'message' => 'Subscription provider is currently unavailable.' |
| 98 | + ) |
| 99 | + end |
| 100 | + |
| 101 | + it 'returns provider rejection shape when provider rejects request' do |
| 102 | + allow(submitter).to receive(:call).and_return(submitter_result_rejected) |
| 103 | + |
| 104 | + post(path, params: payload, as: :json) |
| 105 | + |
| 106 | + expect(response).to have_http_status(:bad_gateway) |
| 107 | + expect(response.parsed_body).to include( |
| 108 | + 'ok' => false, |
| 109 | + 'error_code' => 'subscription_provider_rejected', |
| 110 | + 'message' => 'Subscription provider rejected the request.' |
| 111 | + ) |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments