-
-
Notifications
You must be signed in to change notification settings - Fork 573
Expand file tree
/
Copy pathannual_reports_requests_spec.rb
More file actions
113 lines (101 loc) · 4.61 KB
/
annual_reports_requests_spec.rb
File metadata and controls
113 lines (101 loc) · 4.61 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
RSpec.describe "Annual Reports", type: :request do
let(:organization) { create(:organization, :created_at_2006) }
let(:user) { create(:user, organization: organization) }
let(:organization_admin) { create(:organization_admin, organization: organization) }
let(:default_params) do
{ year: 2018 }
end
context "While signed in" do
before do
sign_in(user)
end
describe "GET /index" do
it "returns http success" do
get reports_annual_reports_path(default_params)
expect(response).to have_http_status(:success)
end
end
describe "GET /show" do
it "returns http success" do
expect(AnnualReport.count).to eq(0)
get reports_annual_report_path(default_params)
expect(response).to have_http_status(:success)
expect(AnnualReport.count).to eq(1)
expect(AnnualReport.last.year).to eq(2018)
expect(AnnualReport.last.organization_id).to eq(organization.id)
end
it "retrieves and uses the existing report if it exists" do
old_time = 1.year.ago
report = AnnualReport.create!(year: 2018,
organization_id: organization.id,
updated_at: old_time,
all_reports: [{ name: 'dummy', entries: [{ dummy: 'text' }] }])
get reports_annual_report_path(default_params)
expect(response).to have_http_status(:success)
expect(AnnualReport.count).to eq(1)
expect(report.reload.updated_at).to be_within(1.second).of(old_time)
end
it "retrieves and updated the existing report if it exists" do
report = AnnualReport.create!(year: 2018, organization_id: organization.id)
expect(report.all_reports).to be_nil
get reports_annual_report_path(default_params)
expect(response).to have_http_status(:success)
expect(AnnualReport.count).to eq(1)
expect(report.reload.all_reports).not_to be_nil
end
it "returns not found if the year params is not number" do
get reports_annual_report_path({ year: 'invalid' })
expect(response).to have_http_status(:not_found)
end
end
describe 'POST /recalculate' do
it "recalculates new reports" do
expect(AnnualReport.count).to eq(0)
post recalculate_reports_annual_report_path(default_params)
expect(response).to have_http_status(:found)
expect(AnnualReport.count).to eq(1)
expect(AnnualReport.last.year).to eq(2018)
expect(AnnualReport.last.organization_id).to eq(organization.id)
end
it 'recalculates an existing report' do
old_time = 1.year.ago
report = AnnualReport.create!(year: 2018,
organization_id: organization.id,
updated_at: old_time,
all_reports: [{ name: 'dummy', entries: [{ dummy: 'text' }] }])
post recalculate_reports_annual_report_path(default_params)
expect(response).to have_http_status(:found)
expect(AnnualReport.count).to eq(1)
expect(report.reload.updated_at).not_to be_within(1.second).of(old_time)
end
end
describe "GET /range" do
it "returns AnnualReports within given range" do
get range_reports_annual_reports_path(year_start: 2016, year_end: 2018, format: :csv)
expect(response.body).to include("2016")
expect(response.body).to include("2017")
expect(response.body).to include("2018")
end
it "returns URL error if years are not valid format" do
expect { get range_reports_annual_reports_path(year_start: 'test', year_end: 'test', format: :csv) }
.to raise_error(ActionController::UrlGenerationError)
end
it "uses the earliest(smallest) year between year_start and organization's earliest_reporting_year" do
get range_reports_annual_reports_path(year_start: 2004, year_end: 2008, format: :csv)
# the organization was created in 2006 (created_at_2006)
# so the below years should not be in the output
expect(response.body).not_to include("2004")
expect(response.body).not_to include("2005")
response.body.split("\n")
end
it "orders the years in ascending order" do
get range_reports_annual_reports_path(year_start: 2018, year_end: 2016, format: :csv)
csv_array = response.body.split("\n")
# csv_array[0] is the header row
expect(csv_array[1]).to include("2016")
expect(csv_array[2]).to include("2017")
expect(csv_array[3]).to include("2018")
end
end
end
end