123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
- require 'rails_helper'
- RSpec.describe OnlineNotification, type: :request do
- let(:admin) { create(:admin, groups: Group.all) }
- let(:agent) { create(:agent, groups: Group.all) }
- let(:user_id) { user.id }
- let(:type_lookup_id) { TypeLookup.by_name('create') }
- let(:object_lookup_id) { ObjectLookup.by_name('User') }
- describe 'request handling' do
- shared_examples 'for successful request' do
- it 'has a good response' do
- get path, params: {}, as: :json
- expect(response).to have_http_status(:ok)
- end
- end
- shared_examples 'for array response' do
- it 'has an array response' do
- get path, params: {}, as: :json
- expect(json_response).to be_a_kind_of(Array)
- end
- end
- shared_examples 'for hash response' do
- it 'has hash response' do
- get path, params: {}, as: :json
- expect(json_response).to be_a_kind_of(Hash)
- end
- end
- shared_examples 'for notification id in array' do
- it 'has a notification id' do
- get path, params: {}, as: :json
- expect(json_response[0]['id']).to be online_notification.id
- end
- end
- shared_examples 'for notification id in hash' do
- it 'has a notification id' do
- get path, params: {}, as: :json
- expect(json_response['id']).to be online_notification.id
- end
- end
- shared_examples 'for user id in array' do
- it 'has a user id' do
- get path, params: {}, as: :json
- expect(json_response[0]['user_id']).to eq(user_id)
- end
- end
- shared_examples 'for user id in hash' do
- it 'has a user id' do
- get path, params: {}, as: :json
- expect(json_response['user_id']).to eq(user_id)
- end
- end
- shared_examples 'for object lookup id in array' do
- it 'has a object lookup id' do
- get path, params: {}, as: :json
- expect(json_response[0]['object_lookup_id']).to eq(object_lookup_id)
- end
- end
- shared_examples 'for object lookup id in hash' do
- it 'has a object lookup' do
- get path, params: {}, as: :json
- expect(json_response['object_lookup_id']).to eq(object_lookup_id)
- end
- end
- shared_examples 'for type lookup id in array' do
- it 'has a type lookup id' do
- get path, params: {}, as: :json
- expect(json_response[0]['type_lookup_id']).to eq(type_lookup_id)
- end
- end
- shared_examples 'for type lookup id in hash' do
- it 'has a type lookup' do
- get path, params: {}, as: :json
- expect(json_response['type_lookup_id']).to eq(type_lookup_id)
- end
- end
- shared_examples 'for response with assests' do
- it 'has an assests attribute' do
- get path, params: {}, as: :json
- expect(json_response['assets']).to be_a_kind_of(Hash)
- end
- end
- shared_examples 'getting all associated online notifications' do
- before { online_notification && authenticated_as(user) }
- context 'when online notifications is requested' do
- let(:path) { '/api/v1/online_notifications' }
- include_examples 'for successful request'
- include_examples 'for array response'
- include_examples 'for notification id in array'
- include_examples 'for user id in array'
- include_examples 'for object lookup id in array'
- include_examples 'for type lookup id in array'
- end
- context 'when online notifications is requested with full params' do
- let(:path) { '/api/v1/online_notifications?full=true' }
- it 'has a record_ids attribute' do
- get path, params: {}, as: :json
- expect(json_response['record_ids'])
- .to be_a_kind_of(Array)
- .and include(online_notification.id)
- end
- include_examples 'for successful request'
- include_examples 'for hash response'
- include_examples 'for response with assests'
- end
- context 'when online notifications is requested with expand params' do
- let(:path) { '/api/v1/online_notifications?expand=true' }
- it 'has a type attribute' do
- get path, params: {}, as: :json
- expect(json_response[0]['type']).to eq('create')
- end
- it 'has a object attribute' do
- get path, params: {}, as: :json
- expect(json_response[0]['object']).to eq('User')
- end
- include_examples 'for successful request'
- include_examples 'for array response'
- include_examples 'for notification id in array'
- include_examples 'for user id in array'
- include_examples 'for object lookup id in array'
- include_examples 'for type lookup id in array'
- end
- end
- shared_examples 'getting specific associated online notification' do
- before { authenticated_as(user) }
- context 'when specific online notifications is requested' do
- let(:path) { "/api/v1/online_notifications/#{online_notification.id}" }
- include_examples 'for successful request'
- include_examples 'for hash response'
- include_examples 'for notification id in hash'
- include_examples 'for user id in hash'
- include_examples 'for object lookup id in hash'
- include_examples 'for type lookup id in hash'
- end
- context 'when specific online notifications is requested with full params' do
- let(:path) { "/api/v1/online_notifications/#{online_notification.id}?full=true" }
- it 'has a notification id' do
- get path, params: {}, as: :json
- expect(json_response['id']).to be online_notification.id
- end
- include_examples 'for successful request'
- include_examples 'for hash response'
- include_examples 'for response with assests'
- end
- context 'when specific online notifications is requested with expand params' do
- let(:path) { "/api/v1/online_notifications/#{online_notification.id}?expand=true" }
- it 'has a type attribute' do
- get path, params: {}, as: :json
- expect(json_response['type']).to eq('create')
- end
- it 'has a object attribute' do
- get path, params: {}, as: :json
- expect(json_response['object']).to eq('User')
- end
- include_examples 'for successful request'
- include_examples 'for hash response'
- include_examples 'for notification id in hash'
- include_examples 'for user id in hash'
- include_examples 'for object lookup id in hash'
- include_examples 'for type lookup id in hash'
- end
- end
- shared_examples 'getting a different online notification' do
- before { authenticated_as(user) }
- context 'when a different notification is request' do
- let(:path) { "/api/v1/online_notifications/#{different_online_notification.id}" }
- it 'has a forbidden response' do
- get path, params: {}, as: :json
- expect(response).to have_http_status(:forbidden)
- end
- it 'has authorized error message' do
- get path, params: {}, as: :json
- expect(json_response['error']).to eq('Not authorized')
- end
- include_examples 'for hash response'
- end
- end
- context 'with authenticated admin' do
- let(:user) { create(:admin, groups: Group.all) }
- let(:online_notification) do
- create(:online_notification, o_id: admin.id, user_id: user_id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
- end
- let(:different_online_notification) do
- create(:online_notification, o_id: admin.id, user_id: agent.id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
- end
- it_behaves_like 'getting all associated online notifications'
- it_behaves_like 'getting specific associated online notification'
- it_behaves_like 'getting a different online notification'
- end
- context 'with authenticated agent' do
- let(:user) { create(:agent, groups: Group.all) }
- let(:online_notification) do
- create(:online_notification, o_id: admin.id, user_id: user_id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
- end
- let(:different_online_notification) do
- create(:online_notification, o_id: admin.id, user_id: admin.id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
- end
- it_behaves_like 'getting all associated online notifications'
- it_behaves_like 'getting specific associated online notification'
- it_behaves_like 'getting a different online notification'
- end
- context 'with authenticated customer' do
- let(:user) { create(:customer) }
- let(:online_notification) do
- create(:online_notification, o_id: user_id, user_id: user_id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
- end
- let(:different_online_notification) do
- create(:online_notification, o_id: admin.id, user_id: agent.id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
- end
- it_behaves_like 'getting all associated online notifications'
- it_behaves_like 'getting specific associated online notification'
- it_behaves_like 'getting a different online notification'
- end
- end
- end
|