api_auth_on_behalf_of_spec.rb 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. require 'rails_helper'
  2. RSpec.describe 'Api Auth On Behalf Of', type: :request do
  3. let(:admin) do
  4. create(:admin, groups: Group.all)
  5. end
  6. let(:agent) do
  7. create(:agent)
  8. end
  9. let(:customer) do
  10. create(:customer, firstname: 'Behalf of')
  11. end
  12. describe 'request handling' do
  13. it 'does X-On-Behalf-Of auth - ticket create admin for customer by id' do
  14. params = {
  15. title: 'a new ticket #3',
  16. group: 'Users',
  17. priority: '2 normal',
  18. state: 'new',
  19. customer_id: customer.id,
  20. article: {
  21. body: 'some test 123',
  22. },
  23. }
  24. authenticated_as(admin, on_behalf_of: customer.id)
  25. post '/api/v1/tickets', params: params, as: :json
  26. expect(response).to have_http_status(:created)
  27. expect(json_response).to be_a_kind_of(Hash)
  28. expect(customer.id).to eq(json_response['created_by_id'])
  29. end
  30. it 'does X-On-Behalf-Of auth - ticket create admin for customer by login (upcase)' do
  31. params = {
  32. title: 'a new ticket #3',
  33. group: 'Users',
  34. priority: '2 normal',
  35. state: 'new',
  36. customer_id: customer.id,
  37. article: {
  38. body: 'some test 123',
  39. },
  40. }
  41. authenticated_as(admin, on_behalf_of: customer.login.upcase)
  42. post '/api/v1/tickets', params: params, as: :json
  43. expect(response).to have_http_status(:created)
  44. expect(json_response).to be_a_kind_of(Hash)
  45. expect(customer.id).to eq(json_response['created_by_id'])
  46. end
  47. it 'does X-On-Behalf-Of auth - ticket create admin for customer by login' do
  48. ActivityStream.cleanup(1.year)
  49. params = {
  50. title: 'a new ticket #3',
  51. group: 'Users',
  52. priority: '2 normal',
  53. state: 'new',
  54. customer_id: customer.id,
  55. article: {
  56. body: 'some test 123',
  57. },
  58. }
  59. authenticated_as(admin, on_behalf_of: customer.login)
  60. post '/api/v1/tickets', params: params, as: :json
  61. expect(response).to have_http_status(:created)
  62. json_response_ticket = json_response
  63. expect(json_response_ticket).to be_a_kind_of(Hash)
  64. expect(customer.id).to eq(json_response_ticket['created_by_id'])
  65. authenticated_as(admin)
  66. get '/api/v1/activity_stream?full=true', params: {}, as: :json
  67. expect(response).to have_http_status(:ok)
  68. json_response_activity = json_response
  69. expect(json_response_activity).to be_a_kind_of(Hash)
  70. ticket_created = nil
  71. json_response_activity['record_ids'].each do |record_id|
  72. activity_stream = ActivityStream.find(record_id)
  73. next if activity_stream.object.name != 'Ticket'
  74. next if activity_stream.o_id != json_response_ticket['id'].to_i
  75. ticket_created = activity_stream
  76. end
  77. expect(ticket_created).to be_truthy
  78. expect(customer.id).to eq(ticket_created.created_by_id)
  79. get '/api/v1/activity_stream', params: {}, as: :json
  80. expect(response).to have_http_status(:ok)
  81. json_response_activity = json_response
  82. expect(json_response_activity).to be_a_kind_of(Array)
  83. ticket_created = nil
  84. json_response_activity.each do |record|
  85. activity_stream = ActivityStream.find(record['id'])
  86. next if activity_stream.object.name != 'Ticket'
  87. next if activity_stream.o_id != json_response_ticket['id']
  88. ticket_created = activity_stream
  89. end
  90. expect(ticket_created).to be_truthy
  91. expect(customer.id).to eq(ticket_created.created_by_id)
  92. end
  93. it 'does X-On-Behalf-Of auth - ticket create admin for customer by email' do
  94. params = {
  95. title: 'a new ticket #3',
  96. group: 'Users',
  97. priority: '2 normal',
  98. state: 'new',
  99. customer_id: customer.id,
  100. article: {
  101. body: 'some test 123',
  102. },
  103. }
  104. authenticated_as(admin, on_behalf_of: customer.email)
  105. post '/api/v1/tickets', params: params, as: :json
  106. expect(response).to have_http_status(:created)
  107. expect(json_response).to be_a_kind_of(Hash)
  108. expect(customer.id).to eq(json_response['created_by_id'])
  109. end
  110. it 'does X-On-Behalf-Of auth - ticket create admin for unknown' do
  111. params = {
  112. title: 'a new ticket #3',
  113. group: 'Users',
  114. priority: '2 normal',
  115. state: 'new',
  116. customer_id: customer.id,
  117. article: {
  118. body: 'some test 123',
  119. },
  120. }
  121. authenticated_as(admin, on_behalf_of: 99_449_494_949)
  122. post '/api/v1/tickets', params: params, as: :json
  123. expect(response).to have_http_status(:forbidden)
  124. expect(@response.header).not_to be_key('Access-Control-Allow-Origin')
  125. expect(json_response).to be_a_kind_of(Hash)
  126. expect(json_response['error']).to eq("No such user '99449494949'")
  127. end
  128. it 'does X-On-Behalf-Of auth - ticket create customer for admin' do
  129. params = {
  130. title: 'a new ticket #3',
  131. group: 'Users',
  132. priority: '2 normal',
  133. state: 'new',
  134. customer_id: customer.id,
  135. article: {
  136. body: 'some test 123',
  137. },
  138. }
  139. authenticated_as(customer, on_behalf_of: admin.email)
  140. post '/api/v1/tickets', params: params, as: :json
  141. expect(response).to have_http_status(:forbidden)
  142. expect(@response.header).not_to be_key('Access-Control-Allow-Origin')
  143. expect(json_response).to be_a_kind_of(Hash)
  144. expect(json_response['error']).to eq("Current user has no permission to use 'X-On-Behalf-Of'!")
  145. end
  146. it 'does X-On-Behalf-Of auth - ticket create admin for customer by email but no permitted action' do
  147. params = {
  148. title: 'a new ticket #3',
  149. group: 'secret1234',
  150. priority: '2 normal',
  151. state: 'new',
  152. customer_id: customer.id,
  153. article: {
  154. body: 'some test 123',
  155. },
  156. }
  157. authenticated_as(admin, on_behalf_of: customer.email)
  158. post '/api/v1/tickets', params: params, as: :json
  159. expect(response).to have_http_status(:unprocessable_entity)
  160. expect(@response.header).not_to be_key('Access-Control-Allow-Origin')
  161. expect(json_response).to be_a_kind_of(Hash)
  162. expect(json_response['error']).to eq('No lookup value found for \'group\': "secret1234"')
  163. end
  164. context 'when Token Admin has no ticket.* permission' do
  165. let(:admin) { create(:user, firstname: 'Requester', roles: [admin_user_role]) }
  166. let(:token) { create(:token, user: admin, permissions: %w[admin.user]) }
  167. let(:admin_user_role) do
  168. create(:role).tap { |role| role.permission_grant('admin.user') }
  169. end
  170. it 'creates Ticket because of behalf of user permission' do
  171. params = {
  172. title: 'a new ticket #3',
  173. group: 'Users',
  174. priority: '2 normal',
  175. state: 'new',
  176. customer_id: customer.id,
  177. article: {
  178. body: 'some test 123',
  179. },
  180. }
  181. authenticated_as(admin, on_behalf_of: customer.email, token: token)
  182. post '/api/v1/tickets', params: params, as: :json
  183. expect(response).to have_http_status(:created)
  184. expect(json_response).to be_a_kind_of(Hash)
  185. expect(customer.id).to eq(json_response['created_by_id'])
  186. end
  187. end
  188. context 'when customer account has device user permission' do
  189. let(:customer_user_devices_role) do
  190. create(:role).tap { |role| role.permission_grant('user_preferences.device') }
  191. end
  192. let(:customer) do
  193. create(:customer, firstname: 'Behalf of', role_ids: Role.signup_role_ids.push(customer_user_devices_role.id))
  194. end
  195. it 'creates Ticket because of behalf of customer user, which should not trigger a new user device' do
  196. params = {
  197. title: 'a new ticket #3',
  198. group: 'Users',
  199. priority: '2 normal',
  200. state: 'new',
  201. customer_id: customer.id,
  202. article: {
  203. body: 'some test 123',
  204. },
  205. }
  206. authenticated_as(admin, on_behalf_of: customer.email)
  207. post '/api/v1/tickets', params: params, as: :json
  208. expect(response).to have_http_status(:created)
  209. expect(customer.id).to eq(json_response['created_by_id'])
  210. expect { Scheduler.worker(true) }.to change(UserDevice, :count).by(0)
  211. end
  212. end
  213. end
  214. describe 'user lookup' do
  215. it 'does X-On-Behalf-Of auth - user lookup by ID' do
  216. authenticated_as(admin, on_behalf_of: customer.id)
  217. get '/api/v1/users/me', as: :json
  218. expect(json_response.fetch('id')).to be customer.id
  219. end
  220. it 'does X-On-Behalf-Of auth - user lookup by login' do
  221. authenticated_as(admin, on_behalf_of: customer.login)
  222. get '/api/v1/users/me', as: :json
  223. expect(json_response.fetch('id')).to be customer.id
  224. end
  225. it 'does X-On-Behalf-Of auth - user lookup by email' do
  226. authenticated_as(admin, on_behalf_of: customer.email)
  227. get '/api/v1/users/me', as: :json
  228. expect(json_response.fetch('id')).to be customer.id
  229. end
  230. # https://github.com/zammad/zammad/issues/2851
  231. it 'does X-On-Behalf-Of auth - user lookup by email even if email starts with a digit' do
  232. customer.update! email: "#{agent.id}#{customer.email}"
  233. authenticated_as(admin, on_behalf_of: customer.email)
  234. get '/api/v1/users/me', as: :json
  235. expect(json_response.fetch('id')).to be customer.id
  236. end
  237. end
  238. end