forked from DFE-Digital/dfe-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics_spec.rb
More file actions
365 lines (295 loc) · 10.8 KB
/
analytics_spec.rb
File metadata and controls
365 lines (295 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# frozen_string_literal: true
RSpec.describe DfE::Analytics do
it 'has a version number' do
expect(DfE::Analytics::VERSION).not_to be nil
end
it 'supports the anonymise method for backwards compatibility' do
expect(DfE::Analytics.anonymise('foo_bar')).to eq('4928cae8b37b3d1113f5e01e60c967df6c2b9e826dc7d91488d23a62fec715ba')
end
it 'has documentation entries for all the config options' do
config_options = DfE::Analytics.config.members
config_options.each do |option|
expect(I18n.t("dfe.analytics.config.#{option}.description")).not_to match(/translation missing/)
expect(I18n.t("dfe.analytics.config.#{option}.default")).not_to match(/translation missing/)
end
end
describe 'when no database connection is available on rails 7.2 or higher' do
before do
allow(Rails).to receive(:version).and_return('7.2')
allow(ActiveRecord::Base).to receive(:with_connection).and_raise(ActiveRecord::ConnectionNotEstablished)
end
it 'recovers and logs' do
expect(Rails.logger).to receive(:error).with(/No database connection/)
expect { DfE::Analytics.initialize! }.not_to raise_error
end
end
describe 'when no database connection is available on rails 7.1 or lower' do
before do
allow(Rails).to receive(:version).and_return('7.1')
allow(ActiveRecord::Base).to receive(:connection).and_raise(ActiveRecord::ConnectionNotEstablished)
end
it 'recovers and logs' do
expect(Rails.logger).to receive(:error).with(/No database connection/)
expect { DfE::Analytics.initialize! }.not_to raise_error
end
end
describe 'when migrations are pending' do
it 'recovers and logs' do
allow(DfE::Analytics::Fields).to receive(:check!).and_raise(ActiveRecord::PendingMigrationError)
expect(Rails.logger).to receive(:error).with(/Database requires migration/)
expect { DfE::Analytics.initialize! }.not_to raise_error
end
end
describe 'when ActiveRecord is not loaded' do
it 'recovers and logs' do
hide_const('ActiveRecord')
expect(Rails.logger).to receive(:info).with(/ActiveRecord not loaded; DfE Analytics will only track non-database requests./)
expect { DfE::Analytics.initialize! }.not_to raise_error
end
end
describe 'field checks on initialization' do
# field validity is computed from allowlist, blocklist and database. See
# Analytics::Fields for more details
with_model :Candidate do
table
end
context 'when the field lists are valid' do
before do
allow(DfE::Analytics).to receive(:allowlist).and_return(
Candidate.table_name.to_sym => ['id']
)
end
it 'raises no error' do
expect { DfE::Analytics.initialize! }.not_to raise_error
end
end
context 'when a field list is invalid' do
before do
allow(DfE::Analytics).to receive(:allowlist).and_return({ invalid: [:fields] })
end
it 'raises an error' do
expect { DfE::Analytics.initialize! }.to raise_error(DfE::Analytics::ConfigurationError)
end
end
end
describe 'auto-inclusion of model callbacks' do
context 'when a model has not included DfE::Analytics::Entities' do
with_model :Candidate do
table
end
before do
allow(DfE::Analytics).to receive(:allowlist).and_return(
Candidate.table_name.to_sym => ['id']
)
end
it 'includes it' do
expect(Candidate.include?(DfE::Analytics::Entities)).to be false
DfE::Analytics.initialize!
expect(Candidate.include?(DfE::Analytics::Entities)).to be true
end
end
context 'when models have already included DfE::Analytics::Entities' do
with_model :Candidate do
table
model do
include DfE::Analytics::Entities
end
end
with_model :School do
table
model do
include DfE::Analytics::Entities
end
end
before do
allow(DfE::Analytics).to receive(:allowlist).and_return(
Candidate.table_name.to_sym => ['id'],
School.table_name.to_sym => ['id']
)
end
it 'logs deprecation warnings' do
allow(Rails.logger).to receive(:warn).and_call_original
DfE::Analytics.initialize!
expect(Rails.logger).to have_received(:warn).twice.with(/DEPRECATION WARNING/)
end
end
end
describe '#entities_for_analytics' do
with_model :Candidate do
table
model do
include DfE::Analytics::Entities
end
end
before do
allow(DfE::Analytics).to receive(:allowlist).and_return({
Candidate.table_name.to_sym => %i[id]
})
end
it 'returns the entities in the allowlist' do
expect(DfE::Analytics.entities_for_analytics).to eq [Candidate.table_name.to_sym]
end
end
describe '#user_identifier' do
let(:user_class) { Struct.new(:id) }
let(:id) { rand(1000) }
let(:user) { user_class.new(id) }
it 'calls the user_identifier configation' do
expect(described_class.user_identifier(user)).to eq id
end
context 'with a customised user_identifier proc' do
let(:user_class) { Struct.new(:identifier) }
before do
allow(described_class.config).to receive(:user_identifier)
.and_return(lambda(&:identifier))
end
it 'delegates to the provided proc' do
expect(described_class.user_identifier(user)).to eq id
end
end
end
describe '.async?' do
context 'when the async config has been set to true' do
before do
described_class.configure { |config| config.async = true }
end
it 'returns true' do
expect(described_class.async?).to be true
end
end
context 'when the async config has been set to false' do
before do
described_class.configure { |config| config.async = false }
end
it 'returns false' do
expect(described_class.async?).to be false
end
end
end
describe '.extract_model_attributes' do
with_model :Candidate do
table do |t|
t.string :email_address
t.string :hidden_data
t.integer :age
end
end
before do
allow(DfE::Analytics).to receive(:allowlist).and_return({
Candidate.table_name.to_sym => %w[email_address hidden_data age]
})
allow(DfE::Analytics).to receive(:hidden_pii).and_return({
Candidate.table_name.to_sym => %w[hidden_data age]
})
end
let(:candidate) { Candidate.create(email_address: 'test@example.com', hidden_data: 'secret', age: 50) }
it 'correctly separates and obfuscates attributes' do
result = described_class.extract_model_attributes(candidate)
expect(result[:data].keys).to include('email_address')
expect(result[:hidden_data]['hidden_data']).to eq('secret')
expect(result[:hidden_data]['age']).to eq(50)
end
it 'correctly separates allowed and hidden attributes' do
result = described_class.extract_model_attributes(candidate)
expect(result[:data].keys).to include('email_address')
expect(result[:data]).not_to have_key('hidden_data')
expect(result[:data]).not_to have_key('age')
expect(result[:hidden_data]['hidden_data']).to eq('secret')
expect(result[:hidden_data]['age']).to eq(50)
end
it 'does not error if no hidden data is sent' do
candidate = Candidate.create(email_address: 'test@example.com')
allow(DfE::Analytics).to receive(:allowlist).and_return(Candidate.table_name.to_sym => %w[email_address])
result = described_class.extract_model_attributes(candidate)
expect(result[:data].keys).to include('email_address')
expect(result[:hidden_data]).to be_nil.or be_empty
expect { DfE::Analytics.extract_model_attributes(candidate) }.not_to raise_error
end
end
describe '.parse_maintenance_window' do
context 'with a valid maintenance window' do
before do
allow(described_class.config).to receive(:bigquery_maintenance_window)
.and_return('01-01-2020 00:00..01-01-2020 23:59')
end
it 'returns the correct start and end times' do
start_time, end_time = described_class.parse_maintenance_window
expect(start_time).to eq(DateTime.new(2020, 1, 1, 0, 0))
expect(end_time).to eq(DateTime.new(2020, 1, 1, 23, 59))
end
end
context 'when start time is after end time' do
before do
allow(described_class.config).to receive(:bigquery_maintenance_window)
.and_return('01-01-2020 23:59..01-01-2020 00:00')
end
it 'logs an error and returns [nil, nil]' do
expect(Rails.logger).to receive(:warn).with(/Start time is after end time/)
expect(described_class.parse_maintenance_window).to eq([nil, nil])
end
end
context 'with an invalid format' do
before do
allow(described_class.config).to receive(:bigquery_maintenance_window)
.and_return('invalid_format')
end
it 'logs an error and returns [nil, nil]' do
expect(Rails.logger).to receive(:error).with(/Unexpected error/)
expect(described_class.parse_maintenance_window).to eq([nil, nil])
end
end
end
describe '.within_maintenance_window?' do
context 'when the current time is within the maintenance window' do
before do
allow(described_class).to receive(:parse_maintenance_window)
.and_return([DateTime.now - 1.hour, DateTime.now + 1.hour])
end
it 'returns true' do
expect(described_class.within_maintenance_window?).to be true
end
end
context 'when the current time is outside the maintenance window' do
before do
allow(described_class).to receive(:parse_maintenance_window)
.and_return([DateTime.now - 2.days, DateTime.now - 1.day])
end
it 'returns false' do
expect(described_class.within_maintenance_window?).to be false
end
end
end
describe 'excluding models via configuration' do
with_model :Candidate do
table do |t|
t.string :email_address
t.string :first_name
t.string :last_name
t.string :dob
end
end
with_model :Teacher do
table do |t|
t.string :email_address
t.string :first_name
t.string :last_name
t.string :dob
end
end
before do
DfE::Analytics.config.excluded_models_proc = proc { |x| x.to_s =~ /Teacher/ }
end
it 'does not use disabled model' do
expect(DfE::Analytics.all_entities_in_application.length).to eq(1)
expect(DfE::Analytics.all_entities_in_application.first.to_s).to match(/with_model_candidates/)
end
end
describe 'when ActiveRecord is not available' do
before do
hide_const('ActiveRecord')
end
it 'initializes without errors' do
expect { DfE::Analytics.initialize! }.not_to raise_error
end
end
end