deploy_key_policy_spec.rb 1.66 KB
Newer Older
1 2
require 'spec_helper'

3
describe DeployKeyPolicy do
4
  subject { described_class.new(current_user, deploy_key) }
5 6 7 8 9 10 11 12 13 14

  describe 'updating a deploy_key' do
    context 'when a regular user' do
      let(:current_user) { create(:user) }

      context 'tries to update private deploy key attached to project' do
        let(:deploy_key) { create(:deploy_key, public: false) }
        let(:project) { create(:project_empty_repo) }

        before do
15
          project.add_maintainer(current_user)
16 17 18
          project.deploy_keys << deploy_key
        end

19
        it { is_expected.to be_allowed(:update_deploy_key) }
20 21 22 23 24 25 26 27 28 29
      end

      context 'tries to update private deploy key attached to other project' do
        let(:deploy_key) { create(:deploy_key, public: false) }
        let(:other_project) { create(:project_empty_repo) }

        before do
          other_project.deploy_keys << deploy_key
        end

30
        it { is_expected.to be_disallowed(:update_deploy_key) }
31 32 33 34 35
      end

      context 'tries to update public deploy key' do
        let(:deploy_key) { create(:another_deploy_key, public: true) }

36
        it { is_expected.to be_disallowed(:update_deploy_key) }
37 38 39 40 41 42 43 44 45
      end
    end

    context 'when an admin user' do
      let(:current_user) { create(:user, :admin) }

      context ' tries to update private deploy key' do
        let(:deploy_key) { create(:deploy_key, public: false) }

46
        it { is_expected.to be_allowed(:update_deploy_key) }
47 48 49 50 51
      end

      context 'when an admin user tries to update public deploy key' do
        let(:deploy_key) { create(:another_deploy_key, public: true) }

52
        it { is_expected.to be_allowed(:update_deploy_key) }
53 54 55 56
      end
    end
  end
end