debian-mirror-gitlab/spec/models/service_desk/custom_email_verification_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

171 lines
4.7 KiB
Ruby
Raw Permalink Normal View History

2023-05-27 22:25:52 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ServiceDesk::CustomEmailVerification, feature_category: :service_desk do
2023-07-09 08:55:56 +05:30
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let(:generate_token_pattern) { /\A\p{Alnum}{12}\z/ }
2023-05-27 22:25:52 +05:30
describe '.generate_token' do
it 'matches expected output' do
2023-07-09 08:55:56 +05:30
expect(described_class.generate_token).to match(generate_token_pattern)
2023-05-27 22:25:52 +05:30
end
end
describe 'validations' do
2023-07-09 08:55:56 +05:30
subject { build(:service_desk_custom_email_verification, project: project) }
2023-05-27 22:25:52 +05:30
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:state) }
2023-07-09 08:55:56 +05:30
context 'when status is :started' do
before do
subject.mark_as_started!(user)
end
it { is_expected.to validate_presence_of(:token) }
it { is_expected.to validate_length_of(:token).is_equal_to(12) }
it 'matches .generate_token pattern' do
expect(subject.token).to match(generate_token_pattern)
end
it { is_expected.to validate_presence_of(:triggerer) }
it { is_expected.to validate_presence_of(:triggered_at) }
it { is_expected.to validate_absence_of(:error) }
end
context 'when status is :finished' do
before do
subject.mark_as_started!(user)
subject.mark_as_finished!
end
it { is_expected.to validate_absence_of(:token) }
it { is_expected.to validate_absence_of(:error) }
end
context 'when status is :failed' do
before do
subject.mark_as_started!(user)
subject.mark_as_failed!(:smtp_host_issue)
end
it { is_expected.to validate_presence_of(:error) }
it { is_expected.to validate_absence_of(:token) }
end
2023-05-27 22:25:52 +05:30
end
2023-07-09 08:55:56 +05:30
describe 'status state machine' do
subject { build(:service_desk_custom_email_verification, project: project) }
describe 'transitioning to started' do
it 'records the started at time and generates token' do
subject.mark_as_started!(user)
is_expected.to be_started
expect(subject.token).to be_present
expect(subject.triggered_at).to be_present
expect(subject.triggerer).to eq(user)
2023-05-27 22:25:52 +05:30
end
end
2023-07-09 08:55:56 +05:30
describe 'transitioning to finished' do
it 'removes the generated token' do
subject.mark_as_started!(user)
subject.mark_as_finished!
2023-05-27 22:25:52 +05:30
2023-07-09 08:55:56 +05:30
is_expected.to be_finished
expect(subject.token).not_to be_present
end
end
2023-05-27 22:25:52 +05:30
2023-07-09 08:55:56 +05:30
describe 'transitioning to failed' do
let(:error) { :smtp_host_issue }
2023-05-27 22:25:52 +05:30
2023-07-09 08:55:56 +05:30
it 'removes the generated token' do
subject.mark_as_started!(user)
subject.mark_as_failed!(error)
2023-05-27 22:25:52 +05:30
2023-07-09 08:55:56 +05:30
is_expected.to be_failed
expect(subject.token).not_to be_present
expect(subject.error).to eq(error.to_s)
2023-05-27 22:25:52 +05:30
end
end
end
2023-07-09 08:55:56 +05:30
describe '#accepted_until' do
it 'returns nil' do
expect(subject.accepted_until).to be_nil
2023-05-27 22:25:52 +05:30
end
2023-07-09 08:55:56 +05:30
context 'when state is :started and successfully transitioned' do
let(:triggered_at) { 2.minutes.ago }
2023-05-27 22:25:52 +05:30
2023-07-09 08:55:56 +05:30
before do
subject.project = project
subject.mark_as_started!(user)
end
2023-05-27 22:25:52 +05:30
2023-07-09 08:55:56 +05:30
it 'returns correct timeframe end time' do
expect(subject.accepted_until).to eq(described_class::TIMEFRAME.since(subject.triggered_at))
end
context 'when triggered_at is not set' do
it 'returns nil' do
subject.triggered_at = nil
expect(subject.accepted_until).to be nil
2023-05-27 22:25:52 +05:30
end
2023-07-09 08:55:56 +05:30
end
end
end
2023-05-27 22:25:52 +05:30
2023-07-09 08:55:56 +05:30
describe '#in_timeframe?' do
it { is_expected.not_to be_in_timeframe }
2023-05-27 22:25:52 +05:30
2023-07-09 08:55:56 +05:30
context 'when state is :started and successfully transitioned' do
before do
subject.project = project
subject.mark_as_started!(user)
end
2023-05-27 22:25:52 +05:30
2023-07-09 08:55:56 +05:30
it { is_expected.to be_in_timeframe }
2023-05-27 22:25:52 +05:30
2023-07-09 08:55:56 +05:30
context 'and timeframe was missed' do
before do
subject.triggered_at = (described_class::TIMEFRAME + 1).ago
2023-05-27 22:25:52 +05:30
end
2023-07-09 08:55:56 +05:30
it { is_expected.not_to be_in_timeframe }
2023-05-27 22:25:52 +05:30
end
end
end
describe 'encrypted #token' do
2023-07-09 08:55:56 +05:30
let(:token) { 'XXXXXXXXXXXX' }
2023-05-27 22:25:52 +05:30
subject { build_stubbed(:service_desk_custom_email_verification, token: token) }
it 'saves and retrieves the encrypted token and iv correctly' do
expect(subject.encrypted_token).not_to be_nil
expect(subject.encrypted_token_iv).not_to be_nil
expect(subject.token).to eq(token)
end
end
describe 'associations' do
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:triggerer) }
it 'can access service desk setting from project' do
2023-07-09 08:55:56 +05:30
subject.project = project
setting = build_stubbed(:service_desk_setting, project: subject.project)
2023-05-27 22:25:52 +05:30
2023-07-09 08:55:56 +05:30
expect(subject.service_desk_setting).to eq(setting)
2023-05-27 22:25:52 +05:30
end
end
end