Welcome to mirror list, hosted at ThFree Co, Russian Federation.

slash_commands_controller_spec.rb « integrations « projects « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3d61f882bdf02f0df3c5e25a57dfe24d9d71f2d3 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ::Projects::Integrations::SlashCommandsController, feature_category: :integrations do
  let_it_be(:project) { create(:project) }
  let_it_be(:user) { create(:user, developer_projects: [project]) }
  let_it_be(:chat_name) { create(:chat_name, user: user) }

  let(:params) do
    {
      command_id: 'command-id',
      integration: 'mattermost_slash_commands',
      team: 1,
      channel: 2,
      response_url: 'http://www.example.com'
    }
  end

  before do
    create(:mattermost_slash_commands_integration, project: project)
  end

  describe 'GET #show' do
    context 'when user is signed in' do
      before do
        sign_in(user)
      end

      context 'when request is invalid' do
        it 'renders the "show" template with expired message' do
          get project_integrations_slash_commands_path(project), params: params

          expect(response).to have_gitlab_http_status(:ok)
          expect(response).to render_template(:show)
          expect(response.body).to include(
            'The slash command verification request has expired. Please run the command again.'
          )
        end
      end

      context 'when request is valid', :use_clean_rails_memory_store_caching do
        before do
          Rails.cache.write(
            "slash-command-requests:#{params[:command_id]}", { team_id: chat_name.team_id, user_id: chat_name.chat_id }
          )
          stub_request(:post, "http://www.example.com/").to_return(status: 200, body: 'ok')
        end

        context 'when user is valid' do
          it 'renders the "show" template with authorize button' do
            get project_integrations_slash_commands_path(project), params: params

            expect(response).to have_gitlab_http_status(:ok)
            expect(response).to render_template(:show)
            expect(response.body).to include('Authorize')
          end
        end

        context 'when user is invalid' do
          let(:chat_name) { create(:chat_name) }

          it 'renders the "show" template' do
            get project_integrations_slash_commands_path(project), params: params

            expect(response).to have_gitlab_http_status(:ok)
            expect(response).to render_template(:show)
            expect(response.body).to include('The slash command request is invalid.')
          end
        end
      end
    end

    context 'when user is not signed in' do
      it 'redirects with a status of 302' do
        get project_integrations_slash_commands_path(project), params: params

        expect(response).to have_gitlab_http_status(:redirect)
      end
    end
  end

  describe 'POST #confirm' do
    let(:params) { super().merge(redirect_url: 'http://www.example.com') }

    context 'when user is signed in' do
      before do
        sign_in(user)
      end

      context 'when request is invalid' do
        it 'renders the "show" template' do
          post confirm_project_integrations_slash_commands_path(project), params: params

          expect(response).to have_gitlab_http_status(:ok)
          expect(response).to render_template(:show)
          expect(response.body).to include('The slash command request is invalid.')
        end
      end

      context 'when request is valid', :use_clean_rails_memory_store_caching do
        before do
          Rails.cache.write(
            "slash-command-requests:#{params[:command_id]}", { team_id: chat_name.team_id, user_id: chat_name.chat_id }
          )
          stub_request(:post, "http://www.example.com/").to_return(status: 200, body: 'ok')
        end

        context 'when user is valid' do
          it 'redirects back to the integration' do
            post confirm_project_integrations_slash_commands_path(project), params: params

            expect(response).to have_gitlab_http_status(:redirect)
          end
        end

        context 'when user is invalid' do
          let(:chat_name) { create(:chat_name) }

          it 'renders the "show" template' do
            post confirm_project_integrations_slash_commands_path(project), params: params

            expect(response).to have_gitlab_http_status(:ok)
            expect(response).to render_template(:show)
            expect(response.body).to include('The slash command request is invalid.')
          end
        end
      end
    end

    context 'when user is not signed in' do
      it 'redirects with a status of 302' do
        post confirm_project_integrations_slash_commands_path(project), params: params

        expect(response).to have_gitlab_http_status(:redirect)
      end
    end
  end
end