ncs_navigator_configuration 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ require File.expand_path("../../../spec_helper.rb", __FILE__)
2
+
3
+ module NcsNavigator
4
+ describe Configuration, "::VERSION" do
5
+ it "exists" do
6
+ lambda { Configuration::VERSION }.should_not raise_error
7
+ end
8
+
9
+ it "has 3 or 4 dot separated parts" do
10
+ Configuration::VERSION.split('.').size.should be_between(3, 4)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,477 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe NcsNavigator do
4
+ # Load the test INI into memory so it can be put into FakeFS
5
+ let(:everything_file) { File.expand_path('../everything.ini', __FILE__) }
6
+ let!(:everything_contents) { File.read(everything_file) }
7
+
8
+ after do
9
+ NcsNavigator.configuration = nil
10
+ end
11
+
12
+ describe '.configuration' do
13
+ include FakeFS::SpecHelpers
14
+
15
+ before do
16
+ File.open('/etc/nubic/ncs/navigator.ini', 'w') do |f|
17
+ f.puts '[Study Center]'
18
+ f.puts 'sc_id = 18'
19
+ f.puts 'sampling_units_file = foo.csv'
20
+ f.puts '[Staff Portal]'
21
+ f.puts 'uri = https://foo.example.com/sp'
22
+ f.puts '[Core]'
23
+ f.puts 'uri = https://foo.example.com/core'
24
+ f.puts '[PSC]'
25
+ f.puts 'uri = https://foo.example.com/psc'
26
+ end
27
+
28
+ File.open(everything_file, 'w') { |f| f.write everything_contents }
29
+ end
30
+
31
+ it 'is read from /etc/nubic/ncs/navigator.ini' do
32
+ NcsNavigator.configuration.study_center_id.should == '18'
33
+ end
34
+
35
+ it 'can be set explicitly' do
36
+ NcsNavigator.configuration = NcsNavigator::Configuration.new(everything_file)
37
+ NcsNavigator.configuration.study_center_id.should == '20000000'
38
+ end
39
+ end
40
+ end
41
+
42
+ module NcsNavigator
43
+ describe Configuration do
44
+ let(:everything) { Configuration.new(everything_file) }
45
+ let(:everything_file) { File.expand_path('../everything.ini', __FILE__) }
46
+
47
+ # input_hash should be kept minimally valid
48
+ let(:input_hash) {
49
+ {
50
+ 'Study Center' => {
51
+ 'sc_id' => '23000000',
52
+ 'sampling_units_file' => 'foo.csv'
53
+ },
54
+ 'Staff Portal' => {
55
+ 'uri' => 'https://sp.example.edu/'
56
+ },
57
+ 'Core' => {
58
+ 'uri' => 'https://ncsn.example.edu/'
59
+ },
60
+ 'PSC' => {
61
+ 'uri' => 'https://psc.example.edu/'
62
+ }
63
+ }
64
+ }
65
+ let(:from_hash) { Configuration.new(input_hash) }
66
+
67
+ describe '#initialize' do
68
+ describe 'from an INI file' do
69
+ it 'works when the file exists' do
70
+ everything.study_center_id.should == '20000000'
71
+ end
72
+
73
+ it 'gives a useful error when the file does not exist' do
74
+ lambda { Configuration.new('/foo/bar.ini') }.should(
75
+ raise_error('NCS Navigator configuration file "/foo/bar.ini" does not exist or is not readable.'))
76
+ end
77
+ end
78
+
79
+ it 'accepts a hash' do
80
+ from_hash.study_center_id.should == '23000000'
81
+ end
82
+
83
+ it 'accepts a hash with symbol keys' do
84
+ input_hash[:'Study Center'] = input_hash.delete 'Study Center'
85
+ input_hash[:'Study Center'][:sc_id] = input_hash[:'Study Center'].delete 'sc_id'
86
+ from_hash.study_center_id.should == '23000000'
87
+ end
88
+ end
89
+
90
+ describe '#study_center_id' do
91
+ it 'reflects the configured value' do
92
+ from_hash.study_center_id.should == '23000000'
93
+ end
94
+
95
+ it 'is always a string' do
96
+ input_hash['Study Center']['sc_id'] = 234
97
+ from_hash.study_center_id.should == '234'
98
+ end
99
+
100
+ it 'is mandatory' do
101
+ input_hash['Study Center'].delete 'sc_id'
102
+ lambda { from_hash }.
103
+ should raise_error("Please set a value for [Study Center]: sc_id")
104
+ end
105
+ end
106
+
107
+ describe '#study_center_username' do
108
+ it 'reflects the configured value' do
109
+ everything.study_center_username.should == 'NetID'
110
+ end
111
+
112
+ it 'defaults to "Username"' do
113
+ from_hash.study_center_username.should == 'Username'
114
+ end
115
+ end
116
+
117
+ describe '#sampling_units_file' do
118
+ it 'reflects the configured value, absolutized, when read from an INI' do
119
+ everything.sampling_units_file.to_s.should ==
120
+ File.expand_path('../every_su.csv', everything_file)
121
+ end
122
+
123
+ it 'is just the value when read from a Hash' do
124
+ from_hash.sampling_units_file.to_s.should == 'foo.csv'
125
+ end
126
+
127
+ it 'is a Pathname' do
128
+ everything.sampling_units_file.should be_a(Pathname)
129
+ end
130
+
131
+ it 'is required' do
132
+ input_hash['Study Center'].delete 'sampling_units_file'
133
+ lambda { from_hash }.
134
+ should raise_error "Please set a value for [Study Center]: sampling_units_file"
135
+ end
136
+ end
137
+
138
+ describe '#sampling_unit_areas' do
139
+ subject { everything.sampling_unit_areas }
140
+
141
+ it 'has the right number' do
142
+ subject.size.should == 2
143
+ end
144
+
145
+ describe 'an individual area' do
146
+ let(:area) { subject.sort_by { |a| a.name }.last }
147
+
148
+ it 'has a name' do
149
+ area.name.should == 'Uptown'
150
+ end
151
+
152
+ it 'has the right number of SSUs' do
153
+ area.secondary_sampling_units.size.should == 2
154
+ end
155
+
156
+ it 'has the right PSU' do
157
+ area.primary_sampling_unit.id.should == '204'
158
+ end
159
+ end
160
+ end
161
+
162
+ describe '#primary_sampling_units' do
163
+ subject { everything.primary_sampling_units }
164
+
165
+ it 'has the right number' do
166
+ subject.size.should == 1
167
+ end
168
+
169
+ describe 'an individual PSU' do
170
+ let(:psu) { subject.first }
171
+
172
+ it 'has the correct ID' do
173
+ psu.id.should == '204'
174
+ end
175
+
176
+ it 'has the correct SSUs' do
177
+ psu.secondary_sampling_units.collect(&:id).should == %w(One Two Three)
178
+ end
179
+ end
180
+ end
181
+
182
+ describe '#secondary_sampling_units' do
183
+ subject { everything.secondary_sampling_units }
184
+
185
+ it 'has the right number' do
186
+ subject.size.should == 3
187
+ end
188
+
189
+ describe 'an individual SSU' do
190
+ let(:ssu) { subject.sort_by { |ssu| ssu.id }.first }
191
+
192
+ it 'has the correct ID' do
193
+ ssu.id.should == 'One'
194
+ end
195
+
196
+ it 'has the correct name' do
197
+ ssu.name.should == 'West Side'
198
+ end
199
+
200
+ it 'has the correct area' do
201
+ ssu.area.name.should == 'Uptown'
202
+ end
203
+
204
+ it 'has the same area as another SSU in the same area' do
205
+ ssu.area.should eql(subject.find { |ssu| ssu.name == 'West Side' }.area)
206
+ end
207
+
208
+ it 'has the correct PSU' do
209
+ ssu.primary_sampling_unit.id.should == '204'
210
+ end
211
+
212
+ it 'has the correct TSUs' do
213
+ ssu.should have(1).tertiary_sampling_units
214
+ end
215
+
216
+ describe 'a TSU' do
217
+ let(:tsu) { ssu.tertiary_sampling_units.first }
218
+
219
+ it 'has a name' do
220
+ tsu.name.should == 'Center'
221
+ end
222
+
223
+ it 'has an ID' do
224
+ tsu.id.should == '1-1'
225
+ end
226
+ end
227
+ end
228
+
229
+ context 'without TSUs' do
230
+ before do
231
+ input_hash['Study Center']['sampling_units_file'] =
232
+ File.expand_path('../no_tsus.csv', __FILE__)
233
+ end
234
+
235
+ subject { from_hash.secondary_sampling_units }
236
+
237
+ it 'has the right number' do
238
+ subject.size.should == 3
239
+ end
240
+
241
+ describe 'an individual SSU' do
242
+ let(:ssu) { subject.first }
243
+
244
+ it 'has no TSUs' do
245
+ ssu.should have(0).tertiary_sampling_units
246
+ end
247
+ end
248
+ end
249
+ end
250
+
251
+ describe 'footer' do
252
+ describe '#footer_logo_left' do
253
+ subject { everything.footer_logo_left }
254
+
255
+ it 'is the correct value' do
256
+ subject.to_s.should == "/etc/nubic/ncs/logos/sc_20000000L.png"
257
+ end
258
+ end
259
+
260
+ describe '#footer_logo_right' do
261
+ subject { everything.footer_logo_right }
262
+
263
+ it 'is the correct value' do
264
+ subject.to_s.should == "/etc/nubic/ncs/logos/sc_20000000R.png"
265
+ end
266
+ end
267
+
268
+ describe '#footer_text' do
269
+ subject { everything.footer_text }
270
+
271
+ it 'is the configured value' do
272
+ subject.should =~ /Greater Chicago Study Center/
273
+ end
274
+
275
+ it 'preserves newlines' do
276
+ subject.split(/\n/).size.should == 5
277
+ end
278
+ end
279
+
280
+ describe '#footer_center_html' do
281
+ subject { everything.footer_center_html }
282
+
283
+ it 'converts newlines to BRs' do
284
+ subject.split("\n")[3].should == "420 East Superior, 10th Floor<br>"
285
+ end
286
+
287
+ it 'is nil if no footer text' do
288
+ from_hash.footer_center_html.should be_nil
289
+ end
290
+ end
291
+ end
292
+
293
+ describe 'Staff Portal parts' do
294
+ describe '#staff_portal_uri' do
295
+ subject { everything.staff_portal_uri }
296
+
297
+ it 'is the configured value' do
298
+ subject.to_s.should == 'https://staffportal.greaterchicagoncs.org/'
299
+ end
300
+
301
+ it 'can expose the hostname, e.g.' do
302
+ subject.host.should == 'staffportal.greaterchicagoncs.org'
303
+ end
304
+ end
305
+
306
+ describe '#staff_portal' do
307
+ it 'exposes all the raw values in the Staff Portal section' do
308
+ everything.staff_portal['mail_from'].should == "staffportal@greaterchicagoncs.org"
309
+ end
310
+ end
311
+ end
312
+
313
+ describe 'Core parts' do
314
+ describe '#core_uri' do
315
+ subject { everything.core_uri }
316
+
317
+ it 'is the configured value' do
318
+ subject.to_s.should == 'https://ncsnavigator.greaterchicagoncs.org/'
319
+ end
320
+
321
+ it 'can expose the hostname, e.g.' do
322
+ subject.host.should == 'ncsnavigator.greaterchicagoncs.org'
323
+ end
324
+ end
325
+
326
+ describe '#core' do
327
+ it 'exposes all the raw values in the Staff Portal section' do
328
+ everything.core['uri'].should == "https://ncsnavigator.greaterchicagoncs.org/"
329
+ end
330
+ end
331
+ end
332
+
333
+ describe 'PSC parts' do
334
+ describe '#psc_uri' do
335
+ subject { everything.psc_uri }
336
+
337
+ it 'is the configured value' do
338
+ subject.to_s.should == 'https://calendar.greaterchicagoncs.org/'
339
+ end
340
+
341
+ it 'can expose the hostname, e.g.' do
342
+ subject.host.should == 'calendar.greaterchicagoncs.org'
343
+ end
344
+ end
345
+
346
+ describe '#psc' do
347
+ it 'exposes all the raw values in the PSC section' do
348
+ everything.psc['uri'].should == "https://calendar.greaterchicagoncs.org/"
349
+ end
350
+ end
351
+ end
352
+
353
+ describe 'SMTP' do
354
+ describe '#smtp_host' do
355
+ it 'defaults to "localhost"' do
356
+ from_hash.smtp_host.should == 'localhost'
357
+ end
358
+
359
+ it 'takes the configured value' do
360
+ everything.smtp_host.should == 'smtp.greaterchicagoncs.org'
361
+ end
362
+ end
363
+
364
+ describe '#smtp_port' do
365
+ it 'defaults to 25' do
366
+ from_hash.smtp_port.should == 25
367
+ end
368
+
369
+ it 'takes the configured value' do
370
+ everything.smtp_port.should == 2025
371
+ end
372
+ end
373
+
374
+ describe '#smtp_helo_domain' do
375
+ it 'defaults to nil' do
376
+ from_hash.smtp_helo_domain.should be_nil
377
+ end
378
+
379
+ it 'takes the configured value' do
380
+ everything.smtp_helo_domain.should == 'greaterchicagoncs.org'
381
+ end
382
+ end
383
+
384
+ describe '#smtp_authentication_method' do
385
+ it 'defaults to nil' do
386
+ from_hash.smtp_authentication_method.should be_nil
387
+ end
388
+
389
+ it 'takes the configured value (as a symbol)' do
390
+ everything.smtp_authentication_method.should == :plain
391
+ end
392
+ end
393
+
394
+ describe '#smtp_username' do
395
+ it 'defaults to nil' do
396
+ from_hash.smtp_username.should be_nil
397
+ end
398
+
399
+ it 'takes the configured value' do
400
+ everything.smtp_username.should == 'mailman'
401
+ end
402
+ end
403
+
404
+ describe '#smtp_password' do
405
+ it 'defaults to nil' do
406
+ from_hash.smtp_password.should be_nil
407
+ end
408
+
409
+ it 'takes the configured value' do
410
+ everything.smtp_password.should == 'tiger'
411
+ end
412
+ end
413
+
414
+ describe '#starttls' do
415
+ it 'defaults to false' do
416
+ from_hash.smtp_starttls.should == false
417
+ end
418
+
419
+ it 'takes the configured value' do
420
+ everything.smtp_starttls.should == true
421
+ end
422
+ end
423
+
424
+ describe '#action_mailer_smtp_settings' do
425
+ context 'with everything' do
426
+ subject { everything.action_mailer_smtp_settings }
427
+
428
+ it 'has :address' do
429
+ subject[:address].should == 'smtp.greaterchicagoncs.org'
430
+ end
431
+
432
+ it 'has :port' do
433
+ subject[:port].should == 2025
434
+ end
435
+
436
+ it 'has :domain' do
437
+ subject[:domain].should == 'greaterchicagoncs.org'
438
+ end
439
+
440
+ it 'has :authentication' do
441
+ subject[:authentication].should == :plain
442
+ end
443
+
444
+ it 'has :user_name' do
445
+ subject[:user_name].should == 'mailman'
446
+ end
447
+
448
+ it 'has :password' do
449
+ subject[:password].should == 'tiger'
450
+ end
451
+
452
+ it 'has :enable_starttls_auto' do
453
+ subject[:enable_starttls_auto].should == true
454
+ end
455
+ end
456
+
457
+ context 'with defaults' do
458
+ subject { from_hash.action_mailer_smtp_settings }
459
+
460
+ it 'has :address' do
461
+ subject[:address].should == 'localhost'
462
+ end
463
+
464
+ it 'has :port' do
465
+ subject[:port].should == 25
466
+ end
467
+
468
+ [:domain, :authentication, :user_name, :password, :enable_starttls_auto].each do |nil_key|
469
+ it "does not have #{nil_key}" do
470
+ subject.should_not have_key(nil_key)
471
+ end
472
+ end
473
+ end
474
+ end
475
+ end
476
+ end
477
+ end
@@ -0,0 +1,4 @@
1
+ PSU_ID,AREA,SSU_ID,SSU_NAME,TSU_ID,TSU_NAME
2
+ 204,Uptown,One,West Side,1-1,Center
3
+ 204,Uptown,Two,East Side,2-1,Far East
4
+ 204,Downtown,Three,Downtown,3-1,Park
@@ -0,0 +1,32 @@
1
+ # Test configuration containing all sections and keys.
2
+
3
+ [Study Center]
4
+ sc_id = "20000000"
5
+ sampling_units_file = "every_su.csv"
6
+ username = "NetID"
7
+ footer_logo_left = "/etc/nubic/ncs/logos/sc_20000000L.png"
8
+ footer_logo_right = "/etc/nubic/ncs/logos/sc_20000000R.png"
9
+ footer_text = "National Children’s Study - Greater Chicago Study Center
10
+ Institute for Healthcare Studies, Feinberg School of Medicine
11
+ Northwestern University
12
+ 420 East Superior, 10th Floor
13
+ Chicago, IL 60611"
14
+
15
+ [Staff Portal]
16
+ uri = "https://staffportal.greaterchicagoncs.org/"
17
+ mail_from = "staffportal@greaterchicagoncs.org"
18
+
19
+ [Core]
20
+ uri = "https://ncsnavigator.greaterchicagoncs.org/"
21
+
22
+ [PSC]
23
+ uri = "https://calendar.greaterchicagoncs.org/"
24
+
25
+ [SMTP]
26
+ host = "smtp.greaterchicagoncs.org"
27
+ port = "2025"
28
+ domain = "greaterchicagoncs.org"
29
+ authentication = "plain"
30
+ username = "mailman"
31
+ password = "tiger"
32
+ starttls = "true"
@@ -0,0 +1,4 @@
1
+ PSU_ID,AREA,SSU_ID,SSU_NAME,TSU_ID,TSU_NAME
2
+ 204,Uptown,One,West Side,,
3
+ 204,Uptown,Two,East Side,,
4
+ 204,Downtown,Three,Downtown,,
@@ -0,0 +1,10 @@
1
+ require 'rspec'
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require 'ncs_navigator/configuration'
5
+
6
+ require 'fakefs/spec_helpers'
7
+
8
+ RSpec.configure do |config|
9
+
10
+ end