shared_context 'gitlab email notification' do set(:group) { create(:group) } set(:subgroup) { create(:group, parent: group) } set(:project) { create(:project, :repository, name: 'a-known-name', group: group) } set(:recipient) { create(:user, email: 'recipient@example.com') } let(:gitlab_sender_display_name) { Gitlab.config.gitlab.email_display_name } let(:gitlab_sender) { Gitlab.config.gitlab.email_from } let(:gitlab_sender_reply_to) { Gitlab.config.gitlab.email_reply_to } let(:new_user_address) { 'newguy@example.com' } before do email = recipient.emails.create(email: "notifications@example.com") recipient.update_attribute(:notification_email, email.email) stub_incoming_email_setting(enabled: true, address: "reply+%{key}@#{Gitlab.config.gitlab.host}") end end shared_context 'reply-by-email is enabled with incoming address without %{key}' do before do stub_incoming_email_setting(enabled: true, address: "reply@#{Gitlab.config.gitlab.host}") end end shared_examples 'a multiple recipients email' do it 'is sent to the given recipient' do is_expected.to deliver_to recipient.notification_email end end shared_examples 'an email sent from GitLab' do it 'has the characteristics of an email sent from GitLab' do sender = subject.header[:from].addrs[0] reply_to = subject.header[:reply_to].addresses aggregate_failures do expect(sender.display_name).to eq(gitlab_sender_display_name) expect(sender.address).to eq(gitlab_sender) expect(reply_to).to eq([gitlab_sender_reply_to]) end end end shared_examples 'an email sent to a user' do let(:group_notification_email) { 'user+group@example.com' } it 'is sent to user\'s global notification email address' do expect(subject).to deliver_to(recipient.notification_email) end context 'that is part of a project\'s group' do it 'is sent to user\'s group notification email address when set' do create(:notification_setting, user: recipient, source: project.group, notification_email: group_notification_email) expect(subject).to deliver_to(group_notification_email) end it 'is sent to user\'s global notification email address when no group email set' do create(:notification_setting, user: recipient, source: project.group, notification_email: '') expect(subject).to deliver_to(recipient.notification_email) end end context 'when project is in a sub-group', :nested_groups do before do project.update!(group: subgroup) end it 'is sent to user\'s subgroup notification email address when set' do # Set top-level group notification email address to make sure it doesn't get selected create(:notification_setting, user: recipient, source: group, notification_email: group_notification_email) subgroup_notification_email = 'user+subgroup@example.com' create(:notification_setting, user: recipient, source: subgroup, notification_email: subgroup_notification_email) expect(subject).to deliver_to(subgroup_notification_email) end it 'is sent to user\'s group notification email address when set and subgroup email address not set' do create(:notification_setting, user: recipient, source: subgroup, notification_email: '') expect(subject).to deliver_to(recipient.notification_email) end end end shared_examples 'an email that contains a header with author username' do it 'has X-GitLab-Author header containing author\'s username' do is_expected.to have_header 'X-GitLab-Author', user.username end end shared_examples 'an email with X-GitLab headers containing IDs' do it 'has X-GitLab-*-ID header' do is_expected.to have_header "X-GitLab-#{model.class.name}-ID", "#{model.id}" end it 'has X-GitLab-*-IID header if model has iid defined' do if model.respond_to?(:iid) is_expected.to have_header "X-GitLab-#{model.class.name}-IID", "#{model.iid}" else expect(subject.header["X-GitLab-#{model.class.name}-IID"]).to eq nil end end end shared_examples 'an email with X-GitLab headers containing project details' do it 'has X-GitLab-Project headers' do aggregate_failures do full_path_as_domain = "#{project.name}.#{project.namespace.path}" is_expected.to have_header('X-GitLab-Project', /#{project.name}/) is_expected.to have_header('X-GitLab-Project-Id', /#{project.id}/) is_expected.to have_header('X-GitLab-Project-Path', /#{project.full_path}/) is_expected.to have_header('List-Id', "#{project.full_path} <#{project.id}.#{full_path_as_domain}.#{Gitlab.config.gitlab.host}>") end end end shared_examples 'a new thread email with reply-by-email enabled' do it 'has the characteristics of a threaded email' do host = Gitlab.config.gitlab.host route_key = "#{model.class.model_name.singular_route_key}_#{model.id}" aggregate_failures do is_expected.to have_header('Message-ID', "<#{route_key}@#{host}>") is_expected.to have_header('References', /\A\Z/ ) end end end shared_examples 'a thread answer email with reply-by-email enabled' do include_examples 'an email with X-GitLab headers containing project details' include_examples 'an email with X-GitLab headers containing IDs' it 'has the characteristics of a threaded reply' do host = Gitlab.config.gitlab.host route_key = "#{model.class.model_name.singular_route_key}_#{model.id}" aggregate_failures do is_expected.to have_header('Message-ID', /\A<.*@#{host}>\Z/) is_expected.to have_header('In-Reply-To', "<#{route_key}@#{host}>") is_expected.to have_header('References', /\A <#{route_key}@#{host}>\Z/ ) is_expected.to have_subject(/^Re: /) end end end shared_examples 'an email starting a new thread with reply-by-email enabled' do include_examples 'an email with X-GitLab headers containing project details' include_examples 'an email with X-GitLab headers containing IDs' include_examples 'a new thread email with reply-by-email enabled' it 'includes "Reply to this email directly or "' do expect(subject.default_part.body).to include(%(Reply to this email directly or view it on GitLab.)) end context 'when reply-by-email is enabled with incoming address with %{key}' do it 'has a Reply-To header' do is_expected.to have_header 'Reply-To', /\Z/ end end context 'when reply-by-email is enabled with incoming address without %{key}' do include_context 'reply-by-email is enabled with incoming address without %{key}' include_examples 'a new thread email with reply-by-email enabled' it 'has a Reply-To header' do is_expected.to have_header 'Reply-To', /\Z/ end end end shared_examples 'an answer to an existing thread with reply-by-email enabled' do include_examples 'an email with X-GitLab headers containing project details' include_examples 'an email with X-GitLab headers containing IDs' include_examples 'a thread answer email with reply-by-email enabled' context 'when reply-by-email is enabled with incoming address with %{key}' do it 'has a Reply-To header' do is_expected.to have_header 'Reply-To', /\Z/ end end context 'when reply-by-email is enabled with incoming address without %{key}' do include_context 'reply-by-email is enabled with incoming address without %{key}' include_examples 'a thread answer email with reply-by-email enabled' it 'has a Reply-To header' do is_expected.to have_header 'Reply-To', /\Z/ end end end shared_examples 'it should have Gmail Actions links' do it do aggregate_failures do is_expected.to have_body_text('