diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-20 12:40:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-20 12:40:42 +0300 |
commit | ee664acb356f8123f4f6b00b73c1e1cf0866c7fb (patch) | |
tree | f8479f94a28f66654c6a4f6fb99bad6b4e86a40e /spec/models/jira_connect/public_key_spec.rb | |
parent | 62f7d5c5b69180e82ae8196b7b429eeffc8e7b4f (diff) |
Add latest changes from gitlab-org/gitlab@15-5-stable-eev15.5.0-rc42
Diffstat (limited to 'spec/models/jira_connect/public_key_spec.rb')
-rw-r--r-- | spec/models/jira_connect/public_key_spec.rb | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/spec/models/jira_connect/public_key_spec.rb b/spec/models/jira_connect/public_key_spec.rb new file mode 100644 index 00000000000..2e79a3ca4d2 --- /dev/null +++ b/spec/models/jira_connect/public_key_spec.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe JiraConnect::PublicKey do + describe '.create!' do + let(:key) { 'key123' } + + subject(:create_public_key) { described_class.create!(key: key) } + + it 'only accepts valid public keys' do + expect { create_public_key }.to raise_error(ArgumentError, 'Invalid public key') + end + + shared_examples 'creates a jira connect public key' do + it 'generates a Uuid' do + expect(create_public_key.uuid).to match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/) + end + + it 'sets the key attribute' do + expect(create_public_key.key).to eq(expected_key) + end + + it 'persists the values' do + Gitlab::Redis::SharedState.with do |redis| + expect(redis).to receive(:set).with(anything, expected_key, anything) + end + + create_public_key + end + end + + context 'with OpenSSL::PKey::RSA object' do + let(:key) { OpenSSL::PKey::RSA.generate(3072).public_key } + let(:expected_key) { key.to_s } + + it_behaves_like 'creates a jira connect public key' + end + + context 'with string public key' do + let(:key) { OpenSSL::PKey::RSA.generate(3072).public_key.to_s } + let(:expected_key) { key } + + it_behaves_like 'creates a jira connect public key' + end + end + + describe '.find' do + let(:uuid) { '1234' } + + subject(:find_public_key) { described_class.find(uuid) } + + it 'raises an error' do + expect { find_public_key }.to raise_error(ActiveRecord::RecordNotFound) + end + + context 'when the public key exists' do + let_it_be(:key) { OpenSSL::PKey::RSA.generate(3072).public_key } + let_it_be(:public_key) { described_class.create!(key: key) } + + let(:uuid) { public_key.uuid } + + it 'loads the public key', :aggregate_failures do + expect(find_public_key).to be_kind_of(described_class) + expect(find_public_key.uuid).to eq(public_key.uuid) + expect(find_public_key.key).to eq(key.to_s) + end + end + end + + describe '#save!' do + let(:key) { OpenSSL::PKey::RSA.generate(3072).public_key } + let(:public_key) { described_class.new(key: key, uuid: '123') } + let(:jira_connect_installation) { build(:jira_connect_installation) } + + subject(:save_public_key) { public_key.save! } + + it 'persists the values' do + Gitlab::Redis::SharedState.with do |redis| + expect(redis).to receive(:set).with(anything, key.to_s, ex: 5.minutes.to_i) + end + + save_public_key + end + + it 'returns itself' do + expect(save_public_key).to eq(public_key) + end + end +end |