about summary refs log tree commit diff
path: root/spec/controllers/application_controller_spec.rb
blob: 67d3c1ce90f7b44b21a61d5e363724d7aa80dcdb (plain) (blame)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# frozen_string_literal: true

require 'rails_helper'

describe ApplicationController, type: :controller do
  controller do
    def success
      head 200
    end

    def routing_error
      raise ActionController::RoutingError, ''
    end

    def record_not_found
      raise ActiveRecord::RecordNotFound, ''
    end

    def invalid_authenticity_token
      raise ActionController::InvalidAuthenticityToken, ''
    end
  end

  shared_examples 'respond_with_error' do |code|
    it "returns http #{code} for any" do
      subject
      expect(response).to have_http_status(code)
    end

    it "returns http #{code} for http" do
      subject
      expect(response).to have_http_status(code)
    end

    it "renders template for http" do
      is_expected.to render_template("errors/#{code}", layout: 'error')
    end
  end

  context 'forgery' do
    subject do
      ActionController::Base.allow_forgery_protection = true
      routes.draw { post 'success' => 'anonymous#success' }
      post 'success'
    end

    include_examples 'respond_with_error', 422
  end

  it "does not force ssl if Rails.env.production? is not 'true'" do
    routes.draw { get 'success' => 'anonymous#success' }
    allow(Rails.env).to receive(:production?).and_return(false)
    get 'success'
    expect(response).to have_http_status(200)
  end

  it "forces ssl if Rails.env.production? is 'true'" do
    routes.draw { get 'success' => 'anonymous#success' }
    allow(Rails.env).to receive(:production?).and_return(true)
    get 'success'
    expect(response).to redirect_to('https://test.host/success')
  end

  describe 'helper_method :current_account' do
    it 'returns nil if not signed in' do
      expect(controller.view_context.current_account).to be_nil
    end

    it 'returns account if signed in' do
      account = Fabricate(:account)
      sign_in(Fabricate(:user, account: account))
      expect(controller.view_context.current_account).to eq account
    end
  end

  describe 'helper_method :single_user_mode?' do
    it 'returns false if it is in single_user_mode but there is no account' do
      allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true)
      expect(controller.view_context.single_user_mode?).to eq false
    end

    it 'returns false if there is an account but it is not in single_user_mode' do
      allow(Rails.configuration.x).to receive(:single_user_mode).and_return(false)
      Fabricate(:account)
      expect(controller.view_context.single_user_mode?).to eq false
    end

    it 'returns true if it is in single_user_mode and there is an account' do
      allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true)
      Fabricate(:account)
      expect(controller.view_context.single_user_mode?).to eq true
    end
  end

  describe 'helper_method :current_flavour' do
    it 'returns "glitch" when theme wasn\'t changed in admin settings' do
      allow(Setting).to receive(:default_settings).and_return({'skin' => 'default'})
      allow(Setting).to receive(:default_settings).and_return({'flavour' => 'glitch'})

      expect(controller.view_context.current_flavour).to eq 'glitch'
    end

    it 'returns instances\'s flavour when user is not signed in' do
      allow(Setting).to receive(:[]).with('skin').and_return 'default'
      allow(Setting).to receive(:[]).with('flavour').and_return 'vanilla'

      expect(controller.view_context.current_flavour).to eq 'vanilla'
    end

    it 'returns instances\'s default flavour when user didn\'t set theme' do
      current_user = Fabricate(:user)
      sign_in current_user

      allow(Setting).to receive(:[]).with('skin').and_return 'default'
      allow(Setting).to receive(:[]).with('flavour').and_return 'vanilla'

      expect(controller.view_context.current_flavour).to eq 'vanilla'
    end

    it 'returns user\'s flavour when it is set' do
      current_user = Fabricate(:user)
      current_user.settings['flavour'] = 'glitch'
      sign_in current_user

      allow(Setting).to receive(:[]).with('skin').and_return 'default'
      allow(Setting).to receive(:[]).with('flavour').and_return 'vanilla'

      expect(controller.view_context.current_flavour).to eq 'glitch'
    end
  end

  context 'ActionController::RoutingError' do
    subject do
      routes.draw { get 'routing_error' => 'anonymous#routing_error' }
      get 'routing_error'
    end

    include_examples 'respond_with_error', 404
  end

  context 'ActiveRecord::RecordNotFound' do
    subject do
      routes.draw { get 'record_not_found' => 'anonymous#record_not_found' }
      get 'record_not_found'
    end

    include_examples 'respond_with_error', 404
  end

  context 'ActionController::InvalidAuthenticityToken' do
    subject do
      routes.draw { get 'invalid_authenticity_token' => 'anonymous#invalid_authenticity_token' }
      get 'invalid_authenticity_token'
    end

    include_examples 'respond_with_error', 422
  end

  describe 'before_action :store_current_location' do
    it 'stores location for user if it is not devise controller' do
      routes.draw { get 'success' => 'anonymous#success' }
      get 'success'
      expect(controller.stored_location_for(:user)).to eq '/success'
    end

    context do
      controller Devise::SessionsController do
      end

      it 'does not store location for user if it is devise controller' do
        @request.env["devise.mapping"] = Devise.mappings[:user]
        get 'create'
        expect(controller.stored_location_for(:user)).to be_nil
      end
    end
  end

  describe 'before_action :check_suspension' do
    before do
      routes.draw { get 'success' => 'anonymous#success' }
    end

    it 'does nothing if not signed in' do
      get 'success'
      expect(response).to have_http_status(200)
    end

    it 'does nothing if user who signed in is not suspended' do
      sign_in(Fabricate(:user, account: Fabricate(:account, suspended: false)))
      get 'success'
      expect(response).to have_http_status(200)
    end

    it 'redirects to account status page' do
      sign_in(Fabricate(:user, account: Fabricate(:account, suspended: true)))
      get 'success'
      expect(response).to redirect_to(edit_user_registration_path)
    end
  end

  describe 'raise_not_found' do
    it 'raises error' do
      controller.params[:unmatched_route] = 'unmatched'
      expect { controller.raise_not_found }.to raise_error(ActionController::RoutingError, 'No route matches unmatched')
    end
  end

  describe 'require_admin!' do
    controller do
      before_action :require_admin!

      def sucesss
        head 200
      end
    end

    before do
      routes.draw { get 'sucesss' => 'anonymous#sucesss' }
    end

    it 'returns a 403 if current user is not admin' do
      sign_in(Fabricate(:user, admin: false))
      get 'sucesss'
      expect(response).to have_http_status(403)
    end

    it 'returns a 403 if current user is only a moderator' do
      sign_in(Fabricate(:user, moderator: true))
      get 'sucesss'
      expect(response).to have_http_status(403)
    end

    it 'does nothing if current user is admin' do
      sign_in(Fabricate(:user, admin: true))
      get 'sucesss'
      expect(response).to have_http_status(200)
    end
  end

  describe 'require_staff!' do
    controller do
      before_action :require_staff!

      def sucesss
        head 200
      end
    end

    before do
      routes.draw { get 'sucesss' => 'anonymous#sucesss' }
    end

    it 'returns a 403 if current user is not admin or moderator' do
      sign_in(Fabricate(:user, admin: false, moderator: false))
      get 'sucesss'
      expect(response).to have_http_status(403)
    end

    it 'does nothing if current user is moderator' do
      sign_in(Fabricate(:user, moderator: true))
      get 'sucesss'
      expect(response).to have_http_status(200)
    end

    it 'does nothing if current user is admin' do
      sign_in(Fabricate(:user, admin: true))
      get 'sucesss'
      expect(response).to have_http_status(200)
    end
  end

  describe 'forbidden' do
    controller do
      def route_forbidden
        forbidden
      end
    end

    subject do
      routes.draw { get 'route_forbidden' => 'anonymous#route_forbidden' }
      get 'route_forbidden'
    end

    include_examples 'respond_with_error', 403
  end

  describe 'not_found' do
    controller do
      def route_not_found
        not_found
      end
    end

    subject do
      routes.draw { get 'route_not_found' => 'anonymous#route_not_found' }
      get 'route_not_found'
    end

    include_examples 'respond_with_error', 404
  end

  describe 'gone' do
    controller do
      def route_gone
        gone
      end
    end

    subject do
      routes.draw { get 'route_gone' => 'anonymous#route_gone' }
      get 'route_gone'
    end

    include_examples 'respond_with_error', 410
  end

  describe 'unprocessable_entity' do
    controller do
      def route_unprocessable_entity
        unprocessable_entity
      end
    end

    subject do
      routes.draw { get 'route_unprocessable_entity' => 'anonymous#route_unprocessable_entity' }
      get 'route_unprocessable_entity'
    end

    include_examples 'respond_with_error', 422
  end

  describe 'cache_collection' do
    class C < ApplicationController
      public :cache_collection
    end

    shared_examples 'receives :with_includes' do |fabricator, klass|
      it 'uses raw if it is not an ActiveRecord::Relation' do
        record = Fabricate(fabricator)
        expect(C.new.cache_collection([record], klass)).to eq [record]
      end
    end

    shared_examples 'cacheable' do |fabricator, klass|
      include_examples 'receives :with_includes', fabricator, klass

      it 'calls cache_ids of raw if it is an ActiveRecord::Relation' do
        record = Fabricate(fabricator)
        relation = klass.none
        allow(relation).to receive(:cache_ids).and_return([record])
        expect(C.new.cache_collection(relation, klass)).to eq [record]
      end
    end

    it 'returns raw unless class responds to :with_includes' do
      raw = Object.new
      expect(C.new.cache_collection(raw, Object)).to eq raw
    end

    context 'Notification' do
      include_examples 'cacheable', :notification, Notification
    end

    context 'Status' do
      include_examples 'cacheable', :status, Status
    end
  end
end