|
11 | 11 | } |
12 | 12 | end |
13 | 13 |
|
14 | | - let(:events_client) { double(:events_client) } |
| 14 | + let(:client) { double(:client) } |
15 | 15 | let(:authorization) { double(:authorization) } |
16 | 16 |
|
17 | 17 | before(:each) do |
18 | 18 | allow(DfE::Analytics.config).to receive(:azure_federated_auth).and_return(true) |
19 | 19 |
|
20 | | - allow(Google::Apis::BigqueryV2::BigqueryService).to receive(:new).and_return(events_client) |
| 20 | + allow(Google::Apis::BigqueryV2::BigqueryService).to receive(:new).and_return(client) |
21 | 21 | allow(DfE::Analytics::AzureFederatedAuth).to receive(:gcp_client_credentials).and_return(authorization) |
22 | | - allow(events_client).to receive(:authorization=).and_return(authorization) |
23 | | - allow(events_client).to receive(:authorization).and_return(authorization) |
| 22 | + allow(client).to receive(:authorization=).and_return(authorization) |
| 23 | + allow(client).to receive(:authorization).and_return(authorization) |
24 | 24 |
|
25 | 25 | DfE::Analytics::Testing.webmock! |
26 | 26 | end |
27 | 27 |
|
28 | | - describe '#events_client' do |
| 28 | + describe '#client' do |
29 | 29 | it 'raises a configuration error on missing config values' do |
30 | 30 | with_analytics_config(bigquery_project_id: nil) do |
31 | | - expect { described_class.events_client }.to raise_error(DfE::Analytics::BigQueryApi::ConfigurationError) |
| 31 | + expect { described_class.client }.to raise_error(DfE::Analytics::Config::ConfigurationError) |
32 | 32 | end |
33 | 33 | end |
34 | 34 |
|
35 | 35 | context 'when authorization endpoint returns OK response' do |
36 | 36 | it 'calls the expected big query apis' do |
37 | 37 | with_analytics_config(test_dummy_config) do |
38 | | - expect(described_class.events_client).to eq(events_client) |
| 38 | + expect(described_class.client).to eq(client) |
39 | 39 | end |
40 | 40 | end |
41 | 41 | end |
|
52 | 52 | let(:response) { double(:response, insert_errors: []) } |
53 | 53 |
|
54 | 54 | it 'does not log the request when event_debug disabled' do |
55 | | - allow(events_client).to receive(:insert_all_table_data).and_return(response) |
| 55 | + allow(client).to receive(:insert_all_table_data).and_return(response) |
56 | 56 | expect(Rails.logger).not_to receive(:info) |
57 | 57 |
|
58 | 58 | insert |
59 | 59 | end |
60 | 60 |
|
61 | 61 | it 'calls the expected big query apis' do |
62 | | - expect(events_client).to receive(:insert_all_table_data) |
| 62 | + expect(client).to receive(:insert_all_table_data) |
63 | 63 | .with( |
64 | 64 | test_dummy_config[:bigquery_project_id], |
65 | 65 | test_dummy_config[:bigquery_dataset], |
|
78 | 78 | let(:insert_error) { double(:insert_error, index: 0, errors: [error]) } |
79 | 79 | let(:error) { double(:error, message: 'An error.') } |
80 | 80 |
|
81 | | - before { expect(events_client).to receive(:insert_all_table_data).and_return(response) } |
| 81 | + before { expect(client).to receive(:insert_all_table_data).and_return(response) } |
82 | 82 |
|
83 | 83 | it 'raises an exception' do |
84 | 84 | expect { insert }.to raise_error(DfE::Analytics::BigQueryApi::SendEventsError, /An error./) |
|
99 | 99 | end |
100 | 100 | end |
101 | 101 | end |
| 102 | + |
| 103 | + describe '.apply_policy_tags' do |
| 104 | + let(:tables) { { users: %w[email name] } } |
| 105 | + let(:policy_tag) { 'projects/my-project/locations/eu/taxonomies/123/policyTags/abc' } |
| 106 | + |
| 107 | + let(:table_schema_fields) do |
| 108 | + [ |
| 109 | + double('field', name: 'email').tap { |f| allow(f).to receive(:policy_tags=) }, |
| 110 | + double('field', name: 'name').tap { |f| allow(f).to receive(:policy_tags=) }, |
| 111 | + double('field', name: 'created_at').tap { |f| allow(f).to receive(:policy_tags=) } |
| 112 | + ] |
| 113 | + end |
| 114 | + |
| 115 | + let(:schema) do |
| 116 | + instance_double(Google::Apis::BigqueryV2::TableSchema, fields: table_schema_fields) |
| 117 | + end |
| 118 | + |
| 119 | + let(:table) do |
| 120 | + instance_double(Google::Apis::BigqueryV2::Table, schema: schema) |
| 121 | + end |
| 122 | + |
| 123 | + before do |
| 124 | + allow(client).to receive(:get_table).with( |
| 125 | + test_dummy_config[:bigquery_project_id], |
| 126 | + 'airbyte_dataset', |
| 127 | + 'users' |
| 128 | + ).and_return(table) |
| 129 | + |
| 130 | + allow(client).to receive(:patch_table) |
| 131 | + end |
| 132 | + |
| 133 | + it 'updates policy tags for matching columns only' do |
| 134 | + with_analytics_config(test_dummy_config.merge(bigquery_airbyte_dataset: 'airbyte_dataset')) do |
| 135 | + expect(client).to receive(:patch_table).with( |
| 136 | + test_dummy_config[:bigquery_project_id], |
| 137 | + 'airbyte_dataset', |
| 138 | + 'users', |
| 139 | + an_instance_of(Google::Apis::BigqueryV2::Table), |
| 140 | + fields: 'schema' |
| 141 | + ) |
| 142 | + |
| 143 | + described_class.apply_policy_tags(tables, policy_tag) |
| 144 | + |
| 145 | + expect(table_schema_fields[0]).to have_received(:policy_tags=) |
| 146 | + expect(table_schema_fields[1]).to have_received(:policy_tags=) |
| 147 | + expect(table_schema_fields[2]).not_to have_received(:policy_tags=) |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + context 'when get_table raises a ClientError' do |
| 152 | + it 'raises a PolicyTagError and logs the error' do |
| 153 | + with_analytics_config(test_dummy_config.merge(bigquery_airbyte_dataset: 'airbyte_dataset')) do |
| 154 | + allow(client).to receive(:get_table).and_raise(Google::Apis::ClientError.new('Table not found')) |
| 155 | + expect(Rails.logger).to receive(:error).with(/Failed to retrieve table: users/) |
| 156 | + expect do |
| 157 | + described_class.apply_policy_tags(tables, policy_tag) |
| 158 | + end.to raise_error(DfE::Analytics::BigQueryApi::PolicyTagError) |
| 159 | + end |
| 160 | + end |
| 161 | + end |
| 162 | + |
| 163 | + context 'when patch_table raises a ClientError' do |
| 164 | + it 'raises a PolicyTagError and logs the error' do |
| 165 | + with_analytics_config(test_dummy_config.merge(bigquery_airbyte_dataset: 'airbyte_dataset')) do |
| 166 | + allow(client).to receive(:patch_table).and_raise(Google::Apis::ClientError.new('Invalid schema')) |
| 167 | + expect(Rails.logger).to receive(:error).with(/Failed to update table: users/) |
| 168 | + expect do |
| 169 | + described_class.apply_policy_tags(tables, policy_tag) |
| 170 | + end.to raise_error(DfE::Analytics::BigQueryApi::PolicyTagError) |
| 171 | + end |
| 172 | + end |
| 173 | + end |
| 174 | + end |
102 | 175 | end |
0 commit comments