Skip to content

Commit

Permalink
Added spec for import method in assignment_participant.rb file
Browse files Browse the repository at this point in the history
  • Loading branch information
ptkab committed Oct 28, 2018
1 parent c17bb25 commit 3c17dd7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/assignment_participant.rb
Expand Up @@ -210,7 +210,7 @@ def self.import(row_hash, _row_header = nil, session, id)
new_part.set_handle
end
prepared_mail = MailerHelper.send_mail_to_user(user, "Your Expertiza account and password have been created.", "user_welcome", "password")
prepared_mail.deliver
prepared_mail.deliver_now
end

# provide export functionality for Assignment Participants
Expand Down
31 changes: 30 additions & 1 deletion spec/models/assignment_particpant_spec.rb
Expand Up @@ -283,7 +283,7 @@
{name: 'no one', fullname: 'no one', email: 'name@email.com', role:'user_role_name', parent: 'user_parent_name'}
end
let(:attributes) do
{role_id: 1, name: 'no one', fullname: 'no one', email: '', email_on_submission: 'name@email.com',
{role_id: 1, name: 'no one', fullname: 'no one', email: 'name@email.com', email_on_submission: 'name@email.com',
email_on_review: 'name@email.com', email_on_review_of_review: 'name@email.com'}
end
before(:each) do
Expand All @@ -308,6 +308,35 @@
end
end
end

context "when participant is added to assignment using csv" do
let(:row) do
{name: 'no one', fullname: 'no one', email: 'name@email.com', role:'user_role_name', parent: 'user_parent_name'}
end
let(:attributes) do
{role_id: 1, name: 'no one', fullname: 'no one', email: 'name@email.com', email_on_submission: 'name@email.com',
email_on_review: 'name@email.com', email_on_review_of_review: 'name@email.com'}
end

it "should send an email to the participant", :mailer => true do
allow(ImportFileHelper).to receive(:define_attributes).with(row).and_return(attributes)
allow(ImportFileHelper).to receive(:create_new_user).with(attributes, {}).and_return(double('User', id: 1, email: 'name@email.com', first_name: 'no name', last_name: 'no name', name: 'no name'))
allow(Assignment).to receive(:find).with(1).and_return(assignment)
allow(AssignmentParticipant).to receive(:exists?).with(user_id: 1, parent_id: 1).and_return(false)
allow(AssignmentParticipant).to receive(:create).with(user_id: 1, parent_id: 1).and_return(participant)
allow(participant).to receive(:set_handle).and_return('handle')

# Check that ActionMailer::Base.deliveries list is updated when email is set by import method.
# The last element in the deliveries list will be the email that was just sent when import method was invoked
# We need to verify the email was correct by checking its subject, from and to.
# In development/test mode, all mails go to expertiza.development@gmail.com and not actual user.to
expect { AssignmentParticipant.import(row, nil, {}, 1) }.to change { ActionMailer::Base.deliveries.count }.by(1)
email = ActionMailer::Base.deliveries.last
expect(email.from[0]).to eq('expertiza.development@gmail.com')
expect(email.to[0]).to eq('expertiza.development@gmail.com')
expect(email.subject).to eq('Your Expertiza account and password have been created.')
end
end
end
end

Expand Down

0 comments on commit 3c17dd7

Please sign in to comment.