summaryrefslogtreecommitdiff
path: root/spec/features/projects/services/user_activates_mattermost_slash_command_spec.rb
blob: b2906e315f70c3ee9b4c8574796e46f44f25b382 (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
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
require 'spec_helper'

feature 'Setup Mattermost slash commands', :js do
  let(:user) { create(:user) }
  let(:project) { create(:project) }
  let(:service) { project.create_mattermost_slash_commands_service }
  let(:mattermost_enabled) { true }

  before do
    stub_mattermost_setting(enabled: mattermost_enabled)
    project.add_master(user)
    sign_in(user)
    visit edit_project_service_path(project, service)
  end

  describe 'user visits the mattermost slash command config page' do
    it 'shows a help message' do
      expect(page).to have_content("This service allows users to perform common")
    end

    it 'shows a token placeholder' do
      token_placeholder = find_field('service_token')['placeholder']

      expect(token_placeholder).to eq('XXxxXXxxXXxxXXxxXXxxXXxx')
    end

    it 'redirects to the integrations page after saving but not activating' do
      token = ('a'..'z').to_a.join

      fill_in 'service_token', with: token
      click_on 'Save changes'

      expect(current_path).to eq(project_settings_integrations_path(project))
      expect(page).to have_content('Mattermost slash commands settings saved, but not activated.')
    end

    it 'redirects to the integrations page after activating' do
      token = ('a'..'z').to_a.join

      fill_in 'service_token', with: token
      check 'service_active'
      click_on 'Save changes'

      expect(current_path).to eq(project_settings_integrations_path(project))
      expect(page).to have_content('Mattermost slash commands activated.')
    end

    it 'shows the add to mattermost button' do
      expect(page).to have_link('Add to Mattermost')
    end

    it 'shows an explanation if user is a member of no teams' do
      stub_teams(count: 0)

      click_link 'Add to Mattermost'

      expect(page).to have_content('You aren’t a member of any team on the Mattermost instance')
      expect(page).to have_link('join a team', href: "#{Gitlab.config.mattermost.host}/select_team")
    end

    it 'shows an explanation if user is a member of 1 team' do
      stub_teams(count: 1)

      click_link 'Add to Mattermost'

      expect(page).to have_content('The team where the slash commands will be used in')
      expect(page).to have_content('This is the only available team.')
    end

    it 'shows a disabled prefilled select if user is a member of 1 team' do
      teams = stub_teams(count: 1)

      click_link 'Add to Mattermost'

      team_name = teams.first['display_name']
      select_element = find('#mattermost_team_id')
      selected_option = select_element.find('option[selected]')

      expect(select_element['disabled']).to eq("true")
      expect(selected_option).to have_content(team_name.to_s)
    end

    it 'has a hidden input for the prefilled value if user is a member of 1 team' do
      teams = stub_teams(count: 1)

      click_link 'Add to Mattermost'

      expect(find('input#mattermost_team_id', visible: false).value).to eq(teams.first['id'])
    end

    it 'shows an explanation user is a member of multiple teams' do
      stub_teams(count: 2)

      click_link 'Add to Mattermost'

      expect(page).to have_content('Select the team where the slash commands will be used in')
      expect(page).to have_content('The list shows all available teams.')
    end

    it 'shows a select with team options user is a member of multiple teams' do
      stub_teams(count: 2)

      click_link 'Add to Mattermost'

      select_element = find('#mattermost_team_id')

      expect(select_element['disabled']).to be_falsey
      expect(select_element.all('option').count).to eq(3)
    end

    it 'shows an error alert with the error message if there is an error requesting teams' do
      allow_any_instance_of(MattermostSlashCommandsService).to receive(:list_teams) { [[], 'test mattermost error message'] }

      click_link 'Add to Mattermost'

      expect(page).to have_selector('.alert')
      expect(page).to have_content('test mattermost error message')
    end

    it 'enables the submit button if the required fields are provided', :js do
      stub_teams(count: 1)

      click_link 'Add to Mattermost'

      expect(find('input[type="submit"]')['disabled']).not_to eq("true")
    end

    it 'disables the submit button if the required fields are not provided', :js do
      stub_teams(count: 1)

      click_link 'Add to Mattermost'

      fill_in('mattermost_trigger', with: '')

      expect(find('input[type="submit"]')['disabled']).to eq("true")
    end

    def stub_teams(count: 0)
      teams = create_teams(count)

      allow_any_instance_of(MattermostSlashCommandsService).to receive(:list_teams) { [teams, nil] }

      teams
    end

    def create_teams(count = 0)
      teams = []

      count.times do |i|
        teams.push({ "id" => "x#{i}", "display_name" => "x#{i}-name" })
      end

      teams
    end

    describe 'mattermost service is not enabled' do
      let(:mattermost_enabled) { false }

      it 'shows the correct trigger url' do
        value = find_field('request_url').value

        expect(value).to match("api/v4/projects/#{project.id}/services/mattermost_slash_commands/trigger")
      end

      it 'shows a token placeholder' do
        token_placeholder = find_field('service_token')['placeholder']

        expect(token_placeholder).to eq('XXxxXXxxXXxxXXxxXXxxXXxx')
      end
    end
  end

  describe 'stable logo url' do
    it 'shows a publicly available logo' do
      expect(File.exist?(Rails.root.join('public/slash-command-logo.png')))
    end
  end
end