summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/static_site_editor_controller_spec.rb
blob: 26161b5fb5cbe6db8a29a5491446c480341c8622 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::StaticSiteEditorController do
  let_it_be(:project) { create(:project, :public, :repository) }
  let_it_be(:user) { create(:user) }

  let(:data) { { key: 'value' } }

  describe 'GET index' do
    let(:default_params) do
      {
        namespace_id: project.namespace,
        project_id: project
      }
    end

    it 'responds with 404 page' do
      get :index, params: default_params

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

  describe 'GET show' do
    render_views

    let(:default_params) do
      {
        namespace_id: project.namespace,
        project_id: project,
        id: 'master/README.md',
        return_url: 'http://example.com'
      }
    end

    let(:service_response) do
      ServiceResponse.success(payload: data)
    end

    before do
      allow_next_instance_of(::StaticSiteEditor::ConfigService) do |instance|
        allow(instance).to receive(:execute).and_return(service_response)
      end
    end

    context 'User roles' do
      context 'anonymous' do
        before do
          get :show, params: default_params
        end

        it 'redirects to sign in and returns' do
          expect(response).to redirect_to(new_user_session_path)
        end
      end

      context 'as guest' do
        before do
          project.add_guest(user)
          sign_in(user)
          get :show, params: default_params
        end

        it 'responds with 404 page' do
          expect(response).to have_gitlab_http_status(:not_found)
        end
      end

      context "as developer" do
        before do
          allow(Gitlab::UsageDataCounters::StaticSiteEditorCounter).to receive(:increment_views_count)
          project.add_role(user, 'developer')
          sign_in(user)
          get :show, params: default_params
        end

        it 'increases the views counter' do
          expect(Gitlab::UsageDataCounters::StaticSiteEditorCounter).to have_received(:increment_views_count)
        end

        it 'renders the edit page' do
          expect(response).to render_template(:show)
        end

        it 'assigns ref and path variables' do
          expect(assigns(:ref)).to eq('master')
          expect(assigns(:path)).to eq('README.md')
        end

        context 'when combination of ref and path is incorrect' do
          let(:default_params) { super().merge(id: 'unknown') }

          it 'responds with 404 page' do
            expect(response).to have_gitlab_http_status(:not_found)
          end
        end

        context 'when invalid config file' do
          let(:service_response) { ServiceResponse.error(message: 'invalid') }

          it 'redirects to project page and flashes error message' do
            expect(response).to redirect_to(project_path(project))
            expect(controller).to set_flash[:alert].to('invalid')
          end
        end

        context 'with a service response payload containing multiple data types' do
          let(:data) do
            {
              a_string: 'string',
              an_array: [
                {
                  foo: 'bar'
                }
              ],
              an_integer: 123,
              a_hash: {
                a_deeper_hash: {
                  foo: 'bar'
                }
              },
              a_boolean: true,
              a_nil: nil
            }
          end

          let(:assigns_data) { assigns(:data) }

          it 'leaves data values which are strings as strings' do
            expect(assigns_data[:a_string]).to eq('string')
          end

          it 'leaves data values which are integers as integers' do
            expect(assigns_data[:an_integer]).to eq(123)
          end

          it 'serializes data values which are booleans to JSON' do
            expect(assigns_data[:a_boolean]).to eq('true')
          end

          it 'serializes data values which are arrays to JSON' do
            expect(assigns_data[:an_array]).to eq('[{"foo":"bar"}]')
          end

          it 'serializes data values which are hashes to JSON' do
            expect(assigns_data[:a_hash]).to eq('{"a_deeper_hash":{"foo":"bar"}}')
          end

          it 'serializes data values which are nil to an empty string' do
            expect(assigns_data[:a_nil]).to eq('')
          end
        end
      end
    end
  end
end