user_spec.rb 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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(:each) 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(422)
  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(422)
  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(422)
  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(422)
  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(422)
  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(422)
  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(201)
  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.role?('Admin')).to be_falsey
  126. expect(user.role?('Agent')).to be_falsey
  127. expect(user.role?('Customer')).to be_truthy
  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(201)
  133. expect(json_response).to be_truthy
  134. user = User.find(json_response['id'])
  135. expect(user.role?('Admin')).to be_falsey
  136. expect(user.role?('Agent')).to be_falsey
  137. expect(user.role?('Customer')).to be_truthy
  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(201)
  143. expect(json_response).to be_truthy
  144. user = User.find(json_response['id'])
  145. expect(user.role?('Admin')).to be_falsey
  146. expect(user.role?('Agent')).to be_falsey
  147. expect(user.role?('Customer')).to be_truthy
  148. # no user (because of no session)
  149. get '/api/v1/users', params: {}, headers: headers, as: :json
  150. expect(response).to have_http_status(401)
  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(401)
  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(401)
  161. expect(json_response['error']).to eq('authentication failed')
  162. get '/api/v1/users', params: {}, as: :json
  163. expect(response).to have_http_status(401)
  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(401)
  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(401)
  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(200)
  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(200)
  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(200)
  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(200)
  199. expect(json_response).to be_truthy
  200. # index
  201. get '/api/v1/users', params: {}, as: :json
  202. expect(response).to have_http_status(200)
  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(200)
  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(200)
  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(201)
  222. expect(json_response).to be_truthy
  223. user = User.find(json_response['id'])
  224. expect(user.role?('Admin')).to be_truthy
  225. expect(user.role?('Agent')).to be_falsey
  226. expect(user.role?('Customer')).to be_falsey
  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(201)
  234. expect(json_response).to be_truthy
  235. user = User.find(json_response['id'])
  236. expect(user.role?('Admin')).to be_falsey
  237. expect(user.role?('Agent')).to be_truthy
  238. expect(user.role?('Customer')).to be_falsey
  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(201)
  245. expect(json_response).to be_truthy
  246. user = User.find(json_response['id'])
  247. expect(user.role?('Admin')).to be_falsey
  248. expect(user.role?('Agent')).to be_truthy
  249. expect(user.role?('Customer')).to be_falsey
  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(422)
  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(422)
  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(422)
  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(201)
  276. expect(json_response).to be_truthy
  277. user = User.find(json_response['id'])
  278. expect(user.role?('Admin')).to be_falsey
  279. expect(user.role?('Agent')).to be_falsey
  280. expect(user.role?('Customer')).to be_truthy
  281. expect(json_response['login'].start_with?('auto-')).to be_truthy
  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(200)
  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(200)
  295. expect(json_response).to be_truthy
  296. # index
  297. get '/api/v1/users', params: {}, as: :json
  298. expect(response).to have_http_status(200)
  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(200)
  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(200)
  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(201)
  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.role?('Admin')).to be_falsey
  326. expect(user.role?('Agent')).to be_falsey
  327. expect(user.role?('Customer')).to be_truthy
  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(201)
  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.role?('Admin')).to be_falsey
  339. expect(user.role?('Agent')).to be_falsey
  340. expect(user.role?('Customer')).to be_truthy
  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(201)
  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.role?('Admin')).to be_falsey
  352. expect(user.role?('Agent')).to be_falsey
  353. expect(user.role?('Customer')).to be_truthy
  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(200)
  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(200)
  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(200)
  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(200)
  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. role = Role.find_by(name: 'Agent')
  392. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, as: :json
  393. expect(response).to have_http_status(200)
  394. expect(json_response).to be_a_kind_of(Array)
  395. expect(json_response.count).to eq(0)
  396. role = Role.find_by(name: 'Customer')
  397. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, as: :json
  398. expect(response).to have_http_status(200)
  399. expect(json_response).to be_a_kind_of(Array)
  400. expect(json_response[0]['id']).to eq(json_response_user1['id'])
  401. expect(json_response[0]['label']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  402. expect(json_response[0]['value']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  403. expect(json_response[0]['role_ids']).to be_falsey
  404. expect(json_response[0]['roles']).to be_falsey
  405. permission = Permission.find_by(name: 'ticket.agent')
  406. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, as: :json
  407. expect(response).to have_http_status(200)
  408. expect(json_response).to be_a_kind_of(Array)
  409. expect(json_response.count).to eq(0)
  410. permission = Permission.find_by(name: 'ticket.customer')
  411. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, as: :json
  412. expect(response).to have_http_status(200)
  413. expect(json_response).to be_a_kind_of(Array)
  414. expect(json_response[0]['id']).to eq(json_response_user1['id'])
  415. expect(json_response[0]['label']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  416. expect(json_response[0]['value']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  417. expect(json_response[0]['role_ids']).to be_falsey
  418. expect(json_response[0]['roles']).to be_falsey
  419. end
  420. it 'does user index and create with customer1' do
  421. authenticated_as(customer_user)
  422. get '/api/v1/users/me', params: {}, as: :json
  423. expect(response).to have_http_status(200)
  424. expect(json_response).to be_truthy
  425. expect('rest-customer1@example.com').to eq(json_response['email'])
  426. # index
  427. get '/api/v1/users', params: {}, as: :json
  428. expect(response).to have_http_status(200)
  429. expect(Array).to eq(json_response.class)
  430. expect(1).to eq(json_response.length)
  431. # show/:id
  432. get "/api/v1/users/#{customer_user.id}", params: {}, as: :json
  433. expect(response).to have_http_status(200)
  434. expect(Hash).to eq(json_response.class)
  435. expect('rest-customer1@example.com').to eq(json_response['email'])
  436. get "/api/v1/users/#{customer_user2.id}", params: {}, as: :json
  437. expect(response).to have_http_status(401)
  438. expect(Hash).to eq(json_response.class)
  439. expect(json_response['error']).to be_truthy
  440. # create user with admin role
  441. role = Role.lookup(name: 'Admin')
  442. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_customer1@example.com', role_ids: [ role.id ] }
  443. post '/api/v1/users', params: params, as: :json
  444. expect(response).to have_http_status(401)
  445. # create user with agent role
  446. role = Role.lookup(name: 'Agent')
  447. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_customer1@example.com', role_ids: [ role.id ] }
  448. post '/api/v1/users', params: params, as: :json
  449. expect(response).to have_http_status(401)
  450. # search
  451. Scheduler.worker(true)
  452. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, as: :json
  453. expect(response).to have_http_status(401)
  454. end
  455. it 'does user index with customer2' do
  456. authenticated_as(customer_user2)
  457. get '/api/v1/users/me', params: {}, as: :json
  458. expect(response).to have_http_status(200)
  459. expect(json_response).to be_truthy
  460. expect('rest-customer2@example.com').to eq(json_response['email'])
  461. # index
  462. get '/api/v1/users', params: {}, as: :json
  463. expect(response).to have_http_status(200)
  464. expect(Array).to eq(json_response.class)
  465. expect(1).to eq(json_response.length)
  466. # show/:id
  467. get "/api/v1/users/#{customer_user2.id}", params: {}, as: :json
  468. expect(response).to have_http_status(200)
  469. expect(Hash).to eq(json_response.class)
  470. expect('rest-customer2@example.com').to eq(json_response['email'])
  471. get "/api/v1/users/#{customer_user.id}", params: {}, as: :json
  472. expect(response).to have_http_status(401)
  473. expect(Hash).to eq(json_response.class)
  474. expect(json_response['error']).to be_truthy
  475. # search
  476. Scheduler.worker(true)
  477. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, as: :json
  478. expect(response).to have_http_status(401)
  479. end
  480. it 'does users show and response format (04.01)' do
  481. user = create(
  482. :customer_user,
  483. login: 'rest-customer3@example.com',
  484. firstname: 'Rest',
  485. lastname: 'Customer3',
  486. email: 'rest-customer3@example.com',
  487. password: 'customer3pw',
  488. active: true,
  489. organization: organization,
  490. updated_by_id: admin_user.id,
  491. created_by_id: admin_user.id,
  492. )
  493. authenticated_as(admin_user)
  494. get "/api/v1/users/#{user.id}", params: {}, as: :json
  495. expect(response).to have_http_status(200)
  496. expect(json_response).to be_a_kind_of(Hash)
  497. expect(json_response['id']).to eq(user.id)
  498. expect(json_response['firstname']).to eq(user.firstname)
  499. expect(json_response['organization']).to be_falsey
  500. expect(json_response['organization_id']).to eq(user.organization_id)
  501. expect(json_response['password']).to be_falsey
  502. expect(json_response['role_ids']).to eq(user.role_ids)
  503. expect(json_response['updated_by_id']).to eq(admin_user.id)
  504. expect(json_response['created_by_id']).to eq(admin_user.id)
  505. get "/api/v1/users/#{user.id}?expand=true", params: {}, as: :json
  506. expect(response).to have_http_status(200)
  507. expect(json_response).to be_a_kind_of(Hash)
  508. expect(json_response['id']).to eq(user.id)
  509. expect(json_response['firstname']).to eq(user.firstname)
  510. expect(json_response['organization_id']).to eq(user.organization_id)
  511. expect(json_response['organization']).to eq(user.organization.name)
  512. expect(json_response['role_ids']).to eq(user.role_ids)
  513. expect(json_response['password']).to be_falsey
  514. expect(json_response['updated_by_id']).to eq(admin_user.id)
  515. expect(json_response['created_by_id']).to eq(admin_user.id)
  516. get "/api/v1/users/#{user.id}?expand=false", params: {}, as: :json
  517. expect(response).to have_http_status(200)
  518. expect(json_response).to be_a_kind_of(Hash)
  519. expect(json_response['id']).to eq(user.id)
  520. expect(json_response['firstname']).to eq(user.firstname)
  521. expect(json_response['organization']).to be_falsey
  522. expect(json_response['organization_id']).to eq(user.organization_id)
  523. expect(json_response['password']).to be_falsey
  524. expect(json_response['role_ids']).to eq(user.role_ids)
  525. expect(json_response['updated_by_id']).to eq(admin_user.id)
  526. expect(json_response['created_by_id']).to eq(admin_user.id)
  527. get "/api/v1/users/#{user.id}?full=true", params: {}, as: :json
  528. expect(response).to have_http_status(200)
  529. expect(json_response).to be_a_kind_of(Hash)
  530. expect(json_response['id']).to eq(user.id)
  531. expect(json_response['assets']).to be_truthy
  532. expect(json_response['assets']['User']).to be_truthy
  533. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  534. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  535. expect(json_response['assets']['User'][user.id.to_s]['firstname']).to eq(user.firstname)
  536. expect(json_response['assets']['User'][user.id.to_s]['organization_id']).to eq(user.organization_id)
  537. expect(json_response['assets']['User'][user.id.to_s]['role_ids']).to eq(user.role_ids)
  538. get "/api/v1/users/#{user.id}?full=false", params: {}, as: :json
  539. expect(response).to have_http_status(200)
  540. expect(json_response).to be_a_kind_of(Hash)
  541. expect(json_response['id']).to eq(user.id)
  542. expect(json_response['firstname']).to eq(user.firstname)
  543. expect(json_response['organization']).to be_falsey
  544. expect(json_response['organization_id']).to eq(user.organization_id)
  545. expect(json_response['password']).to be_falsey
  546. expect(json_response['role_ids']).to eq(user.role_ids)
  547. expect(json_response['updated_by_id']).to eq(admin_user.id)
  548. expect(json_response['created_by_id']).to eq(admin_user.id)
  549. end
  550. it 'does user index and response format (04.02)' do
  551. user = create(
  552. :customer_user,
  553. login: 'rest-customer3@example.com',
  554. firstname: 'Rest',
  555. lastname: 'Customer3',
  556. email: 'rest-customer3@example.com',
  557. password: 'customer3pw',
  558. active: true,
  559. organization: organization,
  560. updated_by_id: admin_user.id,
  561. created_by_id: admin_user.id,
  562. )
  563. authenticated_as(admin_user)
  564. get '/api/v1/users', params: {}, as: :json
  565. expect(response).to have_http_status(200)
  566. expect(json_response).to be_a_kind_of(Array)
  567. expect(json_response[0].class).to eq(Hash)
  568. expect(json_response.last['id']).to eq(user.id)
  569. expect(json_response.last['lastname']).to eq(user.lastname)
  570. expect(json_response.last['organization']).to be_falsey
  571. expect(json_response.last['role_ids']).to eq(user.role_ids)
  572. expect(json_response.last['organization_id']).to eq(user.organization_id)
  573. expect(json_response.last['password']).to be_falsey
  574. expect(json_response.last['updated_by_id']).to eq(admin_user.id)
  575. expect(json_response.last['created_by_id']).to eq(admin_user.id)
  576. get '/api/v1/users?expand=true', params: {}, as: :json
  577. expect(response).to have_http_status(200)
  578. expect(json_response).to be_a_kind_of(Array)
  579. expect(json_response[0].class).to eq(Hash)
  580. expect(json_response.last['id']).to eq(user.id)
  581. expect(json_response.last['lastname']).to eq(user.lastname)
  582. expect(json_response.last['organization_id']).to eq(user.organization_id)
  583. expect(json_response.last['organization']).to eq(user.organization.name)
  584. expect(json_response.last['password']).to be_falsey
  585. expect(json_response.last['updated_by_id']).to eq(admin_user.id)
  586. expect(json_response.last['created_by_id']).to eq(admin_user.id)
  587. get '/api/v1/users?expand=false', params: {}, as: :json
  588. expect(response).to have_http_status(200)
  589. expect(json_response).to be_a_kind_of(Array)
  590. expect(json_response[0].class).to eq(Hash)
  591. expect(json_response.last['id']).to eq(user.id)
  592. expect(json_response.last['lastname']).to eq(user.lastname)
  593. expect(json_response.last['organization']).to be_falsey
  594. expect(json_response.last['role_ids']).to eq(user.role_ids)
  595. expect(json_response.last['organization_id']).to eq(user.organization_id)
  596. expect(json_response.last['password']).to be_falsey
  597. expect(json_response.last['updated_by_id']).to eq(admin_user.id)
  598. expect(json_response.last['created_by_id']).to eq(admin_user.id)
  599. get '/api/v1/users?full=true', params: {}, as: :json
  600. expect(response).to have_http_status(200)
  601. expect(json_response).to be_a_kind_of(Hash)
  602. expect(json_response['record_ids'].class).to eq(Array)
  603. expect(json_response['record_ids'][0]).to eq(1)
  604. expect(json_response['record_ids'].last).to eq(user.id)
  605. expect(json_response['assets']).to be_truthy
  606. expect(json_response['assets']['User']).to be_truthy
  607. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  608. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  609. expect(json_response['assets']['User'][user.id.to_s]['lastname']).to eq(user.lastname)
  610. expect(json_response['assets']['User'][user.id.to_s]['organization_id']).to eq(user.organization_id)
  611. expect(json_response['assets']['User'][user.id.to_s]['password']).to be_falsey
  612. get '/api/v1/users?full=false', params: {}, as: :json
  613. expect(response).to have_http_status(200)
  614. expect(json_response).to be_a_kind_of(Array)
  615. expect(json_response[0].class).to eq(Hash)
  616. expect(json_response.last['id']).to eq(user.id)
  617. expect(json_response.last['lastname']).to eq(user.lastname)
  618. expect(json_response.last['organization']).to be_falsey
  619. expect(json_response.last['role_ids']).to eq(user.role_ids)
  620. expect(json_response.last['organization_id']).to eq(user.organization_id)
  621. expect(json_response.last['password']).to be_falsey
  622. expect(json_response.last['updated_by_id']).to eq(admin_user.id)
  623. expect(json_response.last['created_by_id']).to eq(admin_user.id)
  624. end
  625. it 'does ticket create and response format (04.03)' do
  626. organization = Organization.first
  627. params = {
  628. firstname: 'newfirstname123',
  629. note: 'some note',
  630. organization: organization.name,
  631. }
  632. authenticated_as(admin_user)
  633. post '/api/v1/users', params: params, as: :json
  634. expect(response).to have_http_status(201)
  635. expect(json_response).to be_a_kind_of(Hash)
  636. user = User.find(json_response['id'])
  637. expect(json_response['firstname']).to eq(user.firstname)
  638. expect(json_response['organization_id']).to eq(user.organization_id)
  639. expect(json_response['organization']).to be_falsey
  640. expect(json_response['password']).to be_falsey
  641. expect(json_response['updated_by_id']).to eq(admin_user.id)
  642. expect(json_response['created_by_id']).to eq(admin_user.id)
  643. post '/api/v1/users?expand=true', params: params, as: :json
  644. expect(response).to have_http_status(201)
  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 eq(user.organization.name)
  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?full=true', params: params, as: :json
  654. expect(response).to have_http_status(201)
  655. expect(json_response).to be_a_kind_of(Hash)
  656. user = User.find(json_response['id'])
  657. expect(json_response['assets']).to be_truthy
  658. expect(json_response['assets']['User']).to be_truthy
  659. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  660. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  661. expect(json_response['assets']['User'][user.id.to_s]['firstname']).to eq(user.firstname)
  662. expect(json_response['assets']['User'][user.id.to_s]['lastname']).to eq(user.lastname)
  663. expect(json_response['assets']['User'][user.id.to_s]['password']).to be_falsey
  664. expect(json_response['assets']['User'][admin_user.id.to_s]).to be_truthy
  665. expect(json_response['assets']['User'][admin_user.id.to_s]['id']).to eq(admin_user.id)
  666. expect(json_response['assets']['User'][admin_user.id.to_s]['firstname']).to eq(admin_user.firstname)
  667. expect(json_response['assets']['User'][admin_user.id.to_s]['lastname']).to eq(admin_user.lastname)
  668. expect(json_response['assets']['User'][admin_user.id.to_s]['password']).to be_falsey
  669. end
  670. it 'does ticket update and response formats (04.04)' do
  671. user = create(
  672. :customer_user,
  673. login: 'rest-customer3@example.com',
  674. firstname: 'Rest',
  675. lastname: 'Customer3',
  676. email: 'rest-customer3@example.com',
  677. password: 'customer3pw',
  678. active: true,
  679. organization: organization,
  680. updated_by_id: admin_user.id,
  681. created_by_id: admin_user.id,
  682. )
  683. authenticated_as(admin_user)
  684. params = {
  685. firstname: 'a update firstname #1',
  686. }
  687. put "/api/v1/users/#{user.id}", params: params, as: :json
  688. expect(response).to have_http_status(200)
  689. expect(json_response).to be_a_kind_of(Hash)
  690. user = User.find(json_response['id'])
  691. expect(json_response['lastname']).to eq(user.lastname)
  692. expect(json_response['firstname']).to eq(params[:firstname])
  693. expect(json_response['organization_id']).to eq(user.organization_id)
  694. expect(json_response['organization']).to be_falsey
  695. expect(json_response['password']).to be_falsey
  696. expect(json_response['updated_by_id']).to eq(admin_user.id)
  697. expect(json_response['created_by_id']).to eq(admin_user.id)
  698. params = {
  699. firstname: 'a update firstname #2',
  700. }
  701. put "/api/v1/users/#{user.id}?expand=true", params: params, as: :json
  702. expect(response).to have_http_status(200)
  703. expect(json_response).to be_a_kind_of(Hash)
  704. user = User.find(json_response['id'])
  705. expect(json_response['lastname']).to eq(user.lastname)
  706. expect(json_response['firstname']).to eq(params[:firstname])
  707. expect(json_response['organization_id']).to eq(user.organization_id)
  708. expect(json_response['organization']).to eq(user.organization.name)
  709. expect(json_response['password']).to be_falsey
  710. expect(json_response['updated_by_id']).to eq(admin_user.id)
  711. expect(json_response['created_by_id']).to eq(admin_user.id)
  712. params = {
  713. firstname: 'a update firstname #3',
  714. }
  715. put "/api/v1/users/#{user.id}?full=true", params: params, as: :json
  716. expect(response).to have_http_status(200)
  717. expect(json_response).to be_a_kind_of(Hash)
  718. user = User.find(json_response['id'])
  719. expect(json_response['assets']).to be_truthy
  720. expect(json_response['assets']['User']).to be_truthy
  721. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  722. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  723. expect(json_response['assets']['User'][user.id.to_s]['firstname']).to eq(params[:firstname])
  724. expect(json_response['assets']['User'][user.id.to_s]['lastname']).to eq(user.lastname)
  725. expect(json_response['assets']['User'][user.id.to_s]['password']).to be_falsey
  726. expect(json_response['assets']['User'][admin_user.id.to_s]).to be_truthy
  727. expect(json_response['assets']['User'][admin_user.id.to_s]['id']).to eq(admin_user.id)
  728. expect(json_response['assets']['User'][admin_user.id.to_s]['firstname']).to eq(admin_user.firstname)
  729. expect(json_response['assets']['User'][admin_user.id.to_s]['lastname']).to eq(admin_user.lastname)
  730. expect(json_response['assets']['User'][admin_user.id.to_s]['password']).to be_falsey
  731. end
  732. it 'does csv example - customer no access (05.01)' do
  733. authenticated_as(customer_user)
  734. get '/api/v1/users/import_example', params: {}, as: :json
  735. expect(response).to have_http_status(401)
  736. expect(json_response['error']).to eq('Not authorized (user)!')
  737. end
  738. it 'does csv example - admin access (05.02)' do
  739. authenticated_as(admin_user)
  740. get '/api/v1/users/import_example', params: {}, as: :json
  741. expect(response).to have_http_status(200)
  742. rows = CSV.parse(@response.body)
  743. header = rows.shift
  744. expect(header[0]).to eq('id')
  745. expect(header[1]).to eq('login')
  746. expect(header[2]).to eq('firstname')
  747. expect(header[3]).to eq('lastname')
  748. expect(header[4]).to eq('email')
  749. expect(header.include?('organization')).to be_truthy
  750. end
  751. it 'does csv import - admin access (05.03)' do
  752. # invalid file
  753. csv_file_path = Rails.root.join('test', 'data', 'csv', 'user_simple_col_not_existing.csv')
  754. csv_file = ::Rack::Test::UploadedFile.new(csv_file_path, 'text/csv')
  755. authenticated_as(admin_user)
  756. post '/api/v1/users/import?try=true', params: { file: csv_file, col_sep: ';' }
  757. expect(response).to have_http_status(200)
  758. expect(json_response).to be_a_kind_of(Hash)
  759. expect(json_response['try']).to eq(true)
  760. expect(json_response['records'].count).to eq(2)
  761. expect(json_response['result']).to eq('failed')
  762. expect(json_response['errors'].count).to eq(2)
  763. expect(json_response['errors'][0]).to eq("Line 1: Unable to create record - unknown attribute 'firstname2' for User.")
  764. expect(json_response['errors'][1]).to eq("Line 2: Unable to create record - unknown attribute 'firstname2' for User.")
  765. # valid file try
  766. csv_file_path = Rails.root.join('test', 'data', 'csv', 'user_simple.csv')
  767. csv_file = ::Rack::Test::UploadedFile.new(csv_file_path, 'text/csv')
  768. post '/api/v1/users/import?try=true', params: { file: csv_file, col_sep: ';' }
  769. expect(response).to have_http_status(200)
  770. expect(json_response).to be_a_kind_of(Hash)
  771. expect(json_response['try']).to eq(true)
  772. expect(json_response['records'].count).to eq(2)
  773. expect(json_response['result']).to eq('success')
  774. expect(User.find_by(login: 'user-simple-import1')).to be_nil
  775. expect(User.find_by(login: 'user-simple-import2')).to be_nil
  776. # valid file
  777. csv_file_path = Rails.root.join('test', 'data', 'csv', 'user_simple.csv')
  778. csv_file = ::Rack::Test::UploadedFile.new(csv_file_path, 'text/csv')
  779. post '/api/v1/users/import', params: { file: csv_file, col_sep: ';' }
  780. expect(response).to have_http_status(200)
  781. expect(json_response).to be_a_kind_of(Hash)
  782. expect(json_response['try']).to eq(false)
  783. expect(json_response['records'].count).to eq(2)
  784. expect(json_response['result']).to eq('success')
  785. user1 = User.find_by(login: 'user-simple-import1')
  786. expect(user1).to be_truthy
  787. expect(user1.login).to eq('user-simple-import1')
  788. expect(user1.firstname).to eq('firstname-simple-import1')
  789. expect(user1.lastname).to eq('lastname-simple-import1')
  790. expect(user1.email).to eq('user-simple-import1@example.com')
  791. expect(user1.active).to eq(true)
  792. user2 = User.find_by(login: 'user-simple-import2')
  793. expect(user2).to be_truthy
  794. expect(user2.login).to eq('user-simple-import2')
  795. expect(user2.firstname).to eq('firstname-simple-import2')
  796. expect(user2.lastname).to eq('lastname-simple-import2')
  797. expect(user2.email).to eq('user-simple-import2@example.com')
  798. expect(user2.active).to eq(false)
  799. user1.destroy!
  800. user2.destroy!
  801. end
  802. it 'does user history' do
  803. user1 = create(
  804. :customer_user,
  805. login: 'history@example.com',
  806. firstname: 'History',
  807. lastname: 'Customer1',
  808. email: 'history@example.com',
  809. )
  810. authenticated_as(agent_user)
  811. get "/api/v1/users/history/#{user1.id}", params: {}, as: :json
  812. expect(response).to have_http_status(200)
  813. expect(json_response).to be_a_kind_of(Hash)
  814. expect(json_response['history'].class).to eq(Array)
  815. expect(json_response['assets'].class).to eq(Hash)
  816. expect(json_response['assets']['Ticket']).to be_nil
  817. expect(json_response['assets']['User'][user1.id.to_s]).not_to be_nil
  818. end
  819. it 'does user search sortable' do
  820. firstname = "user_search_sortable #{rand(999_999_999)}"
  821. user1 = create(
  822. :customer_user,
  823. login: 'rest-user_search_sortableA@example.com',
  824. firstname: "#{firstname} A",
  825. lastname: 'user_search_sortableA',
  826. email: 'rest-user_search_sortableA@example.com',
  827. password: 'user_search_sortableA',
  828. active: true,
  829. organization_id: organization.id,
  830. out_of_office: false,
  831. created_at: '2016-02-05 17:42:00',
  832. )
  833. user2 = create(
  834. :customer_user,
  835. login: 'rest-user_search_sortableB@example.com',
  836. firstname: "#{firstname} B",
  837. lastname: 'user_search_sortableB',
  838. email: 'rest-user_search_sortableB@example.com',
  839. password: 'user_search_sortableB',
  840. active: true,
  841. organization_id: organization.id,
  842. out_of_office_start_at: '2016-02-06 19:42:00',
  843. out_of_office_end_at: '2016-02-07 19:42:00',
  844. out_of_office_replacement_id: 1,
  845. out_of_office: true,
  846. created_at: '2016-02-05 19:42:00',
  847. )
  848. Scheduler.worker(true)
  849. sleep 2 # let es time to come ready
  850. authenticated_as(admin_user)
  851. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'created_at', order_by: 'asc' }, as: :json
  852. expect(response).to have_http_status(200)
  853. expect(json_response).to be_a_kind_of(Array)
  854. result = json_response
  855. result.collect! { |v| v['id'] }
  856. expect(result).to eq([user1.id, user2.id])
  857. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'firstname', order_by: 'asc' }, as: :json
  858. expect(response).to have_http_status(200)
  859. expect(json_response).to be_a_kind_of(Array)
  860. result = json_response
  861. result.collect! { |v| v['id'] }
  862. expect(result).to eq([user1.id, user2.id])
  863. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'firstname', order_by: 'desc' }, as: :json
  864. expect(response).to have_http_status(200)
  865. expect(json_response).to be_a_kind_of(Array)
  866. result = json_response
  867. result.collect! { |v| v['id'] }
  868. expect(result).to eq([user2.id, user1.id])
  869. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: %w[firstname created_at], order_by: %w[desc asc] }, as: :json
  870. expect(response).to have_http_status(200)
  871. expect(json_response).to be_a_kind_of(Array)
  872. result = json_response
  873. result.collect! { |v| v['id'] }
  874. expect(result).to eq([user2.id, user1.id])
  875. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: %w[firstname created_at], order_by: %w[desc asc] }, as: :json
  876. expect(response).to have_http_status(200)
  877. expect(json_response).to be_a_kind_of(Array)
  878. result = json_response
  879. result.collect! { |v| v['id'] }
  880. expect(result).to eq([user2.id, user1.id])
  881. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'out_of_office', order_by: 'asc' }, as: :json
  882. expect(response).to have_http_status(200)
  883. expect(json_response).to be_a_kind_of(Array)
  884. result = json_response
  885. result.collect! { |v| v['id'] }
  886. expect(result).to eq([user1.id, user2.id])
  887. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'out_of_office', order_by: 'desc' }, as: :json
  888. expect(response).to have_http_status(200)
  889. expect(json_response).to be_a_kind_of(Array)
  890. result = json_response
  891. result.collect! { |v| v['id'] }
  892. expect(result).to eq([user2.id, user1.id])
  893. 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
  894. expect(response).to have_http_status(200)
  895. expect(json_response).to be_a_kind_of(Array)
  896. result = json_response
  897. result.collect! { |v| v['id'] }
  898. expect(result).to eq([user1.id, user2.id])
  899. end
  900. end
  901. end