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

akismet_mark_as_spam_action_spec.rb « spammable_actions « concerns « controllers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7c10dccdcb98b02bf1001793e7bc462e180fe8ec (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SpammableActions::AkismetMarkAsSpamAction do
  include AfterNextHelpers

  controller(ActionController::Base) do
    include SpammableActions::AkismetMarkAsSpamAction

    private

    def spammable_path
      '/fake_spammable_path'
    end
  end

  let(:spammable_type) { 'SpammableType' }
  let(:spammable) { double(:spammable, spammable_entity_type: double(:spammable_entity_type, titlecase: spammable_type)) }
  let(:current_user) { create(:admin) }

  before do
    allow(Gitlab::Recaptcha).to receive(:load_configurations!) { true }
    routes.draw { get 'mark_as_spam' => 'anonymous#mark_as_spam' }
    allow(controller).to receive(:spammable) { spammable }
    allow(controller).to receive(:current_user) { double(:current_user, admin?: admin) }
    allow(controller).to receive(:current_user).and_return(current_user)
  end

  describe '#mark_as_spam' do
    subject { post :mark_as_spam }

    before do
      expect_next(Spam::AkismetMarkAsSpamService, target: spammable)
        .to receive(:execute).and_return(execute_result)
    end

    context 'when user is admin', :enable_admin_mode do
      let(:admin) { true }

      context 'when service returns truthy' do
        let(:execute_result) { true }

        it 'redirects with notice' do
          expect(subject).to redirect_to('/fake_spammable_path')
          expect(subject.request.flash[:notice]).to match(/#{spammable_type}.*submitted.*successfully/)
        end
      end

      context 'when service returns falsey' do
        let(:execute_result) { false }

        it 'redirects with notice' do
          expect(subject).to redirect_to('/fake_spammable_path')
          expect(subject.request.flash[:alert]).to match(/Error/)
        end
      end
    end

    context 'when user is not admin' do
      let(:admin) { false }
      let(:execute_result) { true }

      it 'calls #access_denied!' do
        expect(controller).to receive(:access_denied!) { false }

        subject
      end
    end
  end
end