summaryrefslogtreecommitdiff
path: root/spec/services/protected_tags/update_service_spec.rb
blob: 8d301dcd82569fbc59483d2f971e5f80bc327f63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ProtectedTags::UpdateService do
  let(:protected_tag) { create(:protected_tag) }
  let(:project) { protected_tag.project }
  let(:user) { project.first_owner }
  let(:params) { { name: new_name } }

  describe '#execute' do
    let(:new_name) { 'new protected tag name' }
    let(:result) { service.execute(protected_tag) }

    subject(:service) { described_class.new(project, user, params) }

    it 'updates a protected tag' do
      expect(result.reload.name).to eq(params[:name])
    end

    context 'when name has escaped HTML' do
      let(:new_name) { 'tag->test' }

      it 'updates protected tag name with unescaped HTML' do
        expect(result.reload.name).to eq('tag->test')
      end

      context 'and name contains HTML tags' do
        let(:new_name) { '<b>tag</b>' }

        it 'updates protected tag name with sanitized name' do
          expect(result.reload.name).to eq('tag')
        end

        context 'and contains unsafe HTML' do
          let(:new_name) { '<script>alert('foo');</script>' }

          it 'does not update the protected tag' do
            expect(result.reload.name).to eq(protected_tag.name)
          end
        end
      end
    end

    context 'when name contains unescaped HTML tags' do
      let(:new_name) { '<b>tag</b>' }

      it 'updates protected tag name with sanitized name' do
        expect(result.reload.name).to eq('tag')
      end
    end

    context 'without admin_project permissions' do
      let(:user) { create(:user) }

      it "raises error" do
        expect { service.execute(protected_tag) }.to raise_error(Gitlab::Access::AccessDeniedError)
      end
    end
  end
end