File: web_commits_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (77 lines) | stat: -rw-r--r-- 2,640 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::WebCommits, :clean_gitlab_redis_cache, feature_category: :source_code_management do
  describe 'GET /web_commits/public_key' do
    context 'when Gitaly is available' do
      let(:public_key) { 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFcykDaUT7x4oXyUCfgqJhfAXRbhtsLl4fi4142zrPCI' }

      before do
        allow_next_instance_of(::Gitlab::GitalyClient::ServerService) do |instance|
          allow(instance).to receive_message_chain(:server_signature, :public_key).and_return(public_key)
        end
      end

      context 'and the public key is not found' do
        let(:public_key) { '' }

        it 'returns not found' do
          get api('/web_commits/public_key')

          expect(response).to have_gitlab_http_status(:not_found)
          expect(json_response['message']).to eq('Public key not found.')
        end

        it 'does not cache the public key' do
          expect(Rails.cache).not_to receive(:fetch).with(
            described_class::GITALY_PUBLIC_KEY_CACHE_KEY, expires_in: 1.hour.to_i, skip_nil: true
          ).and_call_original

          get api('/web_commits/public_key')
        end
      end

      context 'and the public key is found' do
        it 'returns the public key' do
          get api('/web_commits/public_key')

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['public_key']).to eq(public_key)
        end

        it 'caches the public key' do
          expect(Rails.cache).to receive(:fetch).with(
            described_class::GITALY_PUBLIC_KEY_CACHE_KEY, expires_in: 1.hour.to_i, skip_nil: true
          ).and_call_original

          get api('/web_commits/public_key')
        end
      end
    end

    context 'when Gitaly is unavailable' do
      before do
        allow_next_instance_of(::Gitlab::GitalyClient::ServerService) do |instance|
          allow(instance).to receive(:server_signature).and_raise(GRPC::Unavailable)
        end
      end

      it 'does not cache the public key' do
        expect(Rails.cache).not_to receive(:fetch).with(
          described_class::GITALY_PUBLIC_KEY_CACHE_KEY, expires_in: 1.hour.to_i, skip_nil: true
        ).and_call_original

        get api('/web_commits/public_key')
      end

      it 'returns service unavailable' do
        get api('/web_commits/public_key')

        expect(response).to have_gitlab_http_status(:service_unavailable)
        expect(json_response['message'])
          .to eq('The git server, Gitaly, is not available at this time. Please contact your administrator.')
      end
    end
  end
end