summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/changelog/config_spec.rb
blob: c410ba4d1167ddac5607c7bba87c8b9a756dcb54 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Changelog::Config do
  include ProjectForksHelper

  let(:project) { build_stubbed(:project) }

  describe '.from_git' do
    it 'retrieves the configuration from Git' do
      allow(project.repository)
        .to receive(:changelog_config)
        .and_return("---\ndate_format: '%Y'")

      expect(described_class)
        .to receive(:from_hash)
        .with(project, { 'date_format' => '%Y' }, nil)

      described_class.from_git(project)
    end

    it 'returns the default configuration when no YAML file exists in Git' do
      allow(project.repository)
        .to receive(:changelog_config)
        .and_return(nil)

      expect(described_class)
        .to receive(:new)
        .with(project)

      described_class.from_git(project)
    end
  end

  describe '.from_hash' do
    it 'sets the configuration according to a Hash' do
      user1 = create(:user)
      user2 = create(:user)
      user3 = create(:user)
      group = create(:group, path: 'group')
      group2 = create(:group, path: 'group-path')
      group.add_developer(user1)
      group.add_developer(user2)
      group2.add_developer(user3)

      config = described_class.from_hash(
        project,
        {
          'date_format' => 'foo',
          'template' => 'bar',
          'categories' => { 'foo' => 'bar' },
          'tag_regex' => 'foo',
          'include_groups' => %w[group group-path non-existent-group]
        },
        user1
      )

      expect(config.date_format).to eq('foo')
      expect(config.template)
        .to be_instance_of(Gitlab::TemplateParser::AST::Expressions)

      expect(config.categories).to eq({ 'foo' => 'bar' })
      expect(config.tag_regex).to eq('foo')
      expect(config.always_credit_user_ids).to match_array([user1.id, user2.id, user3.id])
    end

    it 'raises Error when the categories are not a Hash' do
      expect { described_class.from_hash(project, 'categories' => 10) }
        .to raise_error(Gitlab::Changelog::Error)
    end

    it 'raises a Gitlab::Changelog::Error when the template is invalid' do
      invalid_template = <<~TPL
        {% each {{foo}} %}
        {% end %}
      TPL

      expect { described_class.from_hash(project, 'template' => invalid_template) }
        .to raise_error(Gitlab::Changelog::Error)
    end
  end

  describe '#contributor?' do
    let(:project) { create(:project, :public, :repository) }

    context 'when user is a member of project' do
      let(:user) { create(:user) }

      before do
        project.add_developer(user)
      end

      it { expect(described_class.new(project).contributor?(user)).to eq(false) }
    end

    context 'when user has at least one merge request merged into default_branch' do
      let(:contributor) { create(:user) }
      let(:user_without_access) { create(:user) }
      let(:user_fork) { fork_project(project, contributor, repository: true) }

      before do
        create(:merge_request, :merged,
               author: contributor,
               target_project: project,
               source_project: user_fork,
               target_branch: project.default_branch.to_s)
      end

      it { expect(described_class.new(project).contributor?(contributor)).to eq(true) }
      it { expect(described_class.new(project).contributor?(user_without_access)).to eq(false) }
    end
  end

  describe '#category' do
    it 'returns the name of a category' do
      config = described_class.new(project)

      config.categories['foo'] = 'Foo'

      expect(config.category('foo')).to eq('Foo')
    end

    it 'returns the raw category name when no alternative name is configured' do
      config = described_class.new(project)

      expect(config.category('bla')).to eq('bla')
    end
  end

  describe '#format_date' do
    it 'formats a date according to the configured date format' do
      config = described_class.new(project)
      time = Time.utc(2021, 1, 5)

      expect(config.format_date(time)).to eq('2021-01-05')
    end
  end

  describe '#always_credit_author?' do
    let_it_be(:group_member) { create(:user) }
    let_it_be(:non_group_member) { create(:user) }
    let_it_be(:group) { create(:group, :private, path: 'group') }

    before do
      group.add_developer(group_member)
    end

    context 'when include_groups is defined' do
      context 'when user generating changelog has access to group' do
        it 'returns whether author should always be credited' do
          config = described_class.from_hash(
            project,
            { 'include_groups' => ['group'] },
            group_member
          )

          expect(config.always_credit_author?(group_member)).to eq(true)
          expect(config.always_credit_author?(non_group_member)).to eq(false)
        end
      end

      context 'when user generating changelog has no access to group' do
        it 'always returns false' do
          config = described_class.from_hash(
            project,
            { 'include_groups' => ['group'] },
            non_group_member
          )

          expect(config.always_credit_author?(group_member)).to eq(false)
          expect(config.always_credit_author?(non_group_member)).to eq(false)
        end
      end
    end

    context 'when include_groups is not defined' do
      it 'always returns false' do
        config = described_class.from_hash(
          project,
          {},
          group_member
        )

        expect(config.always_credit_author?(group_member)).to eq(false)
        expect(config.always_credit_author?(non_group_member)).to eq(false)
      end
    end
  end
end