|
3 | 3 | require 'rails_helper' |
4 | 4 |
|
5 | 5 | RSpec.describe Subscriptions::PardotFormHandlerSubmitter do |
6 | | - let(:endpoint_url) { 'https://example.com/form-handler' } |
7 | | - let(:service) { described_class.new(endpoint_url: endpoint_url) } |
8 | | - let(:payload) { { 'email' => 'teacher@example.com', 'privacy_policy' => true } } |
| 6 | + let(:endpoint_url) { 'https://example.test/form-handler' } |
| 7 | + let(:submitter) { described_class.new(endpoint_url:) } |
| 8 | + let(:connection) { instance_double(Faraday::Connection) } |
| 9 | + |
| 10 | + let(:payload) do |
| 11 | + { |
| 12 | + 'email' => 'teacher@example.com', |
| 13 | + 'test_opt_in' => true, |
| 14 | + 'privacy_policy' => true |
| 15 | + } |
| 16 | + end |
| 17 | + |
| 18 | + let(:headers) { {} } |
| 19 | + let(:response_body) { '' } |
| 20 | + let(:response_status) { 200 } |
| 21 | + let(:response) { instance_double(Faraday::Response, status: response_status, body: response_body, headers:) } |
| 22 | + |
| 23 | + before do |
| 24 | + allow(submitter).to receive(:faraday).and_return(connection) |
| 25 | + allow(connection).to receive(:post).and_return(response) |
| 26 | + allow(Sentry).to receive(:capture_exception) |
| 27 | + end |
9 | 28 |
|
10 | 29 | describe '#call' do |
11 | | - let(:connection) { instance_double(Faraday::Connection) } |
12 | | - let(:response) { instance_double(Faraday::Response, status: status, headers: headers, success?: success) } |
13 | | - let(:headers) { {} } |
14 | | - let(:status) { 200 } |
15 | | - let(:success) { true } |
16 | | - |
17 | | - before do |
18 | | - allow(Faraday).to receive(:new).and_return(connection) |
19 | | - allow(connection).to receive(:post).and_return(response) |
20 | | - end |
| 30 | + it 'returns success when status 200 and body contains success marker' do |
| 31 | + allow(response).to receive(:body).and_return('Cannot find success page to redirect to.') |
21 | 32 |
|
22 | | - it 'returns success for a 200 response' do |
23 | | - result = service.call(form_payload: payload) |
| 33 | + result = submitter.call(form_payload: payload) |
24 | 34 |
|
25 | 35 | expect(result.success?).to be(true) |
26 | 36 | end |
27 | 37 |
|
28 | | - it 'returns success for a 302 success redirect location' do |
29 | | - allow(response).to receive_messages(status: 302, success?: false, headers: { 'location' => '/subscriptions/success' }) |
| 38 | + it 'returns rejected when body contains error marker even with status 200' do |
| 39 | + allow(response).to receive(:body).and_return('Cannot find error page to redirect to.') |
30 | 40 |
|
31 | | - result = service.call(form_payload: payload) |
| 41 | + result = submitter.call(form_payload: payload) |
32 | 42 |
|
33 | | - expect(result.success?).to be(true) |
| 43 | + expect(result.success?).to be(false) |
| 44 | + expect(result.status).to eq(:bad_gateway) |
| 45 | + expect(result.error_code).to eq('subscription_provider_rejected') |
| 46 | + end |
| 47 | + |
| 48 | + it 'returns rejected when status is not 200' do |
| 49 | + allow(response).to receive(:status).and_return(302) |
| 50 | + |
| 51 | + result = submitter.call(form_payload: payload) |
| 52 | + |
| 53 | + expect(result.success?).to be(false) |
| 54 | + expect(result.status).to eq(:bad_gateway) |
| 55 | + expect(result.error_code).to eq('subscription_provider_rejected') |
34 | 56 | end |
35 | 57 |
|
36 | | - it 'returns rejected for a 302 error redirect location' do |
37 | | - allow(response).to receive_messages(status: 302, success?: false, headers: { 'location' => '/subscriptions/error' }) |
| 58 | + it 'returns ambiguous when status is 200 and body has no markers' do |
| 59 | + allow(response).to receive(:body).and_return('ok') |
38 | 60 |
|
39 | | - result = service.call(form_payload: payload) |
| 61 | + result = submitter.call(form_payload: payload) |
40 | 62 |
|
41 | | - aggregate_failures do |
42 | | - expect(result.success?).to be(false) |
43 | | - expect(result.error_code).to eq('subscription_provider_rejected') |
44 | | - end |
| 63 | + expect(result.success?).to be(false) |
| 64 | + expect(result.status).to eq(:bad_gateway) |
| 65 | + expect(result.error_code).to eq('subscription_provider_ambiguous') |
45 | 66 | end |
46 | 67 |
|
47 | | - it 'returns ambiguous for an unknown 302 redirect location' do |
48 | | - allow(response).to receive_messages(status: 302, success?: false, headers: { 'location' => '/subscriptions/unknown' }) |
| 68 | + it 'returns unavailable on Faraday::Error' do |
| 69 | + allow(connection).to receive(:post).and_raise(Faraday::Error, 'connection failed') |
| 70 | + |
| 71 | + result = submitter.call(form_payload: payload) |
| 72 | + |
| 73 | + expect(result.success?).to be(false) |
| 74 | + expect(result.status).to eq(:service_unavailable) |
| 75 | + expect(result.error_code).to eq('subscription_provider_unavailable') |
| 76 | + expect(Sentry).to have_received(:capture_exception) |
| 77 | + end |
| 78 | + |
| 79 | + it 'returns not configured when endpoint_url is blank' do |
| 80 | + blank_submitter = described_class.new(endpoint_url: '') |
| 81 | + |
| 82 | + result = blank_submitter.call(form_payload: payload) |
| 83 | + |
| 84 | + expect(result.success?).to be(false) |
| 85 | + expect(result.status).to eq(:service_unavailable) |
| 86 | + expect(result.error_code).to eq('subscription_provider_not_configured') |
| 87 | + end |
49 | 88 |
|
50 | | - result = service.call(form_payload: payload) |
| 89 | + it 'posts payload mapped to email and Tester only' do |
| 90 | + submitter.call(form_payload: payload) |
51 | 91 |
|
52 | | - aggregate_failures do |
53 | | - expect(result.success?).to be(false) |
54 | | - expect(result.error_code).to eq('subscription_provider_ambiguous') |
55 | | - end |
| 92 | + expect(connection).to have_received(:post).with( |
| 93 | + endpoint_url, |
| 94 | + { |
| 95 | + 'email' => 'teacher@example.com', |
| 96 | + 'Tester' => true |
| 97 | + } |
| 98 | + ) |
56 | 99 | end |
57 | 100 | end |
58 | 101 | end |
0 commit comments