2021-03-31 17:09:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Keys::ExpiryNotificationService do
|
|
|
|
let_it_be_with_reload(:user) { create(:user) }
|
|
|
|
|
2021-04-07 20:09:11 -04:00
|
|
|
let(:params) { { keys: user.keys, expiring_soon: expiring_soon } }
|
2021-03-31 17:09:15 -04:00
|
|
|
|
|
|
|
subject { described_class.new(user, params) }
|
|
|
|
|
2021-04-07 20:09:11 -04:00
|
|
|
shared_examples 'sends a notification' do
|
|
|
|
it do
|
2021-03-31 17:09:15 -04:00
|
|
|
perform_enqueued_jobs do
|
|
|
|
subject.execute
|
|
|
|
end
|
|
|
|
should_email(user)
|
|
|
|
end
|
2021-04-07 20:09:11 -04:00
|
|
|
end
|
2021-03-31 17:09:15 -04:00
|
|
|
|
2021-04-07 20:09:11 -04:00
|
|
|
shared_examples 'uses notification service to send email to the user' do |notification_method|
|
|
|
|
it do
|
2021-03-31 17:09:15 -04:00
|
|
|
expect_next_instance_of(NotificationService) do |notification_service|
|
2021-04-07 20:09:11 -04:00
|
|
|
expect(notification_service).to receive(notification_method).with(key.user, [key.fingerprint])
|
2021-03-31 17:09:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
2021-04-07 20:09:11 -04:00
|
|
|
end
|
2021-03-31 17:09:15 -04:00
|
|
|
|
2021-04-07 20:09:11 -04:00
|
|
|
shared_examples 'does not send notification' do
|
|
|
|
it do
|
|
|
|
perform_enqueued_jobs do
|
|
|
|
subject.execute
|
|
|
|
end
|
|
|
|
should_not_email(user)
|
2021-03-31 17:09:15 -04:00
|
|
|
end
|
2021-04-07 20:09:11 -04:00
|
|
|
end
|
2021-03-31 17:09:15 -04:00
|
|
|
|
2021-04-07 20:09:11 -04:00
|
|
|
shared_context 'block user' do
|
|
|
|
before do
|
|
|
|
user.block!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with key expiring today', :mailer do
|
|
|
|
let_it_be_with_reload(:key) { create(:key, expires_at: Time.current, user: user) }
|
|
|
|
|
|
|
|
let(:expiring_soon) { false }
|
|
|
|
|
|
|
|
context 'when user has permission to receive notification' do
|
|
|
|
it_behaves_like 'sends a notification'
|
|
|
|
|
|
|
|
it_behaves_like 'uses notification service to send email to the user', :ssh_key_expired
|
|
|
|
|
|
|
|
it 'updates notified column' do
|
|
|
|
expect { subject.execute }.to change { key.reload.expiry_notification_delivered_at }
|
2021-03-31 17:09:15 -04:00
|
|
|
end
|
2021-04-07 20:09:11 -04:00
|
|
|
end
|
2021-03-31 17:09:15 -04:00
|
|
|
|
2021-04-07 20:09:11 -04:00
|
|
|
context 'when user does NOT have permission to receive notification' do
|
|
|
|
include_context 'block user'
|
|
|
|
|
|
|
|
it_behaves_like 'does not send notification'
|
|
|
|
|
|
|
|
it 'does not update notified column' do
|
|
|
|
expect { subject.execute }.not_to change { key.reload.expiry_notification_delivered_at }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with key expiring soon', :mailer do
|
|
|
|
let_it_be_with_reload(:key) { create(:key, expires_at: 3.days.from_now, user: user) }
|
|
|
|
|
|
|
|
let(:expiring_soon) { true }
|
|
|
|
|
|
|
|
context 'when user has permission to receive notification' do
|
|
|
|
it_behaves_like 'sends a notification'
|
|
|
|
|
|
|
|
it_behaves_like 'uses notification service to send email to the user', :ssh_key_expiring_soon
|
|
|
|
|
|
|
|
it 'updates notified column' do
|
|
|
|
expect { subject.execute }.to change { key.reload.before_expiry_notification_delivered_at }
|
2021-03-31 17:09:15 -04:00
|
|
|
end
|
2021-04-07 20:09:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does NOT have permission to receive notification' do
|
|
|
|
include_context 'block user'
|
|
|
|
|
|
|
|
it_behaves_like 'does not send notification'
|
2021-03-31 17:09:15 -04:00
|
|
|
|
|
|
|
it 'does not update notified column' do
|
2021-04-07 20:09:11 -04:00
|
|
|
expect { subject.execute }.not_to change { key.reload.before_expiry_notification_delivered_at }
|
2021-03-31 17:09:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|