summaryrefslogtreecommitdiff
path: root/spec/support/services/deploy_token_shared_examples.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support/services/deploy_token_shared_examples.rb')
-rw-r--r--spec/support/services/deploy_token_shared_examples.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/spec/support/services/deploy_token_shared_examples.rb b/spec/support/services/deploy_token_shared_examples.rb
new file mode 100644
index 00000000000..b49f4743f7d
--- /dev/null
+++ b/spec/support/services/deploy_token_shared_examples.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+RSpec.shared_examples 'a deploy token creation service' do
+ let(:user) { create(:user) }
+ let(:deploy_token_params) { attributes_for(:deploy_token) }
+
+ describe '#execute' do
+ subject { described_class.new(entity, user, deploy_token_params).execute }
+
+ context 'when the deploy token is valid' do
+ it 'creates a new DeployToken' do
+ expect { subject }.to change { DeployToken.count }.by(1)
+ end
+
+ it 'creates a new ProjectDeployToken' do
+ expect { subject }.to change { deploy_token_class.count }.by(1)
+ end
+
+ it 'returns a DeployToken' do
+ expect(subject).to be_an_instance_of DeployToken
+ end
+ end
+
+ context 'when expires at date is not passed' do
+ let(:deploy_token_params) { attributes_for(:deploy_token, expires_at: '') }
+
+ it 'sets Forever.date' do
+ expect(subject.read_attribute(:expires_at)).to eq(Forever.date)
+ end
+ end
+
+ context 'when username is empty string' do
+ let(:deploy_token_params) { attributes_for(:deploy_token, username: '') }
+
+ it 'converts it to nil' do
+ expect(subject.read_attribute(:username)).to be_nil
+ end
+ end
+
+ context 'when username is provided' do
+ let(:deploy_token_params) { attributes_for(:deploy_token, username: 'deployer') }
+
+ it 'keeps the provided username' do
+ expect(subject.read_attribute(:username)).to eq('deployer')
+ end
+ end
+
+ context 'when the deploy token is invalid' do
+ let(:deploy_token_params) { attributes_for(:deploy_token, read_repository: false, read_registry: false) }
+
+ it 'does not create a new DeployToken' do
+ expect { subject }.not_to change { DeployToken.count }
+ end
+
+ it 'does not create a new ProjectDeployToken' do
+ expect { subject }.not_to change { deploy_token_class.count }
+ end
+ end
+ end
+end