summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/fix_projects_without_project_feature_spec.rb
blob: e2175c415132c9de7aaeff952a70f6749b8c490d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::FixProjectsWithoutProjectFeature, schema: 2020_01_27_111840 do
  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:project_features) { table(:project_features) }

  let(:namespace) { namespaces.create(name: 'foo', path: 'foo') }

  let!(:project) { projects.create!(namespace_id: namespace.id) }
  let(:private_project_without_feature) { projects.create!(namespace_id: namespace.id, visibility_level: 0) }
  let(:public_project_without_feature) { projects.create!(namespace_id: namespace.id, visibility_level: 20) }
  let!(:projects_without_feature) { [private_project_without_feature, public_project_without_feature] }

  before do
    project_features.create({ project_id: project.id, pages_access_level: 20 })
  end

  subject { described_class.new.perform(Project.minimum(:id), Project.maximum(:id)) }

  def project_feature_records
    project_features.order(:project_id).pluck(:project_id)
  end

  def features(project)
    project_features.find_by(project_id: project.id)&.attributes
  end

  it 'creates a ProjectFeature for projects without it' do
    expect { subject }.to change { project_feature_records }.from([project.id]).to([project.id, *projects_without_feature.map(&:id)])
  end

  it 'creates ProjectFeature records with default values for a public project' do
    subject

    expect(features(public_project_without_feature)).to include(
      {
        "merge_requests_access_level" => 20,
        "issues_access_level" => 20,
        "wiki_access_level" => 20,
        "snippets_access_level" => 20,
        "builds_access_level" => 20,
        "repository_access_level" => 20,
        "pages_access_level" => 20,
        "forking_access_level" => 20
      }
    )
  end

  it 'creates ProjectFeature records with default values for a private project' do
    subject

    expect(features(private_project_without_feature)).to include("pages_access_level" => 10)
  end

  context 'when access control to pages is forced' do
    before do
      allow(::Gitlab::Pages).to receive(:access_control_is_forced?).and_return(true)
    end

    it 'creates ProjectFeature records with default values for a public project' do
      subject

      expect(features(public_project_without_feature)).to include("pages_access_level" => 10)
    end
  end

  it 'sets created_at/updated_at timestamps' do
    subject

    expect(project_features.where('created_at IS NULL OR updated_at IS NULL')).to be_empty
  end
end