Skip to content

Commit

Permalink
user Factory: have to create a user in order to have associated shf_a…
Browse files Browse the repository at this point in the history
…pp, payments, etc etc.
  • Loading branch information
weedySeaDragon committed Apr 8, 2021
1 parent 3d3fa43 commit eef19d7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
contact_email { email }
end

after(:build) do |member, evaluator|
after(:create) do |member, evaluator|

create_list(:shf_application, 1, :accepted, user: member,
company_number: evaluator.company_number,
Expand Down
23 changes: 18 additions & 5 deletions spec/mailers/previews/member_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,21 @@ def first_membership_fee_owed

def membership_renewed
# Select a current member
member_to_renew = User.current_member.sort_by(&:membership_expire_date).first
member_to_renew = User.in_grace_period.sort_by(&:membership_expire_date).first

# Create a member to renew if we didn't find any
unless member_to_renew
member_to_renew = User.current.sort_by(&:membership_expire_date).first

# if we still haven't found one, create one:
unless membership_to_renew
timestamp = Time.now.to_i
member_to_renew = FactoryBot.create(:member, first_name: "Just-#{timestamp}", last_name: 'Renewed',
email: "just-renewed-#{timestamp}@example.com",
expiration_date: (Date.current - 1.day),
membership_status: :in_grace_period)
end
end

unless member_to_renew.companies.detect { |co| !co.information_complete? }
incomplete_company = FactoryBot.create(:company, name: 'Incomplete (no region)')
Expand All @@ -124,7 +138,7 @@ def membership_renewed
# TODO when Company uses Membership, then change this to (from using payments)
FactoryBot.create(:h_branding_fee_payment, user: member_to_renew,
company: expired_company,
expire_date: Date.current - 1.day )
expire_date: Date.current - 1.day)
member_to_renew.shf_application.companies << expired_company
end

Expand All @@ -133,7 +147,7 @@ def membership_renewed
# TODO when Company uses Membership, then change this to (from using payments)
FactoryBot.create(:h_branding_fee_payment, user: member_to_renew,
company: expired_and_incomplete_company,
expire_date: Date.current - 1.day )
expire_date: Date.current - 1.day)
expired_and_incomplete_company.addresses.first.update(region: nil)
member_to_renew.shf_application.companies << expired_and_incomplete_company
end
Expand All @@ -142,8 +156,7 @@ def membership_renewed
end


private

private

# create a unique email address based on the Time right now
def unique_email
Expand Down
8 changes: 4 additions & 4 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@
end

it 'creates an uploaded file for the current membership term (default)' do
member = build(:member_with_expiration_date)
member = create(:member_with_expiration_date)
expect(member.uploaded_files.size > 0).to be_truthy
expect(member.shf_application.uploaded_files_count).to eq 0 # The file was not associated with an ShfApplication
end

context 'has_uploaded_docs is false' do
it 'does not create an uploaded file for the current membership term' do
member = build(:member_with_expiration_date, has_uploaded_docs: false)
member = create(:member_with_expiration_date, has_uploaded_docs: false)
expect(member.uploaded_files.size > 0).to be_falsey
expect(member.shf_application.uploaded_files_count).to eq 0 # The file was not associated with an ShfApplication
end
Expand Down Expand Up @@ -1416,7 +1416,7 @@

context 'is a current member' do
it 'returns the first day for the current membership' do
current_member = build(:member_with_membership_app)
current_member = create(:member)
expect(current_member.membership_start_date).to eq Date.current
end
end
Expand All @@ -1430,7 +1430,7 @@
describe 'membership_expire_date' do
context 'is a current member' do
it 'returns the first day for the current membership' do
current_member = build(:member_with_membership_app)
current_member = create(:member)
expect(current_member.membership_expire_date).to eq(Membership.last_day_from_first(Date.current))
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/shared_context/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@


let(:member_paid_up) do
user = build(:member_with_membership_app)
user = create(:member_with_membership_app)
user.payments << create(:membership_fee_payment)
user.save!
user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
describe 'for each member that needs a packet, it shows:' do

let(:member_paid_up1) do
user = build(:member_with_membership_app, first_name: 'member', last_name: 'paid_up 1')
user = create(:member_with_membership_app, first_name: 'member', last_name: 'paid_up 1')
user.payments << build(:membership_fee_payment)
user
end

let(:member_2cos) do
user = build(:member_with_membership_app, first_name: 'member', last_name: '2 companies')
user = create(:member_with_membership_app, first_name: 'member', last_name: '2 companies')
user.payments << build(:membership_fee_payment)
co2 = build(:company, name: 'Second company', website: 'www.secondcompany.com')
co2 = create(:company, name: 'Second company', website: 'www.secondcompany.com')
user.shf_application.companies << co2
user
end
Expand Down

0 comments on commit eef19d7

Please sign in to comment.