summaryrefslogtreecommitdiff
path: root/spec/requests/api/container_registry_event_spec.rb
blob: e83a3faae938838ba0c0aa5927aac7f0fa15d2e6 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::ContainerRegistryEvent, feature_category: :container_registry do
  let(:secret_token) { 'secret_token' }
  let(:events) { [{ action: 'push' }, { action: 'pull' }, { action: 'mount' }] }
  let(:registry_headers) { { 'Content-Type' => ::API::ContainerRegistryEvent::DOCKER_DISTRIBUTION_EVENTS_V1_JSON } }

  describe 'POST /container_registry_event/events' do
    before do
      allow(Gitlab.config.registry).to receive(:notification_secret) { secret_token }
    end

    subject(:post_events) do
      post api('/container_registry_event/events'),
           params: { events: events }.to_json,
           headers: registry_headers.merge('Authorization' => secret_token)
    end

    it 'returns 200 status and events are passed to event handler' do
      allow_next_instance_of(::ContainerRegistry::Event) do |event|
        if event.supported?
          expect(event).to receive(:handle!).once
          expect(event).to receive(:track!).once
        end
      end

      post_events

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'returns 401 error status when token is invalid' do
      post api('/container_registry_event/events'),
           params: { events: events }.to_json,
           headers: registry_headers.merge('Authorization' => 'invalid_token')

      expect(response).to have_gitlab_http_status(:unauthorized)
    end

    context 'when the event should update project statistics' do
      let_it_be(:project) { create(:project) }

      let(:events) do
        [
          {
            action: 'push',
            target: {
              tag: 'latest',
              repository: project.full_path
            }
          },
          {
            action: 'delete',
            target: {
              tag: 'latest',
              repository: project.full_path
            }
          }
        ]
      end

      it 'enqueues a project statistics update twice' do
        expect(ProjectCacheWorker)
          .to receive(:perform_async)
          .with(project.id, [], [:container_registry_size])
          .twice.and_call_original

        expect { post_events }.to change { ProjectCacheWorker.jobs.size }.from(0).to(1)
      end
    end
  end
end