File: models_controller_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 (205 lines) | stat: -rw-r--r-- 4,937 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::Ml::ModelsController, feature_category: :mlops do
  let_it_be(:project) { create(:project) }
  let_it_be(:user) { project.first_owner }
  let_it_be(:model1) { create(:ml_models, :with_versions, project: project) }

  let(:read_model_registry) { true }
  let(:write_model_registry) { true }

  let(:params) { {} }

  before do
    allow(Ability).to receive(:allowed?).and_call_original
    allow(Ability).to receive(:allowed?)
                        .with(user, :read_model_registry, project)
                        .and_return(read_model_registry)
    allow(Ability).to receive(:allowed?)
                        .with(user, :write_model_registry, project)
                        .and_return(write_model_registry)

    sign_in(user)
  end

  describe 'GET index' do
    subject(:index_request) do
      list_models
      response
    end

    it 'renders the template' do
      expect(index_request).to render_template('projects/ml/models/index')
    end

    context 'when user does not have access' do
      let(:read_model_registry) { false }

      it 'renders 404' do
        is_expected.to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe 'show' do
    let(:model_id) { model1.id }
    let(:request_project) { model1.project }

    subject(:show_request) do
      show_model
      response
    end

    before do
      show_request
    end

    it 'renders the template' do
      is_expected.to render_template('projects/ml/models/show')
    end

    it 'fetches the correct model' do
      show_request

      expect(assigns(:model)).to eq(model1)
    end

    context 'when model id does not exist' do
      let(:model_id) { non_existing_record_id }

      it { is_expected.to have_gitlab_http_status(:not_found) }
    end

    context 'when model project does not match project id' do
      let_it_be(:request_project) { create(:project) }

      it { is_expected.to have_gitlab_http_status(:not_found) }
    end

    context 'when user does not have access' do
      let(:read_model_registry) { false }

      it { is_expected.to have_gitlab_http_status(:not_found) }
    end
  end

  describe 'GET new' do
    subject(:create_model_request) do
      new_model
      response
    end

    before do
      create_model_request
    end

    it 'renders the template' do
      is_expected.to render_template('projects/ml/models/new')
    end

    context 'when user does not have access' do
      let(:write_model_registry) { false }

      it { is_expected.to have_gitlab_http_status(:not_found) }
    end
  end

  describe 'edit' do
    let(:model_id) { model1.id }
    let(:request_project) { model1.project }

    subject(:edit_request) do
      edit_model
      response
    end

    before do
      edit_request
    end

    it 'renders the template' do
      is_expected.to render_template('projects/ml/models/edit')
    end

    it 'fetches the correct model' do
      edit_request

      expect(assigns(:model)).to eq(model1)
    end

    context 'when model id does not exist' do
      let(:model_id) { non_existing_record_id }

      it { is_expected.to have_gitlab_http_status(:not_found) }
    end

    context 'when model project does not match project id' do
      let(:request_project) { create(:project) }

      it { is_expected.to have_gitlab_http_status(:not_found) }
    end

    context 'when user does not have access' do
      let(:write_model_registry) { false }

      it { is_expected.to have_gitlab_http_status(:not_found) }
    end
  end

  describe 'destroy' do
    let(:model_for_deletion) do
      create(:ml_models, project: project)
    end

    let(:model_id) { model_for_deletion.id }

    subject(:delete_request) do
      delete_model
      response
    end

    it 'deletes the model', :aggregate_failures do
      is_expected.to have_gitlab_http_status(:found)

      expect(flash[:notice]).to eq('Model removed')
      expect(response).to redirect_to("/#{project.full_path}/-/ml/models")
      expect { Ml::Model.find(id: model_id) }.to raise_error(ActiveRecord::RecordNotFound)
    end

    context 'when model does not exist' do
      let(:model_id) { non_existing_record_id }

      it { is_expected.to have_gitlab_http_status(:not_found) }
    end

    describe 'when user does not have write_model_registry rights' do
      let(:write_model_registry) { false }

      it { is_expected.to have_gitlab_http_status(:not_found) }
    end
  end

  private

  def list_models(new_params = nil)
    get project_ml_models_path(project), params: new_params || params
  end

  def show_model
    get project_ml_model_path(request_project, model_id)
  end

  def delete_model
    delete project_ml_model_path(project, model_id)
  end

  def new_model
    get new_project_ml_model_path(project)
  end

  def edit_model
    get edit_project_ml_model_path(request_project, model_id)
  end
end