user_spec.rb 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. require 'rails_helper'
  2. RSpec.describe 'User', type: :request, searchindex: true do
  3. let!(:admin_user) do
  4. create(
  5. :admin_user,
  6. groups: Group.all,
  7. login: 'rest-admin',
  8. firstname: 'Rest',
  9. lastname: 'Agent',
  10. email: 'rest-admin@example.com',
  11. )
  12. end
  13. let!(:admin_user_pw) do
  14. create(
  15. :admin_user,
  16. groups: Group.all,
  17. login: 'rest-admin-pw',
  18. firstname: 'Rest',
  19. lastname: 'Agent',
  20. email: 'rest-admin-pw@example.com',
  21. password: 'adminpw',
  22. )
  23. end
  24. let!(:agent_user) do
  25. create(
  26. :agent_user,
  27. groups: Group.all,
  28. login: 'rest-agent@example.com',
  29. firstname: 'Rest',
  30. lastname: 'Agent',
  31. email: 'rest-agent@example.com',
  32. )
  33. end
  34. let!(:customer_user) do
  35. create(
  36. :customer_user,
  37. login: 'rest-customer1@example.com',
  38. firstname: 'Rest',
  39. lastname: 'Customer1',
  40. email: 'rest-customer1@example.com',
  41. )
  42. end
  43. let!(:organization) do
  44. create(:organization, name: 'Rest Org')
  45. end
  46. let!(:organization2) do
  47. create(:organization, name: 'Rest Org #2')
  48. end
  49. let!(:organization3) do
  50. create(:organization, name: 'Rest Org #3')
  51. end
  52. let!(:customer_user2) do
  53. create(
  54. :customer_user,
  55. organization: organization,
  56. login: 'rest-customer2@example.com',
  57. firstname: 'Rest',
  58. lastname: 'Customer2',
  59. email: 'rest-customer2@example.com',
  60. )
  61. end
  62. before do
  63. configure_elasticsearch do
  64. travel 1.minute
  65. rebuild_searchindex
  66. # execute background jobs
  67. Scheduler.worker(true)
  68. sleep 6
  69. end
  70. end
  71. describe 'request handling' do
  72. it 'does user create tests - no user' do
  73. post '/api/v1/signshow', params: {}, as: :json
  74. # create user with disabled feature
  75. Setting.set('user_create_account', false)
  76. token = @response.headers['CSRF-TOKEN']
  77. # token based on form
  78. params = { email: 'some_new_customer@example.com', authenticity_token: token }
  79. post '/api/v1/users', params: params, as: :json
  80. expect(response).to have_http_status(:unprocessable_entity)
  81. expect(json_response['error']).to be_truthy
  82. expect(json_response['error']).to eq('Feature not enabled!')
  83. # token based on headers
  84. headers = { 'X-CSRF-Token' => token }
  85. params = { email: 'some_new_customer@example.com' }
  86. post '/api/v1/users', params: params, headers: headers, as: :json
  87. expect(response).to have_http_status(:unprocessable_entity)
  88. expect(json_response['error']).to be_truthy
  89. expect(json_response['error']).to eq('Feature not enabled!')
  90. Setting.set('user_create_account', true)
  91. # no signup param with enabled feature
  92. params = { email: 'some_new_customer@example.com' }
  93. post '/api/v1/users', params: params, headers: headers, as: :json
  94. expect(response).to have_http_status(:unprocessable_entity)
  95. expect(json_response['error']).to be_truthy
  96. expect(json_response['error']).to eq('Only signup with not authenticate user possible!')
  97. # already existing user with enabled feature
  98. params = { email: 'rest-customer1@example.com', signup: true }
  99. post '/api/v1/users', params: params, headers: headers, as: :json
  100. expect(response).to have_http_status(:unprocessable_entity)
  101. expect(json_response['error']).to be_truthy
  102. expect(json_response['error']).to eq('Email address is already used for other user.')
  103. # email missing with enabled feature
  104. params = { firstname: 'some firstname', signup: true }
  105. post '/api/v1/users', params: params, headers: headers, as: :json
  106. expect(response).to have_http_status(:unprocessable_entity)
  107. expect(json_response['error']).to be_truthy
  108. expect(json_response['error']).to eq('Attribute \'email\' required!')
  109. # email missing with enabled feature
  110. params = { firstname: 'some firstname', signup: true }
  111. post '/api/v1/users', params: params, headers: headers, as: :json
  112. expect(response).to have_http_status(:unprocessable_entity)
  113. expect(json_response['error']).to be_truthy
  114. expect(json_response['error']).to eq('Attribute \'email\' required!')
  115. # create user with enabled feature (take customer role)
  116. params = { firstname: 'Me First', lastname: 'Me Last', email: 'new_here@example.com', signup: true }
  117. post '/api/v1/users', params: params, headers: headers, as: :json
  118. expect(response).to have_http_status(:created)
  119. expect(json_response).to be_truthy
  120. expect(json_response['firstname']).to eq('Me First')
  121. expect(json_response['lastname']).to eq('Me Last')
  122. expect(json_response['login']).to eq('new_here@example.com')
  123. expect(json_response['email']).to eq('new_here@example.com')
  124. user = User.find(json_response['id'])
  125. expect(user).not_to be_role('Admin')
  126. expect(user).not_to be_role('Agent')
  127. expect(user).to be_role('Customer')
  128. # create user with admin role (not allowed for signup, take customer role)
  129. role = Role.lookup(name: 'Admin')
  130. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin@example.com', role_ids: [ role.id ], signup: true }
  131. post '/api/v1/users', params: params, headers: headers, as: :json
  132. expect(response).to have_http_status(:created)
  133. expect(json_response).to be_truthy
  134. user = User.find(json_response['id'])
  135. expect(user).not_to be_role('Admin')
  136. expect(user).not_to be_role('Agent')
  137. expect(user).to be_role('Customer')
  138. # create user with agent role (not allowed for signup, take customer role)
  139. role = Role.lookup(name: 'Agent')
  140. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent@example.com', role_ids: [ role.id ], signup: true }
  141. post '/api/v1/users', params: params, headers: headers, as: :json
  142. expect(response).to have_http_status(:created)
  143. expect(json_response).to be_truthy
  144. user = User.find(json_response['id'])
  145. expect(user).not_to be_role('Admin')
  146. expect(user).not_to be_role('Agent')
  147. expect(user).to be_role('Customer')
  148. # no user (because of no session)
  149. get '/api/v1/users', params: {}, headers: headers, as: :json
  150. expect(response).to have_http_status(:unauthorized)
  151. expect(json_response['error']).to eq('authentication failed')
  152. # me
  153. get '/api/v1/users/me', params: {}, headers: headers, as: :json
  154. expect(response).to have_http_status(:unauthorized)
  155. expect(json_response['error']).to eq('authentication failed')
  156. end
  157. it 'does auth tests - not existing user' do
  158. authenticated_as(nil, login: 'not_existing@example.com', password: 'adminpw')
  159. get '/api/v1/users/me', params: {}, as: :json
  160. expect(response).to have_http_status(:unauthorized)
  161. expect(json_response['error']).to eq('authentication failed')
  162. get '/api/v1/users', params: {}, as: :json
  163. expect(response).to have_http_status(:unauthorized)
  164. expect(json_response['error']).to eq('authentication failed')
  165. end
  166. it 'does auth tests - username auth, wrong pw' do
  167. authenticated_as(admin_user, password: 'not_existing')
  168. get '/api/v1/users', params: {}, as: :json
  169. expect(response).to have_http_status(:unauthorized)
  170. expect(json_response['error']).to eq('authentication failed')
  171. end
  172. it 'does auth tests - email auth, wrong pw' do
  173. authenticated_as(nil, login: 'rest-admin@example.com', password: 'not_existing')
  174. get '/api/v1/users', params: {}, as: :json
  175. expect(response).to have_http_status(:unauthorized)
  176. expect(json_response['error']).to eq('authentication failed')
  177. end
  178. it 'does auth tests - username auth' do
  179. authenticated_as(nil, login: 'rest-admin-pw', password: 'adminpw')
  180. get '/api/v1/users', params: {}, as: :json
  181. expect(response).to have_http_status(:ok)
  182. expect(json_response).to be_truthy
  183. end
  184. it 'does auth tests - email auth' do
  185. authenticated_as(nil, login: 'rest-admin-pw@example.com', password: 'adminpw')
  186. get '/api/v1/users', params: {}, as: :json
  187. expect(response).to have_http_status(:ok)
  188. expect(json_response).to be_truthy
  189. end
  190. it 'does user index and create with admin' do
  191. authenticated_as(admin_user)
  192. get '/api/v1/users/me', params: {}, as: :json
  193. expect(response).to have_http_status(:ok)
  194. expect(json_response).to be_truthy
  195. expect('rest-admin@example.com').to eq(json_response['email'])
  196. # index
  197. get '/api/v1/users', params: {}, as: :json
  198. expect(response).to have_http_status(:ok)
  199. expect(json_response).to be_truthy
  200. # index
  201. get '/api/v1/users', params: {}, as: :json
  202. expect(response).to have_http_status(:ok)
  203. expect(json_response).to be_truthy
  204. expect(Array).to eq(json_response.class)
  205. expect(json_response.length >= 3).to be_truthy
  206. # show/:id
  207. get "/api/v1/users/#{agent_user.id}", params: {}, as: :json
  208. expect(response).to have_http_status(:ok)
  209. expect(json_response).to be_truthy
  210. expect(Hash).to eq(json_response.class)
  211. expect('rest-agent@example.com').to eq(json_response['email'])
  212. get "/api/v1/users/#{customer_user.id}", params: {}, as: :json
  213. expect(response).to have_http_status(:ok)
  214. expect(json_response).to be_truthy
  215. expect(Hash).to eq(json_response.class)
  216. expect('rest-customer1@example.com').to eq(json_response['email'])
  217. # create user with admin role
  218. role = Role.lookup(name: 'Admin')
  219. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_admin@example.com', role_ids: [ role.id ] }
  220. post '/api/v1/users', params: params, as: :json
  221. expect(response).to have_http_status(:created)
  222. expect(json_response).to be_truthy
  223. user = User.find(json_response['id'])
  224. expect(user).to be_role('Admin')
  225. expect(user).not_to be_role('Agent')
  226. expect(user).not_to be_role('Customer')
  227. expect(json_response['login']).to eq('new_admin_by_admin@example.com')
  228. expect(json_response['email']).to eq('new_admin_by_admin@example.com')
  229. # create user with agent role
  230. role = Role.lookup(name: 'Agent')
  231. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_admin1@example.com', role_ids: [ role.id ] }
  232. post '/api/v1/users', params: params, as: :json
  233. expect(response).to have_http_status(:created)
  234. expect(json_response).to be_truthy
  235. user = User.find(json_response['id'])
  236. expect(user).not_to be_role('Admin')
  237. expect(user).to be_role('Agent')
  238. expect(user).not_to be_role('Customer')
  239. expect(json_response['login']).to eq('new_agent_by_admin1@example.com')
  240. expect(json_response['email']).to eq('new_agent_by_admin1@example.com')
  241. role = Role.lookup(name: 'Agent')
  242. params = { firstname: 'Agent First', email: 'new_agent_by_admin2@example.com', role_ids: [ role.id ] }
  243. post '/api/v1/users', params: params, as: :json
  244. expect(response).to have_http_status(:created)
  245. expect(json_response).to be_truthy
  246. user = User.find(json_response['id'])
  247. expect(user).not_to be_role('Admin')
  248. expect(user).to be_role('Agent')
  249. expect(user).not_to be_role('Customer')
  250. expect(json_response['login']).to eq('new_agent_by_admin2@example.com')
  251. expect(json_response['email']).to eq('new_agent_by_admin2@example.com')
  252. expect(json_response['firstname']).to eq('Agent')
  253. expect(json_response['lastname']).to eq('First')
  254. role = Role.lookup(name: 'Agent')
  255. params = { firstname: 'Agent First', email: 'new_agent_by_admin2@example.com', role_ids: [ role.id ] }
  256. post '/api/v1/users', params: params, as: :json
  257. expect(response).to have_http_status(:unprocessable_entity)
  258. expect(json_response).to be_truthy
  259. expect(json_response['error']).to eq('Email address is already used for other user.')
  260. # missing required attributes
  261. params = { note: 'some note' }
  262. post '/api/v1/users', params: params, as: :json
  263. expect(response).to have_http_status(:unprocessable_entity)
  264. expect(json_response).to be_truthy
  265. expect(json_response['error']).to eq('Minimum one identifier (login, firstname, lastname, phone or email) for user is required.')
  266. # invalid email
  267. params = { firstname: 'newfirstname123', email: 'some_what', note: 'some note' }
  268. post '/api/v1/users', params: params, as: :json
  269. expect(response).to have_http_status(:unprocessable_entity)
  270. expect(json_response).to be_truthy
  271. expect(json_response['error']).to eq('Invalid email')
  272. # with valid attributes
  273. params = { firstname: 'newfirstname123', note: 'some note' }
  274. post '/api/v1/users', params: params, as: :json
  275. expect(response).to have_http_status(:created)
  276. expect(json_response).to be_truthy
  277. user = User.find(json_response['id'])
  278. expect(user).not_to be_role('Admin')
  279. expect(user).not_to be_role('Agent')
  280. expect(user).to be_role('Customer')
  281. expect(json_response['login']).to be_start_with('auto-')
  282. expect(json_response['email']).to eq('')
  283. expect(json_response['firstname']).to eq('newfirstname123')
  284. expect(json_response['lastname']).to eq('')
  285. end
  286. it 'does user index and create with agent' do
  287. authenticated_as(agent_user)
  288. get '/api/v1/users/me', params: {}, as: :json
  289. expect(response).to have_http_status(:ok)
  290. expect(json_response).to be_truthy
  291. expect('rest-agent@example.com').to eq(json_response['email'])
  292. # index
  293. get '/api/v1/users', params: {}, as: :json
  294. expect(response).to have_http_status(:ok)
  295. expect(json_response).to be_truthy
  296. # index
  297. get '/api/v1/users', params: {}, as: :json
  298. expect(response).to have_http_status(:ok)
  299. expect(json_response).to be_truthy
  300. expect(Array).to eq(json_response.class)
  301. expect(json_response.length >= 3).to be_truthy
  302. get '/api/v1/users?limit=40&page=1&per_page=2', params: {}, as: :json
  303. expect(response).to have_http_status(:ok)
  304. expect(json_response).to be_a_kind_of(Array)
  305. users = User.order(:id).limit(2)
  306. expect(json_response[0]['id']).to eq(users[0].id)
  307. expect(json_response[1]['id']).to eq(users[1].id)
  308. expect(json_response.count).to eq(2)
  309. get '/api/v1/users?limit=40&page=2&per_page=2', params: {}, as: :json
  310. expect(response).to have_http_status(:ok)
  311. expect(json_response).to be_a_kind_of(Array)
  312. users = User.order(:id).limit(4)
  313. expect(json_response[0]['id']).to eq(users[2].id)
  314. expect(json_response[1]['id']).to eq(users[3].id)
  315. expect(json_response.count).to eq(2)
  316. # create user with admin role
  317. firstname = "First test#{rand(999_999_999)}"
  318. role = Role.lookup(name: 'Admin')
  319. params = { firstname: "Admin#{firstname}", lastname: 'Admin Last', email: 'new_admin_by_agent@example.com', role_ids: [ role.id ] }
  320. post '/api/v1/users', params: params, as: :json
  321. expect(response).to have_http_status(:created)
  322. json_response_user1 = JSON.parse(@response.body)
  323. expect(json_response_user1).to be_truthy
  324. user = User.find(json_response_user1['id'])
  325. expect(user).not_to be_role('Admin')
  326. expect(user).not_to be_role('Agent')
  327. expect(user).to be_role('Customer')
  328. expect(json_response_user1['login']).to eq('new_admin_by_agent@example.com')
  329. expect(json_response_user1['email']).to eq('new_admin_by_agent@example.com')
  330. # create user with agent role
  331. role = Role.lookup(name: 'Agent')
  332. params = { firstname: "Agent#{firstname}", lastname: 'Agent Last', email: 'new_agent_by_agent@example.com', role_ids: [ role.id ] }
  333. post '/api/v1/users', params: params, as: :json
  334. expect(response).to have_http_status(:created)
  335. json_response_user1 = JSON.parse(@response.body)
  336. expect(json_response_user1).to be_truthy
  337. user = User.find(json_response_user1['id'])
  338. expect(user).not_to be_role('Admin')
  339. expect(user).not_to be_role('Agent')
  340. expect(user).to be_role('Customer')
  341. expect(json_response_user1['login']).to eq('new_agent_by_agent@example.com')
  342. expect(json_response_user1['email']).to eq('new_agent_by_agent@example.com')
  343. # create user with customer role
  344. role = Role.lookup(name: 'Customer')
  345. params = { firstname: "Customer#{firstname}", lastname: 'Customer Last', email: 'new_customer_by_agent@example.com', role_ids: [ role.id ] }
  346. post '/api/v1/users', params: params, as: :json
  347. expect(response).to have_http_status(:created)
  348. json_response_user1 = JSON.parse(@response.body)
  349. expect(json_response_user1).to be_truthy
  350. user = User.find(json_response_user1['id'])
  351. expect(user).not_to be_role('Admin')
  352. expect(user).not_to be_role('Agent')
  353. expect(user).to be_role('Customer')
  354. expect(json_response_user1['login']).to eq('new_customer_by_agent@example.com')
  355. expect(json_response_user1['email']).to eq('new_customer_by_agent@example.com')
  356. # search as agent
  357. Scheduler.worker(true)
  358. sleep 2 # let es time to come ready
  359. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}", params: {}, as: :json
  360. expect(response).to have_http_status(:ok)
  361. expect(json_response).to be_a_kind_of(Array)
  362. expect(json_response[0]['id']).to eq(json_response_user1['id'])
  363. expect(json_response[0]['firstname']).to eq("Customer#{firstname}")
  364. expect(json_response[0]['lastname']).to eq('Customer Last')
  365. expect(json_response[0]['role_ids']).to be_truthy
  366. expect(json_response[0]['roles']).to be_falsey
  367. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&expand=true", params: {}, as: :json
  368. expect(response).to have_http_status(:ok)
  369. expect(json_response).to be_a_kind_of(Array)
  370. expect(json_response[0]['id']).to eq(json_response_user1['id'])
  371. expect(json_response[0]['firstname']).to eq("Customer#{firstname}")
  372. expect(json_response[0]['lastname']).to eq('Customer Last')
  373. expect(json_response[0]['role_ids']).to be_truthy
  374. expect(json_response[0]['roles']).to be_truthy
  375. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&label=true", params: {}, as: :json
  376. expect(response).to have_http_status(:ok)
  377. expect(json_response).to be_a_kind_of(Array)
  378. expect(json_response[0]['id']).to eq(json_response_user1['id'])
  379. expect(json_response[0]['label']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  380. expect(json_response[0]['value']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  381. expect(json_response[0]['role_ids']).to be_falsey
  382. expect(json_response[0]['roles']).to be_falsey
  383. get "/api/v1/users/search?term=#{CGI.escape("Customer#{firstname}")}", params: {}, as: :json
  384. expect(response).to have_http_status(:ok)
  385. expect(json_response).to be_a_kind_of(Array)
  386. expect(json_response[0]['id']).to eq(json_response_user1['id'])
  387. expect(json_response[0]['label']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  388. expect(json_response[0]['value']).to eq('new_customer_by_agent@example.com')
  389. expect(json_response[0]['role_ids']).to be_falsey
  390. expect(json_response[0]['roles']).to be_falsey
  391. # Regression test for issue #2539 - search pagination broken in users_controller.rb
  392. # Get the total number of users N, then search with one result per page, so there should N pages with one result each
  393. get '/api/v1/users/search', params: { query: '*' }, as: :json
  394. total_user_number = json_response.count
  395. (1..total_user_number).each do |i|
  396. get '/api/v1/users/search', params: { query: '*', per_page: 1, page: i }, as: :json
  397. expect(response).to have_http_status(:ok)
  398. expect(json_response).to be_a_kind_of(Array)
  399. expect(json_response.count).to eq(1), "Page #{i}/#{total_user_number} of the user search pagination test have the wrong result!"
  400. end
  401. role = Role.find_by(name: 'Agent')
  402. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, as: :json
  403. expect(response).to have_http_status(:ok)
  404. expect(json_response).to be_a_kind_of(Array)
  405. expect(json_response.count).to eq(0)
  406. role = Role.find_by(name: 'Customer')
  407. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, as: :json
  408. expect(response).to have_http_status(:ok)
  409. expect(json_response).to be_a_kind_of(Array)
  410. expect(json_response[0]['id']).to eq(json_response_user1['id'])
  411. expect(json_response[0]['label']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  412. expect(json_response[0]['value']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  413. expect(json_response[0]['role_ids']).to be_falsey
  414. expect(json_response[0]['roles']).to be_falsey
  415. permission = Permission.find_by(name: 'ticket.agent')
  416. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, as: :json
  417. expect(response).to have_http_status(:ok)
  418. expect(json_response).to be_a_kind_of(Array)
  419. expect(json_response.count).to eq(0)
  420. permission = Permission.find_by(name: 'ticket.customer')
  421. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, as: :json
  422. expect(response).to have_http_status(:ok)
  423. expect(json_response).to be_a_kind_of(Array)
  424. expect(json_response[0]['id']).to eq(json_response_user1['id'])
  425. expect(json_response[0]['label']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  426. expect(json_response[0]['value']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  427. expect(json_response[0]['role_ids']).to be_falsey
  428. expect(json_response[0]['roles']).to be_falsey
  429. end
  430. it 'does user index and create with customer1' do
  431. authenticated_as(customer_user)
  432. get '/api/v1/users/me', params: {}, as: :json
  433. expect(response).to have_http_status(:ok)
  434. expect(json_response).to be_truthy
  435. expect('rest-customer1@example.com').to eq(json_response['email'])
  436. # index
  437. get '/api/v1/users', params: {}, as: :json
  438. expect(response).to have_http_status(:ok)
  439. expect(Array).to eq(json_response.class)
  440. expect(1).to eq(json_response.length)
  441. # show/:id
  442. get "/api/v1/users/#{customer_user.id}", params: {}, as: :json
  443. expect(response).to have_http_status(:ok)
  444. expect(Hash).to eq(json_response.class)
  445. expect('rest-customer1@example.com').to eq(json_response['email'])
  446. get "/api/v1/users/#{customer_user2.id}", params: {}, as: :json
  447. expect(response).to have_http_status(:unauthorized)
  448. expect(Hash).to eq(json_response.class)
  449. expect(json_response['error']).to be_truthy
  450. # create user with admin role
  451. role = Role.lookup(name: 'Admin')
  452. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_customer1@example.com', role_ids: [ role.id ] }
  453. post '/api/v1/users', params: params, as: :json
  454. expect(response).to have_http_status(:unauthorized)
  455. # create user with agent role
  456. role = Role.lookup(name: 'Agent')
  457. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_customer1@example.com', role_ids: [ role.id ] }
  458. post '/api/v1/users', params: params, as: :json
  459. expect(response).to have_http_status(:unauthorized)
  460. # search
  461. Scheduler.worker(true)
  462. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, as: :json
  463. expect(response).to have_http_status(:unauthorized)
  464. end
  465. it 'does user index with customer2' do
  466. authenticated_as(customer_user2)
  467. get '/api/v1/users/me', params: {}, as: :json
  468. expect(response).to have_http_status(:ok)
  469. expect(json_response).to be_truthy
  470. expect('rest-customer2@example.com').to eq(json_response['email'])
  471. # index
  472. get '/api/v1/users', params: {}, as: :json
  473. expect(response).to have_http_status(:ok)
  474. expect(Array).to eq(json_response.class)
  475. expect(1).to eq(json_response.length)
  476. # show/:id
  477. get "/api/v1/users/#{customer_user2.id}", params: {}, as: :json
  478. expect(response).to have_http_status(:ok)
  479. expect(Hash).to eq(json_response.class)
  480. expect('rest-customer2@example.com').to eq(json_response['email'])
  481. get "/api/v1/users/#{customer_user.id}", params: {}, as: :json
  482. expect(response).to have_http_status(:unauthorized)
  483. expect(Hash).to eq(json_response.class)
  484. expect(json_response['error']).to be_truthy
  485. # search
  486. Scheduler.worker(true)
  487. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, as: :json
  488. expect(response).to have_http_status(:unauthorized)
  489. end
  490. it 'does users show and response format (04.01)' do
  491. user = create(
  492. :customer_user,
  493. login: 'rest-customer3@example.com',
  494. firstname: 'Rest',
  495. lastname: 'Customer3',
  496. email: 'rest-customer3@example.com',
  497. password: 'customer3pw',
  498. active: true,
  499. organization: organization,
  500. updated_by_id: admin_user.id,
  501. created_by_id: admin_user.id,
  502. )
  503. authenticated_as(admin_user)
  504. get "/api/v1/users/#{user.id}", params: {}, as: :json
  505. expect(response).to have_http_status(:ok)
  506. expect(json_response).to be_a_kind_of(Hash)
  507. expect(json_response['id']).to eq(user.id)
  508. expect(json_response['firstname']).to eq(user.firstname)
  509. expect(json_response['organization']).to be_falsey
  510. expect(json_response['organization_id']).to eq(user.organization_id)
  511. expect(json_response['password']).to be_falsey
  512. expect(json_response['role_ids']).to eq(user.role_ids)
  513. expect(json_response['updated_by_id']).to eq(admin_user.id)
  514. expect(json_response['created_by_id']).to eq(admin_user.id)
  515. get "/api/v1/users/#{user.id}?expand=true", params: {}, as: :json
  516. expect(response).to have_http_status(:ok)
  517. expect(json_response).to be_a_kind_of(Hash)
  518. expect(json_response['id']).to eq(user.id)
  519. expect(json_response['firstname']).to eq(user.firstname)
  520. expect(json_response['organization_id']).to eq(user.organization_id)
  521. expect(json_response['organization']).to eq(user.organization.name)
  522. expect(json_response['role_ids']).to eq(user.role_ids)
  523. expect(json_response['password']).to be_falsey
  524. expect(json_response['updated_by_id']).to eq(admin_user.id)
  525. expect(json_response['created_by_id']).to eq(admin_user.id)
  526. get "/api/v1/users/#{user.id}?expand=false", params: {}, as: :json
  527. expect(response).to have_http_status(:ok)
  528. expect(json_response).to be_a_kind_of(Hash)
  529. expect(json_response['id']).to eq(user.id)
  530. expect(json_response['firstname']).to eq(user.firstname)
  531. expect(json_response['organization']).to be_falsey
  532. expect(json_response['organization_id']).to eq(user.organization_id)
  533. expect(json_response['password']).to be_falsey
  534. expect(json_response['role_ids']).to eq(user.role_ids)
  535. expect(json_response['updated_by_id']).to eq(admin_user.id)
  536. expect(json_response['created_by_id']).to eq(admin_user.id)
  537. get "/api/v1/users/#{user.id}?full=true", params: {}, as: :json
  538. expect(response).to have_http_status(:ok)
  539. expect(json_response).to be_a_kind_of(Hash)
  540. expect(json_response['id']).to eq(user.id)
  541. expect(json_response['assets']).to be_truthy
  542. expect(json_response['assets']['User']).to be_truthy
  543. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  544. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  545. expect(json_response['assets']['User'][user.id.to_s]['firstname']).to eq(user.firstname)
  546. expect(json_response['assets']['User'][user.id.to_s]['organization_id']).to eq(user.organization_id)
  547. expect(json_response['assets']['User'][user.id.to_s]['role_ids']).to eq(user.role_ids)
  548. get "/api/v1/users/#{user.id}?full=false", params: {}, as: :json
  549. expect(response).to have_http_status(:ok)
  550. expect(json_response).to be_a_kind_of(Hash)
  551. expect(json_response['id']).to eq(user.id)
  552. expect(json_response['firstname']).to eq(user.firstname)
  553. expect(json_response['organization']).to be_falsey
  554. expect(json_response['organization_id']).to eq(user.organization_id)
  555. expect(json_response['password']).to be_falsey
  556. expect(json_response['role_ids']).to eq(user.role_ids)
  557. expect(json_response['updated_by_id']).to eq(admin_user.id)
  558. expect(json_response['created_by_id']).to eq(admin_user.id)
  559. end
  560. it 'does user index and response format (04.02)' do
  561. user = create(
  562. :customer_user,
  563. login: 'rest-customer3@example.com',
  564. firstname: 'Rest',
  565. lastname: 'Customer3',
  566. email: 'rest-customer3@example.com',
  567. password: 'customer3pw',
  568. active: true,
  569. organization: organization,
  570. updated_by_id: admin_user.id,
  571. created_by_id: admin_user.id,
  572. )
  573. authenticated_as(admin_user)
  574. get '/api/v1/users', params: {}, as: :json
  575. expect(response).to have_http_status(:ok)
  576. expect(json_response).to be_a_kind_of(Array)
  577. expect(json_response[0].class).to eq(Hash)
  578. expect(json_response.last['id']).to eq(user.id)
  579. expect(json_response.last['lastname']).to eq(user.lastname)
  580. expect(json_response.last['organization']).to be_falsey
  581. expect(json_response.last['role_ids']).to eq(user.role_ids)
  582. expect(json_response.last['organization_id']).to eq(user.organization_id)
  583. expect(json_response.last['password']).to be_falsey
  584. expect(json_response.last['updated_by_id']).to eq(admin_user.id)
  585. expect(json_response.last['created_by_id']).to eq(admin_user.id)
  586. get '/api/v1/users?expand=true', params: {}, as: :json
  587. expect(response).to have_http_status(:ok)
  588. expect(json_response).to be_a_kind_of(Array)
  589. expect(json_response[0].class).to eq(Hash)
  590. expect(json_response.last['id']).to eq(user.id)
  591. expect(json_response.last['lastname']).to eq(user.lastname)
  592. expect(json_response.last['organization_id']).to eq(user.organization_id)
  593. expect(json_response.last['organization']).to eq(user.organization.name)
  594. expect(json_response.last['password']).to be_falsey
  595. expect(json_response.last['updated_by_id']).to eq(admin_user.id)
  596. expect(json_response.last['created_by_id']).to eq(admin_user.id)
  597. get '/api/v1/users?expand=false', params: {}, as: :json
  598. expect(response).to have_http_status(:ok)
  599. expect(json_response).to be_a_kind_of(Array)
  600. expect(json_response[0].class).to eq(Hash)
  601. expect(json_response.last['id']).to eq(user.id)
  602. expect(json_response.last['lastname']).to eq(user.lastname)
  603. expect(json_response.last['organization']).to be_falsey
  604. expect(json_response.last['role_ids']).to eq(user.role_ids)
  605. expect(json_response.last['organization_id']).to eq(user.organization_id)
  606. expect(json_response.last['password']).to be_falsey
  607. expect(json_response.last['updated_by_id']).to eq(admin_user.id)
  608. expect(json_response.last['created_by_id']).to eq(admin_user.id)
  609. get '/api/v1/users?full=true', params: {}, as: :json
  610. expect(response).to have_http_status(:ok)
  611. expect(json_response).to be_a_kind_of(Hash)
  612. expect(json_response['record_ids'].class).to eq(Array)
  613. expect(json_response['record_ids'][0]).to eq(1)
  614. expect(json_response['record_ids'].last).to eq(user.id)
  615. expect(json_response['assets']).to be_truthy
  616. expect(json_response['assets']['User']).to be_truthy
  617. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  618. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  619. expect(json_response['assets']['User'][user.id.to_s]['lastname']).to eq(user.lastname)
  620. expect(json_response['assets']['User'][user.id.to_s]['organization_id']).to eq(user.organization_id)
  621. expect(json_response['assets']['User'][user.id.to_s]['password']).to be_falsey
  622. get '/api/v1/users?full=false', params: {}, as: :json
  623. expect(response).to have_http_status(:ok)
  624. expect(json_response).to be_a_kind_of(Array)
  625. expect(json_response[0].class).to eq(Hash)
  626. expect(json_response.last['id']).to eq(user.id)
  627. expect(json_response.last['lastname']).to eq(user.lastname)
  628. expect(json_response.last['organization']).to be_falsey
  629. expect(json_response.last['role_ids']).to eq(user.role_ids)
  630. expect(json_response.last['organization_id']).to eq(user.organization_id)
  631. expect(json_response.last['password']).to be_falsey
  632. expect(json_response.last['updated_by_id']).to eq(admin_user.id)
  633. expect(json_response.last['created_by_id']).to eq(admin_user.id)
  634. end
  635. it 'does ticket create and response format (04.03)' do
  636. organization = Organization.first
  637. params = {
  638. firstname: 'newfirstname123',
  639. note: 'some note',
  640. organization: organization.name,
  641. }
  642. authenticated_as(admin_user)
  643. post '/api/v1/users', params: params, as: :json
  644. expect(response).to have_http_status(:created)
  645. expect(json_response).to be_a_kind_of(Hash)
  646. user = User.find(json_response['id'])
  647. expect(json_response['firstname']).to eq(user.firstname)
  648. expect(json_response['organization_id']).to eq(user.organization_id)
  649. expect(json_response['organization']).to be_falsey
  650. expect(json_response['password']).to be_falsey
  651. expect(json_response['updated_by_id']).to eq(admin_user.id)
  652. expect(json_response['created_by_id']).to eq(admin_user.id)
  653. post '/api/v1/users?expand=true', params: params, as: :json
  654. expect(response).to have_http_status(:created)
  655. expect(json_response).to be_a_kind_of(Hash)
  656. user = User.find(json_response['id'])
  657. expect(json_response['firstname']).to eq(user.firstname)
  658. expect(json_response['organization_id']).to eq(user.organization_id)
  659. expect(json_response['organization']).to eq(user.organization.name)
  660. expect(json_response['password']).to be_falsey
  661. expect(json_response['updated_by_id']).to eq(admin_user.id)
  662. expect(json_response['created_by_id']).to eq(admin_user.id)
  663. post '/api/v1/users?full=true', params: params, as: :json
  664. expect(response).to have_http_status(:created)
  665. expect(json_response).to be_a_kind_of(Hash)
  666. user = User.find(json_response['id'])
  667. expect(json_response['assets']).to be_truthy
  668. expect(json_response['assets']['User']).to be_truthy
  669. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  670. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  671. expect(json_response['assets']['User'][user.id.to_s]['firstname']).to eq(user.firstname)
  672. expect(json_response['assets']['User'][user.id.to_s]['lastname']).to eq(user.lastname)
  673. expect(json_response['assets']['User'][user.id.to_s]['password']).to be_falsey
  674. expect(json_response['assets']['User'][admin_user.id.to_s]).to be_truthy
  675. expect(json_response['assets']['User'][admin_user.id.to_s]['id']).to eq(admin_user.id)
  676. expect(json_response['assets']['User'][admin_user.id.to_s]['firstname']).to eq(admin_user.firstname)
  677. expect(json_response['assets']['User'][admin_user.id.to_s]['lastname']).to eq(admin_user.lastname)
  678. expect(json_response['assets']['User'][admin_user.id.to_s]['password']).to be_falsey
  679. end
  680. it 'does ticket update and response formats (04.04)' do
  681. user = create(
  682. :customer_user,
  683. login: 'rest-customer3@example.com',
  684. firstname: 'Rest',
  685. lastname: 'Customer3',
  686. email: 'rest-customer3@example.com',
  687. password: 'customer3pw',
  688. active: true,
  689. organization: organization,
  690. updated_by_id: admin_user.id,
  691. created_by_id: admin_user.id,
  692. )
  693. authenticated_as(admin_user)
  694. params = {
  695. firstname: 'a update firstname #1',
  696. }
  697. put "/api/v1/users/#{user.id}", params: params, as: :json
  698. expect(response).to have_http_status(:ok)
  699. expect(json_response).to be_a_kind_of(Hash)
  700. user = User.find(json_response['id'])
  701. expect(json_response['lastname']).to eq(user.lastname)
  702. expect(json_response['firstname']).to eq(params[:firstname])
  703. expect(json_response['organization_id']).to eq(user.organization_id)
  704. expect(json_response['organization']).to be_falsey
  705. expect(json_response['password']).to be_falsey
  706. expect(json_response['updated_by_id']).to eq(admin_user.id)
  707. expect(json_response['created_by_id']).to eq(admin_user.id)
  708. params = {
  709. firstname: 'a update firstname #2',
  710. }
  711. put "/api/v1/users/#{user.id}?expand=true", params: params, as: :json
  712. expect(response).to have_http_status(:ok)
  713. expect(json_response).to be_a_kind_of(Hash)
  714. user = User.find(json_response['id'])
  715. expect(json_response['lastname']).to eq(user.lastname)
  716. expect(json_response['firstname']).to eq(params[:firstname])
  717. expect(json_response['organization_id']).to eq(user.organization_id)
  718. expect(json_response['organization']).to eq(user.organization.name)
  719. expect(json_response['password']).to be_falsey
  720. expect(json_response['updated_by_id']).to eq(admin_user.id)
  721. expect(json_response['created_by_id']).to eq(admin_user.id)
  722. params = {
  723. firstname: 'a update firstname #3',
  724. }
  725. put "/api/v1/users/#{user.id}?full=true", params: params, as: :json
  726. expect(response).to have_http_status(:ok)
  727. expect(json_response).to be_a_kind_of(Hash)
  728. user = User.find(json_response['id'])
  729. expect(json_response['assets']).to be_truthy
  730. expect(json_response['assets']['User']).to be_truthy
  731. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  732. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  733. expect(json_response['assets']['User'][user.id.to_s]['firstname']).to eq(params[:firstname])
  734. expect(json_response['assets']['User'][user.id.to_s]['lastname']).to eq(user.lastname)
  735. expect(json_response['assets']['User'][user.id.to_s]['password']).to be_falsey
  736. expect(json_response['assets']['User'][admin_user.id.to_s]).to be_truthy
  737. expect(json_response['assets']['User'][admin_user.id.to_s]['id']).to eq(admin_user.id)
  738. expect(json_response['assets']['User'][admin_user.id.to_s]['firstname']).to eq(admin_user.firstname)
  739. expect(json_response['assets']['User'][admin_user.id.to_s]['lastname']).to eq(admin_user.lastname)
  740. expect(json_response['assets']['User'][admin_user.id.to_s]['password']).to be_falsey
  741. end
  742. it 'does csv example - customer no access (05.01)' do
  743. authenticated_as(customer_user)
  744. get '/api/v1/users/import_example', params: {}, as: :json
  745. expect(response).to have_http_status(:unauthorized)
  746. expect(json_response['error']).to eq('Not authorized (user)!')
  747. end
  748. it 'does csv example - admin access (05.02)' do
  749. authenticated_as(admin_user)
  750. get '/api/v1/users/import_example', params: {}, as: :json
  751. expect(response).to have_http_status(:ok)
  752. rows = CSV.parse(@response.body)
  753. header = rows.shift
  754. expect(header[0]).to eq('id')
  755. expect(header[1]).to eq('login')
  756. expect(header[2]).to eq('firstname')
  757. expect(header[3]).to eq('lastname')
  758. expect(header[4]).to eq('email')
  759. expect(header).to include('organization')
  760. end
  761. it 'does csv import - admin access (05.03)' do
  762. # invalid file
  763. csv_file = fixture_file_upload('csv_import/user/simple_col_not_existing.csv', 'text/csv')
  764. authenticated_as(admin_user)
  765. post '/api/v1/users/import?try=true', params: { file: csv_file, col_sep: ';' }
  766. expect(response).to have_http_status(:ok)
  767. expect(json_response).to be_a_kind_of(Hash)
  768. expect(json_response['try']).to eq(true)
  769. expect(json_response['records'].count).to eq(2)
  770. expect(json_response['result']).to eq('failed')
  771. expect(json_response['errors'].count).to eq(2)
  772. expect(json_response['errors'][0]).to eq("Line 1: Unable to create record - unknown attribute 'firstname2' for User.")
  773. expect(json_response['errors'][1]).to eq("Line 2: Unable to create record - unknown attribute 'firstname2' for User.")
  774. # valid file try
  775. csv_file = fixture_file_upload('csv_import/user/simple.csv', 'text/csv')
  776. post '/api/v1/users/import?try=true', params: { file: csv_file, col_sep: ';' }
  777. expect(response).to have_http_status(:ok)
  778. expect(json_response).to be_a_kind_of(Hash)
  779. expect(json_response['try']).to eq(true)
  780. expect(json_response['records'].count).to eq(2)
  781. expect(json_response['result']).to eq('success')
  782. expect(User.find_by(login: 'user-simple-import1')).to be_nil
  783. expect(User.find_by(login: 'user-simple-import2')).to be_nil
  784. # valid file
  785. csv_file = fixture_file_upload('csv_import/user/simple.csv', 'text/csv')
  786. post '/api/v1/users/import', params: { file: csv_file, col_sep: ';' }
  787. expect(response).to have_http_status(:ok)
  788. expect(json_response).to be_a_kind_of(Hash)
  789. expect(json_response['try']).to eq(false)
  790. expect(json_response['records'].count).to eq(2)
  791. expect(json_response['result']).to eq('success')
  792. user1 = User.find_by(login: 'user-simple-import1')
  793. expect(user1).to be_truthy
  794. expect(user1.login).to eq('user-simple-import1')
  795. expect(user1.firstname).to eq('firstname-simple-import1')
  796. expect(user1.lastname).to eq('lastname-simple-import1')
  797. expect(user1.email).to eq('user-simple-import1@example.com')
  798. expect(user1.active).to eq(true)
  799. user2 = User.find_by(login: 'user-simple-import2')
  800. expect(user2).to be_truthy
  801. expect(user2.login).to eq('user-simple-import2')
  802. expect(user2.firstname).to eq('firstname-simple-import2')
  803. expect(user2.lastname).to eq('lastname-simple-import2')
  804. expect(user2.email).to eq('user-simple-import2@example.com')
  805. expect(user2.active).to eq(false)
  806. user1.destroy!
  807. user2.destroy!
  808. end
  809. it 'does user history' do
  810. user1 = create(
  811. :customer_user,
  812. login: 'history@example.com',
  813. firstname: 'History',
  814. lastname: 'Customer1',
  815. email: 'history@example.com',
  816. )
  817. authenticated_as(agent_user)
  818. get "/api/v1/users/history/#{user1.id}", params: {}, as: :json
  819. expect(response).to have_http_status(:ok)
  820. expect(json_response).to be_a_kind_of(Hash)
  821. expect(json_response['history'].class).to eq(Array)
  822. expect(json_response['assets'].class).to eq(Hash)
  823. expect(json_response['assets']['Ticket']).to be_nil
  824. expect(json_response['assets']['User'][user1.id.to_s]).not_to be_nil
  825. end
  826. it 'does user search sortable' do
  827. firstname = "user_search_sortable #{rand(999_999_999)}"
  828. user1 = create(
  829. :customer_user,
  830. login: 'rest-user_search_sortableA@example.com',
  831. firstname: "#{firstname} A",
  832. lastname: 'user_search_sortableA',
  833. email: 'rest-user_search_sortableA@example.com',
  834. password: 'user_search_sortableA',
  835. active: true,
  836. organization_id: organization.id,
  837. out_of_office: false,
  838. created_at: '2016-02-05 17:42:00',
  839. )
  840. user2 = create(
  841. :customer_user,
  842. login: 'rest-user_search_sortableB@example.com',
  843. firstname: "#{firstname} B",
  844. lastname: 'user_search_sortableB',
  845. email: 'rest-user_search_sortableB@example.com',
  846. password: 'user_search_sortableB',
  847. active: true,
  848. organization_id: organization.id,
  849. out_of_office_start_at: '2016-02-06 19:42:00',
  850. out_of_office_end_at: '2016-02-07 19:42:00',
  851. out_of_office_replacement_id: 1,
  852. out_of_office: true,
  853. created_at: '2016-02-05 19:42:00',
  854. )
  855. Scheduler.worker(true)
  856. sleep 2 # let es time to come ready
  857. authenticated_as(admin_user)
  858. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'created_at', order_by: 'asc' }, as: :json
  859. expect(response).to have_http_status(:ok)
  860. expect(json_response).to be_a_kind_of(Array)
  861. result = json_response
  862. result.collect! { |v| v['id'] }
  863. expect(result).to eq([user1.id, user2.id])
  864. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'firstname', order_by: 'asc' }, as: :json
  865. expect(response).to have_http_status(:ok)
  866. expect(json_response).to be_a_kind_of(Array)
  867. result = json_response
  868. result.collect! { |v| v['id'] }
  869. expect(result).to eq([user1.id, user2.id])
  870. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'firstname', order_by: 'desc' }, as: :json
  871. expect(response).to have_http_status(:ok)
  872. expect(json_response).to be_a_kind_of(Array)
  873. result = json_response
  874. result.collect! { |v| v['id'] }
  875. expect(result).to eq([user2.id, user1.id])
  876. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: %w[firstname created_at], order_by: %w[desc asc] }, as: :json
  877. expect(response).to have_http_status(:ok)
  878. expect(json_response).to be_a_kind_of(Array)
  879. result = json_response
  880. result.collect! { |v| v['id'] }
  881. expect(result).to eq([user2.id, user1.id])
  882. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: %w[firstname created_at], order_by: %w[desc asc] }, as: :json
  883. expect(response).to have_http_status(:ok)
  884. expect(json_response).to be_a_kind_of(Array)
  885. result = json_response
  886. result.collect! { |v| v['id'] }
  887. expect(result).to eq([user2.id, user1.id])
  888. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'out_of_office', order_by: 'asc' }, as: :json
  889. expect(response).to have_http_status(:ok)
  890. expect(json_response).to be_a_kind_of(Array)
  891. result = json_response
  892. result.collect! { |v| v['id'] }
  893. expect(result).to eq([user1.id, user2.id])
  894. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'out_of_office', order_by: 'desc' }, as: :json
  895. expect(response).to have_http_status(:ok)
  896. expect(json_response).to be_a_kind_of(Array)
  897. result = json_response
  898. result.collect! { |v| v['id'] }
  899. expect(result).to eq([user2.id, user1.id])
  900. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: %w[created_by_id created_at], order_by: %w[asc asc] }, as: :json
  901. expect(response).to have_http_status(:ok)
  902. expect(json_response).to be_a_kind_of(Array)
  903. result = json_response
  904. result.collect! { |v| v['id'] }
  905. expect(result).to eq([user1.id, user2.id])
  906. end
  907. end
  908. end