summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/registry/repositories_controller_spec.rb
blob: badb84f9b503362370c927e877d3832e71616ea1 (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
# frozen_string_literal: true

require 'spec_helper'

describe Projects::Registry::RepositoriesController do
  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project, :private) }

  before do
    sign_in(user)
    stub_container_registry_config(enabled: true)
  end

  context 'when user has access to registry' do
    before do
      project.add_developer(user)
    end

    shared_examples 'with name parameter' do
      let_it_be(:repo) { create(:container_repository, project: project, name: 'my_searched_image') }
      let_it_be(:another_repo) { create(:container_repository, project: project, name: 'bar') }

      it 'returns the searched repo' do
        go_to_index(format: :json, params: { name: 'my_searched_image' })

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response.length).to eq 1
        expect(json_response.first).to include(
          'id' => repo.id,
          'name' => repo.name
        )
      end
    end

    shared_examples 'renders a list of repositories' do
      context 'when root container repository exists' do
        before do
          create(:container_repository, :root, project: project)
        end

        it 'does not create root container repository' do
          expect { go_to_index }.not_to change { ContainerRepository.all.count }
        end
      end

      context 'when root container repository is not created' do
        context 'when there are tags for this repository' do
          before do
            stub_container_registry_tags(repository: project.full_path,
                                         tags: %w[rc1 latest])
          end

          it 'successfully renders container repositories' do
            expect(Gitlab::Tracking).not_to receive(:event)

            go_to_index

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

          it 'tracks the event' do
            expect(Gitlab::Tracking).to receive(:event).with(anything, 'list_repositories', {})

            go_to_index(format: :json)
          end

          it 'creates a root container repository' do
            expect { go_to_index }.to change { ContainerRepository.all.count }.by(1)
            expect(ContainerRepository.first).to be_root_repository
          end

          it 'json has a list of projects' do
            go_to_index(format: :json)

            expect(response).to have_gitlab_http_status(:ok)
            expect(response).to match_response_schema('registry/repositories')
            expect(response).to include_pagination_headers
          end

          it_behaves_like 'with name parameter'
        end

        context 'when there are no tags for this repository' do
          before do
            stub_container_registry_tags(repository: :any, tags: [])
          end

          it 'successfully renders container repositories' do
            go_to_index

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

          it 'does not ensure root container repository' do
            expect { go_to_index }.not_to change { ContainerRepository.all.count }
          end

          it 'responds with json if asked' do
            go_to_index(format: :json)

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response).to be_kind_of(Array)
          end
        end
      end
    end

    describe 'GET #index' do
      it_behaves_like 'renders a list of repositories'
    end

    describe 'GET #show' do
      it_behaves_like 'renders a list of repositories'
    end

    describe 'DELETE #destroy' do
      context 'when root container repository exists' do
        let!(:repository) do
          create(:container_repository, :root, project: project)
        end

        before do
          stub_container_registry_tags(repository: :any, tags: [])
        end

        it 'schedules a job to delete a repository' do
          expect(DeleteContainerRepositoryWorker).to receive(:perform_async).with(user.id, repository.id)

          delete_repository(repository)

          expect(repository.reload).to be_delete_scheduled
          expect(response).to have_gitlab_http_status(:no_content)
        end

        it 'tracks the event' do
          expect(Gitlab::Tracking).to receive(:event).with(anything, 'delete_repository', {})
          allow(DeleteContainerRepositoryWorker).to receive(:perform_async).with(user.id, repository.id)

          delete_repository(repository)
        end
      end
    end
  end

  context 'when user does not have access to registry' do
    describe 'GET #index' do
      it 'responds with 404' do
        go_to_index

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

      it 'does not ensure root container repository' do
        expect { go_to_index }.not_to change { ContainerRepository.all.count }
      end
    end
  end

  def go_to_index(format: :html, params: {} )
    get :index, params: params.merge({
                  namespace_id: project.namespace,
                  project_id: project
                }),
                format: format
  end

  def delete_repository(repository)
    delete :destroy, params: {
                       namespace_id: project.namespace,
                       project_id: project,
                       id: repository
                     },
                     format: :json
  end
end