summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorRobert Speicher <robert@gitlab.com>2016-08-31 20:47:20 +0000
committerRobert Speicher <robert@gitlab.com>2016-08-31 20:47:20 +0000
commit177cc4e4cbde21e8b56a9f3e0104d6319d79e6cc (patch)
tree87ee80915732d5c9ea651834e1aa999c9eecdd65 /spec
parent286f10afb9c9945e37fb903009f823ba694a95b4 (diff)
parent9d4af1b14068aa77848b982372ddf9e11567ae5f (diff)
downloadgitlab-ce-177cc4e4cbde21e8b56a9f3e0104d6319d79e6cc.tar.gz
Merge branch 'rs-broadcast-message-api' into 'master'
Add a BroadcastMessage API Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/21628 See merge request !6112
Diffstat (limited to 'spec')
-rw-r--r--spec/requests/api/broadcast_messages_spec.rb180
1 files changed, 180 insertions, 0 deletions
diff --git a/spec/requests/api/broadcast_messages_spec.rb b/spec/requests/api/broadcast_messages_spec.rb
new file mode 100644
index 00000000000..7c9078b2864
--- /dev/null
+++ b/spec/requests/api/broadcast_messages_spec.rb
@@ -0,0 +1,180 @@
+require 'spec_helper'
+
+describe API::BroadcastMessages, api: true do
+ include ApiHelpers
+
+ let(:user) { create(:user) }
+ let(:admin) { create(:admin) }
+
+ describe 'GET /broadcast_messages' do
+ it 'returns a 401 for anonymous users' do
+ get api('/broadcast_messages')
+
+ expect(response).to have_http_status(401)
+ end
+
+ it 'returns a 403 for users' do
+ get api('/broadcast_messages', user)
+
+ expect(response).to have_http_status(403)
+ end
+
+ it 'returns an Array of BroadcastMessages for admins' do
+ create(:broadcast_message)
+
+ get api('/broadcast_messages', admin)
+
+ expect(response).to have_http_status(200)
+ expect(json_response).to be_kind_of(Array)
+ expect(json_response.first.keys)
+ .to match_array(%w(id message starts_at ends_at color font active))
+ end
+ end
+
+ describe 'GET /broadcast_messages/:id' do
+ let!(:message) { create(:broadcast_message) }
+
+ it 'returns a 401 for anonymous users' do
+ get api("/broadcast_messages/#{message.id}")
+
+ expect(response).to have_http_status(401)
+ end
+
+ it 'returns a 403 for users' do
+ get api("/broadcast_messages/#{message.id}", user)
+
+ expect(response).to have_http_status(403)
+ end
+
+ it 'returns the specified message for admins' do
+ get api("/broadcast_messages/#{message.id}", admin)
+
+ expect(response).to have_http_status(200)
+ expect(json_response['id']).to eq message.id
+ expect(json_response.keys)
+ .to match_array(%w(id message starts_at ends_at color font active))
+ end
+ end
+
+ describe 'POST /broadcast_messages' do
+ it 'returns a 401 for anonymous users' do
+ post api('/broadcast_messages'), attributes_for(:broadcast_message)
+
+ expect(response).to have_http_status(401)
+ end
+
+ it 'returns a 403 for users' do
+ post api('/broadcast_messages', user), attributes_for(:broadcast_message)
+
+ expect(response).to have_http_status(403)
+ end
+
+ context 'as an admin' do
+ it 'requires the `message` parameter' do
+ attrs = attributes_for(:broadcast_message)
+ attrs.delete(:message)
+
+ post api('/broadcast_messages', admin), attrs
+
+ expect(response).to have_http_status(400)
+ 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
+ post api('/broadcast_messages', admin), message: 'Test message'
+
+ expect(response).to have_http_status(201)
+ 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')
+
+ post api('/broadcast_messages', admin), attrs
+
+ expect(response).to have_http_status(201)
+ expect(json_response['color']).to eq attrs[:color]
+ expect(json_response['font']).to eq attrs[:font]
+ end
+ end
+ end
+
+ describe 'PUT /broadcast_messages/:id' do
+ let!(:message) { create(:broadcast_message) }
+
+ it 'returns a 401 for anonymous users' do
+ put api("/broadcast_messages/#{message.id}"),
+ attributes_for(:broadcast_message)
+
+ expect(response).to have_http_status(401)
+ end
+
+ it 'returns a 403 for users' do
+ put api("/broadcast_messages/#{message.id}", user),
+ attributes_for(:broadcast_message)
+
+ expect(response).to have_http_status(403)
+ end
+
+ context 'as an admin' do
+ it 'accepts new background and foreground colors' do
+ attrs = { color: '#000000', font: '#cecece' }
+
+ put api("/broadcast_messages/#{message.id}", admin), attrs
+
+ expect(response).to have_http_status(200)
+ 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 }
+
+ put api("/broadcast_messages/#{message.id}", admin), attrs
+
+ expect(response).to have_http_status(200)
+ 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' }
+
+ put api("/broadcast_messages/#{message.id}", admin), attrs
+
+ expect(response).to have_http_status(200)
+ expect { message.reload }.to change { message.message }.to('new message')
+ end
+ end
+ end
+
+ describe 'DELETE /broadcast_messages/:id' do
+ let!(:message) { create(:broadcast_message) }
+
+ it 'returns a 401 for anonymous users' do
+ delete api("/broadcast_messages/#{message.id}"),
+ attributes_for(:broadcast_message)
+
+ expect(response).to have_http_status(401)
+ end
+
+ it 'returns a 403 for users' do
+ delete api("/broadcast_messages/#{message.id}", user),
+ attributes_for(:broadcast_message)
+
+ expect(response).to have_http_status(403)
+ end
+
+ it 'deletes the broadcast message for admins' do
+ expect { delete api("/broadcast_messages/#{message.id}", admin) }
+ .to change { BroadcastMessage.count }.by(-1)
+ end
+ end
+end