|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe DfE::Analytics::ApiRequests, type: :request do |
| 4 | + before do |
| 5 | + controller = Class.new(ApplicationController) do |
| 6 | + include DfE::Analytics::ApiRequests |
| 7 | + |
| 8 | + def index |
| 9 | + render plain: 'hello' |
| 10 | + end |
| 11 | + |
| 12 | + private |
| 13 | + |
| 14 | + def current_user |
| 15 | + Struct.new(:id).new(1) |
| 16 | + end |
| 17 | + |
| 18 | + def current_namespace |
| 19 | + 'example_namespace' |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + unauthenticated_controller = Class.new(ApplicationController) do |
| 24 | + include DfE::Analytics::ApiRequests |
| 25 | + |
| 26 | + def index |
| 27 | + render plain: 'hello' |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + stub_const('TestController', controller) |
| 32 | + stub_const('TestUnauthenticatedController', unauthenticated_controller) |
| 33 | + end |
| 34 | + |
| 35 | + around do |ex| |
| 36 | + Rails.application.routes.draw do |
| 37 | + get '/example/path' => 'test#index' |
| 38 | + get '/unauthenticated_example' => 'test_unauthenticated#index' |
| 39 | + get '/healthcheck' => 'test#index' |
| 40 | + get '/regex_path/test' => 'test#index' |
| 41 | + get '/some/path/with/regex_path' => 'test#index' |
| 42 | + get '/another/path/to/regex_path' => 'test#index' |
| 43 | + get '/included/path' => 'test#index' |
| 44 | + end |
| 45 | + |
| 46 | + ex.run |
| 47 | + ensure |
| 48 | + Rails.application.routes_reloader.reload! |
| 49 | + end |
| 50 | + |
| 51 | + let!(:event) do |
| 52 | + { environment: 'test', |
| 53 | + event_type: 'api_request', |
| 54 | + request_user_agent: 'Test agent', |
| 55 | + request_method: 'GET', |
| 56 | + request_path: '/example/path', |
| 57 | + request_query: [{ key: 'page', |
| 58 | + value: ['1'] }, |
| 59 | + { key: 'per_page', |
| 60 | + value: ['25'] }, |
| 61 | + { key: 'array_param[]', |
| 62 | + value: %w[1 2] }], |
| 63 | + request_referer: nil, |
| 64 | + anonymised_user_agent_and_ip: '6b3f52c670279e133a78a03e34eea436c65395417ec264eb9f8c1e6da4f5ed56', |
| 65 | + response_content_type: 'text/plain; charset=utf-8', |
| 66 | + response_status: 200, |
| 67 | + namespace: 'example_namespace', |
| 68 | + user_id: 1 } |
| 69 | + end |
| 70 | + |
| 71 | + before do |
| 72 | + DfE::Analytics.configure do |config| |
| 73 | + config.excluded_paths = ['/healthcheck', %r{^/regex_path/.*$}, /regex_path$/] |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + it 'sends request data to BigQuery' do |
| 78 | + request = stub_analytics_event_submission |
| 79 | + DfE::Analytics::Testing.webmock! do |
| 80 | + perform_enqueued_jobs do |
| 81 | + get('/example/path', |
| 82 | + params: { page: '1', per_page: '25', array_param: %w[1 2] }, |
| 83 | + headers: { 'HTTP_USER_AGENT' => 'Test agent', 'X-REAL-IP' => '1.2.3.4' }) |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + expect(request.with do |req| |
| 88 | + body = JSON.parse(req.body) |
| 89 | + payload = body['rows'].first['json'] |
| 90 | + expect(payload.except('occurred_at', 'request_uuid')).to match(event.deep_stringify_keys) |
| 91 | + end).to have_been_made |
| 92 | + end |
| 93 | + |
| 94 | + describe 'an event without user or namespace' do |
| 95 | + let!(:event) do |
| 96 | + { environment: 'test', |
| 97 | + event_type: 'api_request', |
| 98 | + request_user_agent: nil, |
| 99 | + request_method: 'GET', |
| 100 | + request_path: '/unauthenticated_example', |
| 101 | + request_query: [], |
| 102 | + request_referer: nil, |
| 103 | + anonymised_user_agent_and_ip: '6694f83c9f476da31f5df6bcc520034e7e57d421d247b9d34f49edbfc84a764c', |
| 104 | + response_content_type: 'text/plain; charset=utf-8', |
| 105 | + response_status: 200 } |
| 106 | + end |
| 107 | + |
| 108 | + it 'does not require a user' do |
| 109 | + request = stub_analytics_event_submission |
| 110 | + |
| 111 | + DfE::Analytics::Testing.webmock! do |
| 112 | + perform_enqueued_jobs do |
| 113 | + get('/unauthenticated_example', |
| 114 | + headers: { 'X-REAL-IP' => '1.2.3.4' }) |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + expect(request.with do |req| |
| 119 | + body = JSON.parse(req.body) |
| 120 | + payload = body['rows'].first['json'] |
| 121 | + expect(payload.except('occurred_at', 'request_uuid')).to match(event.deep_stringify_keys) |
| 122 | + end).to have_been_made |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + context 'when request path is in the skip list' do |
| 127 | + it 'does not send healthcheck request data to BigQuery' do |
| 128 | + request = stub_analytics_event_submission |
| 129 | + |
| 130 | + DfE::Analytics::Testing.webmock! do |
| 131 | + perform_enqueued_jobs do |
| 132 | + get('/healthcheck') |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + expect(request).not_to have_been_made |
| 137 | + end |
| 138 | + |
| 139 | + it 'does not send regex_path/test request data to BigQuery' do |
| 140 | + request = stub_analytics_event_submission |
| 141 | + |
| 142 | + DfE::Analytics::Testing.webmock! do |
| 143 | + perform_enqueued_jobs do |
| 144 | + get('/regex_path/test') |
| 145 | + end |
| 146 | + end |
| 147 | + |
| 148 | + expect(request).not_to have_been_made |
| 149 | + end |
| 150 | + |
| 151 | + it 'does not send some/path/with/regex_path request data to BigQuery' do |
| 152 | + request = stub_analytics_event_submission |
| 153 | + |
| 154 | + DfE::Analytics::Testing.webmock! do |
| 155 | + perform_enqueued_jobs do |
| 156 | + get('/some/path/with/regex_path') |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + expect(request).not_to have_been_made |
| 161 | + end |
| 162 | + |
| 163 | + it 'does not send another/path/to/regex_path request data to BigQuery' do |
| 164 | + request = stub_analytics_event_submission |
| 165 | + |
| 166 | + DfE::Analytics::Testing.webmock! do |
| 167 | + perform_enqueued_jobs do |
| 168 | + get('/another/path/to/regex_path') |
| 169 | + end |
| 170 | + end |
| 171 | + |
| 172 | + expect(request).not_to have_been_made |
| 173 | + end |
| 174 | + end |
| 175 | +end |
0 commit comments