2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
RSpec.describe API::BroadcastMessages, :aggregate_failures, feature_category: :onboarding do
|
2020-03-13 15:44:24 +05:30
|
|
|
let_it_be(:admin) { create(:admin) }
|
|
|
|
let_it_be(:message) { create(:broadcast_message) }
|
2023-06-20 00:43:36 +05:30
|
|
|
let_it_be(:path) { '/broadcast_messages' }
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
describe 'GET /broadcast_messages' do
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'returns an Array of BroadcastMessages' do
|
2016-09-29 09:46:39 +05:30
|
|
|
create(:broadcast_message)
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(path)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(response).to include_pagination_headers
|
2016-09-29 09:46:39 +05:30
|
|
|
expect(json_response).to be_kind_of(Array)
|
|
|
|
expect(json_response.first.keys)
|
2022-05-07 20:08:51 +05:30
|
|
|
.to match_array(%w(id message starts_at ends_at color font active target_access_levels target_path broadcast_type dismissable))
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /broadcast_messages/:id' do
|
2023-06-20 00:43:36 +05:30
|
|
|
let_it_be(:path) { "#{path}/#{message.id}" }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'returns the specified message' do
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(path)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2016-09-29 09:46:39 +05:30
|
|
|
expect(json_response['id']).to eq message.id
|
|
|
|
expect(json_response.keys)
|
2022-05-07 20:08:51 +05:30
|
|
|
.to match_array(%w(id message starts_at ends_at color font active target_access_levels target_path broadcast_type dismissable))
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /broadcast_messages' do
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like 'POST request permissions for admin mode' do
|
|
|
|
let(:params) { { message: 'Test message' } }
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it 'returns a 401 for anonymous users' do
|
|
|
|
post api(path), params: attributes_for(:broadcast_message)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'as an admin' do
|
|
|
|
it 'requires the `message` parameter' do
|
|
|
|
attrs = attributes_for(:broadcast_message)
|
|
|
|
attrs.delete(:message)
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: attrs
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2016-09-29 09:46:39 +05:30
|
|
|
expect(json_response['error']).to eq 'message is missing'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defines sane default start and end times' do
|
|
|
|
time = Time.zone.parse('2016-07-02 10:11:12')
|
|
|
|
travel_to(time) do
|
2023-06-20 00:43:36 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { message: 'Test message' }
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2016-09-29 09:46:39 +05:30
|
|
|
expect(json_response['starts_at']).to eq '2016-07-02T10:11:12.000Z'
|
|
|
|
expect(json_response['ends_at']).to eq '2016-07-02T11:11:12.000Z'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts a custom background and foreground color' do
|
|
|
|
attrs = attributes_for(:broadcast_message, color: '#000000', font: '#cecece')
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: attrs
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2016-09-29 09:46:39 +05:30
|
|
|
expect(json_response['color']).to eq attrs[:color]
|
|
|
|
expect(json_response['font']).to eq attrs[:font]
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
it 'accepts target access levels' do
|
|
|
|
target_access_levels = [Gitlab::Access::GUEST, Gitlab::Access::DEVELOPER]
|
|
|
|
attrs = attributes_for(:broadcast_message, target_access_levels: target_access_levels)
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: attrs
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
|
|
|
expect(json_response['target_access_levels']).to eq attrs[:target_access_levels]
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
it 'accepts a target path' do
|
|
|
|
attrs = attributes_for(:broadcast_message, target_path: "*/welcome")
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: attrs
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(json_response['target_path']).to eq attrs[:target_path]
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
it 'accepts a broadcast type' do
|
|
|
|
attrs = attributes_for(:broadcast_message, broadcast_type: 'notification')
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: attrs
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(json_response['broadcast_type']).to eq attrs[:broadcast_type]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses default broadcast type' do
|
|
|
|
attrs = attributes_for(:broadcast_message)
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: attrs
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(json_response['broadcast_type']).to eq 'banner'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'errors for invalid broadcast type' do
|
|
|
|
attrs = attributes_for(:broadcast_message, broadcast_type: 'invalid-type')
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: attrs
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it 'accepts an active dismissable value' do
|
2020-04-08 14:13:33 +05:30
|
|
|
attrs = { message: 'new message', dismissable: true }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: attrs
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
|
|
|
expect(json_response['dismissable']).to eq true
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT /broadcast_messages/:id' do
|
2023-06-20 00:43:36 +05:30
|
|
|
let_it_be(:path) { "#{path}/#{message.id}" }
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like 'PUT request permissions for admin mode' do
|
|
|
|
let(:params) { { message: 'Test message' } }
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it 'returns a 401 for anonymous users' do
|
|
|
|
put api(path),
|
2019-02-15 15:39:39 +05:30
|
|
|
params: attributes_for(:broadcast_message)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'as an admin' do
|
|
|
|
it 'accepts new background and foreground colors' do
|
|
|
|
attrs = { color: '#000000', font: '#cecece' }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
put api(path, admin, admin_mode: true), params: attrs
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
expect(json_response['color']).to eq attrs[:color]
|
|
|
|
expect(json_response['font']).to eq attrs[:font]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts new start and end times' do
|
|
|
|
time = Time.zone.parse('2016-07-02 10:11:12')
|
|
|
|
travel_to(time) do
|
|
|
|
attrs = { starts_at: Time.zone.now, ends_at: 3.hours.from_now }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
put api(path, admin, admin_mode: true), params: attrs
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2016-09-29 09:46:39 +05:30
|
|
|
expect(json_response['starts_at']).to eq '2016-07-02T10:11:12.000Z'
|
|
|
|
expect(json_response['ends_at']).to eq '2016-07-02T13:11:12.000Z'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts a new message' do
|
|
|
|
attrs = { message: 'new message' }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
put api(path, admin, admin_mode: true), params: attrs
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2016-09-29 09:46:39 +05:30
|
|
|
expect { message.reload }.to change { message.message }.to('new message')
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
it 'accepts a new target_access_levels' do
|
|
|
|
attrs = { target_access_levels: [Gitlab::Access::MAINTAINER] }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
put api(path, admin, admin_mode: true), params: attrs
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['target_access_levels']).to eq attrs[:target_access_levels]
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
it 'accepts a new target_path' do
|
|
|
|
attrs = { target_path: '*/welcome' }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
put api(path, admin, admin_mode: true), params: attrs
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(json_response['target_path']).to eq attrs[:target_path]
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
it 'accepts a new broadcast_type' do
|
|
|
|
attrs = { broadcast_type: 'notification' }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
put api(path, admin, admin_mode: true), params: attrs
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(json_response['broadcast_type']).to eq attrs[:broadcast_type]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'errors for invalid broadcast type' do
|
|
|
|
attrs = { broadcast_type: 'invalid-type' }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
put api(path, admin, admin_mode: true), params: attrs
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it 'accepts a new dismissable value' do
|
2020-04-08 14:13:33 +05:30
|
|
|
attrs = { message: 'new message', dismissable: true }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
put api(path, admin, admin_mode: true), params: attrs
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['dismissable']).to eq true
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /broadcast_messages/:id' do
|
2023-06-20 00:43:36 +05:30
|
|
|
let_it_be(:path) { "#{path}/#{message.id}" }
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like 'DELETE request permissions for admin mode'
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it 'returns a 401 for anonymous users' do
|
|
|
|
delete api(path),
|
2019-02-15 15:39:39 +05:30
|
|
|
params: attributes_for(:broadcast_message)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '412 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
let(:request) { api("/broadcast_messages/#{message.id}", admin, admin_mode: true) }
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes the broadcast message for admins' do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect do
|
2023-06-20 00:43:36 +05:30
|
|
|
delete api(path, admin, admin_mode: true)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2017-08-17 22:00:37 +05:30
|
|
|
end.to change { BroadcastMessage.count }.by(-1)
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|