1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe 'Set up Mattermost slash commands', :js do
6  describe 'user visits the mattermost slash command config page' do
7    include_context 'project service activation'
8
9    before do
10      stub_mattermost_setting(enabled: mattermost_enabled)
11      visit_project_integration('Mattermost slash commands')
12    end
13
14    context 'mattermost service is enabled' do
15      let(:mattermost_enabled) { true }
16
17      describe 'activation' do
18        let(:edit_path) { edit_project_service_path(project, :mattermost_slash_commands) }
19
20        include_examples 'user activates the Mattermost Slash Command integration'
21      end
22
23      it 'shows the add to mattermost button' do
24        expect(page).to have_link('Add to Mattermost')
25      end
26
27      it 'shows an explanation if user is a member of no teams' do
28        stub_teams(count: 0)
29
30        click_link 'Add to Mattermost'
31
32        expect(page).to have_content('You aren’t a member of any team on the Mattermost instance')
33        expect(page).to have_link('join a team', href: "#{Gitlab.config.mattermost.host}/select_team")
34      end
35
36      it 'shows an explanation if user is a member of 1 team' do
37        stub_teams(count: 1)
38
39        click_link 'Add to Mattermost'
40
41        expect(page).to have_content('The team where the slash commands will be used in')
42        expect(page).to have_content('This is the only available team that you are a member of.')
43      end
44
45      it 'shows a disabled prefilled select if user is a member of 1 team' do
46        teams = stub_teams(count: 1)
47
48        click_link 'Add to Mattermost'
49
50        team_name = teams.first['display_name']
51        select_element = find('#mattermost_team_id')
52        selected_option = select_element.find('option[selected]')
53
54        expect(select_element['disabled']).to eq("true")
55        expect(selected_option).to have_content(team_name.to_s)
56      end
57
58      it 'has a hidden input for the prefilled value if user is a member of 1 team' do
59        teams = stub_teams(count: 1)
60
61        click_link 'Add to Mattermost'
62
63        expect(find('input#mattermost_team_id', visible: false).value).to eq(teams.first['id'])
64      end
65
66      it 'shows an explanation user is a member of multiple teams' do
67        stub_teams(count: 2)
68
69        click_link 'Add to Mattermost'
70
71        expect(page).to have_content('Select the team where the slash commands will be used in')
72        expect(page).to have_content('The list shows all available teams that you are a member of.')
73      end
74
75      it 'shows a select with team options user is a member of multiple teams' do
76        stub_teams(count: 2)
77
78        click_link 'Add to Mattermost'
79
80        select_element = find('#mattermost_team_id')
81
82        expect(select_element['disabled']).to be_falsey
83        expect(select_element.all('option').count).to eq(3)
84      end
85
86      it 'shows an error alert with the error message if there is an error requesting teams' do
87        allow_any_instance_of(Integrations::MattermostSlashCommands).to receive(:list_teams) { [[], 'test mattermost error message'] }
88
89        click_link 'Add to Mattermost'
90
91        expect(page).to have_selector('.gl-alert')
92        expect(page).to have_content('test mattermost error message')
93      end
94
95      it 'enables the submit button if the required fields are provided', :js do
96        stub_teams(count: 1)
97
98        click_link 'Add to Mattermost'
99
100        expect(find('input[type="submit"]')['disabled']).not_to eq("true")
101      end
102
103      it 'disables the submit button if the required fields are not provided', :js do
104        stub_teams(count: 1)
105
106        click_link 'Add to Mattermost'
107
108        fill_in('mattermost_trigger', with: '')
109
110        expect(find('input[type="submit"]')['disabled']).to eq("true")
111      end
112
113      def stub_teams(count: 0)
114        teams = create_teams(count)
115
116        allow_any_instance_of(Integrations::MattermostSlashCommands).to receive(:list_teams) { [teams, nil] }
117
118        teams
119      end
120
121      def create_teams(count = 0)
122        teams = []
123
124        count.times do |i|
125          teams.push({ "id" => "x#{i}", "display_name" => "x#{i}-name" })
126        end
127
128        teams
129      end
130    end
131
132    context 'mattermost service is not enabled' do
133      let(:mattermost_enabled) { false }
134
135      it 'shows the correct trigger url' do
136        value = find_field('request_url').value
137
138        expect(value).to match("api/v4/projects/#{project.id}/services/mattermost_slash_commands/trigger")
139      end
140
141      it 'shows a token placeholder' do
142        token_placeholder = find_field('service_token')['placeholder']
143
144        expect(token_placeholder).to eq('XXxxXXxxXXxxXXxxXXxxXXxx')
145      end
146    end
147  end
148
149  describe 'stable logo url' do
150    it 'shows a publicly available logo' do
151      expect(File.exist?(Rails.root.join('public/slash-command-logo.png'))).to be_truthy
152    end
153  end
154end
155