Skip to content

Commit 5ee39ea

Browse files
committed
Improve test coverage
1 parent 8b5e957 commit 5ee39ea

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'BetterTogether::Metrics::PageViewReportsController download', 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+
it 'downloads an attached report file' do
14+
report = BetterTogether::Metrics::PageViewReport.create!(file_format: 'csv')
15+
report.report_file.attach(
16+
io: StringIO.new('col1,col2\n1,2\n'),
17+
filename: 'report.csv',
18+
content_type: 'text/csv'
19+
)
20+
21+
get better_together.download_metrics_page_view_report_path(locale:, id: report.id)
22+
expect(response).to have_http_status(:ok)
23+
expect(response.header['Content-Type']).to include('text/csv')
24+
end
25+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'BetterTogether::Metrics::PageViewsController', 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+
it 'creates a page view with valid params' do
14+
page = create(:better_together_page)
15+
16+
post better_together.metrics_page_views_path(locale:), params: {
17+
viewable_type: page.class.name,
18+
viewable_id: page.id,
19+
locale: locale.to_s
20+
}
21+
22+
expect(response).to have_http_status(:ok)
23+
expect(JSON.parse(response.body)['success']).to eq(true)
24+
end
25+
26+
it 'returns 422 for invalid viewable' do
27+
post better_together.metrics_page_views_path(locale:), params: {
28+
viewable_type: 'NonExistent',
29+
viewable_id: '123',
30+
locale: locale.to_s
31+
}
32+
33+
expect(response).to have_http_status(:unprocessable_content)
34+
end
35+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'BetterTogether::Metrics::SearchQueriesController', 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+
it 'tracks a search query with valid params' do
14+
post better_together.metrics_search_queries_path(locale:), params: {
15+
query: 'test',
16+
results_count: 3
17+
}
18+
expect(response).to have_http_status(:ok)
19+
expect(JSON.parse(response.body)['success']).to eq(true)
20+
end
21+
22+
it 'returns 422 for invalid params' do
23+
post better_together.metrics_search_queries_path(locale:), params: { query: '', results_count: '' }
24+
expect(response).to have_http_status(:unprocessable_content)
25+
end
26+
end
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::Metrics::SharesController', 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+
it 'tracks a share with valid params' do
14+
post better_together.metrics_shares_path(locale:), params: {
15+
platform: 'facebook',
16+
url: 'https://example.com/post/1',
17+
shareable_type: 'BetterTogether::Post',
18+
shareable_id: SecureRandom.uuid
19+
}
20+
expect(response).to have_http_status(:ok)
21+
expect(JSON.parse(response.body)['success']).to eq(true)
22+
end
23+
24+
it 'returns 422 for invalid platform/url' do
25+
post better_together.metrics_shares_path(locale:), params: { platform: 'unknown', url: 'notaurl' }
26+
expect(response).to have_http_status(:unprocessable_content)
27+
end
28+
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
# rubocop:todo Metrics/BlockLength
6+
RSpec.describe 'BetterTogether::PersonCommunityMembershipsController', 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 'POST /:locale/.../host/communities/:community_id/person_community_memberships' do
15+
it 'creates a membership and redirects when actor has update_community permission' do
16+
community = create(:better_together_community)
17+
18+
# Ensure current user has the required permission on this community
19+
coordinator_role = BetterTogether::Role.find_by(identifier: 'community_coordinator')
20+
BetterTogether::PersonCommunityMembership.create!(
21+
joinable: community,
22+
member: BetterTogether::User.find_by(email: '[email protected]').person,
23+
role: coordinator_role
24+
)
25+
26+
member = create(:better_together_person)
27+
target_role = BetterTogether::Role.find_by(identifier: 'community_member')
28+
29+
post better_together.community_person_community_memberships_path(locale:, community_id: community.id), params: {
30+
person_community_membership: {
31+
member_id: member.id,
32+
role_id: target_role.id
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+
42+
describe 'DELETE /:locale/.../host/communities/:community_id/person_community_memberships/:id' do
43+
it 'destroys a membership and redirects' do
44+
community = create(:better_together_community)
45+
coordinator_role = BetterTogether::Role.find_by(identifier: 'community_coordinator')
46+
BetterTogether::PersonCommunityMembership.create!(
47+
joinable: community,
48+
member: BetterTogether::User.find_by(email: '[email protected]').person,
49+
role: coordinator_role
50+
)
51+
52+
member = create(:better_together_person)
53+
target_role = BetterTogether::Role.find_by(identifier: 'community_member')
54+
membership = BetterTogether::PersonCommunityMembership.create!(
55+
joinable: community,
56+
member: member,
57+
role: target_role
58+
)
59+
60+
delete better_together.community_person_community_membership_path(locale:, community_id: community.id,
61+
id: membership.id)
62+
expect(response).to have_http_status(:found)
63+
follow_redirect!
64+
expect(response).to have_http_status(:ok)
65+
end
66+
end
67+
end
68+
# rubocop:enable Metrics/BlockLength

0 commit comments

Comments
 (0)