File: cookie_setting_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 (95 lines) | stat: -rw-r--r-- 3,428 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
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rspec-parameterized'
require 'gitlab/dangerfiles/spec_helper'

require_relative '../../../tooling/danger/cookie_setting'
require_relative '../../../tooling/danger/project_helper'

RSpec.describe Tooling::Danger::CookieSetting, feature_category: :tooling do
  include_context 'with dangerfile'

  let(:fake_danger) { DangerSpecHelper.fake_danger }
  let(:fake_project_helper) { instance_double(Tooling::Danger::ProjectHelper) }
  let(:comment_text) { "\n#{described_class::SUGGESTION}" }
  let(:file_lines) { file_diff.map { |line| line.delete_prefix('+') } }

  before do
    allow(cookie_setting).to receive(:project_helper).and_return(fake_project_helper)
    allow(cookie_setting.project_helper).to receive(:file_lines).and_return(file_lines)
    allow(cookie_setting.helper).to receive(:added_files).and_return([filename])
    allow(cookie_setting.helper).to receive(:changed_lines).with(filename).and_return(file_diff)

    cookie_setting.define_singleton_method(:add_suggestions_for) do |filename|
      Tooling::Danger::CookieSetting.new(filename, context: self).suggest
    end
  end

  subject(:cookie_setting) { fake_danger.new(helper: fake_helper) }

  context 'for single line method call' do
    let(:file_diff) do
      <<~DIFF.split("\n")
        +    def index
        +      #{code}
        +
        +      render text: 'OK'
        +    end
      DIFF
    end

    context 'when file is a non-spec Ruby file' do
      let(:filename) { 'app/controllers/user_settings/active_sessions_controller.rb' }

      using RSpec::Parameterized::TableSyntax

      context 'when comment is expected' do
        where(:code) do
          [
            'cookies[:my_key] = true',
            'cookies["my_key"] = true',
            'cookies[\'my_key\'] = true',
            'cookies[:my_key] = { value: "nbd", expires: 1.year, domain: "example.com" }',
            'cookies.encrypted[:my_key] = true',
            'cookies.permanent[:my_key] = true',
            'cookies.signed[:my_key] = true',
            'cookies.signed.encrypted.permanent[:my_key] = true',
            'cookies[Example::Class::With::CONSTANT] = true',
            'cookies[Example::Class::With::CONSTANT] = { value: "nbd", expires: 1.year, domain: "example.com" }',
            'cookies.encrypted[Example::Class::With::CONSTANT] = { value: "nbd", domain: "example.com" }',
            'cookies.permanent[Example::Class::With::CONSTANT] = { value: "nbd", domain: "example.com" }',
            'cookies.signed[Example::Class::With::CONSTANT] = { value: "nbd", domain: "example.com" }'
          ]
        end

        with_them do
          specify do
            expect(cookie_setting).to receive(:markdown).with(comment_text.chomp, file: filename, line: 2)

            cookie_setting.add_suggestions_for(filename)
          end
        end
      end

      context 'when no comment is expected' do
        where(:code) do
          [
            'cookies[:my_cookie].blank?',
            'cookies.signed[Some::Class::With::CONSTANTS].blank?',
            'cookies[:my_cookie] == "true"',
            'cookies.encrypted[:my_cookie] += "true"'
          ]
        end

        with_them do
          specify do
            expect(cookie_setting).not_to receive(:markdown)

            cookie_setting.add_suggestions_for(filename)
          end
        end
      end
    end
  end
end