summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/environments_controller_spec.rb
blob: 7ac1d62d1b1a63ddee9dae16289ddba4d062c752 (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
require 'spec_helper'

describe Projects::EnvironmentsController do
  include ApiHelpers

  let(:environment) { create(:environment) }
  let(:project)     { environment.project }
  let(:user)        { create(:user) }

  before do
    project.team << [user, :master]

    sign_in(user)
  end

  describe 'GET index' do
    context 'when standardrequest has been made' do
      it 'responds with status code 200' do
        get :index, environment_params

        expect(response).to be_ok
      end
    end

    context 'when requesting JSON response' do
      it 'responds with correct JSON' do
        get :index, environment_params(format: :json)

        first_environment = json_response.first

        expect(first_environment).not_to be_empty
        expect(first_environment['name']). to eq environment.name
      end
    end
  end

  describe 'GET show' do
    context 'with valid id' do
      it 'responds with a status code 200' do
        get :show, environment_params

        expect(response).to be_ok
      end
    end

    context 'with invalid id' do
      it 'responds with a status code 404' do
        params = environment_params
        params[:id] = 12345
        get :show, params

        expect(response).to have_http_status(404)
      end
    end
  end

  describe 'GET edit' do
    it 'responds with a status code 200' do
      get :edit, environment_params

      expect(response).to be_ok
    end
  end

  describe 'PATCH #update' do
    it 'responds with a 302' do
      patch_params = environment_params.merge(environment: { external_url: 'https://git.gitlab.com' })
      patch :update, patch_params

      expect(response).to have_http_status(302)
    end
  end

  describe 'GET #terminal' do
    context 'with valid id' do
      it 'responds with a status code 200' do
        get :terminal, environment_params

        expect(response).to have_http_status(200)
      end

      it 'loads the terminals for the enviroment' do
        expect_any_instance_of(Environment).to receive(:terminals)

        get :terminal, environment_params
      end
    end

    context 'with invalid id' do
      it 'responds with a status code 404' do
        get :terminal, environment_params(id: 666)

        expect(response).to have_http_status(404)
      end
    end
  end

  describe 'GET #terminal_websocket_authorize' do
    context 'with valid workhorse signature' do
      before do
        allow(Gitlab::Workhorse).to receive(:verify_api_request!).and_return(nil)
      end

      context 'and valid id' do
        it 'returns the first terminal for the environment' do
          expect_any_instance_of(Environment).
            to receive(:terminals).
            and_return([:fake_terminal])

          expect(Gitlab::Workhorse).
            to receive(:terminal_websocket).
            with(:fake_terminal).
            and_return(workhorse: :response)

          get :terminal_websocket_authorize, environment_params

          expect(response).to have_http_status(200)
          expect(response.headers["Content-Type"]).to eq(Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE)
          expect(response.body).to eq('{"workhorse":"response"}')
        end
      end

      context 'and invalid id' do
        it 'returns 404' do
          get :terminal_websocket_authorize, environment_params(id: 666)

          expect(response).to have_http_status(404)
        end
      end
    end

    context 'with invalid workhorse signature' do
      it 'aborts with an exception' do
        allow(Gitlab::Workhorse).to receive(:verify_api_request!).and_raise(JWT::DecodeError)

        expect { get :terminal_websocket_authorize, environment_params }.to raise_error(JWT::DecodeError)
        # controller tests don't set the response status correctly. It's enough
        # to check that the action raised an exception
      end
    end
  end

  def environment_params(opts = {})
    opts.reverse_merge(namespace_id: project.namespace,
                       project_id: project,
                       id: environment.id)
  end
end