OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
replace mail legacy spec
ulfertse7e4cbf961a5
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rbindex 4525238033e..64772ac53a5 100644--- a/app/models/mail_handler.rb+++ b/app/models/mail_handler.rb@@ -50,7 +50,7 @@ class MailHandler < ActionMailer::Baseenddef self.with_options(options)- handler = self.new+ handler = newhandler.options = options@@ -292,7 +292,7 @@ class MailHandler < ActionMailer::Baseelse@keywords[attr] = beginif (options[:override] || self.options[:allow_override].include?(attr)) &&- (v = extract_keyword!(plain_text_body, attr, options[:format]))+ (v = extract_keyword!(plain_text_body, attr, options[:format]))velse# Return either default or nildiff --git a/spec/models/mail_handler_spec.rb b/spec/models/mail_handler_spec.rbindex 872ca6d5ccc..60191a6800b 100644--- a/spec/models/mail_handler_spec.rb+++ b/spec/models/mail_handler_spec.rb@@ -50,16 +50,19 @@ describe MailHandler, type: :model doendshared_context 'wp_on_given_project' do- let(:permissions) { %i[add_work_packages] }+ let(:permissions) { %i[add_work_packages assign_versions] }let!(:user) doFactoryBot.create(:user,mail: '[email protected]',+ firstname: 'John',+ lastname: 'Smith',member_in_project: project,member_with_permissions: permissions)end+ let(:submit_options) { {} }subject do- submit_email('wp_on_given_project.eml')+ submit_email('wp_on_given_project.eml', **submit_options)endend@@ -120,68 +123,193 @@ describe MailHandler, type: :model doendend- it 'should add a work_package by create user on public project' do- Role.non_member.update_attribute :permissions, [:add_work_packages]- project.update_attribute :public, true- expect do- work_package = submit_email('ticket_by_unknown_user.eml', issue: { project: 'onlinestore' }, unknown_user: 'create')- work_package_created(work_package)- expect(work_package.author.active?).to be_truthy- expect(work_package.author.mail).to eq('[email protected]')- expect(work_package.author.firstname).to eq('John')- expect(work_package.author.lastname).to eq('Doe')-- # account information- email = ActionMailer::Base.deliveries.first- expect(email).not_to be_nil- expect(email.subject).to eq(I18n.t('mail_subject_register', value: Setting.app_title))- login = email.body.encoded.match(/\* Username: (\S+)\s?$/)[1]- password = email.body.encoded.match(/\* Password: (\S+)\s?$/)[1]-- # Can't log in here since randomly assigned password must be changed- found_user = User.find_by_login(login)- expect(work_package.author).to eq(found_user)- expect(found_user.check_password?(password)).to be_truthy- end.to change(User, :count).by(1)- end+ context 'create work package' do+ context 'in a given project' do+ let!(:status) { FactoryBot.create(:status, name: 'Resolved') }+ let!(:version) { FactoryBot.create(:version, name: 'alpha', project: project) }- context 'email from emission address', with_settings: { mail_from: '[email protected]' } do- before do- Role.non_member.add_permission!(:add_work_packages)- end+ include_context 'wp_on_given_project' do+ let(:submit_options) { { allow_override: 'fixed_version' } }+ end++ it_behaves_like 'work package created'++ it 'sets the referenced project' do+ expect(subject.project)+ .to eql(project)+ end++ it 'sets the first type in the project' do+ expect(subject.type)+ .to eql(project.types.first)+ end++ it 'sets the subject' do+ expect(subject.subject)+ .to eql('New ticket on a given project')+ end++ it 'sets the sender as the author' do+ expect(subject.author)+ .to eql(user)+ end++ it 'set the description' do+ expect(subject.description)+ .to include('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')+ end++ it 'sets the start date' do+ expect(subject.start_date.to_s)+ .to eql('2010-01-01')+ end++ it 'sets the due date' do+ expect(subject.due_date.to_s)+ .to eql('2010-12-31')+ end++ it 'sets the assignee' do+ expect(subject.assigned_to)+ .to eql(user)+ end++ it 'sets the status' do+ expect(subject.status)+ .to eql(status)+ end++ it 'sets the version' do+ expect(subject.fixed_version)+ .to eql(version)+ end++ it 'sets the estimated_hours' do+ expect(subject.estimated_hours)+ .to eql(2.5)+ end++ it 'sets the done_ratio' do+ expect(subject.done_ratio)+ .to eql(30)+ end++ it 'removes keywords' do+ expect(subject.description)+ .not_to match(/^Project:/i)- subject do- submit_email('ticket_from_emission_address.eml',- issue: { project: public_project.identifier },- unknown_user: 'create')+ expect(subject.description)+ .not_to match(/^Status:/i)++ expect(subject.description)+ .not_to match(/^Start Date:/i)+ end++ context 'with a user watching every creation' do+ let!(:other_user) do+ FactoryBot.create(:user,+ mail_notification: 'all',+ member_in_project: project,+ member_with_permissions: %i[view_work_packages])+ end++ it 'sends a mail as a work package has been created' do+ subject+ # Email notification should be sent+ mail = ActionMailer::Base.deliveries.last++ expect(mail)+ .not_to be_nil+ expect(mail.subject)+ .to include('New ticket on a given project')+ end+ endend- it 'returns false' do- expect(subject).to be_falsey+ context 'in given project with a default type' do+ let(:default_type) do+ FactoryBot.create(:type, is_default: true).tap do |t|+ project.types << t+ end+ end++ include_context 'wp_on_given_project' do+ let(:submit_options) { { issue: { type: default_type.name } } }+ end++ it_behaves_like 'work package created'++ it 'sets the default type' do+ expect(subject.type.name)+ .to eql(default_type.name)+ endend- it 'does not create the user' do- expect { subject }- .not_to(change { User.count })+ context 'email by unknown user' do+ it 'adds a work_package by create user on public project' do+ Role.non_member.update_attribute :permissions, [:add_work_packages]+ project.update_attribute :public, true+ expect do+ work_package = submit_email('ticket_by_unknown_user.eml', issue: { project: 'onlinestore' }, unknown_user: 'create')+ work_package_created(work_package)+ expect(work_package.author.active?).to be_truthy+ expect(work_package.author.mail).to eq('[email protected]')+ expect(work_package.author.firstname).to eq('John')+ expect(work_package.author.lastname).to eq('Doe')++ # account information+ email = ActionMailer::Base.deliveries.first+ expect(email).not_to be_nil+ expect(email.subject).to eq(I18n.t('mail_subject_register', value: Setting.app_title))+ login = email.body.encoded.match(/\* Username: (\S+)\s?$/)[1]+ password = email.body.encoded.match(/\* Password: (\S+)\s?$/)[1]++ # Can't log in here since randomly assigned password must be changed+ found_user = User.find_by_login(login)+ expect(work_package.author).to eq(found_user)+ expect(found_user.check_password?(password)).to be_truthy+ end.to change(User, :count).by(1)+ endend- it 'does not create the work_package' do- expect { subject }- .not_to(change { WorkPackage.count })+ context 'email from emission address', with_settings: { mail_from: '[email protected]' } do+ before do+ Role.non_member.add_permission!(:add_work_packages)+ end++ subject do+ submit_email('ticket_from_emission_address.eml',+ issue: { project: public_project.identifier },+ unknown_user: 'create')+ end++ it 'returns false' do+ expect(subject).to be_falsey+ end++ it 'does not create the user' do+ expect { subject }+ .not_to(change { User.count })+ end++ it 'does not create the work_package' do+ expect { subject }+ .not_to(change { WorkPackage.count })+ endend- end- context 'wp with status' do- let!(:status) { FactoryBot.create(:status, name: 'Resolved') }+ context 'wp with status' do+ let!(:status) { FactoryBot.create(:status, name: 'Resolved') }- # This email contains: 'Project: onlinestore' and 'Status: Resolved'- include_context 'wp_on_given_project'+ # This email contains: 'Project: onlinestore' and 'Status: Resolved'+ include_context 'wp_on_given_project'- it_behaves_like 'work package created'+ it_behaves_like 'work package created'- it 'assigns the status to the created work package' do- expect(subject.status)- .to eql(status)+ it 'assigns the status to the created work package' do+ expect(subject.status)+ .to eql(status)+ endendenddiff --git a/spec_legacy/unit/mail_handler_spec.rb b/spec_legacy/unit/mail_handler_spec.rbindex 7594c96f878..6f02a94845c 100644--- a/spec_legacy/unit/mail_handler_spec.rb+++ b/spec_legacy/unit/mail_handler_spec.rb@@ -38,45 +38,6 @@ describe MailHandler, type: :model doallow(Setting).to receive(:notified_events).and_return(Redmine::Notifiable.all.map(&:name))end- it 'should add work package' do- # This email contains: 'Project: onlinestore'- issue = submit_email('ticket_on_given_project.eml', allow_override: 'fixed_version')- assert issue.is_a?(WorkPackage)- assert !issue.new_record?- issue.reload- assert_equal Project.find(2), issue.project- assert_equal issue.project.types.first, issue.type- assert_equal 'New ticket on a given project', issue.subject- assert_equal User.find_by_login('jsmith'), issue.author- assert_equal Status.find_by(name: 'Resolved'), issue.status- assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')- assert_equal '2010-01-01', issue.start_date.to_s- assert_equal '2010-12-31', issue.due_date.to_s- assert_equal User.find_by_login('jsmith'), issue.assigned_to- assert_equal Version.find_by(name: 'alpha'), issue.fixed_version- assert_equal 2.5, issue.estimated_hours- assert_equal 30, issue.done_ratio- assert issue.root?- assert issue.leaf?- # keywords should be removed from the email body- assert !issue.description.match(/^Project:/i)- assert !issue.description.match(/^Status:/i)- assert !issue.description.match(/^Start Date:/i)- # Email notification should be sent- mail = ActionMailer::Base.deliveries.last- refute_nil mail- assert mail.subject.include?('New ticket on a given project')- end-- it 'should add work package with default type' do- # This email contains: 'Project: onlinestore'- issue = submit_email('ticket_on_given_project.eml', issue: { type: 'Support request' })- assert issue.is_a?(WorkPackage)- assert !issue.new_record?- issue.reload- assert_equal 'Support request', issue.type.name- end-it 'should add work package with attributes override' doissue = submit_email('ticket_with_attributes.eml', allow_override: 'type,category,priority')assert issue.is_a?(WorkPackage)