File: text_replace_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 (124 lines) | stat: -rw-r--r-- 3,939 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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe "projectTextReplace", feature_category: :source_code_management do
  include GraphqlHelpers

  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:current_user) { create(:user, owner_of: project) }
  let_it_be(:repo) { project.repository }

  let(:project_path) { project.full_path }
  let(:mutation_params) { { project_path: project_path, replacements: replacements } }
  let(:mutation) { graphql_mutation(:project_text_replace, mutation_params) }
  let(:replacements) do
    %w[
      p455w0rd
      foo==>bar
      literal:MM/DD/YYYY==>YYYY-MM-DD
    ]
  end

  let(:literal_replacements) do
    %w[
      literal:p455w0rd
      literal:foo==>bar
      literal:MM/DD/YYYY==>YYYY-MM-DD
    ]
  end

  subject(:post_mutation) { post_graphql_mutation(mutation, current_user: current_user) }

  describe 'Replacing text' do
    it 'processes text redaction asynchoronously' do
      expect(Repositories::RewriteHistoryWorker).to receive(:perform_async).with(
        project_id: project.id, user_id: current_user.id, redactions: literal_replacements, blob_oids: []
      )

      post_mutation

      expect(graphql_mutation_response(:project_text_replace)['errors']).not_to be_present
    end
  end

  describe 'Invalid requests:' do
    context 'when the current_user is a maintainer' do
      let(:current_user) { create(:user, maintainer_of: project) }

      it_behaves_like 'a mutation on an unauthorized resource'
    end

    context 'when arg `projectPath` is invalid' do
      let(:project_path) { 'gid://Gitlab/User/1' }

      it 'returns an error' do
        post_mutation

        expect(graphql_errors).to include(a_hash_including('message' => <<~MESSAGE.strip))
          The resource that you are attempting to access does not exist or you don't have permission to perform this action
        MESSAGE
      end
    end

    context 'when arg `replacements` is nil' do
      let(:replacements) { nil }

      it 'returns an error' do
        post_mutation

        expect(graphql_errors).to include(a_hash_including('message' => <<~MESSAGE.strip))
          Variable $projectTextReplaceInput of type projectTextReplaceInput! was provided invalid value for replacements (Expected value to not be null)
        MESSAGE
      end
    end

    context 'when arg `replacements` is an empty list' do
      let(:replacements) { [] }

      it 'returns an error' do
        post_mutation

        expect(graphql_errors).to include(a_hash_including('message' => <<~MESSAGE))
          Argument 'replacements' on InputObject 'projectTextReplaceInput' is required. Expected type [String!]!
        MESSAGE
      end
    end

    context 'when arg `replacements` does not contain any valid strings' do
      let(:replacements) { ["", ""] }

      it 'returns an error' do
        post_mutation

        expect(graphql_errors).to include(a_hash_including('message' => <<~MESSAGE))
          Argument 'replacements' on InputObject 'projectTextReplaceInput' is required. Expected type [String!]!
        MESSAGE
      end
    end

    context 'when arg `replacements` includes a regex' do
      let(:replacements) { ['regex:\bdriver\b==>pilot'] }

      it 'returns an error' do
        post_mutation

        expect(graphql_errors).to include(a_hash_including('message' => <<~MESSAGE))
          Argument 'replacements' on InputObject 'projectTextReplaceInput' does not support 'regex:' or 'glob:' values.
        MESSAGE
      end
    end

    context 'when arg `replacements` includes a glob' do
      let(:replacements) { ['glob:**string**==>'] }

      it 'returns an error' do
        post_mutation

        expect(graphql_errors).to include(a_hash_including('message' => <<~MESSAGE))
          Argument 'replacements' on InputObject 'projectTextReplaceInput' does not support 'regex:' or 'glob:' values.
        MESSAGE
      end
    end
  end
end