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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Projects::Gcp::ArtifactRegistry::DockerImagesController, feature_category: :container_registry do
let_it_be(:project) { create(:project, :private) }
let(:user) { project.owner }
let(:gcp_project_id) { 'gcp_project_id' }
let(:gcp_location) { 'gcp_location' }
let(:gcp_ar_repository) { 'gcp_ar_repository' }
let(:gcp_wlif_url) { 'gcp_wlif_url' }
describe '#index' do
let(:service_response) { ServiceResponse.success(payload: dummy_client_payload) }
let(:service_double) do
instance_double('GoogleCloudPlatform::ArtifactRegistry::ListDockerImagesService')
end
subject(:get_index_page) do
get(
project_gcp_artifact_registry_docker_images_path(
project,
gcp_project_id: gcp_project_id,
gcp_location: gcp_location,
gcp_ar_repository: gcp_ar_repository,
gcp_wlif_url: gcp_wlif_url
)
)
end
before do
allow_next_instance_of(GoogleCloudPlatform::ArtifactRegistry::ListDockerImagesService) do |service|
allow(service).to receive(:execute).and_return(service_response)
end
end
shared_examples 'returning the error message' do |message|
it 'displays an error message' do
sign_in(user)
get_index_page
expect(response).to have_gitlab_http_status(:success)
expect(response.body).to include(message)
end
end
context 'when on saas', :saas do
it 'returns the images' do
sign_in(user)
get_index_page
expect(response).to have_gitlab_http_status(:success)
expect(response.body).to include('image@sha256:6a')
expect(response.body).to include('tag1')
expect(response.body).to include('tag2')
expect(response.body).to include('Prev')
expect(response.body).to include('Next')
expect(response.body).to include('https://location.pkg.dev/project/repo/image@sha256:6a')
end
context 'when the service returns an error response' do
let(:service_response) { ServiceResponse.error(message: 'boom') }
it_behaves_like 'returning the error message', 'boom'
end
%i[gcp_project_id gcp_location gcp_ar_repository gcp_wlif_url].each do |field|
context "when a gcp parameter #{field} is missing" do
let(field) { nil }
it 'redirects to setup page' do
sign_in(user)
get_index_page
expect(response).to redirect_to new_project_gcp_artifact_registry_setup_path(project)
end
end
end
context 'with the feature flag disabled' do
before do
stub_feature_flags(gcp_technical_demo: false)
end
it_behaves_like 'returning the error message', 'Feature flag disabled'
end
context 'with non private project' do
before do
allow_next_found_instance_of(Project) do |project|
allow(project).to receive(:private?).and_return(false)
end
end
it_behaves_like 'returning the error message', 'Can only run on private projects'
end
context 'with unauthorized user' do
let_it_be(:user) { create(:user) }
it 'returns success' do
sign_in(user)
get_index_page
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
context 'when not on saas' do
it_behaves_like 'returning the error message', "Can't run here"
end
def dummy_client_payload
{
images: [
{
built_at: '2023-11-30T23:23:11.980068941Z',
media_type: 'application/vnd.docker.distribution.manifest.v2+json',
name: 'projects/project/locations/location/repositories/repo/dockerImages/image@sha256:6a',
size_bytes: 2827903,
tags: %w[tag1 tag2],
updated_at: '2023-12-07T11:48:50.840751Z',
uploaded_at: '2023-12-07T11:48:47.598511Z',
uri: 'location.pkg.dev/project/repo/image@sha256:6a'
}
],
next_page_token: 'next_page_token'
}
end
end
end
|