summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/static_site_editor/config_spec.rb
blob: 56cdb5737856104efa4e31abb11af6b32dc81367 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::StaticSiteEditor::Config do
  subject(:config) { described_class.new(repository, ref, file_path, return_url) }

  let_it_be(:namespace) { create(:namespace, name: 'namespace') }
  let_it_be(:root_group) { create(:group, name: 'group') }
  let_it_be(:subgroup) { create(:group, name: 'subgroup', parent: root_group) }
  let_it_be(:project) { create(:project, :public, :repository, name: 'project', namespace: namespace) }
  let_it_be(:project_with_subgroup) { create(:project, :public, :repository, name: 'project', group: subgroup) }
  let_it_be(:repository) { project.repository }

  let(:ref) { 'master' }
  let(:file_path) { 'README.md' }
  let(:return_url) { 'http://example.com' }

  describe '#payload' do
    subject { config.payload }

    it 'returns data for the frontend component' do
      is_expected.to eq(
        branch: 'master',
        commit_id: repository.commit.id,
        namespace: 'namespace',
        path: 'README.md',
        project: 'project',
        project_id: project.id,
        return_url: 'http://example.com',
        is_supported_content: 'true',
        base_url: '/namespace/project/-/sse/master%2FREADME.md'
      )
    end

    context 'when namespace is a subgroup' do
      let(:repository) { project_with_subgroup.repository }

      it 'returns data for the frontend component' do
        is_expected.to include(
          namespace: 'group/subgroup',
          project: 'project',
          base_url: '/group/subgroup/project/-/sse/master%2FREADME.md'
        )
      end
    end

    context 'when file has .md.erb extension' do
      before do
        repository.create_file(
          project.creator,
          file_path,
          '',
          message: 'message',
          branch_name: 'master'
        )
      end

      context 'when feature flag is enabled' do
        let(:file_path) { 'FEATURE_ON.md.erb' }

        before do
          stub_feature_flags(sse_erb_support: project)
        end

        it { is_expected.to include(is_supported_content: 'true') }
      end

      context 'when feature flag is disabled' do
        let(:file_path) { 'FEATURE_OFF.md.erb' }

        before do
          stub_feature_flags(sse_erb_support: false)
        end

        it { is_expected.to include(is_supported_content: 'false') }
      end
    end

    context 'when file path is nested' do
      let(:file_path) { 'lib/README.md' }

      it { is_expected.to include(base_url: '/namespace/project/-/sse/master%2Flib%2FREADME.md') }
    end

    context 'when branch is not master' do
      let(:ref) { 'my-branch' }

      it { is_expected.to include(is_supported_content: 'false') }
    end

    context 'when file does not have a markdown extension' do
      let(:file_path) { 'README.txt' }

      it { is_expected.to include(is_supported_content: 'false') }
    end

    context 'when file does not have an extension' do
      let(:file_path) { 'README' }

      it { is_expected.to include(is_supported_content: 'false') }
    end

    context 'when file does not exist' do
      let(:file_path) { 'UNKNOWN.md' }

      it { is_expected.to include(is_supported_content: 'false') }
    end

    context 'when repository is empty' do
      let(:repository) { create(:project_empty_repo).repository }

      it { is_expected.to include(is_supported_content: 'false') }
    end

    context 'when return_url is not a valid URL' do
      let(:return_url) { 'example.com' }

      it { is_expected.to include(return_url: nil) }
    end

    context 'when return_url has a javascript scheme' do
      let(:return_url) { 'javascript:alert(document.domain)' }

      it { is_expected.to include(return_url: nil) }
    end

    context 'when return_url is missing' do
      let(:return_url) { nil }

      it { is_expected.to include(return_url: nil) }
    end
  end
end