File: edit_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 (92 lines) | stat: -rw-r--r-- 3,106 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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Editing of a machine learning model version', feature_category: :mlops do
  include GraphqlHelpers
  let_it_be(:model_version) { create(:ml_model_versions, :with_package, description: 'A **description**') }
  let_it_be(:project) { model_version.project }
  let_it_be(:current_user) { project.owner }
  let_it_be(:guest) { create(:user) }

  let(:model_id) { model_version.model.to_gid }
  let(:version) { model_version.version }
  let(:description) { 'A description' }
  let(:new_description) { 'A **new** description' }

  let(:edit_input) do
    { project_path: project.full_path, description: new_description, model_id: model_id, version: version }
  end

  let(:mutation) { graphql_mutation(:ml_model_version_edit, edit_input, nil, ['version']) }
  let(:mutation_response) { graphql_mutation_response(:ml_model_version_edit) }

  context 'when user is not allowed write changes' do
    before do
      allow(Ability).to receive(:allowed?).and_call_original
      allow(Ability).to receive(:allowed?)
                          .with(current_user, :write_model_registry, project)
                          .and_return(false)
    end

    it_behaves_like 'a mutation that returns a top-level access error'
  end

  context 'when the user is not part of the project' do
    it 'does not update description' do
      post_graphql_mutation(mutation, current_user: guest)
      expect { mutation }.to not_change { model_version.reload.description }
      expect(mutation_response).to be_nil
    end
  end

  context 'when the user is authenticated' do
    context 'when the model does not exist' do
      let(:model_id) { "gid://gitlab/Ml::Model/0" }

      it 'returns an error' do
        post_graphql_mutation(mutation, current_user: current_user)

        expect(mutation_response['errors']).to match_array(['Model not found'])
      end
    end

    context 'when the model exists' do
      it 'updates the model description' do
        post_graphql_mutation(mutation, current_user: current_user)

        expect(mutation_response['errors']).to be_empty

        model_version.reload
        expect(model_version.description).to eq(new_description)
      end
    end

    context 'when the model is not part of the project' do
      let(:project) { create(:project) }

      before do
        post_graphql_mutation(mutation, current_user: current_user)
      end

      it_behaves_like 'a mutation that returns a top-level access error'
    end

    context 'when the update service fails' do
      before do
        allow_next_instance_of(::Ml::ModelVersions::UpdateModelVersionService) do |instance|
          allow(instance).to receive(:execute).and_return(
            ServiceResponse.error(message: 'Model update failed')
          )
        end
      end

      it 'returns an error' do
        post_graphql_mutation(mutation, current_user: current_user)

        expect(mutation_response['errors']).to match_array(['Model update failed'])
        expect(mutation_response['modelVersion']).to be_nil
      end
    end
  end
end