Skip to content

Commit d596b41

Browse files
committed
Increase spec coverage
1 parent 2ab1155 commit d596b41

File tree

7 files changed

+308
-8
lines changed

7 files changed

+308
-8
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'BetterTogether::Geography::CountriesController', type: :request do
6+
let(:locale) { I18n.default_locale }
7+
8+
before do
9+
configure_host_platform
10+
login('[email protected]', 'password12345')
11+
end
12+
13+
describe 'GET /:locale/.../host/geography/countries' do
14+
it 'renders index' do
15+
get better_together.geography_countries_path(locale:)
16+
expect(response).to have_http_status(:ok)
17+
end
18+
end
19+
20+
describe 'GET /:locale/.../host/geography/countries/:id' do
21+
let!(:country) { create(:geography_country) }
22+
23+
it 'renders show' do
24+
get better_together.geography_country_path(locale:, id: country.slug)
25+
expect(response).to have_http_status(:ok)
26+
end
27+
end
28+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
# rubocop:todo Metrics/BlockLength
6+
RSpec.describe 'BetterTogether::Metrics::LinkClickReportsController', type: :request do
7+
let(:locale) { I18n.default_locale }
8+
9+
before do
10+
configure_host_platform
11+
login('[email protected]', 'password12345')
12+
end
13+
14+
describe 'GET /:locale/.../metrics/link_click_reports' do
15+
it 'renders index' do
16+
get better_together.metrics_link_click_reports_path(locale:)
17+
expect(response).to have_http_status(:ok)
18+
end
19+
20+
it 'renders new' do
21+
get better_together.new_metrics_link_click_report_path(locale:)
22+
expect(response).to have_http_status(:ok)
23+
end
24+
end
25+
26+
describe 'POST /:locale/.../metrics/link_click_reports' do
27+
it 'creates a report and redirects with valid params' do
28+
post better_together.metrics_link_click_reports_path(locale:), params: {
29+
metrics_link_click_report: {
30+
file_format: 'csv',
31+
sort_by_total_clicks: false,
32+
filters: { from_date: '', to_date: '', filter_internal: '' }
33+
}
34+
}
35+
36+
expect(response).to have_http_status(:found)
37+
follow_redirect!
38+
expect(response).to have_http_status(:ok)
39+
end
40+
end
41+
end
42+
# rubocop:enable Metrics/BlockLength
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
# rubocop:todo Metrics/BlockLength
6+
RSpec.describe 'BetterTogether::Metrics::PageViewReportsController', type: :request do
7+
let(:locale) { I18n.default_locale }
8+
9+
before do
10+
configure_host_platform
11+
login('[email protected]', 'password12345')
12+
end
13+
14+
describe 'GET /:locale/.../metrics/page_view_reports' do
15+
it 'renders index' do
16+
get better_together.metrics_page_view_reports_path(locale:)
17+
expect(response).to have_http_status(:ok)
18+
end
19+
20+
it 'renders new' do
21+
get better_together.new_metrics_page_view_report_path(locale:)
22+
expect(response).to have_http_status(:ok)
23+
end
24+
end
25+
26+
describe 'POST /:locale/.../metrics/page_view_reports' do
27+
it 'creates a report and redirects with valid params' do
28+
post better_together.metrics_page_view_reports_path(locale:), params: {
29+
metrics_page_view_report: {
30+
file_format: 'csv',
31+
sort_by_total_views: false,
32+
filters: { from_date: '', to_date: '', filter_pageable_type: '' }
33+
}
34+
}
35+
36+
expect(response).to have_http_status(:found)
37+
follow_redirect!
38+
expect(response).to have_http_status(:ok)
39+
end
40+
end
41+
end
42+
# rubocop:enable Metrics/BlockLength
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'BetterTogether::NavigationAreasController', type: :request do # rubocop:todo Metrics/BlockLength
6+
let(:locale) { I18n.default_locale }
7+
8+
before do
9+
configure_host_platform
10+
login('[email protected]', 'password12345')
11+
end
12+
13+
describe 'GET /:locale/.../navigation_areas' do
14+
it 'renders index' do
15+
get better_together.navigation_areas_path(locale:)
16+
expect(response).to have_http_status(:ok)
17+
end
18+
19+
it 'renders new' do
20+
get better_together.new_navigation_area_path(locale:)
21+
expect(response).to have_http_status(:ok)
22+
end
23+
end
24+
25+
describe 'POST /:locale/.../navigation_areas' do
26+
it 'creates and redirects on valid params' do
27+
post better_together.navigation_areas_path(locale:), params: {
28+
navigation_area: {
29+
name: 'Main Nav',
30+
visible: true,
31+
style: 'primary'
32+
}
33+
}
34+
35+
expect(response).to have_http_status(:found)
36+
follow_redirect!
37+
expect(response).to have_http_status(:ok)
38+
end
39+
40+
it 'renders new on invalid params (HTML 200)' do
41+
post better_together.navigation_areas_path(locale:), params: { navigation_area: { name: '' } }
42+
expect(response).to have_http_status(:ok)
43+
end
44+
end
45+
46+
describe 'PATCH /:locale/.../navigation_areas/:id' do
47+
let!(:area) { create(:better_together_navigation_area, protected: false) }
48+
49+
it 'updates and redirects on valid params' do
50+
patch better_together.navigation_area_path(locale:, id: area.slug), params: {
51+
navigation_area: { style: 'secondary' }
52+
}
53+
expect(response).to have_http_status(:found)
54+
follow_redirect!
55+
expect(response).to have_http_status(:ok)
56+
end
57+
58+
it 'renders edit on invalid params (HTML 200)' do
59+
patch better_together.navigation_area_path(locale:, id: area.slug), params: {
60+
navigation_area: { name: '' }
61+
}
62+
expect(response).to have_http_status(:ok)
63+
end
64+
end
65+
66+
describe 'DELETE /:locale/.../navigation_areas/:id' do
67+
let!(:area) { create(:better_together_navigation_area, protected: false) }
68+
69+
it 'destroys and redirects' do
70+
delete better_together.navigation_area_path(locale:, id: area.slug)
71+
expect(response).to have_http_status(:found)
72+
follow_redirect!
73+
expect(response).to have_http_status(:ok)
74+
end
75+
end
76+
end
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'BetterTogether::NavigationItemsController', type: :request do # rubocop:todo Metrics/BlockLength
6+
let(:locale) { I18n.default_locale }
7+
let(:user) { create(:better_together_user, :confirmed, :platform_manager) }
8+
let!(:navigation_area) { create(:better_together_navigation_area) }
9+
10+
before do
11+
configure_host_platform
12+
login('[email protected]', 'password12345')
13+
end
14+
15+
describe 'GET /:locale/.../navigation_areas/:navigation_area_id/navigation_items' do
16+
it 'returns ok for index' do
17+
get better_together.navigation_area_navigation_items_path(
18+
locale:,
19+
navigation_area_id: navigation_area.slug
20+
)
21+
22+
expect(response).to have_http_status(:ok)
23+
end
24+
end
25+
26+
describe 'POST /:locale/.../navigation_areas/:navigation_area_id/navigation_items' do
27+
let(:params) do
28+
# Start with factory attributes, then adapt to permitted keys
29+
raw_attrs = attributes_for(:better_together_navigation_item)
30+
permitted = raw_attrs.slice(:url, :icon, :position, :visible, :item_type, :parent_id, :route_name)
31+
# Use localized title key instead of :title; drop non-permitted keys (:slug, :id, :linkable_*)
32+
permitted["title_#{locale}"] = raw_attrs[:title]
33+
34+
{ navigation_item: permitted }
35+
end
36+
37+
it 'creates a navigation item and redirects (HTML)' do
38+
post better_together.navigation_area_navigation_items_path(
39+
locale:,
40+
navigation_area_id: navigation_area.slug
41+
), params: params
42+
43+
expect(response).to have_http_status(:found)
44+
follow_redirect!
45+
expect(response).to have_http_status(:ok)
46+
end
47+
48+
it 'renders errors on invalid params' do
49+
post better_together.navigation_area_navigation_items_path(
50+
locale:,
51+
navigation_area_id: navigation_area.slug
52+
), params: { navigation_item: { "title_#{locale}": '' } }
53+
54+
expect(response).to have_http_status(:unprocessable_content)
55+
end
56+
end
57+
58+
describe 'GET/PUT/DELETE on a navigation item' do # rubocop:todo Metrics/BlockLength
59+
let!(:item) { create(:better_together_navigation_item, navigation_area: navigation_area, protected: false) }
60+
61+
it 'shows the item' do
62+
get better_together.navigation_area_navigation_item_path(locale:, navigation_area_id: navigation_area.slug,
63+
# rubocop:todo Layout/LineLength
64+
id: item.slug)
65+
# rubocop:enable Layout/LineLength
66+
expect(response).to have_http_status(:ok)
67+
end
68+
69+
it 'updates with valid params then redirects' do
70+
put better_together.navigation_area_navigation_item_path(
71+
locale:,
72+
navigation_area_id: navigation_area.slug,
73+
id: item.slug
74+
), params: { navigation_item: { "title_#{locale}": 'Updated Title' } }
75+
76+
expect(response).to have_http_status(:found)
77+
follow_redirect!
78+
expect(response).to have_http_status(:ok)
79+
end
80+
81+
it 'renders edit on invalid params (422)' do
82+
put better_together.navigation_area_navigation_item_path(
83+
locale:,
84+
navigation_area_id: navigation_area.slug,
85+
id: item.slug
86+
), params: { navigation_item: { "title_#{locale}": '' } }
87+
88+
expect(response).to have_http_status(:unprocessable_content)
89+
end
90+
91+
it 'destroys and redirects' do
92+
delete better_together.navigation_area_navigation_item_path(
93+
locale:,
94+
navigation_area_id: navigation_area.slug,
95+
id: item.slug
96+
)
97+
expect(response).to have_http_status(:found)
98+
follow_redirect!
99+
expect(response).to have_http_status(:ok)
100+
end
101+
end
102+
end

spec/spec_helper.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
require 'simplecov'
2222
require 'coveralls'
2323

24+
# Allow CI/local runs to override coverage output to avoid permission issues
25+
SimpleCov.coverage_dir ENV['SIMPLECOV_DIR'] if ENV['SIMPLECOV_DIR']
26+
2427
Capybara.asset_host = ENV.fetch('APP_HOST', 'http://localhost:3000')
2528

2629
Coveralls.wear!('rails')
2730

28-
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new(
29-
[
30-
SimpleCov::Formatter::HTMLFormatter,
31-
Coveralls::SimpleCov::Formatter
32-
]
33-
)
31+
formatters = [Coveralls::SimpleCov::Formatter]
32+
formatters.unshift(SimpleCov::Formatter::HTMLFormatter) unless ENV['SIMPLECOV_NO_HTML'] == '1'
33+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new(formatters)
3434

3535
SimpleCov.start 'rails' do
3636
add_filter '/bin/'

spec/support/request_spec_helper.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ def json
55
JSON.parse(response.body)
66
end
77

8-
def login(user)
8+
def login(email, password)
99
post better_together.user_session_path, params: {
10-
user: { email: user.email, password: user.password }
10+
user: { email: email, password: password }
1111
}
1212
end
13+
14+
def configure_host_platform
15+
host_platform = create(:better_together_platform, :host, privacy: 'public')
16+
wizard = BetterTogether::Wizard.find_or_create_by(identifier: 'host_setup')
17+
wizard.mark_completed
18+
create(:user, :confirmed, :platform_manager,
19+
20+
password: 'password12345')
21+
host_platform
22+
end
1323
end

0 commit comments

Comments
 (0)