container_moved_spec.rb 4.6 KB
Newer Older
1 2
# frozen_string_literal: true

3
require 'spec_helper'
4

5
RSpec.describe Gitlab::Checks::ContainerMoved, :clean_gitlab_redis_shared_state do
6 7
  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project, :repository, :wiki_repo, namespace: user.namespace) }
8

9 10 11 12 13 14
  let(:repository) { project.repository }
  let(:protocol) { 'http' }
  let(:git_user) { user }
  let(:redirect_path) { 'foo/bar' }

  subject { described_class.new(repository, git_user, protocol, redirect_path) }
15

16
  describe '.fetch_message' do
17 18 19
    let(:key) { "redirect_namespace:#{user.id}:#{project.repository.gl_repository}" }
    let(:legacy_key) { "redirect_namespace:#{user.id}:#{project.id}" }

20
    context 'with a redirect message queue' do
21 22 23
      before do
        subject.add_message
      end
24

25
      it 'returns the redirect message' do
26
        expect(described_class.fetch_message(user, project.repository)).to eq(subject.message)
27 28
      end

Tiago Botelho's avatar
Tiago Botelho committed
29
      it 'deletes the redirect message from redis' do
30
        expect(Gitlab::Redis::SharedState.with { |redis| redis.get(key) }).not_to be_nil
31

32
        described_class.fetch_message(user, project.repository)
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        expect(Gitlab::Redis::SharedState.with { |redis| redis.get(key) }).to be_nil
      end

      context 'with a message in the legacy key' do
        before do
          Gitlab::Redis::SharedState.with do |redis|
            redis.set(legacy_key, 'legacy message')
          end
        end

        it 'returns and deletes the legacy message' do
          expect(Gitlab::Redis::SharedState.with { |redis| redis.get(key) }).not_to be_nil
          expect(Gitlab::Redis::SharedState.with { |redis| redis.get(legacy_key) }).not_to be_nil

          expect(described_class.fetch_message(user, project.repository)).to eq('legacy message')

          expect(Gitlab::Redis::SharedState.with { |redis| redis.get(key) }).to be_nil
          expect(Gitlab::Redis::SharedState.with { |redis| redis.get(legacy_key) }).to be_nil
        end
53 54 55 56
      end
    end

    context 'with no redirect message queue' do
Tiago Botelho's avatar
Tiago Botelho committed
57
      it 'returns nil' do
58
        expect(described_class.fetch_message(user, project.repository)).to be_nil
59 60 61 62
      end
    end
  end

63
  describe '#add_message' do
Tiago Botelho's avatar
Tiago Botelho committed
64
    it 'queues a redirect message' do
65
      expect(subject.add_message).to eq("OK")
66
    end
67

68 69
    context 'when user is nil' do
      let(:git_user) { nil }
70

71 72 73
      it 'handles anonymous clones' do
        expect(subject.add_message).to be_nil
      end
74
    end
75 76
  end

77
  describe '#message' do
78 79 80 81
    shared_examples 'errors per protocol' do
      shared_examples 'returns redirect message' do
        it do
          message = <<~MSG
82
            #{container_label} '#{redirect_path}' was moved to '#{repository.container.full_path}'.
83

84
            Please update your Git remote:
85

86 87
              git remote set-url origin #{url_to_repo}
          MSG
88 89 90 91 92 93 94 95 96 97 98 99 100

          expect(subject.message).to eq(message)
        end
      end

      context 'when protocol is http' do
        it_behaves_like 'returns redirect message' do
          let(:url_to_repo) { http_url_to_repo }
        end
      end

      context 'when protocol is ssh' do
        let(:protocol) { 'ssh' }
101

102 103 104 105 106 107 108 109
        it_behaves_like 'returns redirect message' do
          let(:url_to_repo) { ssh_url_to_repo }
        end
      end
    end

    context 'with project' do
      it_behaves_like 'errors per protocol' do
110
        let(:container_label) { 'Project' }
111 112 113 114 115 116 117
        let(:http_url_to_repo) { project.http_url_to_repo }
        let(:ssh_url_to_repo) { project.ssh_url_to_repo }
      end
    end

    context 'with wiki' do
      let(:repository) { project.wiki.repository }
118

119
      it_behaves_like 'errors per protocol' do
120
        let(:container_label) { 'Project wiki' }
121 122 123 124
        let(:http_url_to_repo) { project.wiki.http_url_to_repo }
        let(:ssh_url_to_repo) { project.wiki.ssh_url_to_repo }
      end
    end
125

126 127
    context 'with project snippet' do
      let_it_be(:snippet) { create(:project_snippet, :repository, project: project, author: user) }
128

129 130 131
      let(:repository) { snippet.repository }

      it_behaves_like 'errors per protocol' do
132
        let(:container_label) { 'Project snippet' }
133 134 135 136 137 138 139
        let(:http_url_to_repo) { snippet.http_url_to_repo }
        let(:ssh_url_to_repo) { snippet.ssh_url_to_repo }
      end
    end

    context 'with personal snippet' do
      let_it_be(:snippet) { create(:personal_snippet, :repository, author: user) }
140

141 142
      let(:repository) { snippet.repository }

143 144 145 146
      it_behaves_like 'errors per protocol' do
        let(:container_label) { 'Personal snippet' }
        let(:http_url_to_repo) { snippet.http_url_to_repo }
        let(:ssh_url_to_repo) { snippet.ssh_url_to_repo }
147
      end
148 149 150
    end
  end
end