Skip to content

Commit 2c97368

Browse files
committed
Improve specs
1 parent 4b38343 commit 2c97368

File tree

13 files changed

+121
-56
lines changed

13 files changed

+121
-56
lines changed

app/builders/better_together/access_control_builder.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,44 @@ def seed_data
1616
end
1717

1818
def build_community_roles
19-
::BetterTogether::Role.create!(community_role_attrs)
19+
community_role_attrs.each do |attrs|
20+
# Idempotent: find by unique identifier and update attributes if needed
21+
role = ::BetterTogether::Role.find_or_initialize_by(identifier: attrs[:identifier])
22+
role.assign_attributes(attrs)
23+
role.save! if role.changed?
24+
end
2025
end
2126

2227
def build_community_resource_permissions
23-
::BetterTogether::ResourcePermission.create!(community_resource_permission_attrs)
28+
community_resource_permission_attrs.each do |attrs|
29+
perm = ::BetterTogether::ResourcePermission.find_or_initialize_by(identifier: attrs[:identifier])
30+
perm.assign_attributes(attrs)
31+
perm.save! if perm.changed?
32+
end
2433
end
2534

2635
def build_platform_resource_permissions
27-
::BetterTogether::ResourcePermission.create!(platform_resource_permission_attrs)
36+
platform_resource_permission_attrs.each do |attrs|
37+
perm = ::BetterTogether::ResourcePermission.find_or_initialize_by(identifier: attrs[:identifier])
38+
perm.assign_attributes(attrs)
39+
perm.save! if perm.changed?
40+
end
2841
end
2942

3043
def build_platform_roles
31-
::BetterTogether::Role.create!(platform_role_attrs)
44+
platform_role_attrs.each do |attrs|
45+
role = ::BetterTogether::Role.find_or_initialize_by(identifier: attrs[:identifier])
46+
role.assign_attributes(attrs)
47+
role.save! if role.changed?
48+
end
3249
end
3350

3451
def build_person_resource_permissions
35-
::BetterTogether::ResourcePermission.create!(person_resource_permission_attrs)
52+
person_resource_permission_attrs.each do |attrs|
53+
perm = ::BetterTogether::ResourcePermission.find_or_initialize_by(identifier: attrs[:identifier])
54+
perm.assign_attributes(attrs)
55+
perm.save! if perm.changed?
56+
end
3657
end
3758

3859
# Clear existing data - Use with caution!

app/controllers/better_together/setup_wizard_controller.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
module BetterTogether
55
# Handles the setup wizard process
66
class SetupWizardController < WizardsController
7-
def show; end
7+
def show
8+
# Always land on the first step of the host setup wizard
9+
redirect_to setup_wizard_step_platform_details_path
10+
end
811

912
private
1013

app/controllers/concerns/better_together/notification_readable.rb

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,32 @@ module NotificationReadable
77

88
# Marks notifications (for the current person) as read for events bound to a record
99
# via Noticed::Event#record_id (generic helper used across features).
10-
def mark_notifications_read_for_record(record, recipient: helpers.current_person) # rubocop:todo Metrics/AbcSize
10+
def mark_notifications_read_for_record(record, recipient: helpers.current_person)
1111
return unless recipient && record.respond_to?(:id)
1212

13-
nn = Noticed::Notification.arel_table
14-
ne = Noticed::Event.arel_table
15-
16-
join = nn.join(ne).on(ne[:id].eq(nn[:event_id])).join_sources
17-
18-
relation = Noticed::Notification
19-
.where(recipient:)
20-
.where(nn[:read_at].eq(nil))
21-
.joins(join)
22-
.where(ne[:record_id].eq(record.id))
13+
event_ids = Noticed::Event.where(record_id: record.id).select(:id)
2314

24-
relation.update_all(read_at: Time.current)
15+
Noticed::Notification
16+
.where(recipient:)
17+
.where(event_id: event_ids)
18+
.where(read_at: nil)
19+
.update_all(read_at: Time.current)
2520
end
2621

2722
# Marks notifications as read for a set of records associated to a given Noticed event class
2823
# using the event's record_id field.
29-
def mark_notifications_read_for_event_records(event_class, record_ids, recipient: helpers.current_person) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
24+
def mark_notifications_read_for_event_records(event_class, record_ids, recipient: helpers.current_person)
3025
return unless recipient && record_ids.present?
3126

32-
nn = Noticed::Notification.arel_table
33-
ne = Noticed::Event.arel_table
34-
35-
join = nn.join(ne).on(ne[:id].eq(nn[:event_id])).join_sources
36-
37-
relation = Noticed::Notification
38-
.where(recipient:)
39-
.where(nn[:read_at].eq(nil))
40-
.joins(join)
41-
.where(ne[:type].eq(event_class.to_s))
42-
.where(ne[:record_id].in(Array(record_ids)))
27+
event_ids = Noticed::Event
28+
.where(type: event_class.to_s, record_id: Array(record_ids))
29+
.select(:id)
4330

44-
relation.update_all(read_at: Time.current)
31+
Noticed::Notification
32+
.where(recipient:)
33+
.where(event_id: event_ids)
34+
.where(read_at: nil)
35+
.update_all(read_at: Time.current)
4536
end
4637

4738
# Marks Joatu match notifications as read for an Offer or Request record by matching

app/policies/better_together/calendar_policy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ module BetterTogether
44
# Access control for calendars
55
class CalendarPolicy < ApplicationPolicy
66
def index?
7-
user.present? && permitted_to?('manage_platform')
7+
user.present?
88
end
99

1010
def show?
11-
user.present? && (record.creator == agent or permitted_to?('manage_platform'))
11+
user.present?
1212
end
1313

1414
def update?

app/services/better_together/joatu/matchmaker.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def self.match(record) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
2222
.where(BetterTogether::Joatu::Category.table_name => { id: record.category_ids })
2323
end
2424

25-
# Target type must align; target_id supports wildcard semantics
26-
candidates = candidates.where(target_type: record.target_type)
25+
# Target type must align when present; target_id supports wildcard semantics
26+
candidates = candidates.where(target_type: record.target_type) if record.target_type.present?
2727
if record.target_id.present?
2828
candidates = candidates.where(
2929
"#{offer_klass.table_name}.target_id = ? OR #{offer_klass.table_name}.target_id IS NULL",
@@ -54,7 +54,7 @@ def self.match(record) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
5454
.where(BetterTogether::Joatu::Category.table_name => { id: record.category_ids })
5555
end
5656

57-
candidates = candidates.where(target_type: record.target_type)
57+
candidates = candidates.where(target_type: record.target_type) if record.target_type.present?
5858
if record.target_id.present?
5959
candidates = candidates.where(
6060
"#{request_klass.table_name}.target_id = ? OR #{request_klass.table_name}.target_id IS NULL",

spec/factories/better_together/conversations.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
association :creator, factory: :person
99

1010
after(:build) do |conversation|
11-
conversation.participants << conversation.creator
11+
conversation.participants << conversation.creator unless conversation.participants.include?(conversation.creator)
12+
end
13+
14+
after(:create) do |conversation|
15+
unless conversation.participants.exists?(conversation.creator.id)
16+
conversation.participants << conversation.creator
17+
end
1218
end
1319
end
1420
end

spec/factories/better_together/platforms.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
name { Faker::Company.name }
1111
description { Faker::Lorem.paragraph }
1212
identifier { Faker::Internet.unique.username(specifier: 10..20) }
13-
url { 'http://127.0.0.1:3000' }
13+
# Ensure uniqueness to avoid validation collisions across specs
14+
sequence(:url) { |n| "http://platform-#{n}.test" }
1415
host { false }
1516
time_zone { Faker::Address.time_zone }
1617
privacy { 'private' }

spec/rails_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
config.include FactoryBot::Syntax::Methods
6464

6565
config.include Devise::Test::IntegrationHelpers, type: :feature
66+
config.include Devise::Test::IntegrationHelpers, type: :request
6667

6768
config.include Warden::Test::Helpers
6869
config.after { Warden.test_reset! }

spec/requests/better_together/joatu/agreements_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
# rubocop:disable Metrics/BlockLength
66
RSpec.describe 'BetterTogether::Joatu::Agreements', :as_user do
7-
let(:user) { create(:user, :confirmed) }
7+
let(:user) { find_or_create_test_user('[email protected]', 'password12345', :user) }
8+
let(:person) { user.person }
89
let(:offer) { create(:joatu_offer) }
9-
let(:request_record) { create(:joatu_request) }
10+
let(:request_record) { create(:joatu_request, creator: person) }
1011
let(:valid_attributes) { { offer_id: offer.id, request_id: request_record.id, terms: 'terms', value: 'value' } }
11-
let(:agreement) { create(:joatu_agreement) }
12+
let(:agreement) { create(:joatu_agreement, offer: offer, request: request_record) }
1213

1314
describe 'routing' do
1415
it 'routes to #index' do
@@ -54,7 +55,7 @@
5455

5556
describe 'DELETE /destroy' do
5657
it 'destroys the agreement' do
57-
to_delete = create(:joatu_agreement)
58+
to_delete = create(:joatu_agreement, offer: offer, request: request_record)
5859
expect do
5960
delete better_together.joatu_agreement_path(to_delete, locale: I18n.locale)
6061
end.to change(BetterTogether::Joatu::Agreement, :count).by(-1)

spec/requests/better_together/joatu/offers_aggregated_matches_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
# rubocop:todo RSpec/MultipleExpectations
77
it 'shows Potential Matches for my offers with matching requests' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
88
# rubocop:enable RSpec/MultipleExpectations
9-
# Current user creating an offer
10-
current_user = BetterTogether::User.find_by(email: '[email protected]')
9+
# Current authenticated user creating an offer
10+
current_user = BetterTogether::User.find_by(email: '[email protected]') ||
11+
FactoryBot.create(:better_together_user, :confirmed,
12+
email: '[email protected]', password: 'password12345')
1113
my_person = current_user.person
1214

1315
# Ensure both records share a category so they match

0 commit comments

Comments
 (0)