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

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

require 'spec_helper'

RSpec.describe Admin::BroadcastMessagesController, :enable_admin_mode do
  let(:broadcast_message) { build(:broadcast_message) }
  let(:broadcast_message_params) { broadcast_message.as_json(root: true, only: [:message, :starts_at, :ends_at]) }

  let_it_be(:invalid_broadcast_message) { { broadcast_message: { message: '' } } }
  let_it_be(:test_message) { 'you owe me a new acorn' }

  before do
    sign_in(create(:admin))
  end

  describe 'GET #index' do
    it 'renders index template' do
      get admin_broadcast_messages_path

      expect(response).to have_gitlab_http_status(:ok)
      expect(response.body).to render_template(:index)
    end
  end

  describe 'POST /preview' do
    it 'renders preview partial' do
      post preview_admin_broadcast_messages_path, params: { broadcast_message: { message: "Hello, world!" } }

      expect(response).to have_gitlab_http_status(:ok)
      expect(response.body).to render_template(:_preview)
    end
  end

  describe 'POST #create' do
    context 'when format json' do
      it 'persists the message and returns ok on success' do
        post admin_broadcast_messages_path(format: :json), params: broadcast_message_params
        expect(response).to have_gitlab_http_status(:ok)
        expect(Gitlab::Json.parse(response.body)['message']).to eq(broadcast_message.message)
      end

      it 'does not persist the message on failure' do
        post admin_broadcast_messages_path(format: :json), params: invalid_broadcast_message
        expect(response).to have_gitlab_http_status(:bad_request)
        expect(Gitlab::Json.parse(response.body)['errors']).to be_present
      end
    end

    context 'when format html' do
      it 'persists the message and redirects to broadcast_messages on success' do
        post admin_broadcast_messages_path(format: :html), params: broadcast_message_params
        expect(response).to redirect_to(admin_broadcast_messages_path)
      end

      it 'does not persist and renders the index page on failure' do
        post admin_broadcast_messages_path(format: :html), params: invalid_broadcast_message
        expect(response.body).to render_template(:index)
      end
    end
  end

  describe 'PATCH #update' do
    context 'when format json' do
      it 'persists the message and returns ok on success' do
        broadcast_message.save!
        patch admin_broadcast_message_path(format: :json, id: broadcast_message.id), params: {
          broadcast_message: { message: test_message }
        }

        expect(response).to have_gitlab_http_status(:ok)
        expect(Gitlab::Json.parse(response.body)['message']).to eq(test_message)
      end

      it 'does not persist the message on failure' do
        broadcast_message.message = test_message
        broadcast_message.save!
        patch admin_broadcast_message_path(format: :json, id: broadcast_message.id), params: {
          broadcast_message: { message: '' }
        }

        expect(response).to have_gitlab_http_status(:bad_request)
        expect(Gitlab::Json.parse(response.body)['errors']).to be_present
      end
    end

    context 'when format html' do
      it 'persists the message and redirects to broadcast_messages on success' do
        broadcast_message.save!
        patch admin_broadcast_message_path(id: broadcast_message.id), params: {
          broadcast_message: { message: test_message }
        }

        expect(response).to redirect_to(admin_broadcast_messages_path)
      end

      it 'does not persist and renders the edit page on failure' do
        broadcast_message.message = test_message
        broadcast_message.save!
        patch admin_broadcast_message_path(id: broadcast_message.id), params: {
          **invalid_broadcast_message
        }

        expect(response.body).to render_template(:edit)
      end
    end
  end
end