user_organization_controller_test.rb 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. require 'test_helper'
  2. require 'rake'
  3. class UserOrganizationControllerTest < 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. end
  102. test 'user create tests - no user' do
  103. post '/api/v1/signshow', params: {}, headers: @headers
  104. # create user with disabled feature
  105. Setting.set('user_create_account', false)
  106. token = @response.headers['CSRF-TOKEN']
  107. # token based on form
  108. params = { email: 'some_new_customer@example.com', authenticity_token: token }
  109. post '/api/v1/users', params: params.to_json, headers: @headers
  110. assert_response(422)
  111. result = JSON.parse(@response.body)
  112. assert(result['error'])
  113. assert_equal('Feature not enabled!', result['error'])
  114. # token based on headers
  115. headers = @headers.merge('X-CSRF-Token' => token)
  116. params = { email: 'some_new_customer@example.com' }
  117. post '/api/v1/users', params: params.to_json, headers: headers
  118. assert_response(422)
  119. result = JSON.parse(@response.body)
  120. assert(result['error'])
  121. assert_equal('Feature not enabled!', result['error'])
  122. Setting.set('user_create_account', true)
  123. # no signup param with enabled feature
  124. params = { email: 'some_new_customer@example.com' }
  125. post '/api/v1/users', params: params.to_json, headers: headers
  126. assert_response(422)
  127. result = JSON.parse(@response.body)
  128. assert(result['error'])
  129. assert_equal('Only signup with not authenticate user possible!', result['error'])
  130. # already existing user with enabled feature
  131. params = { email: 'rest-customer1@example.com', signup: true }
  132. post '/api/v1/users', params: params.to_json, headers: headers
  133. assert_response(422)
  134. result = JSON.parse(@response.body)
  135. assert(result['error'])
  136. assert_equal('Email address is already used for other user.', result['error'])
  137. # email missing with enabled feature
  138. params = { firstname: 'some firstname', signup: true }
  139. post '/api/v1/users', params: params.to_json, headers: headers
  140. assert_response(422)
  141. result = JSON.parse(@response.body)
  142. assert(result['error'])
  143. assert_equal('Attribute \'email\' required!', result['error'])
  144. # email missing with enabled feature
  145. params = { firstname: 'some firstname', signup: true }
  146. post '/api/v1/users', params: params.to_json, headers: headers
  147. assert_response(422)
  148. result = JSON.parse(@response.body)
  149. assert(result['error'])
  150. assert_equal('Attribute \'email\' required!', result['error'])
  151. # create user with enabled feature (take customer role)
  152. params = { firstname: 'Me First', lastname: 'Me Last', email: 'new_here@example.com', signup: true }
  153. post '/api/v1/users', params: params.to_json, headers: headers
  154. assert_response(201)
  155. result = JSON.parse(@response.body)
  156. assert(result)
  157. assert_equal('Me First', result['firstname'])
  158. assert_equal('Me Last', result['lastname'])
  159. assert_equal('new_here@example.com', result['login'])
  160. assert_equal('new_here@example.com', result['email'])
  161. user = User.find(result['id'])
  162. assert_not(user.role?('Admin'))
  163. assert_not(user.role?('Agent'))
  164. assert(user.role?('Customer'))
  165. # create user with admin role (not allowed for signup, take customer role)
  166. role = Role.lookup(name: 'Admin')
  167. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin@example.com', role_ids: [ role.id ], signup: true }
  168. post '/api/v1/users', params: params.to_json, headers: headers
  169. assert_response(201)
  170. result = JSON.parse(@response.body)
  171. assert(result)
  172. user = User.find(result['id'])
  173. assert_not(user.role?('Admin'))
  174. assert_not(user.role?('Agent'))
  175. assert(user.role?('Customer'))
  176. # create user with agent role (not allowed for signup, take customer role)
  177. role = Role.lookup(name: 'Agent')
  178. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent@example.com', role_ids: [ role.id ], signup: true }
  179. post '/api/v1/users', params: params.to_json, headers: headers
  180. assert_response(201)
  181. result = JSON.parse(@response.body)
  182. assert(result)
  183. user = User.find(result['id'])
  184. assert_not(user.role?('Admin'))
  185. assert_not(user.role?('Agent'))
  186. assert(user.role?('Customer'))
  187. # no user (because of no session)
  188. get '/api/v1/users', params: {}, headers: headers
  189. assert_response(401)
  190. result = JSON.parse(@response.body)
  191. assert_equal('authentication failed', result['error'])
  192. # me
  193. get '/api/v1/users/me', params: {}, headers: headers
  194. assert_response(401)
  195. result = JSON.parse(@response.body)
  196. assert_equal('authentication failed', result['error'])
  197. end
  198. test 'auth tests - not existing user' do
  199. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('not_existing@example.com', 'adminpw')
  200. # me
  201. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  202. assert_response(401)
  203. result = JSON.parse(@response.body)
  204. assert_equal('authentication failed', result['error'])
  205. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  206. assert_response(401)
  207. result = JSON.parse(@response.body)
  208. assert_equal('authentication failed', result['error'])
  209. end
  210. test 'auth tests - username auth, wrong pw' do
  211. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin', 'not_existing')
  212. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  213. assert_response(401)
  214. result = JSON.parse(@response.body)
  215. assert_equal('authentication failed', result['error'])
  216. end
  217. test 'auth tests - email auth, wrong pw' do
  218. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'not_existing')
  219. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  220. assert_response(401)
  221. result = JSON.parse(@response.body)
  222. assert_equal('authentication failed', result['error'])
  223. end
  224. test 'auth tests - username auth' do
  225. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin', 'adminpw')
  226. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  227. assert_response(200)
  228. result = JSON.parse(@response.body)
  229. assert(result)
  230. end
  231. test 'auth tests - email auth' do
  232. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  233. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  234. assert_response(200)
  235. result = JSON.parse(@response.body)
  236. assert(result)
  237. end
  238. test 'user index and create with admin' do
  239. # email auth
  240. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  241. # me
  242. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  243. assert_response(200)
  244. result = JSON.parse(@response.body)
  245. assert(result)
  246. assert_equal(result['email'], 'rest-admin@example.com')
  247. # index
  248. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  249. assert_response(200)
  250. result = JSON.parse(@response.body)
  251. assert(result)
  252. # index
  253. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  254. assert_response(200)
  255. result = JSON.parse(@response.body)
  256. assert(result)
  257. assert_equal(result.class, Array)
  258. assert(result.length >= 3)
  259. # show/:id
  260. get "/api/v1/users/#{@agent.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  261. assert_response(200)
  262. result = JSON.parse(@response.body)
  263. assert(result)
  264. assert_equal(result.class, Hash)
  265. assert_equal(result['email'], 'rest-agent@example.com')
  266. get "/api/v1/users/#{@customer_without_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  267. assert_response(200)
  268. result = JSON.parse(@response.body)
  269. assert(result)
  270. assert_equal(result.class, Hash)
  271. assert_equal(result['email'], 'rest-customer1@example.com')
  272. # create user with admin role
  273. role = Role.lookup(name: 'Admin')
  274. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_admin@example.com', role_ids: [ role.id ] }
  275. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  276. assert_response(201)
  277. result = JSON.parse(@response.body)
  278. assert(result)
  279. user = User.find(result['id'])
  280. assert(user.role?('Admin'))
  281. assert_not(user.role?('Agent'))
  282. assert_not(user.role?('Customer'))
  283. assert_equal('new_admin_by_admin@example.com', result['login'])
  284. assert_equal('new_admin_by_admin@example.com', result['email'])
  285. # create user with agent role
  286. role = Role.lookup(name: 'Agent')
  287. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_admin1@example.com', role_ids: [ role.id ] }
  288. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  289. assert_response(201)
  290. result = JSON.parse(@response.body)
  291. assert(result)
  292. user = User.find(result['id'])
  293. assert_not(user.role?('Admin'))
  294. assert(user.role?('Agent'))
  295. assert_not(user.role?('Customer'))
  296. assert_equal('new_agent_by_admin1@example.com', result['login'])
  297. assert_equal('new_agent_by_admin1@example.com', result['email'])
  298. role = Role.lookup(name: 'Agent')
  299. params = { firstname: 'Agent First', email: 'new_agent_by_admin2@example.com', role_ids: [ role.id ] }
  300. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  301. assert_response(201)
  302. result = JSON.parse(@response.body)
  303. assert(result)
  304. user = User.find(result['id'])
  305. assert_not(user.role?('Admin'))
  306. assert(user.role?('Agent'))
  307. assert_not(user.role?('Customer'))
  308. assert_equal('new_agent_by_admin2@example.com', result['login'])
  309. assert_equal('new_agent_by_admin2@example.com', result['email'])
  310. assert_equal('Agent', result['firstname'])
  311. assert_equal('First', result['lastname'])
  312. role = Role.lookup(name: 'Agent')
  313. params = { firstname: 'Agent First', email: 'new_agent_by_admin2@example.com', role_ids: [ role.id ] }
  314. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  315. assert_response(422)
  316. result = JSON.parse(@response.body)
  317. assert(result)
  318. assert_equal('Email address is already used for other user.', result['error'])
  319. # missing required attributes
  320. params = { note: 'some note' }
  321. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  322. assert_response(422)
  323. result = JSON.parse(@response.body)
  324. assert(result)
  325. assert_equal('Minimum one identifier (login, firstname, lastname, phone or email) for user is required.', result['error'])
  326. # invalid email
  327. params = { firstname: 'newfirstname123', email: 'some_what', note: 'some note' }
  328. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  329. assert_response(422)
  330. result = JSON.parse(@response.body)
  331. assert(result)
  332. assert_equal('Invalid email', result['error'])
  333. # with valid attributes
  334. params = { firstname: 'newfirstname123', note: 'some note' }
  335. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  336. assert_response(201)
  337. result = JSON.parse(@response.body)
  338. assert(result)
  339. user = User.find(result['id'])
  340. assert_not(user.role?('Admin'))
  341. assert_not(user.role?('Agent'))
  342. assert(user.role?('Customer'))
  343. assert(result['login'].start_with?('auto-'))
  344. assert_equal('', result['email'])
  345. assert_equal('newfirstname123', result['firstname'])
  346. assert_equal('', result['lastname'])
  347. end
  348. test 'user index and create with agent' do
  349. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-agent@example.com', 'agentpw')
  350. # me
  351. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  352. assert_response(200)
  353. result = JSON.parse(@response.body)
  354. assert(result)
  355. assert_equal(result['email'], 'rest-agent@example.com')
  356. # index
  357. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  358. assert_response(200)
  359. result = JSON.parse(@response.body)
  360. assert(result)
  361. # index
  362. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  363. assert_response(200)
  364. result = JSON.parse(@response.body)
  365. assert(result)
  366. assert_equal(result.class, Array)
  367. assert(result.length >= 3)
  368. get '/api/v1/users?limit=40&page=1&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  369. assert_response(200)
  370. result = JSON.parse(@response.body)
  371. assert_equal(Array, result.class)
  372. users = User.order(:id).limit(2)
  373. assert_equal(users[0].id, result[0]['id'])
  374. assert_equal(users[1].id, result[1]['id'])
  375. assert_equal(2, result.count)
  376. get '/api/v1/users?limit=40&page=2&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  377. assert_response(200)
  378. result = JSON.parse(@response.body)
  379. assert_equal(Array, result.class)
  380. users = User.order(:id).limit(4)
  381. assert_equal(users[2].id, result[0]['id'])
  382. assert_equal(users[3].id, result[1]['id'])
  383. assert_equal(2, result.count)
  384. # create user with admin role
  385. firstname = "First test#{rand(999_999_999)}"
  386. role = Role.lookup(name: 'Admin')
  387. params = { firstname: "Admin#{firstname}", lastname: 'Admin Last', email: 'new_admin_by_agent@example.com', role_ids: [ role.id ] }
  388. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  389. assert_response(201)
  390. result_user1 = JSON.parse(@response.body)
  391. assert(result_user1)
  392. user = User.find(result_user1['id'])
  393. assert_not(user.role?('Admin'))
  394. assert_not(user.role?('Agent'))
  395. assert(user.role?('Customer'))
  396. assert_equal('new_admin_by_agent@example.com', result_user1['login'])
  397. assert_equal('new_admin_by_agent@example.com', result_user1['email'])
  398. # create user with agent role
  399. role = Role.lookup(name: 'Agent')
  400. params = { firstname: "Agent#{firstname}", lastname: 'Agent Last', email: 'new_agent_by_agent@example.com', role_ids: [ role.id ] }
  401. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  402. assert_response(201)
  403. result_user1 = JSON.parse(@response.body)
  404. assert(result_user1)
  405. user = User.find(result_user1['id'])
  406. assert_not(user.role?('Admin'))
  407. assert_not(user.role?('Agent'))
  408. assert(user.role?('Customer'))
  409. assert_equal('new_agent_by_agent@example.com', result_user1['login'])
  410. assert_equal('new_agent_by_agent@example.com', result_user1['email'])
  411. # create user with customer role
  412. role = Role.lookup(name: 'Customer')
  413. params = { firstname: "Customer#{firstname}", lastname: 'Customer Last', email: 'new_customer_by_agent@example.com', role_ids: [ role.id ] }
  414. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  415. assert_response(201)
  416. result_user1 = JSON.parse(@response.body)
  417. assert(result_user1)
  418. user = User.find(result_user1['id'])
  419. assert_not(user.role?('Admin'))
  420. assert_not(user.role?('Agent'))
  421. assert(user.role?('Customer'))
  422. assert_equal('new_customer_by_agent@example.com', result_user1['login'])
  423. assert_equal('new_customer_by_agent@example.com', result_user1['email'])
  424. # search as agent
  425. Scheduler.worker(true)
  426. sleep 2 # let es time to come ready
  427. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}", params: {}, headers: @headers.merge('Authorization' => credentials)
  428. assert_response(200)
  429. result = JSON.parse(@response.body)
  430. assert_equal(Array, result.class)
  431. assert_equal(result_user1['id'], result[0]['id'])
  432. assert_equal("Customer#{firstname}", result[0]['firstname'])
  433. assert_equal('Customer Last', result[0]['lastname'])
  434. assert(result[0]['role_ids'])
  435. assert_not(result[0]['roles'])
  436. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&expand=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  437. assert_response(200)
  438. result = JSON.parse(@response.body)
  439. assert_equal(Array, result.class)
  440. assert_equal(result_user1['id'], result[0]['id'])
  441. assert_equal("Customer#{firstname}", result[0]['firstname'])
  442. assert_equal('Customer Last', result[0]['lastname'])
  443. assert(result[0]['role_ids'])
  444. assert(result[0]['roles'])
  445. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  446. assert_response(200)
  447. result = JSON.parse(@response.body)
  448. assert_equal(Array, result.class)
  449. assert_equal(result_user1['id'], result[0]['id'])
  450. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['label'])
  451. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['value'])
  452. assert_not(result[0]['role_ids'])
  453. assert_not(result[0]['roles'])
  454. role = Role.find_by(name: 'Agent')
  455. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  456. assert_response(200)
  457. result = JSON.parse(@response.body)
  458. assert_equal(Array, result.class)
  459. assert_equal(0, result.count)
  460. role = Role.find_by(name: 'Customer')
  461. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  462. assert_response(200)
  463. result = JSON.parse(@response.body)
  464. assert_equal(Array, result.class)
  465. assert_equal(result_user1['id'], result[0]['id'])
  466. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['label'])
  467. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['value'])
  468. assert_not(result[0]['role_ids'])
  469. assert_not(result[0]['roles'])
  470. permission = Permission.find_by(name: 'ticket.agent')
  471. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  472. assert_response(200)
  473. result = JSON.parse(@response.body)
  474. assert_equal(Array, result.class)
  475. assert_equal(0, result.count)
  476. permission = Permission.find_by(name: 'ticket.customer')
  477. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  478. assert_response(200)
  479. result = JSON.parse(@response.body)
  480. assert_equal(Array, result.class)
  481. assert_equal(result_user1['id'], result[0]['id'])
  482. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['label'])
  483. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['value'])
  484. assert_not(result[0]['role_ids'])
  485. assert_not(result[0]['roles'])
  486. end
  487. test 'user index and create with customer1' do
  488. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer1@example.com', 'customer1pw')
  489. # me
  490. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  491. assert_response(200)
  492. result = JSON.parse(@response.body)
  493. assert(result)
  494. assert_equal(result['email'], 'rest-customer1@example.com')
  495. # index
  496. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  497. assert_response(200)
  498. result = JSON.parse(@response.body)
  499. assert_equal(result.class, Array)
  500. assert_equal(result.length, 1)
  501. # show/:id
  502. get "/api/v1/users/#{@customer_without_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  503. assert_response(200)
  504. result = JSON.parse(@response.body)
  505. assert_equal(result.class, Hash)
  506. assert_equal(result['email'], 'rest-customer1@example.com')
  507. get "/api/v1/users/#{@customer_with_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  508. assert_response(401)
  509. result = JSON.parse(@response.body)
  510. assert_equal(result.class, Hash)
  511. assert(result['error'])
  512. # create user with admin role
  513. role = Role.lookup(name: 'Admin')
  514. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_customer1@example.com', role_ids: [ role.id ] }
  515. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  516. assert_response(401)
  517. # create user with agent role
  518. role = Role.lookup(name: 'Agent')
  519. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_customer1@example.com', role_ids: [ role.id ] }
  520. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  521. assert_response(401)
  522. # search
  523. Scheduler.worker(true)
  524. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  525. assert_response(401)
  526. end
  527. test 'user index with customer2' do
  528. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer2@example.com', 'customer2pw')
  529. # me
  530. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  531. assert_response(200)
  532. result = JSON.parse(@response.body)
  533. assert(result)
  534. assert_equal(result['email'], 'rest-customer2@example.com')
  535. # index
  536. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  537. assert_response(200)
  538. result = JSON.parse(@response.body)
  539. assert_equal(result.class, Array)
  540. assert_equal(result.length, 1)
  541. # show/:id
  542. get "/api/v1/users/#{@customer_with_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  543. assert_response(200)
  544. result = JSON.parse(@response.body)
  545. assert_equal(result.class, Hash)
  546. assert_equal(result['email'], 'rest-customer2@example.com')
  547. get "/api/v1/users/#{@customer_without_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  548. assert_response(401)
  549. result = JSON.parse(@response.body)
  550. assert_equal(result.class, Hash)
  551. assert(result['error'])
  552. # search
  553. Scheduler.worker(true)
  554. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  555. assert_response(401)
  556. end
  557. test 'organization index with agent' do
  558. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-agent@example.com', 'agentpw')
  559. # index
  560. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => credentials)
  561. assert_response(200)
  562. result = JSON.parse(@response.body)
  563. assert_equal(result.class, Array)
  564. assert_equal(result[0]['member_ids'].class, Array)
  565. assert(result.length >= 3)
  566. get '/api/v1/organizations?limit=40&page=1&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  567. assert_response(200)
  568. result = JSON.parse(@response.body)
  569. assert_equal(Array, result.class)
  570. organizations = Organization.order(:id).limit(2)
  571. assert_equal(organizations[0].id, result[0]['id'])
  572. assert_equal(organizations[0].member_ids, result[0]['member_ids'])
  573. assert_equal(organizations[1].id, result[1]['id'])
  574. assert_equal(organizations[1].member_ids, result[1]['member_ids'])
  575. assert_equal(2, result.count)
  576. get '/api/v1/organizations?limit=40&page=2&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  577. assert_response(200)
  578. result = JSON.parse(@response.body)
  579. assert_equal(Array, result.class)
  580. organizations = Organization.order(:id).limit(4)
  581. assert_equal(organizations[2].id, result[0]['id'])
  582. assert_equal(organizations[2].member_ids, result[0]['member_ids'])
  583. assert_equal(organizations[3].id, result[1]['id'])
  584. assert_equal(organizations[3].member_ids, result[1]['member_ids'])
  585. assert_equal(2, result.count)
  586. # show/:id
  587. get "/api/v1/organizations/#{@organization.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  588. assert_response(200)
  589. result = JSON.parse(@response.body)
  590. assert_equal(result.class, Hash)
  591. assert_equal(result['member_ids'].class, Array)
  592. assert_not(result['members'])
  593. assert_equal(result['name'], 'Rest Org')
  594. get "/api/v1/organizations/#{@organization2.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  595. assert_response(200)
  596. result = JSON.parse(@response.body)
  597. assert_equal(result.class, Hash)
  598. assert_equal(result['member_ids'].class, Array)
  599. assert_not(result['members'])
  600. assert_equal(result['name'], 'Rest Org #2')
  601. # search as agent
  602. Scheduler.worker(true)
  603. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  604. assert_response(200)
  605. result = JSON.parse(@response.body)
  606. assert_equal(Array, result.class)
  607. assert_equal('Zammad Foundation', result[0]['name'])
  608. assert(result[0]['member_ids'])
  609. assert_not(result[0]['members'])
  610. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&expand=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  611. assert_response(200)
  612. result = JSON.parse(@response.body)
  613. assert_equal(Array, result.class)
  614. assert_equal('Zammad Foundation', result[0]['name'])
  615. assert(result[0]['member_ids'])
  616. assert(result[0]['members'])
  617. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  618. assert_response(200)
  619. result = JSON.parse(@response.body)
  620. assert_equal(Array, result.class)
  621. assert_equal('Zammad Foundation', result[0]['label'])
  622. assert_equal('Zammad Foundation', result[0]['value'])
  623. assert_not(result[0]['member_ids'])
  624. assert_not(result[0]['members'])
  625. end
  626. test 'organization index with customer1' do
  627. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer1@example.com', 'customer1pw')
  628. # index
  629. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => credentials)
  630. assert_response(200)
  631. result = JSON.parse(@response.body)
  632. assert_equal(result.class, Array)
  633. assert_equal(result.length, 0)
  634. # show/:id
  635. get "/api/v1/organizations/#{@organization.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  636. assert_response(200)
  637. result = JSON.parse(@response.body)
  638. assert_equal(result.class, Hash)
  639. assert_nil(result['name'])
  640. get "/api/v1/organizations/#{@organization2.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  641. assert_response(200)
  642. result = JSON.parse(@response.body)
  643. assert_equal(result.class, Hash)
  644. assert_nil(result['name'])
  645. # search
  646. Scheduler.worker(true)
  647. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  648. assert_response(401)
  649. end
  650. test 'organization index with customer2' do
  651. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer2@example.com', 'customer2pw')
  652. # index
  653. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => credentials)
  654. assert_response(200)
  655. result = JSON.parse(@response.body)
  656. assert_equal(result.class, Array)
  657. assert_equal(result.length, 1)
  658. # show/:id
  659. get "/api/v1/organizations/#{@organization.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  660. assert_response(200)
  661. result = JSON.parse(@response.body)
  662. assert_equal(result.class, Hash)
  663. assert_equal(result['name'], 'Rest Org')
  664. get "/api/v1/organizations/#{@organization2.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  665. assert_response(401)
  666. result = JSON.parse(@response.body)
  667. assert_equal(result.class, Hash)
  668. assert_nil(result['name'])
  669. # search
  670. Scheduler.worker(true)
  671. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  672. assert_response(401)
  673. end
  674. end