-
-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathorganization_spec.rb
More file actions
426 lines (363 loc) · 17.1 KB
/
organization_spec.rb
File metadata and controls
426 lines (363 loc) · 17.1 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# == Schema Information
#
# Table name: organizations
#
# id :integer not null, primary key
# bank_is_set_up :boolean default(FALSE), not null
# city :string
# deadline_day :integer
# default_storage_location :integer
# distribute_monthly :boolean default(FALSE), not null
# email :string
# enable_child_based_requests :boolean default(TRUE), not null
# enable_individual_requests :boolean default(TRUE), not null
# enable_quantity_based_requests :boolean default(TRUE), not null
# hide_package_column_on_receipt :boolean default(FALSE)
# hide_value_columns_on_receipt :boolean default(FALSE)
# include_in_kind_values_in_exported_files :boolean default(FALSE), not null
# include_packages_in_distribution_export :boolean default(FALSE), not null
# intake_location :integer
# invitation_text :text
# latitude :float
# longitude :float
# name :string
# one_step_partner_invite :boolean default(FALSE), not null
# partner_form_fields :text default([]), is an Array
# receive_email_on_requests :boolean default(FALSE), not null
# reminder_day :integer
# reminder_schedule_definition :string
# repackage_essentials :boolean default(FALSE), not null
# signature_for_distribution_pdf :boolean default(FALSE)
# state :string
# street :string
# url :string
# ytd_on_distribution_printout :boolean default(TRUE), not null
# zipcode :string
# created_at :datetime not null
# updated_at :datetime not null
# account_request_id :integer
# ndbn_member_id :bigint
#
RSpec.describe Organization, type: :model do
let(:organization) { create(:organization) }
describe "validations" do
it "validates that attachments are png or jpgs" do
expect(build(:organization,
logo: Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/files/logo.jpg"),
"image/jpeg")))
.to be_valid
expect(build(:organization,
logo: Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/files/logo.gif"),
"image/gif")))
.to_not be_valid
end
it "validates that at least one distribution type is enabled" do
expect(build(:organization, enable_child_based_requests: true)).to be_valid
expect(build(:organization, enable_individual_requests: true)).to be_valid
expect(build(:organization, enable_quantity_based_requests: true)).to be_valid
expect(build(
:organization,
enable_child_based_requests: false,
enable_individual_requests: false,
enable_quantity_based_requests: false
)).to_not be_valid
end
it "validates that attachment file size is not higher than 1 MB" do
fixture_path = Rails.root.join('spec', 'fixtures', 'files', 'logo.jpg')
fixture_file = File.open(fixture_path)
organization = build(:organization)
allow(fixture_file).to receive(:size) { 2.megabytes }
organization.logo.attach(io: fixture_file, filename: 'logo.jpg')
expect(organization).to_not be_valid
allow(fixture_file).to receive(:size) { 10.kilobytes }
organization.logo.attach(io: fixture_file, filename: 'logo.jpg')
expect(organization).to be_valid
end
it "validates deadline_day and reminder date are different for day of month reminders" do
organization.update(deadline_day: 10)
organization.reminder_schedule.assign_attributes(by_month_or_week: "day_of_month", day_of_month: 10)
expect(organization).to_not be_valid
organization.reminder_schedule.assign_attributes(day_of_month: 11)
expect(organization).to be_valid
end
it "does not validate deadline_day and reminder date are different for day of week reminders" do
# Deadline_day and day_of_month both aren't set and so are the same
organization.reminder_schedule.assign_attributes(by_month_or_week: "day_of_week", day_of_week: 0, every_nth_day: 1)
expect(organization).to be_valid
organization.update(deadline_day: 10)
organization.reminder_schedule.assign_attributes(day_of_month: 10)
expect(organization).to be_valid
end
end
context "Associations >" do
it { should have_many(:item_categories) }
it { should have_many(:product_drive_tags) }
it { should belong_to(:ndbn_member).class_name("NDBNMember").optional }
describe 'users' do
subject { organization.users }
context 'when a organizaton has a user that has two roles' do
let(:user) { create(:user) }
before do
user.add_role(:admin, organization)
user.add_role(:volunteer, organization)
end
it 'should returns users without duplications' do
expect(subject).to eq([user])
end
end
end
it { is_expected.to have_many(:users).through(:roles) }
describe "barcode_items" do
before do
BarcodeItem.delete_all
create(:barcode_item, organization: organization)
create(:global_barcode_item) # global
end
it "returns only this organization's barcodes, no globals" do
expect(organization.barcode_items.count).to eq(1)
end
describe ".all" do
it "includes global barcode items also" do
expect(organization.barcode_items.all.count).to eq(2)
end
end
end
describe "distributions" do
describe "upcoming" do
before do
travel_to Time.zone.local(2019, 7, 3) # Wednesday
end
it "retrieves the distributions scheduled for this week that have not yet happened" do
wednesday_distribution_scheduled = create(:distribution, organization: organization, state: :scheduled, issued_at: Time.zone.local(2019, 7, 3))
create(:distribution, organization: organization, state: :complete, issued_at: Time.zone.local(2019, 7, 3))
sunday_distribution = create(:distribution, organization: organization, state: :scheduled, issued_at: Time.zone.local(2019, 7, 7))
upcoming_distributions = organization.distributions.upcoming
expect(upcoming_distributions).to match_array([wednesday_distribution_scheduled, sunday_distribution])
end
end
end
end
describe '#assign_attributes_from_account_request' do
subject { organization.assign_attributes_from_account_request(account_request) }
let(:organization) { Organization.new }
let(:account_request) { FactoryBot.create(:account_request) }
it 'should assign the proper attributes to the organization' do
expect(subject.attributes).to include({
name: account_request.organization_name,
url: account_request.organization_website,
email: account_request.email,
account_request_id: account_request.id
}.stringify_keys)
end
end
describe 'after_create' do
let(:account_request) { FactoryBot.create(:account_request) }
it 'should update the state of the account request' do
org = build(:organization, account_request: account_request)
expect(account_request).not_to be_admin_approved
org.save!
expect(account_request.reload).to be_admin_approved
end
end
describe ".seed_items" do
context "when provided with an organization to seed" do
it "loads the base items into Item records" do
create(:base_item, name: "Foo", partner_key: "foo")
Organization.seed_items(organization)
expect(organization.items.count).to eq(1)
end
it "should exclude kit" do
create(:base_item, name: "Kit", partner_key: "foo")
Organization.seed_items(organization)
expect(organization.items.count).to eq(0)
end
end
context "when no organization is provided" do
it "updates all organizations" do
first_organization = create(:organization)
second_organization = create(:organization)
create(:base_item, name: "Foo", partner_key: "foo")
expect do
Organization.seed_items
second_organization
end.to change { first_organization.items.count }.by(1)
.and change { second_organization.items.count }.by(1)
end
end
end
describe "#seed_items" do
it "allows a single base item to be seeded" do
base_item = create(:base_item, name: "Foo", partner_key: "foo").to_h
expect do
organization.seed_items(base_item)
end.to change { organization.items.size }.by(1)
end
it "allows a collection of items to be seeded" do
base_items = [create(:base_item, name: "Foo", partner_key: "foo").to_h, create(:base_item, name: "Bar", partner_key: "bar").to_h]
expect do
organization.seed_items(base_items)
end.to change { organization.items.size }.by(2)
end
context "when given an item that already exists" do
it "gracefully skips the item" do
base_item = create(:base_item, name: "Foo", partner_key: "foo")
base_items = [base_item.to_h, BaseItem.first.to_h]
expect do
organization.seed_items(base_items)
end.to change { organization.items.size }.by(1)
end
end
context "when given an item name that already exists, but with an 'other' partner key" do
it "updates the old item to use the new base item as its base" do
create(:base_item, name: "Other", partner_key: "other")
item = create(:item, name: "Foo", organization:, partner_key: "other")
organization.items << item
base_item = create(:base_item, name: "Foo", partner_key: "foo")
base_items = [base_item.to_h]
expect do
organization.seed_items(base_items)
item.reload
end.to change { organization.items.size }.by(0)
.and change { item.partner_key }.to("foo")
end
end
end
describe "#ordered_requests" do
let!(:new_active_request) { create(:request, comments: "first active") }
let!(:old_active_request) { create(:request, comments: "second active") }
let!(:fulfilled_request) { create(:request, :fulfilled, comments: "first fulfilled") }
let!(:organization) { create(:organization, requests: [old_active_request, fulfilled_request, new_active_request]) }
it "puts active requests before fulfilled requests" do
expect(organization.ordered_requests.pluck(:comments)).to eq(["first active", "second active", "first fulfilled"])
end
context "ordering of requests with matching status" do
before do
old_active_request.update(updated_at: 5.minutes.after)
end
it "puts the most recently updated request before older requests" do
expect(organization.ordered_requests.pluck(:comments)).to eq(["second active", "first active", "first fulfilled"])
end
end
end
describe 'is_active' do
let!(:active_organization) { create(:organization) }
let!(:inactive_organization) { create(:organization) }
let!(:active_user) { create(:user, organization: active_organization, last_sign_in_at: 1.month.ago) }
let!(:inactive_user) { create(:user, organization: inactive_organization, last_sign_in_at: 6.months.ago) }
it 'returns active organizations' do
expect(Organization.is_active).to contain_exactly(active_organization)
end
end
describe "geocode" do
before do
organization.update(
street: "1500 Remount Road",
city: "Front Royal",
state: "VA",
zipcode: "22630"
)
end
it "adds coordinates to the database" do
expect(organization.latitude).to be_a(Float)
expect(organization.longitude).to be_a(Float)
end
end
describe 'default storage location' do
it 'returns nil when not set' do
expect(Organization.new.default_storage_location).to be_nil
end
it 'associates the default storage location with a storage location' do
storage_location = FactoryBot.build(:storage_location)
org = Organization.new(default_storage_location: storage_location.id,
street: '123 Main St.',
city: 'Anytown',
state: 'KS',
zipcode: '12345')
expect(org.default_storage_location).to eq(storage_location.id)
end
end
describe 'address' do
it 'returns an empty string when the org has no address components' do
expect(Organization.new.address).to be_blank
end
it 'correctly formats an address string with commas and spaces' do
org = Organization.new(street: '123 Main St.', city: 'Anytown', state: 'KS', zipcode: '12345')
expect(org.address).to eq('123 Main St., Anytown, KS 12345')
end
it 'does not add a trailing space when the zip code is missing' do
org = Organization.new(street: '123 Main St.', city: 'Anytown', state: 'KS')
expect(org.address).to eq('123 Main St., Anytown, KS')
end
it 'does not add any separators before the city when street is missing' do
org = Organization.new(city: 'Anytown', state: 'KS', zipcode: '12345')
expect(org.address).to eq('Anytown, KS 12345')
end
it 'does not add any separators after street when city, state, and zip are missing' do
org = Organization.new(street: '123 Main St.')
expect(org.address).to eq('123 Main St.')
end
end
describe 'valid_items' do
it 'returns an array of item partner keys' do
item = create(:item, organization: organization)
expected = { name: item.name, id: item.id, partner_key: item.partner_key }
expect(organization.valid_items.count).to eq(organization.items.count)
expect(organization.valid_items).to include(expected)
end
context 'with invisible items' do
let!(:item1) { create(:item, organization: organization, active: true, visible_to_partners: true) }
let!(:item2) { create(:item, organization: organization, active: true, visible_to_partners: false) }
let!(:item3) { create(:item, organization: organization, active: false, visible_to_partners: true) }
let!(:item4) { create(:item, organization: organization, active: false, visible_to_partners: false) }
it 'only shows active and visible items' do
expect(organization.valid_items).to eq([{ id: item1.id, partner_key: item1.partner_key, name: item1.name }])
end
end
end
describe 'from_email' do
it 'returns email when present' do
organization.update(email: "email@testthis.com")
expect(organization.from_email).to eq(organization.email)
end
it 'returns admin email when not present' do
org = create(:organization, email: nil)
admin = create(:organization_admin, organization: org)
expect(org.from_email).to eq(admin.email)
end
it "returns admin email when it's empty" do
org = create(:organization, email: "")
admin = create(:organization_admin, organization: org)
expect(org.from_email).to eq(admin.email)
end
it "returns admin email when it's empty space" do
org = create(:organization, email: " ")
admin = create(:organization_admin, organization: org)
expect(org.from_email).to eq(admin.email)
end
end
describe 'earliest reporting year' do
# re 2813 update annual report -- allowing an earliest reporting year will let us do system testing and staging for annual reports
it 'is the organization created year if no associated data' do
expect(organization.earliest_reporting_year).to eq(organization.created_at.year)
end
it 'is the year of the earliest of donation, purchase, or distribution if they are earlier ' do
freeze_time do
create(:donation, organization: organization, issued_at: 6.months.from_now)
create(:purchase, organization: organization, issued_at: 6.months.from_now)
create(:distribution, organization: organization, issued_at: 6.months.from_now)
expect(organization.earliest_reporting_year).to eq(organization.created_at.year)
create(:donation, organization: organization, issued_at: 5.years.ago)
expect(organization.earliest_reporting_year).to eq(5.years.ago.year)
create(:purchase, organization: organization, issued_at: 6.years.ago)
expect(organization.earliest_reporting_year).to eq(6.years.ago.year)
create(:purchase, organization: organization, issued_at: 7.years.ago)
expect(organization.earliest_reporting_year).to eq(7.years.ago.year)
ensure
travel_back
end
end
end
describe "versioning" do
it { is_expected.to be_versioned }
end
end