summaryrefslogtreecommitdiff
path: root/spec/serializers/service_field_entity_spec.rb
blob: 20ca98416f88c07f2be53e85fb40cc2e6a7e8f3a (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ServiceFieldEntity do
  let(:request) { double('request') }

  subject { described_class.new(field, request: request, service: service).as_json }

  before do
    allow(request).to receive(:service).and_return(service)
  end

  describe '#as_json' do
    context 'Jira Service' do
      let(:service) { create(:jira_service) }

      context 'field with type text' do
        let(:field) { service.global_fields.find { |field| field[:name] == 'username' } }

        it 'exposes correct attributes' do
          expected_hash = {
            type: 'text',
            name: 'username',
            title: 'Username or Email',
            placeholder: nil,
            help: 'Use a username for server version and an email for cloud version.',
            required: true,
            choices: nil,
            value: 'jira_username'
          }

          is_expected.to eq(expected_hash)
        end
      end

      context 'field with type password' do
        let(:field) { service.global_fields.find { |field| field[:name] == 'password' } }

        it 'exposes correct attributes but hides password' do
          expected_hash = {
            type: 'password',
            name: 'password',
            title: 'Enter new password or API token',
            placeholder: nil,
            help: 'Leave blank to use your current password or API token.',
            required: true,
            choices: nil,
            value: 'true'
          }

          is_expected.to eq(expected_hash)
        end
      end
    end

    context 'EmailsOnPush Service' do
      let(:integration) { create(:emails_on_push_integration, send_from_committer_email: '1') }
      let(:service) { integration } # TODO: remove when https://gitlab.com/gitlab-org/gitlab/-/issues/330300 is complete

      context 'field with type checkbox' do
        let(:field) { integration.global_fields.find { |field| field[:name] == 'send_from_committer_email' } }

        it 'exposes correct attributes and casts value to Boolean' do
          expected_hash = {
            type: 'checkbox',
            name: 'send_from_committer_email',
            title: 'Send from committer',
            placeholder: nil,
            required: nil,
            choices: nil,
            value: 'true'
          }

          is_expected.to include(expected_hash)
          expect(subject[:help]).to include("Send notifications from the committer's email address if the domain matches the domain used by your GitLab instance")
        end
      end

      context 'field with type select' do
        let(:field) { integration.global_fields.find { |field| field[:name] == 'branches_to_be_notified' } }

        it 'exposes correct attributes' do
          expected_hash = {
            type: 'select',
            name: 'branches_to_be_notified',
            title: nil,
            placeholder: nil,
            required: nil,
            choices: [['All branches', 'all'], ['Default branch', 'default'], ['Protected branches', 'protected'], ['Default branch and protected branches', 'default_and_protected']],
            help: nil,
            value: nil
          }

          is_expected.to eq(expected_hash)
        end
      end
    end
  end
end