online_notifications_controller_spec.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe OnlineNotification, type: :request do
  4. let(:admin) { create(:admin, groups: Group.all) }
  5. let(:agent) { create(:agent, groups: Group.all) }
  6. let(:user_id) { user.id }
  7. let(:type_lookup_id) { TypeLookup.by_name('create') }
  8. let(:object_lookup_id) { ObjectLookup.by_name('User') }
  9. describe 'request handling' do
  10. shared_examples 'for successful request' do
  11. it 'has a good response' do
  12. get path, params: {}, as: :json
  13. expect(response).to have_http_status(:ok)
  14. end
  15. end
  16. shared_examples 'for array response' do
  17. it 'has an array response' do
  18. get path, params: {}, as: :json
  19. expect(json_response).to be_a_kind_of(Array)
  20. end
  21. end
  22. shared_examples 'for hash response' do
  23. it 'has hash response' do
  24. get path, params: {}, as: :json
  25. expect(json_response).to be_a_kind_of(Hash)
  26. end
  27. end
  28. shared_examples 'for notification id in array' do
  29. it 'has a notification id' do
  30. get path, params: {}, as: :json
  31. expect(json_response[0]['id']).to be online_notification.id
  32. end
  33. end
  34. shared_examples 'for notification id in hash' do
  35. it 'has a notification id' do
  36. get path, params: {}, as: :json
  37. expect(json_response['id']).to be online_notification.id
  38. end
  39. end
  40. shared_examples 'for user id in array' do
  41. it 'has a user id' do
  42. get path, params: {}, as: :json
  43. expect(json_response[0]['user_id']).to eq(user_id)
  44. end
  45. end
  46. shared_examples 'for user id in hash' do
  47. it 'has a user id' do
  48. get path, params: {}, as: :json
  49. expect(json_response['user_id']).to eq(user_id)
  50. end
  51. end
  52. shared_examples 'for object lookup id in array' do
  53. it 'has a object lookup id' do
  54. get path, params: {}, as: :json
  55. expect(json_response[0]['object_lookup_id']).to eq(object_lookup_id)
  56. end
  57. end
  58. shared_examples 'for object lookup id in hash' do
  59. it 'has a object lookup' do
  60. get path, params: {}, as: :json
  61. expect(json_response['object_lookup_id']).to eq(object_lookup_id)
  62. end
  63. end
  64. shared_examples 'for type lookup id in array' do
  65. it 'has a type lookup id' do
  66. get path, params: {}, as: :json
  67. expect(json_response[0]['type_lookup_id']).to eq(type_lookup_id)
  68. end
  69. end
  70. shared_examples 'for type lookup id in hash' do
  71. it 'has a type lookup' do
  72. get path, params: {}, as: :json
  73. expect(json_response['type_lookup_id']).to eq(type_lookup_id)
  74. end
  75. end
  76. shared_examples 'for response with assests' do
  77. it 'has an assests attribute' do
  78. get path, params: {}, as: :json
  79. expect(json_response['assets']).to be_a_kind_of(Hash)
  80. end
  81. end
  82. shared_examples 'getting all associated online notifications' do
  83. before { online_notification && authenticated_as(user) }
  84. context 'when online notifications is requested' do
  85. let(:path) { '/api/v1/online_notifications' }
  86. include_examples 'for successful request'
  87. include_examples 'for array response'
  88. include_examples 'for notification id in array'
  89. include_examples 'for user id in array'
  90. include_examples 'for object lookup id in array'
  91. include_examples 'for type lookup id in array'
  92. end
  93. context 'when online notifications is requested with full params' do
  94. let(:path) { '/api/v1/online_notifications?full=true' }
  95. it 'has a record_ids attribute' do
  96. get path, params: {}, as: :json
  97. expect(json_response['record_ids'])
  98. .to be_a_kind_of(Array)
  99. .and include(online_notification.id)
  100. end
  101. include_examples 'for successful request'
  102. include_examples 'for hash response'
  103. include_examples 'for response with assests'
  104. end
  105. context 'when online notifications is requested with expand params' do
  106. let(:path) { '/api/v1/online_notifications?expand=true' }
  107. it 'has a type attribute' do
  108. get path, params: {}, as: :json
  109. expect(json_response[0]['type']).to eq('create')
  110. end
  111. it 'has a object attribute' do
  112. get path, params: {}, as: :json
  113. expect(json_response[0]['object']).to eq('User')
  114. end
  115. include_examples 'for successful request'
  116. include_examples 'for array response'
  117. include_examples 'for notification id in array'
  118. include_examples 'for user id in array'
  119. include_examples 'for object lookup id in array'
  120. include_examples 'for type lookup id in array'
  121. end
  122. end
  123. shared_examples 'getting specific associated online notification' do
  124. before { authenticated_as(user) }
  125. context 'when specific online notifications is requested' do
  126. let(:path) { "/api/v1/online_notifications/#{online_notification.id}" }
  127. include_examples 'for successful request'
  128. include_examples 'for hash response'
  129. include_examples 'for notification id in hash'
  130. include_examples 'for user id in hash'
  131. include_examples 'for object lookup id in hash'
  132. include_examples 'for type lookup id in hash'
  133. end
  134. context 'when specific online notifications is requested with full params' do
  135. let(:path) { "/api/v1/online_notifications/#{online_notification.id}?full=true" }
  136. it 'has a notification id' do
  137. get path, params: {}, as: :json
  138. expect(json_response['id']).to be online_notification.id
  139. end
  140. include_examples 'for successful request'
  141. include_examples 'for hash response'
  142. include_examples 'for response with assests'
  143. end
  144. context 'when specific online notifications is requested with expand params' do
  145. let(:path) { "/api/v1/online_notifications/#{online_notification.id}?expand=true" }
  146. it 'has a type attribute' do
  147. get path, params: {}, as: :json
  148. expect(json_response['type']).to eq('create')
  149. end
  150. it 'has a object attribute' do
  151. get path, params: {}, as: :json
  152. expect(json_response['object']).to eq('User')
  153. end
  154. include_examples 'for successful request'
  155. include_examples 'for hash response'
  156. include_examples 'for notification id in hash'
  157. include_examples 'for user id in hash'
  158. include_examples 'for object lookup id in hash'
  159. include_examples 'for type lookup id in hash'
  160. end
  161. end
  162. shared_examples 'getting a different online notification' do
  163. before { authenticated_as(user) }
  164. context 'when a different notification is request' do
  165. let(:path) { "/api/v1/online_notifications/#{different_online_notification.id}" }
  166. it 'has a forbidden response' do
  167. get path, params: {}, as: :json
  168. expect(response).to have_http_status(:forbidden)
  169. end
  170. it 'has authorized error message' do
  171. get path, params: {}, as: :json
  172. expect(json_response['error']).to eq('Not authorized')
  173. end
  174. include_examples 'for hash response'
  175. end
  176. end
  177. context 'with authenticated admin' do
  178. let(:user) { create(:admin, groups: Group.all) }
  179. let(:online_notification) do
  180. create(:online_notification, o_id: admin.id, user_id: user_id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
  181. end
  182. let(:different_online_notification) do
  183. create(:online_notification, o_id: admin.id, user_id: agent.id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
  184. end
  185. it_behaves_like 'getting all associated online notifications'
  186. it_behaves_like 'getting specific associated online notification'
  187. it_behaves_like 'getting a different online notification'
  188. end
  189. context 'with authenticated agent' do
  190. let(:user) { create(:agent, groups: Group.all) }
  191. let(:online_notification) do
  192. create(:online_notification, o_id: admin.id, user_id: user_id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
  193. end
  194. let(:different_online_notification) do
  195. create(:online_notification, o_id: admin.id, user_id: admin.id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
  196. end
  197. it_behaves_like 'getting all associated online notifications'
  198. it_behaves_like 'getting specific associated online notification'
  199. it_behaves_like 'getting a different online notification'
  200. end
  201. context 'with authenticated customer' do
  202. let(:user) { create(:customer) }
  203. let(:online_notification) do
  204. create(:online_notification, o_id: user_id, user_id: user_id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
  205. end
  206. let(:different_online_notification) do
  207. create(:online_notification, o_id: admin.id, user_id: agent.id, type_lookup_id: type_lookup_id, object_lookup_id: object_lookup_id)
  208. end
  209. it_behaves_like 'getting all associated online notifications'
  210. it_behaves_like 'getting specific associated online notification'
  211. it_behaves_like 'getting a different online notification'
  212. end
  213. end
  214. end