user_controller_test.rb 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. require 'test_helper'
  2. require 'rake'
  3. class UserControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. # create agent
  8. roles = Role.where(name: %w[Admin Agent])
  9. groups = Group.all
  10. UserInfo.current_user_id = 1
  11. @backup_admin = User.create_or_update(
  12. login: 'backup-admin',
  13. firstname: 'Backup',
  14. lastname: 'Agent',
  15. email: 'backup-admin@example.com',
  16. password: 'adminpw',
  17. active: true,
  18. roles: roles,
  19. groups: groups,
  20. )
  21. @admin = User.create_or_update(
  22. login: 'rest-admin',
  23. firstname: 'Rest',
  24. lastname: 'Agent',
  25. email: 'rest-admin@example.com',
  26. password: 'adminpw',
  27. active: true,
  28. roles: roles,
  29. groups: groups,
  30. )
  31. # create agent
  32. roles = Role.where(name: 'Agent')
  33. @agent = User.create_or_update(
  34. login: 'rest-agent@example.com',
  35. firstname: 'Rest',
  36. lastname: 'Agent',
  37. email: 'rest-agent@example.com',
  38. password: 'agentpw',
  39. active: true,
  40. roles: roles,
  41. groups: groups,
  42. )
  43. # create customer without org
  44. roles = Role.where(name: 'Customer')
  45. @customer_without_org = User.create_or_update(
  46. login: 'rest-customer1@example.com',
  47. firstname: 'Rest',
  48. lastname: 'Customer1',
  49. email: 'rest-customer1@example.com',
  50. password: 'customer1pw',
  51. active: true,
  52. roles: roles,
  53. )
  54. # create orgs
  55. @organization = Organization.create_or_update(
  56. name: 'Rest Org',
  57. )
  58. @organization2 = Organization.create_or_update(
  59. name: 'Rest Org #2',
  60. )
  61. @organization3 = Organization.create_or_update(
  62. name: 'Rest Org #3',
  63. )
  64. # create customer with org
  65. @customer_with_org = User.create_or_update(
  66. login: 'rest-customer2@example.com',
  67. firstname: 'Rest',
  68. lastname: 'Customer2',
  69. email: 'rest-customer2@example.com',
  70. password: 'customer2pw',
  71. active: true,
  72. roles: roles,
  73. organization_id: @organization.id,
  74. )
  75. # configure es
  76. if ENV['ES_URL'].present?
  77. #fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
  78. Setting.set('es_url', ENV['ES_URL'])
  79. # Setting.set('es_url', 'http://127.0.0.1:9200')
  80. # Setting.set('es_index', 'estest.local_zammad')
  81. # Setting.set('es_user', 'elasticsearch')
  82. # Setting.set('es_password', 'zammad')
  83. if ENV['ES_INDEX_RAND'].present?
  84. ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
  85. end
  86. if ENV['ES_INDEX'].blank?
  87. raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
  88. end
  89. Setting.set('es_index', ENV['ES_INDEX'])
  90. travel 1.minute
  91. # drop/create indexes
  92. Rake::Task.clear
  93. Zammad::Application.load_tasks
  94. #Rake::Task["searchindex:drop"].execute
  95. #Rake::Task["searchindex:create"].execute
  96. Rake::Task['searchindex:rebuild'].execute
  97. # execute background jobs
  98. Scheduler.worker(true)
  99. sleep 6
  100. end
  101. UserInfo.current_user_id = nil
  102. end
  103. test 'user create tests - no user' do
  104. post '/api/v1/signshow', params: {}, headers: @headers
  105. # create user with disabled feature
  106. Setting.set('user_create_account', false)
  107. token = @response.headers['CSRF-TOKEN']
  108. # token based on form
  109. params = { email: 'some_new_customer@example.com', authenticity_token: token }
  110. post '/api/v1/users', params: params.to_json, headers: @headers
  111. assert_response(422)
  112. result = JSON.parse(@response.body)
  113. assert(result['error'])
  114. assert_equal('Feature not enabled!', result['error'])
  115. # token based on headers
  116. headers = @headers.merge('X-CSRF-Token' => token)
  117. params = { email: 'some_new_customer@example.com' }
  118. post '/api/v1/users', params: params.to_json, headers: headers
  119. assert_response(422)
  120. result = JSON.parse(@response.body)
  121. assert(result['error'])
  122. assert_equal('Feature not enabled!', result['error'])
  123. Setting.set('user_create_account', true)
  124. # no signup param with enabled feature
  125. params = { email: 'some_new_customer@example.com' }
  126. post '/api/v1/users', params: params.to_json, headers: headers
  127. assert_response(422)
  128. result = JSON.parse(@response.body)
  129. assert(result['error'])
  130. assert_equal('Only signup with not authenticate user possible!', result['error'])
  131. # already existing user with enabled feature
  132. params = { email: 'rest-customer1@example.com', signup: true }
  133. post '/api/v1/users', params: params.to_json, headers: headers
  134. assert_response(422)
  135. result = JSON.parse(@response.body)
  136. assert(result['error'])
  137. assert_equal('Email address is already used for other user.', result['error'])
  138. # email missing with enabled feature
  139. params = { firstname: 'some firstname', signup: true }
  140. post '/api/v1/users', params: params.to_json, headers: headers
  141. assert_response(422)
  142. result = JSON.parse(@response.body)
  143. assert(result['error'])
  144. assert_equal('Attribute \'email\' required!', result['error'])
  145. # email missing with enabled feature
  146. params = { firstname: 'some firstname', signup: true }
  147. post '/api/v1/users', params: params.to_json, headers: headers
  148. assert_response(422)
  149. result = JSON.parse(@response.body)
  150. assert(result['error'])
  151. assert_equal('Attribute \'email\' required!', result['error'])
  152. # create user with enabled feature (take customer role)
  153. params = { firstname: 'Me First', lastname: 'Me Last', email: 'new_here@example.com', signup: true }
  154. post '/api/v1/users', params: params.to_json, headers: headers
  155. assert_response(201)
  156. result = JSON.parse(@response.body)
  157. assert(result)
  158. assert_equal('Me First', result['firstname'])
  159. assert_equal('Me Last', result['lastname'])
  160. assert_equal('new_here@example.com', result['login'])
  161. assert_equal('new_here@example.com', result['email'])
  162. user = User.find(result['id'])
  163. assert_not(user.role?('Admin'))
  164. assert_not(user.role?('Agent'))
  165. assert(user.role?('Customer'))
  166. # create user with admin role (not allowed for signup, take customer role)
  167. role = Role.lookup(name: 'Admin')
  168. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin@example.com', role_ids: [ role.id ], signup: true }
  169. post '/api/v1/users', params: params.to_json, headers: headers
  170. assert_response(201)
  171. result = JSON.parse(@response.body)
  172. assert(result)
  173. user = User.find(result['id'])
  174. assert_not(user.role?('Admin'))
  175. assert_not(user.role?('Agent'))
  176. assert(user.role?('Customer'))
  177. # create user with agent role (not allowed for signup, take customer role)
  178. role = Role.lookup(name: 'Agent')
  179. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent@example.com', role_ids: [ role.id ], signup: true }
  180. post '/api/v1/users', params: params.to_json, headers: headers
  181. assert_response(201)
  182. result = JSON.parse(@response.body)
  183. assert(result)
  184. user = User.find(result['id'])
  185. assert_not(user.role?('Admin'))
  186. assert_not(user.role?('Agent'))
  187. assert(user.role?('Customer'))
  188. # no user (because of no session)
  189. get '/api/v1/users', params: {}, headers: headers
  190. assert_response(401)
  191. result = JSON.parse(@response.body)
  192. assert_equal('authentication failed', result['error'])
  193. # me
  194. get '/api/v1/users/me', params: {}, headers: headers
  195. assert_response(401)
  196. result = JSON.parse(@response.body)
  197. assert_equal('authentication failed', result['error'])
  198. end
  199. test 'auth tests - not existing user' do
  200. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('not_existing@example.com', 'adminpw')
  201. # me
  202. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  203. assert_response(401)
  204. result = JSON.parse(@response.body)
  205. assert_equal('authentication failed', result['error'])
  206. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  207. assert_response(401)
  208. result = JSON.parse(@response.body)
  209. assert_equal('authentication failed', result['error'])
  210. end
  211. test 'auth tests - username auth, wrong pw' do
  212. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin', 'not_existing')
  213. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  214. assert_response(401)
  215. result = JSON.parse(@response.body)
  216. assert_equal('authentication failed', result['error'])
  217. end
  218. test 'auth tests - email auth, wrong pw' do
  219. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'not_existing')
  220. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  221. assert_response(401)
  222. result = JSON.parse(@response.body)
  223. assert_equal('authentication failed', result['error'])
  224. end
  225. test 'auth tests - username auth' do
  226. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin', 'adminpw')
  227. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  228. assert_response(200)
  229. result = JSON.parse(@response.body)
  230. assert(result)
  231. end
  232. test 'auth tests - email auth' do
  233. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  234. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  235. assert_response(200)
  236. result = JSON.parse(@response.body)
  237. assert(result)
  238. end
  239. test 'user index and create with admin' do
  240. # email auth
  241. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  242. # me
  243. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  244. assert_response(200)
  245. result = JSON.parse(@response.body)
  246. assert(result)
  247. assert_equal(result['email'], 'rest-admin@example.com')
  248. # index
  249. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  250. assert_response(200)
  251. result = JSON.parse(@response.body)
  252. assert(result)
  253. # index
  254. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  255. assert_response(200)
  256. result = JSON.parse(@response.body)
  257. assert(result)
  258. assert_equal(result.class, Array)
  259. assert(result.length >= 3)
  260. # show/:id
  261. get "/api/v1/users/#{@agent.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  262. assert_response(200)
  263. result = JSON.parse(@response.body)
  264. assert(result)
  265. assert_equal(result.class, Hash)
  266. assert_equal(result['email'], 'rest-agent@example.com')
  267. get "/api/v1/users/#{@customer_without_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  268. assert_response(200)
  269. result = JSON.parse(@response.body)
  270. assert(result)
  271. assert_equal(result.class, Hash)
  272. assert_equal(result['email'], 'rest-customer1@example.com')
  273. # create user with admin role
  274. role = Role.lookup(name: 'Admin')
  275. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_admin@example.com', role_ids: [ role.id ] }
  276. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  277. assert_response(201)
  278. result = JSON.parse(@response.body)
  279. assert(result)
  280. user = User.find(result['id'])
  281. assert(user.role?('Admin'))
  282. assert_not(user.role?('Agent'))
  283. assert_not(user.role?('Customer'))
  284. assert_equal('new_admin_by_admin@example.com', result['login'])
  285. assert_equal('new_admin_by_admin@example.com', result['email'])
  286. # create user with agent role
  287. role = Role.lookup(name: 'Agent')
  288. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_admin1@example.com', role_ids: [ role.id ] }
  289. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  290. assert_response(201)
  291. result = JSON.parse(@response.body)
  292. assert(result)
  293. user = User.find(result['id'])
  294. assert_not(user.role?('Admin'))
  295. assert(user.role?('Agent'))
  296. assert_not(user.role?('Customer'))
  297. assert_equal('new_agent_by_admin1@example.com', result['login'])
  298. assert_equal('new_agent_by_admin1@example.com', result['email'])
  299. role = Role.lookup(name: 'Agent')
  300. params = { firstname: 'Agent First', email: 'new_agent_by_admin2@example.com', role_ids: [ role.id ] }
  301. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  302. assert_response(201)
  303. result = JSON.parse(@response.body)
  304. assert(result)
  305. user = User.find(result['id'])
  306. assert_not(user.role?('Admin'))
  307. assert(user.role?('Agent'))
  308. assert_not(user.role?('Customer'))
  309. assert_equal('new_agent_by_admin2@example.com', result['login'])
  310. assert_equal('new_agent_by_admin2@example.com', result['email'])
  311. assert_equal('Agent', result['firstname'])
  312. assert_equal('First', result['lastname'])
  313. role = Role.lookup(name: 'Agent')
  314. params = { firstname: 'Agent First', email: 'new_agent_by_admin2@example.com', role_ids: [ role.id ] }
  315. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  316. assert_response(422)
  317. result = JSON.parse(@response.body)
  318. assert(result)
  319. assert_equal('Email address is already used for other user.', result['error'])
  320. # missing required attributes
  321. params = { note: 'some note' }
  322. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  323. assert_response(422)
  324. result = JSON.parse(@response.body)
  325. assert(result)
  326. assert_equal('Minimum one identifier (login, firstname, lastname, phone or email) for user is required.', result['error'])
  327. # invalid email
  328. params = { firstname: 'newfirstname123', email: 'some_what', note: 'some note' }
  329. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  330. assert_response(422)
  331. result = JSON.parse(@response.body)
  332. assert(result)
  333. assert_equal('Invalid email', result['error'])
  334. # with valid attributes
  335. params = { firstname: 'newfirstname123', note: 'some note' }
  336. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  337. assert_response(201)
  338. result = JSON.parse(@response.body)
  339. assert(result)
  340. user = User.find(result['id'])
  341. assert_not(user.role?('Admin'))
  342. assert_not(user.role?('Agent'))
  343. assert(user.role?('Customer'))
  344. assert(result['login'].start_with?('auto-'))
  345. assert_equal('', result['email'])
  346. assert_equal('newfirstname123', result['firstname'])
  347. assert_equal('', result['lastname'])
  348. end
  349. test 'user index and create with agent' do
  350. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-agent@example.com', 'agentpw')
  351. # me
  352. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  353. assert_response(200)
  354. result = JSON.parse(@response.body)
  355. assert(result)
  356. assert_equal(result['email'], 'rest-agent@example.com')
  357. # index
  358. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  359. assert_response(200)
  360. result = JSON.parse(@response.body)
  361. assert(result)
  362. # index
  363. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  364. assert_response(200)
  365. result = JSON.parse(@response.body)
  366. assert(result)
  367. assert_equal(result.class, Array)
  368. assert(result.length >= 3)
  369. get '/api/v1/users?limit=40&page=1&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  370. assert_response(200)
  371. result = JSON.parse(@response.body)
  372. assert_equal(Array, result.class)
  373. users = User.order(:id).limit(2)
  374. assert_equal(users[0].id, result[0]['id'])
  375. assert_equal(users[1].id, result[1]['id'])
  376. assert_equal(2, result.count)
  377. get '/api/v1/users?limit=40&page=2&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  378. assert_response(200)
  379. result = JSON.parse(@response.body)
  380. assert_equal(Array, result.class)
  381. users = User.order(:id).limit(4)
  382. assert_equal(users[2].id, result[0]['id'])
  383. assert_equal(users[3].id, result[1]['id'])
  384. assert_equal(2, result.count)
  385. # create user with admin role
  386. firstname = "First test#{rand(999_999_999)}"
  387. role = Role.lookup(name: 'Admin')
  388. params = { firstname: "Admin#{firstname}", lastname: 'Admin Last', email: 'new_admin_by_agent@example.com', role_ids: [ role.id ] }
  389. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  390. assert_response(201)
  391. result_user1 = JSON.parse(@response.body)
  392. assert(result_user1)
  393. user = User.find(result_user1['id'])
  394. assert_not(user.role?('Admin'))
  395. assert_not(user.role?('Agent'))
  396. assert(user.role?('Customer'))
  397. assert_equal('new_admin_by_agent@example.com', result_user1['login'])
  398. assert_equal('new_admin_by_agent@example.com', result_user1['email'])
  399. # create user with agent role
  400. role = Role.lookup(name: 'Agent')
  401. params = { firstname: "Agent#{firstname}", lastname: 'Agent Last', email: 'new_agent_by_agent@example.com', role_ids: [ role.id ] }
  402. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  403. assert_response(201)
  404. result_user1 = JSON.parse(@response.body)
  405. assert(result_user1)
  406. user = User.find(result_user1['id'])
  407. assert_not(user.role?('Admin'))
  408. assert_not(user.role?('Agent'))
  409. assert(user.role?('Customer'))
  410. assert_equal('new_agent_by_agent@example.com', result_user1['login'])
  411. assert_equal('new_agent_by_agent@example.com', result_user1['email'])
  412. # create user with customer role
  413. role = Role.lookup(name: 'Customer')
  414. params = { firstname: "Customer#{firstname}", lastname: 'Customer Last', email: 'new_customer_by_agent@example.com', role_ids: [ role.id ] }
  415. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  416. assert_response(201)
  417. result_user1 = JSON.parse(@response.body)
  418. assert(result_user1)
  419. user = User.find(result_user1['id'])
  420. assert_not(user.role?('Admin'))
  421. assert_not(user.role?('Agent'))
  422. assert(user.role?('Customer'))
  423. assert_equal('new_customer_by_agent@example.com', result_user1['login'])
  424. assert_equal('new_customer_by_agent@example.com', result_user1['email'])
  425. # search as agent
  426. Scheduler.worker(true)
  427. sleep 2 # let es time to come ready
  428. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}", params: {}, headers: @headers.merge('Authorization' => credentials)
  429. assert_response(200)
  430. result = JSON.parse(@response.body)
  431. assert_equal(Array, result.class)
  432. assert_equal(result_user1['id'], result[0]['id'])
  433. assert_equal("Customer#{firstname}", result[0]['firstname'])
  434. assert_equal('Customer Last', result[0]['lastname'])
  435. assert(result[0]['role_ids'])
  436. assert_not(result[0]['roles'])
  437. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&expand=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  438. assert_response(200)
  439. result = JSON.parse(@response.body)
  440. assert_equal(Array, result.class)
  441. assert_equal(result_user1['id'], result[0]['id'])
  442. assert_equal("Customer#{firstname}", result[0]['firstname'])
  443. assert_equal('Customer Last', result[0]['lastname'])
  444. assert(result[0]['role_ids'])
  445. assert(result[0]['roles'])
  446. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  447. assert_response(200)
  448. result = JSON.parse(@response.body)
  449. assert_equal(Array, result.class)
  450. assert_equal(result_user1['id'], result[0]['id'])
  451. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['label'])
  452. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['value'])
  453. assert_not(result[0]['role_ids'])
  454. assert_not(result[0]['roles'])
  455. role = Role.find_by(name: 'Agent')
  456. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  457. assert_response(200)
  458. result = JSON.parse(@response.body)
  459. assert_equal(Array, result.class)
  460. assert_equal(0, result.count)
  461. role = Role.find_by(name: 'Customer')
  462. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  463. assert_response(200)
  464. result = JSON.parse(@response.body)
  465. assert_equal(Array, result.class)
  466. assert_equal(result_user1['id'], result[0]['id'])
  467. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['label'])
  468. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['value'])
  469. assert_not(result[0]['role_ids'])
  470. assert_not(result[0]['roles'])
  471. permission = Permission.find_by(name: 'ticket.agent')
  472. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  473. assert_response(200)
  474. result = JSON.parse(@response.body)
  475. assert_equal(Array, result.class)
  476. assert_equal(0, result.count)
  477. permission = Permission.find_by(name: 'ticket.customer')
  478. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  479. assert_response(200)
  480. result = JSON.parse(@response.body)
  481. assert_equal(Array, result.class)
  482. assert_equal(result_user1['id'], result[0]['id'])
  483. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['label'])
  484. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['value'])
  485. assert_not(result[0]['role_ids'])
  486. assert_not(result[0]['roles'])
  487. end
  488. test 'user index and create with customer1' do
  489. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer1@example.com', 'customer1pw')
  490. # me
  491. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  492. assert_response(200)
  493. result = JSON.parse(@response.body)
  494. assert(result)
  495. assert_equal(result['email'], 'rest-customer1@example.com')
  496. # index
  497. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  498. assert_response(200)
  499. result = JSON.parse(@response.body)
  500. assert_equal(result.class, Array)
  501. assert_equal(result.length, 1)
  502. # show/:id
  503. get "/api/v1/users/#{@customer_without_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  504. assert_response(200)
  505. result = JSON.parse(@response.body)
  506. assert_equal(result.class, Hash)
  507. assert_equal(result['email'], 'rest-customer1@example.com')
  508. get "/api/v1/users/#{@customer_with_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  509. assert_response(401)
  510. result = JSON.parse(@response.body)
  511. assert_equal(result.class, Hash)
  512. assert(result['error'])
  513. # create user with admin role
  514. role = Role.lookup(name: 'Admin')
  515. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_customer1@example.com', role_ids: [ role.id ] }
  516. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  517. assert_response(401)
  518. # create user with agent role
  519. role = Role.lookup(name: 'Agent')
  520. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_customer1@example.com', role_ids: [ role.id ] }
  521. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  522. assert_response(401)
  523. # search
  524. Scheduler.worker(true)
  525. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  526. assert_response(401)
  527. end
  528. test 'user index with customer2' do
  529. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer2@example.com', 'customer2pw')
  530. # me
  531. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  532. assert_response(200)
  533. result = JSON.parse(@response.body)
  534. assert(result)
  535. assert_equal(result['email'], 'rest-customer2@example.com')
  536. # index
  537. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  538. assert_response(200)
  539. result = JSON.parse(@response.body)
  540. assert_equal(result.class, Array)
  541. assert_equal(result.length, 1)
  542. # show/:id
  543. get "/api/v1/users/#{@customer_with_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  544. assert_response(200)
  545. result = JSON.parse(@response.body)
  546. assert_equal(result.class, Hash)
  547. assert_equal(result['email'], 'rest-customer2@example.com')
  548. get "/api/v1/users/#{@customer_without_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  549. assert_response(401)
  550. result = JSON.parse(@response.body)
  551. assert_equal(result.class, Hash)
  552. assert(result['error'])
  553. # search
  554. Scheduler.worker(true)
  555. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  556. assert_response(401)
  557. end
  558. test '04.01 users show and response format' do
  559. roles = Role.where(name: 'Customer')
  560. organization = Organization.first
  561. user = User.create!(
  562. login: 'rest-customer3@example.com',
  563. firstname: 'Rest',
  564. lastname: 'Customer3',
  565. email: 'rest-customer3@example.com',
  566. password: 'customer3pw',
  567. active: true,
  568. organization: organization,
  569. roles: roles,
  570. updated_by_id: @admin.id,
  571. created_by_id: @admin.id,
  572. )
  573. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  574. get "/api/v1/users/#{user.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  575. assert_response(200)
  576. result = JSON.parse(@response.body)
  577. assert_equal(Hash, result.class)
  578. assert_equal(user.id, result['id'])
  579. assert_equal(user.firstname, result['firstname'])
  580. assert_not(result['organization'])
  581. assert_equal(user.organization_id, result['organization_id'])
  582. assert_not(result['password'])
  583. assert_equal(user.role_ids, result['role_ids'])
  584. assert_equal(@admin.id, result['updated_by_id'])
  585. assert_equal(@admin.id, result['created_by_id'])
  586. get "/api/v1/users/#{user.id}?expand=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  587. assert_response(200)
  588. result = JSON.parse(@response.body)
  589. assert_equal(Hash, result.class)
  590. assert_equal(user.id, result['id'])
  591. assert_equal(user.firstname, result['firstname'])
  592. assert_equal(user.organization_id, result['organization_id'])
  593. assert_equal(user.organization.name, result['organization'])
  594. assert_equal(user.role_ids, result['role_ids'])
  595. assert_not(result['password'])
  596. assert_equal(@admin.id, result['updated_by_id'])
  597. assert_equal(@admin.id, result['created_by_id'])
  598. get "/api/v1/users/#{user.id}?expand=false", params: {}, headers: @headers.merge('Authorization' => credentials)
  599. assert_response(200)
  600. result = JSON.parse(@response.body)
  601. assert_equal(Hash, result.class)
  602. assert_equal(user.id, result['id'])
  603. assert_equal(user.firstname, result['firstname'])
  604. assert_not(result['organization'])
  605. assert_equal(user.organization_id, result['organization_id'])
  606. assert_not(result['password'])
  607. assert_equal(user.role_ids, result['role_ids'])
  608. assert_equal(@admin.id, result['updated_by_id'])
  609. assert_equal(@admin.id, result['created_by_id'])
  610. get "/api/v1/users/#{user.id}?full=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  611. assert_response(200)
  612. result = JSON.parse(@response.body)
  613. assert_equal(Hash, result.class)
  614. assert_equal(user.id, result['id'])
  615. assert(result['assets'])
  616. assert(result['assets']['User'])
  617. assert(result['assets']['User'][user.id.to_s])
  618. assert_equal(user.id, result['assets']['User'][user.id.to_s]['id'])
  619. assert_equal(user.firstname, result['assets']['User'][user.id.to_s]['firstname'])
  620. assert_equal(user.organization_id, result['assets']['User'][user.id.to_s]['organization_id'])
  621. assert_equal(user.role_ids, result['assets']['User'][user.id.to_s]['role_ids'])
  622. get "/api/v1/users/#{user.id}?full=false", params: {}, headers: @headers.merge('Authorization' => credentials)
  623. assert_response(200)
  624. result = JSON.parse(@response.body)
  625. assert_equal(Hash, result.class)
  626. assert_equal(user.id, result['id'])
  627. assert_equal(user.firstname, result['firstname'])
  628. assert_not(result['organization'])
  629. assert_equal(user.organization_id, result['organization_id'])
  630. assert_not(result['password'])
  631. assert_equal(user.role_ids, result['role_ids'])
  632. assert_equal(@admin.id, result['updated_by_id'])
  633. assert_equal(@admin.id, result['created_by_id'])
  634. end
  635. test '04.02 user index and response format' do
  636. roles = Role.where(name: 'Customer')
  637. organization = Organization.first
  638. user = User.create!(
  639. login: 'rest-customer3@example.com',
  640. firstname: 'Rest',
  641. lastname: 'Customer3',
  642. email: 'rest-customer3@example.com',
  643. password: 'customer3pw',
  644. active: true,
  645. organization: organization,
  646. roles: roles,
  647. updated_by_id: @admin.id,
  648. created_by_id: @admin.id,
  649. )
  650. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  651. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  652. assert_response(200)
  653. result = JSON.parse(@response.body)
  654. assert_equal(Array, result.class)
  655. assert_equal(Hash, result[0].class)
  656. assert_equal(user.id, result.last['id'])
  657. assert_equal(user.lastname, result.last['lastname'])
  658. assert_not(result.last['organization'])
  659. assert_equal(user.role_ids, result.last['role_ids'])
  660. assert_equal(user.organization_id, result.last['organization_id'])
  661. assert_not(result.last['password'])
  662. assert_equal(@admin.id, result.last['updated_by_id'])
  663. assert_equal(@admin.id, result.last['created_by_id'])
  664. get '/api/v1/users?expand=true', params: {}, headers: @headers.merge('Authorization' => credentials)
  665. assert_response(200)
  666. result = JSON.parse(@response.body)
  667. assert_equal(Array, result.class)
  668. assert_equal(Hash, result[0].class)
  669. assert_equal(user.id, result.last['id'])
  670. assert_equal(user.lastname, result.last['lastname'])
  671. assert_equal(user.organization_id, result.last['organization_id'])
  672. assert_equal(user.organization.name, result.last['organization'])
  673. assert_not(result.last['password'])
  674. assert_equal(@admin.id, result.last['updated_by_id'])
  675. assert_equal(@admin.id, result.last['created_by_id'])
  676. get '/api/v1/users?expand=false', params: {}, headers: @headers.merge('Authorization' => credentials)
  677. assert_response(200)
  678. result = JSON.parse(@response.body)
  679. assert_equal(Array, result.class)
  680. assert_equal(Hash, result[0].class)
  681. assert_equal(user.id, result.last['id'])
  682. assert_equal(user.lastname, result.last['lastname'])
  683. assert_not(result.last['organization'])
  684. assert_equal(user.role_ids, result.last['role_ids'])
  685. assert_equal(user.organization_id, result.last['organization_id'])
  686. assert_not(result.last['password'])
  687. assert_equal(@admin.id, result.last['updated_by_id'])
  688. assert_equal(@admin.id, result.last['created_by_id'])
  689. get '/api/v1/users?full=true', params: {}, headers: @headers.merge('Authorization' => credentials)
  690. assert_response(200)
  691. result = JSON.parse(@response.body)
  692. assert_equal(Hash, result.class)
  693. assert_equal(Array, result['record_ids'].class)
  694. assert_equal(1, result['record_ids'][0])
  695. assert_equal(user.id, result['record_ids'].last)
  696. assert(result['assets'])
  697. assert(result['assets']['User'])
  698. assert(result['assets']['User'][user.id.to_s])
  699. assert_equal(user.id, result['assets']['User'][user.id.to_s]['id'])
  700. assert_equal(user.lastname, result['assets']['User'][user.id.to_s]['lastname'])
  701. assert_equal(user.organization_id, result['assets']['User'][user.id.to_s]['organization_id'])
  702. assert_not(result['assets']['User'][user.id.to_s]['password'])
  703. get '/api/v1/users?full=false', params: {}, headers: @headers.merge('Authorization' => credentials)
  704. assert_response(200)
  705. result = JSON.parse(@response.body)
  706. assert_equal(Array, result.class)
  707. assert_equal(Hash, result[0].class)
  708. assert_equal(user.id, result.last['id'])
  709. assert_equal(user.lastname, result.last['lastname'])
  710. assert_not(result.last['organization'])
  711. assert_equal(user.role_ids, result.last['role_ids'])
  712. assert_equal(user.organization_id, result.last['organization_id'])
  713. assert_not(result.last['password'])
  714. assert_equal(@admin.id, result.last['updated_by_id'])
  715. assert_equal(@admin.id, result.last['created_by_id'])
  716. end
  717. test '04.03 ticket create and response format' do
  718. organization = Organization.first
  719. params = {
  720. firstname: 'newfirstname123',
  721. note: 'some note',
  722. organization: organization.name,
  723. }
  724. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  725. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  726. assert_response(201)
  727. result = JSON.parse(@response.body)
  728. assert_equal(Hash, result.class)
  729. user = User.find(result['id'])
  730. assert_equal(user.firstname, result['firstname'])
  731. assert_equal(user.organization_id, result['organization_id'])
  732. assert_not(result['organization'])
  733. assert_not(result['password'])
  734. assert_equal(@admin.id, result['updated_by_id'])
  735. assert_equal(@admin.id, result['created_by_id'])
  736. post '/api/v1/users?expand=true', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  737. assert_response(201)
  738. result = JSON.parse(@response.body)
  739. assert_equal(Hash, result.class)
  740. user = User.find(result['id'])
  741. assert_equal(user.firstname, result['firstname'])
  742. assert_equal(user.organization_id, result['organization_id'])
  743. assert_equal(user.organization.name, result['organization'])
  744. assert_not(result['password'])
  745. assert_equal(@admin.id, result['updated_by_id'])
  746. assert_equal(@admin.id, result['created_by_id'])
  747. post '/api/v1/users?full=true', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  748. assert_response(201)
  749. result = JSON.parse(@response.body)
  750. assert_equal(Hash, result.class)
  751. user = User.find(result['id'])
  752. assert(result['assets'])
  753. assert(result['assets']['User'])
  754. assert(result['assets']['User'][user.id.to_s])
  755. assert_equal(user.id, result['assets']['User'][user.id.to_s]['id'])
  756. assert_equal(user.firstname, result['assets']['User'][user.id.to_s]['firstname'])
  757. assert_equal(user.lastname, result['assets']['User'][user.id.to_s]['lastname'])
  758. assert_not(result['assets']['User'][user.id.to_s]['password'])
  759. assert(result['assets']['User'][@admin.id.to_s])
  760. assert_equal(@admin.id, result['assets']['User'][@admin.id.to_s]['id'])
  761. assert_equal(@admin.firstname, result['assets']['User'][@admin.id.to_s]['firstname'])
  762. assert_equal(@admin.lastname, result['assets']['User'][@admin.id.to_s]['lastname'])
  763. assert_not(result['assets']['User'][@admin.id.to_s]['password'])
  764. end
  765. test '04.04 ticket update and response formats' do
  766. roles = Role.where(name: 'Customer')
  767. organization = Organization.first
  768. user = User.create!(
  769. login: 'rest-customer3@example.com',
  770. firstname: 'Rest',
  771. lastname: 'Customer3',
  772. email: 'rest-customer3@example.com',
  773. password: 'customer3pw',
  774. active: true,
  775. organization: organization,
  776. roles: roles,
  777. updated_by_id: @admin.id,
  778. created_by_id: @admin.id,
  779. )
  780. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  781. params = {
  782. firstname: 'a update firstname #1',
  783. }
  784. put "/api/v1/users/#{user.id}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  785. assert_response(200)
  786. result = JSON.parse(@response.body)
  787. assert_equal(Hash, result.class)
  788. user = User.find(result['id'])
  789. assert_equal(user.lastname, result['lastname'])
  790. assert_equal(params[:firstname], result['firstname'])
  791. assert_equal(user.organization_id, result['organization_id'])
  792. assert_not(result['organization'])
  793. assert_not(result['password'])
  794. assert_equal(@admin.id, result['updated_by_id'])
  795. assert_equal(@admin.id, result['created_by_id'])
  796. params = {
  797. firstname: 'a update firstname #2',
  798. }
  799. put "/api/v1/users/#{user.id}?expand=true", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  800. assert_response(200)
  801. result = JSON.parse(@response.body)
  802. assert_equal(Hash, result.class)
  803. user = User.find(result['id'])
  804. assert_equal(user.lastname, result['lastname'])
  805. assert_equal(params[:firstname], result['firstname'])
  806. assert_equal(user.organization_id, result['organization_id'])
  807. assert_equal(user.organization.name, result['organization'])
  808. assert_not(result['password'])
  809. assert_equal(@admin.id, result['updated_by_id'])
  810. assert_equal(@admin.id, result['created_by_id'])
  811. params = {
  812. firstname: 'a update firstname #3',
  813. }
  814. put "/api/v1/users/#{user.id}?full=true", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  815. assert_response(200)
  816. result = JSON.parse(@response.body)
  817. assert_equal(Hash, result.class)
  818. user = User.find(result['id'])
  819. assert(result['assets'])
  820. assert(result['assets']['User'])
  821. assert(result['assets']['User'][user.id.to_s])
  822. assert_equal(user.id, result['assets']['User'][user.id.to_s]['id'])
  823. assert_equal(params[:firstname], result['assets']['User'][user.id.to_s]['firstname'])
  824. assert_equal(user.lastname, result['assets']['User'][user.id.to_s]['lastname'])
  825. assert_not(result['assets']['User'][user.id.to_s]['password'])
  826. assert(result['assets']['User'][@admin.id.to_s])
  827. assert_equal(@admin.id, result['assets']['User'][@admin.id.to_s]['id'])
  828. assert_equal(@admin.firstname, result['assets']['User'][@admin.id.to_s]['firstname'])
  829. assert_equal(@admin.lastname, result['assets']['User'][@admin.id.to_s]['lastname'])
  830. assert_not(result['assets']['User'][@admin.id.to_s]['password'])
  831. end
  832. end