user_spec.rb 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'User', performs_jobs: true, type: :request do
  4. describe 'request handling' do
  5. let!(:admin) do
  6. create(
  7. :admin,
  8. groups: Group.all,
  9. login: 'rest-admin',
  10. firstname: 'Rest',
  11. lastname: 'Agent',
  12. email: 'rest-admin@example.com',
  13. )
  14. end
  15. let!(:admin_with_pw) do
  16. create(
  17. :admin,
  18. groups: Group.all,
  19. login: 'rest-admin-pw',
  20. firstname: 'Rest',
  21. lastname: 'Agent',
  22. email: 'rest-admin-pw@example.com',
  23. password: 'adminpw',
  24. )
  25. end
  26. let!(:agent) do
  27. create(
  28. :agent,
  29. groups: Group.all,
  30. login: 'rest-agent@example.com',
  31. firstname: 'Rest',
  32. lastname: 'Agent',
  33. email: 'rest-agent@example.com',
  34. )
  35. end
  36. let!(:customer) do
  37. create(
  38. :customer,
  39. login: 'rest-customer1@example.com',
  40. firstname: 'Rest',
  41. lastname: 'Customer1',
  42. email: 'rest-customer1@example.com',
  43. )
  44. end
  45. let!(:organization) do
  46. create(:organization, name: 'Rest Org')
  47. end
  48. let!(:organization2) do
  49. create(:organization, name: 'Rest Org #2')
  50. end
  51. let!(:organization3) do
  52. create(:organization, name: 'Rest Org #3')
  53. end
  54. let!(:customer2) do
  55. create(
  56. :customer,
  57. organization: organization,
  58. login: 'rest-customer2@example.com',
  59. firstname: 'Rest',
  60. lastname: 'Customer2',
  61. email: 'rest-customer2@example.com',
  62. )
  63. end
  64. let!(:customer_inactive) do
  65. create(
  66. :customer,
  67. organization: organization,
  68. login: 'rest-customer_inactive@example.com',
  69. firstname: 'Rest',
  70. lastname: 'CustomerInactive',
  71. email: 'rest-customer_inactive@example.com',
  72. active: false,
  73. )
  74. end
  75. it 'does user create tests - no user' do
  76. post '/api/v1/signshow', params: {}, as: :json
  77. # create user with disabled feature
  78. Setting.set('user_create_account', false)
  79. token = @response.headers['CSRF-TOKEN']
  80. # token based on form
  81. params = { email: 'some_new_customer@example.com', signup: true, authenticity_token: token }
  82. post '/api/v1/users', params: params, as: :json
  83. expect(response).to have_http_status(:unprocessable_entity)
  84. expect(json_response['error']).to be_truthy
  85. expect(json_response['error']).to eq('Feature not enabled!')
  86. # token based on headers
  87. headers = { 'X-CSRF-Token' => token }
  88. params = { email: 'some_new_customer@example.com', signup: true }
  89. post '/api/v1/users', params: params, headers: headers, as: :json
  90. expect(response).to have_http_status(:unprocessable_entity)
  91. expect(json_response['error']).to be_truthy
  92. expect(json_response['error']).to eq('Feature not enabled!')
  93. Setting.set('user_create_account', true)
  94. # no signup param without password
  95. params = { email: 'some_new_customer@example.com', signup: true }
  96. post '/api/v1/users', params: params, headers: headers, as: :json
  97. expect(response).to have_http_status(:unprocessable_entity)
  98. expect(json_response['error']).to be_truthy
  99. # already existing user with enabled feature, pretend signup is successful
  100. params = { email: 'rest-customer1@example.com', password: 'asd1ASDasd!', signup: true }
  101. post '/api/v1/users', params: params, headers: headers, as: :json
  102. expect(response).to have_http_status(:created)
  103. expect(json_response).to be_truthy
  104. # email missing with enabled feature
  105. params = { firstname: 'some firstname', signup: true }
  106. post '/api/v1/users', params: params, headers: headers, as: :json
  107. expect(response).to have_http_status(:unprocessable_entity)
  108. expect(json_response['error']).to be_truthy
  109. expect(json_response['error']).to eq('Attribute \'email\' required!')
  110. # email missing with enabled feature
  111. params = { firstname: 'some firstname', signup: true }
  112. post '/api/v1/users', params: params, headers: headers, as: :json
  113. expect(response).to have_http_status(:unprocessable_entity)
  114. expect(json_response['error']).to be_truthy
  115. expect(json_response['error']).to eq('Attribute \'email\' required!')
  116. # create user with enabled feature (take customer role)
  117. params = { firstname: 'Me First', lastname: 'Me Last', email: 'new_here@example.com', password: '1asdASDasd', signup: true }
  118. post '/api/v1/users', params: params, headers: headers, as: :json
  119. expect(response).to have_http_status(:created)
  120. expect(json_response).to be_truthy
  121. expect(json_response['message']).to eq('ok')
  122. user = User.find_by email: 'new_here@example.com'
  123. expect(user).not_to be_role('Admin')
  124. expect(user).not_to be_role('Agent')
  125. expect(user).to be_role('Customer')
  126. # create user with admin role (not allowed for signup, take customer role)
  127. role = Role.lookup(name: 'Admin')
  128. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin@example.com', role_ids: [ role.id ], signup: true, password: '1asdASDasd' }
  129. post '/api/v1/users', params: params, headers: headers, as: :json
  130. expect(response).to have_http_status(:created)
  131. expect(json_response).to be_truthy
  132. user = User.find_by email: 'new_admin@example.com'
  133. expect(user).not_to be_role('Admin')
  134. expect(user).not_to be_role('Agent')
  135. expect(user).to be_role('Customer')
  136. # create user with agent role (not allowed for signup, take customer role)
  137. role = Role.lookup(name: 'Agent')
  138. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent@example.com', role_ids: [ role.id ], signup: true, password: '1asdASDasd' }
  139. post '/api/v1/users', params: params, headers: headers, as: :json
  140. expect(response).to have_http_status(:created)
  141. expect(json_response).to be_truthy
  142. user = User.find_by email: 'new_agent@example.com'
  143. expect(user).not_to be_role('Admin')
  144. expect(user).not_to be_role('Agent')
  145. expect(user).to be_role('Customer')
  146. # no user (because of no session)
  147. get '/api/v1/users', params: {}, headers: headers, as: :json
  148. expect(response).to have_http_status(:forbidden)
  149. expect(json_response['error']).to eq('Authentication required')
  150. # me
  151. get '/api/v1/users/me', params: {}, headers: headers, as: :json
  152. expect(response).to have_http_status(:forbidden)
  153. expect(json_response['error']).to eq('Authentication required')
  154. end
  155. context 'password security' do
  156. it 'verified with no current user' do
  157. params = { email: 'some_new_customer@example.com', password: 'asdasdasdasd', signup: true }
  158. post '/api/v1/users', params: params, headers: headers, as: :json
  159. expect(response).to have_http_status(:unprocessable_entity)
  160. expect(json_response['error']).to be_a(Array).and(include(match(%r{Invalid password})))
  161. end
  162. it 'verified with no current user', authenticated_as: :admin do
  163. params = { email: 'some_new_customer@example.com', password: 'asd' }
  164. post '/api/v1/users', params: params, headers: headers, as: :json
  165. expect(response).to have_http_status(:created)
  166. end
  167. end
  168. it 'does auth tests - not existing user' do
  169. authenticated_as(nil, login: 'not_existing@example.com', password: 'adminpw')
  170. get '/api/v1/users/me', params: {}, as: :json
  171. expect(response).to have_http_status(:unauthorized)
  172. expect(json_response['error']).to eq('Invalid BasicAuth credentials')
  173. get '/api/v1/users', params: {}, as: :json
  174. expect(response).to have_http_status(:unauthorized)
  175. expect(json_response['error']).to eq('Invalid BasicAuth credentials')
  176. end
  177. it 'does auth tests - username auth, wrong pw' do
  178. authenticated_as(admin, password: 'not_existing')
  179. get '/api/v1/users', params: {}, as: :json
  180. expect(response).to have_http_status(:unauthorized)
  181. expect(json_response['error']).to eq('Invalid BasicAuth credentials')
  182. end
  183. it 'does auth tests - email auth, wrong pw' do
  184. authenticated_as(nil, login: 'rest-admin@example.com', password: 'not_existing')
  185. get '/api/v1/users', params: {}, as: :json
  186. expect(response).to have_http_status(:unauthorized)
  187. expect(json_response['error']).to eq('Invalid BasicAuth credentials')
  188. end
  189. it 'does auth tests - username auth' do
  190. authenticated_as(nil, login: 'rest-admin-pw', password: 'adminpw')
  191. get '/api/v1/users', params: {}, as: :json
  192. expect(response).to have_http_status(:ok)
  193. expect(json_response).to be_truthy
  194. end
  195. it 'does auth tests - email auth' do
  196. authenticated_as(nil, login: 'rest-admin-pw@example.com', password: 'adminpw')
  197. get '/api/v1/users', params: {}, as: :json
  198. expect(response).to have_http_status(:ok)
  199. expect(json_response).to be_truthy
  200. end
  201. it 'does user index and create with admin' do
  202. authenticated_as(admin)
  203. get '/api/v1/users/me', params: {}, as: :json
  204. expect(response).to have_http_status(:ok)
  205. expect(json_response).to be_truthy
  206. expect(json_response['email']).to eq('rest-admin@example.com')
  207. # index
  208. get '/api/v1/users', params: {}, as: :json
  209. expect(response).to have_http_status(:ok)
  210. expect(json_response).to be_truthy
  211. # index
  212. get '/api/v1/users', params: {}, as: :json
  213. expect(response).to have_http_status(:ok)
  214. expect(json_response).to be_truthy
  215. expect(Array).to eq(json_response.class)
  216. expect(json_response.length >= 3).to be_truthy
  217. # show/:id
  218. get "/api/v1/users/#{agent.id}", params: {}, as: :json
  219. expect(response).to have_http_status(:ok)
  220. expect(json_response).to be_truthy
  221. expect(Hash).to eq(json_response.class)
  222. expect(json_response['email']).to eq('rest-agent@example.com')
  223. get "/api/v1/users/#{customer.id}", params: {}, as: :json
  224. expect(response).to have_http_status(:ok)
  225. expect(json_response).to be_truthy
  226. expect(Hash).to eq(json_response.class)
  227. expect(json_response['email']).to eq('rest-customer1@example.com')
  228. # create user with admin role
  229. role = Role.lookup(name: 'Admin')
  230. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_admin@example.com', role_ids: [ role.id ] }
  231. post '/api/v1/users', params: params, as: :json
  232. expect(response).to have_http_status(:created)
  233. expect(json_response).to be_truthy
  234. user = User.find(json_response['id'])
  235. expect(user).to be_role('Admin')
  236. expect(user).not_to be_role('Agent')
  237. expect(user).not_to be_role('Customer')
  238. expect(json_response['login']).to eq('new_admin_by_admin@example.com')
  239. expect(json_response['email']).to eq('new_admin_by_admin@example.com')
  240. # create user with agent role
  241. role = Role.lookup(name: 'Agent')
  242. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_admin1@example.com', role_ids: [ role.id ] }
  243. post '/api/v1/users', params: params, as: :json
  244. expect(response).to have_http_status(:created)
  245. expect(json_response).to be_truthy
  246. user = User.find(json_response['id'])
  247. expect(user).not_to be_role('Admin')
  248. expect(user).to be_role('Agent')
  249. expect(user).not_to be_role('Customer')
  250. expect(json_response['login']).to eq('new_agent_by_admin1@example.com')
  251. expect(json_response['email']).to eq('new_agent_by_admin1@example.com')
  252. role = Role.lookup(name: 'Agent')
  253. params = { firstname: 'Agent First', email: 'new_agent_by_admin2@example.com', role_ids: [ role.id ] }
  254. post '/api/v1/users', params: params, as: :json
  255. expect(response).to have_http_status(:created)
  256. expect(json_response).to be_truthy
  257. user = User.find(json_response['id'])
  258. expect(user).not_to be_role('Admin')
  259. expect(user).to be_role('Agent')
  260. expect(user).not_to be_role('Customer')
  261. expect(json_response['login']).to eq('new_agent_by_admin2@example.com')
  262. expect(json_response['email']).to eq('new_agent_by_admin2@example.com')
  263. expect(json_response['firstname']).to eq('Agent')
  264. expect(json_response['lastname']).to eq('First')
  265. role = Role.lookup(name: 'Agent')
  266. params = { firstname: 'Agent First', email: 'new_agent_by_admin2@example.com', role_ids: [ role.id ] }
  267. post '/api/v1/users', params: params, as: :json
  268. expect(response).to have_http_status(:unprocessable_entity)
  269. expect(json_response).to be_truthy
  270. expect(json_response['error']).to eq("Email address 'new_agent_by_admin2@example.com' is already used for other user.")
  271. # missing required attributes
  272. params = { note: 'some note' }
  273. post '/api/v1/users', params: params, as: :json
  274. expect(response).to have_http_status(:unprocessable_entity)
  275. expect(json_response).to be_truthy
  276. expect(json_response['error']).to eq('At least one identifier (firstname, lastname, phone or email) for user is required.')
  277. # invalid email
  278. params = { firstname: 'newfirstname123', email: 'some_what', note: 'some note' }
  279. post '/api/v1/users', params: params, as: :json
  280. expect(response).to have_http_status(:unprocessable_entity)
  281. expect(json_response).to be_truthy
  282. expect(json_response['error']).to eq("Invalid email 'some_what'")
  283. # with valid attributes
  284. params = { firstname: 'newfirstname123', note: 'some note' }
  285. post '/api/v1/users', params: params, as: :json
  286. expect(response).to have_http_status(:created)
  287. expect(json_response).to be_truthy
  288. user = User.find(json_response['id'])
  289. expect(user).not_to be_role('Admin')
  290. expect(user).not_to be_role('Agent')
  291. expect(user).to be_role('Customer')
  292. expect(json_response['login']).to be_start_with('auto-')
  293. expect(json_response['email']).to eq('')
  294. expect(json_response['firstname']).to eq('newfirstname123')
  295. expect(json_response['lastname']).to eq('')
  296. end
  297. it 'does user index and create with agent' do
  298. authenticated_as(agent)
  299. get '/api/v1/users/me', params: {}, as: :json
  300. expect(response).to have_http_status(:ok)
  301. expect(json_response).to be_truthy
  302. expect(json_response['email']).to eq('rest-agent@example.com')
  303. # index
  304. get '/api/v1/users', params: {}, as: :json
  305. expect(response).to have_http_status(:ok)
  306. expect(json_response).to be_truthy
  307. # index
  308. get '/api/v1/users', params: {}, as: :json
  309. expect(response).to have_http_status(:ok)
  310. expect(json_response).to be_truthy
  311. expect(Array).to eq(json_response.class)
  312. expect(json_response.length >= 3).to be_truthy
  313. get '/api/v1/users?limit=40&page=1&per_page=2', params: {}, as: :json
  314. expect(response).to have_http_status(:ok)
  315. expect(json_response).to be_a(Array)
  316. users = User.order(:id).limit(2)
  317. expect(json_response[0]['id']).to eq(users[0].id)
  318. expect(json_response[1]['id']).to eq(users[1].id)
  319. expect(json_response.count).to eq(2)
  320. get '/api/v1/users?limit=40&page=2&per_page=2', params: {}, as: :json
  321. expect(response).to have_http_status(:ok)
  322. expect(json_response).to be_a(Array)
  323. users = User.order(:id).limit(4)
  324. expect(json_response[0]['id']).to eq(users[2].id)
  325. expect(json_response[1]['id']).to eq(users[3].id)
  326. expect(json_response.count).to eq(2)
  327. # create user with admin role
  328. firstname = "First test#{SecureRandom.uuid}"
  329. role = Role.lookup(name: 'Admin')
  330. params = { firstname: "Admin#{firstname}", lastname: 'Admin Last', email: 'new_admin_by_agent@example.com', role_ids: [ role.id ] }
  331. post '/api/v1/users', params: params, as: :json
  332. expect(response).to have_http_status(:created)
  333. json_response1 = JSON.parse(@response.body)
  334. expect(json_response1).to be_truthy
  335. user = User.find(json_response1['id'])
  336. expect(user).not_to be_role('Admin')
  337. expect(user).not_to be_role('Agent')
  338. expect(user).to be_role('Customer')
  339. expect(json_response1['login']).to eq('new_admin_by_agent@example.com')
  340. expect(json_response1['email']).to eq('new_admin_by_agent@example.com')
  341. # create user with agent role
  342. role = Role.lookup(name: 'Agent')
  343. params = { firstname: "Agent#{firstname}", lastname: 'Agent Last', email: 'new_agent_by_agent@example.com', role_ids: [ role.id ] }
  344. post '/api/v1/users', params: params, as: :json
  345. expect(response).to have_http_status(:created)
  346. json_response1 = JSON.parse(@response.body)
  347. expect(json_response1).to be_truthy
  348. user = User.find(json_response1['id'])
  349. expect(user).not_to be_role('Admin')
  350. expect(user).not_to be_role('Agent')
  351. expect(user).to be_role('Customer')
  352. expect(json_response1['login']).to eq('new_agent_by_agent@example.com')
  353. expect(json_response1['email']).to eq('new_agent_by_agent@example.com')
  354. # create user with customer role
  355. role = Role.lookup(name: 'Customer')
  356. params = { firstname: "Customer#{firstname}", lastname: 'Customer Last', email: 'new_customer_by_agent@example.com', role_ids: [ role.id ] }
  357. post '/api/v1/users', params: params, as: :json
  358. expect(response).to have_http_status(:created)
  359. json_response1 = JSON.parse(@response.body)
  360. expect(json_response1).to be_truthy
  361. user = User.find(json_response1['id'])
  362. expect(user).not_to be_role('Admin')
  363. expect(user).not_to be_role('Agent')
  364. expect(user).to be_role('Customer')
  365. expect(json_response1['login']).to eq('new_customer_by_agent@example.com')
  366. expect(json_response1['email']).to eq('new_customer_by_agent@example.com')
  367. # search as agent
  368. perform_enqueued_jobs
  369. sleep 2 # let es time to come ready
  370. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}", params: {}, as: :json
  371. expect(response).to have_http_status(:ok)
  372. expect(json_response).to be_a(Array)
  373. expect(json_response[0]['id']).to eq(json_response1['id'])
  374. expect(json_response[0]['firstname']).to eq("Customer#{firstname}")
  375. expect(json_response[0]['lastname']).to eq('Customer Last')
  376. expect(json_response[0]['role_ids']).to be_truthy
  377. expect(json_response[0]['roles']).to be_falsey
  378. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&expand=true", params: {}, as: :json
  379. expect(response).to have_http_status(:ok)
  380. expect(json_response).to be_a(Array)
  381. expect(json_response[0]['id']).to eq(json_response1['id'])
  382. expect(json_response[0]['firstname']).to eq("Customer#{firstname}")
  383. expect(json_response[0]['lastname']).to eq('Customer Last')
  384. expect(json_response[0]['role_ids']).to be_truthy
  385. expect(json_response[0]['roles']).to be_truthy
  386. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&label=true", params: {}, as: :json
  387. expect(response).to have_http_status(:ok)
  388. expect(json_response).to be_a(Array)
  389. expect(json_response[0]['id']).to eq(json_response1['id'])
  390. expect(json_response[0]['label']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  391. expect(json_response[0]['value']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  392. expect(json_response[0]['role_ids']).to be_falsey
  393. expect(json_response[0]['roles']).to be_falsey
  394. get "/api/v1/users/search?term=#{CGI.escape("Customer#{firstname}")}", params: {}, as: :json
  395. expect(response).to have_http_status(:ok)
  396. expect(json_response).to be_a(Array)
  397. expect(json_response[0]['id']).to eq(json_response1['id'])
  398. expect(json_response[0]['label']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  399. expect(json_response[0]['value']).to eq('new_customer_by_agent@example.com')
  400. expect(json_response[0]['inactive']).to be(false)
  401. expect(json_response[0]['role_ids']).to be_falsey
  402. expect(json_response[0]['roles']).to be_falsey
  403. get "/api/v1/users/search?term=#{CGI.escape('CustomerInactive')}", params: {}, as: :json
  404. expect(response).to have_http_status(:ok)
  405. expect(json_response).to be_a(Array)
  406. expect(json_response[0]['inactive']).to be(true)
  407. # Regression test for issue #2539 - search pagination broken in users_controller.rb
  408. # Get the total number of users N, then search with one result per page, so there should N pages with one result each
  409. get '/api/v1/users/search', params: { query: '*' }, as: :json
  410. total_number = json_response.count
  411. (1..total_number).each do |i|
  412. get '/api/v1/users/search', params: { query: '*', per_page: 1, page: i }, as: :json
  413. expect(response).to have_http_status(:ok)
  414. expect(json_response).to be_a(Array)
  415. expect(json_response.count).to eq(1), "Page #{i}/#{total_number} of the user search pagination test have the wrong result!"
  416. end
  417. role = Role.find_by(name: 'Agent')
  418. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, as: :json
  419. expect(response).to have_http_status(:ok)
  420. expect(json_response).to be_a(Array)
  421. expect(json_response.count).to eq(0)
  422. role = Role.find_by(name: 'Customer')
  423. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, as: :json
  424. expect(response).to have_http_status(:ok)
  425. expect(json_response).to be_a(Array)
  426. expect(json_response[0]['id']).to eq(json_response1['id'])
  427. expect(json_response[0]['label']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  428. expect(json_response[0]['value']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  429. expect(json_response[0]['role_ids']).to be_falsey
  430. expect(json_response[0]['roles']).to be_falsey
  431. permission = Permission.find_by(name: 'ticket.agent')
  432. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, as: :json
  433. expect(response).to have_http_status(:ok)
  434. expect(json_response).to be_a(Array)
  435. expect(json_response.count).to eq(0)
  436. permission = Permission.find_by(name: 'ticket.customer')
  437. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, as: :json
  438. expect(response).to have_http_status(:ok)
  439. expect(json_response).to be_a(Array)
  440. expect(json_response[0]['id']).to eq(json_response1['id'])
  441. expect(json_response[0]['label']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  442. expect(json_response[0]['value']).to eq("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>")
  443. expect(json_response[0]['role_ids']).to be_falsey
  444. expect(json_response[0]['roles']).to be_falsey
  445. end
  446. it 'does user index and create with customer1' do
  447. authenticated_as(customer)
  448. get '/api/v1/users/me', params: {}, as: :json
  449. expect(response).to have_http_status(:ok)
  450. expect(json_response).to be_truthy
  451. expect(json_response['email']).to eq('rest-customer1@example.com')
  452. # index
  453. get '/api/v1/users', params: {}, as: :json
  454. expect(response).to have_http_status(:ok)
  455. expect(Array).to eq(json_response.class)
  456. expect(json_response.length).to eq(1)
  457. # show/:id
  458. get "/api/v1/users/#{customer.id}", params: {}, as: :json
  459. expect(response).to have_http_status(:ok)
  460. expect(Hash).to eq(json_response.class)
  461. expect(json_response['email']).to eq('rest-customer1@example.com')
  462. get "/api/v1/users/#{customer2.id}", params: {}, as: :json
  463. expect(response).to have_http_status(:forbidden)
  464. expect(Hash).to eq(json_response.class)
  465. expect(json_response['error']).to be_truthy
  466. # create user with admin role
  467. role = Role.lookup(name: 'Admin')
  468. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_customer1@example.com', role_ids: [ role.id ] }
  469. post '/api/v1/users', params: params, as: :json
  470. expect(response).to have_http_status(:forbidden)
  471. # create user with agent role
  472. role = Role.lookup(name: 'Agent')
  473. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_customer1@example.com', role_ids: [ role.id ] }
  474. post '/api/v1/users', params: params, as: :json
  475. expect(response).to have_http_status(:forbidden)
  476. # search
  477. perform_enqueued_jobs
  478. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, as: :json
  479. expect(response).to have_http_status(:forbidden)
  480. end
  481. it 'does user index with customer2' do
  482. authenticated_as(customer2)
  483. get '/api/v1/users/me', params: {}, as: :json
  484. expect(response).to have_http_status(:ok)
  485. expect(json_response).to be_truthy
  486. expect(json_response['email']).to eq('rest-customer2@example.com')
  487. # index
  488. get '/api/v1/users', params: {}, as: :json
  489. expect(response).to have_http_status(:ok)
  490. expect(Array).to eq(json_response.class)
  491. expect(json_response.length).to eq(1)
  492. # show/:id
  493. get "/api/v1/users/#{customer2.id}", params: {}, as: :json
  494. expect(response).to have_http_status(:ok)
  495. expect(Hash).to eq(json_response.class)
  496. expect(json_response['email']).to eq('rest-customer2@example.com')
  497. get "/api/v1/users/#{customer.id}", params: {}, as: :json
  498. expect(response).to have_http_status(:forbidden)
  499. expect(Hash).to eq(json_response.class)
  500. expect(json_response['error']).to be_truthy
  501. # search
  502. perform_enqueued_jobs
  503. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, as: :json
  504. expect(response).to have_http_status(:forbidden)
  505. end
  506. it 'does users show and response format (04.01)' do
  507. user = create(
  508. :customer,
  509. login: 'rest-customer3@example.com',
  510. firstname: 'Rest',
  511. lastname: 'Customer3',
  512. email: 'rest-customer3@example.com',
  513. password: 'customer3pw',
  514. active: true,
  515. organization: organization,
  516. updated_by_id: admin.id,
  517. created_by_id: admin.id,
  518. )
  519. authenticated_as(admin)
  520. get "/api/v1/users/#{user.id}", params: {}, as: :json
  521. expect(response).to have_http_status(:ok)
  522. expect(json_response).to be_a(Hash)
  523. expect(json_response['id']).to eq(user.id)
  524. expect(json_response['firstname']).to eq(user.firstname)
  525. expect(json_response['organization']).to be_falsey
  526. expect(json_response['organization_id']).to eq(user.organization_id)
  527. expect(json_response['password']).to be_falsey
  528. expect(json_response['role_ids']).to eq(user.role_ids)
  529. expect(json_response['updated_by_id']).to eq(admin.id)
  530. expect(json_response['created_by_id']).to eq(admin.id)
  531. get "/api/v1/users/#{user.id}?expand=true", params: {}, as: :json
  532. expect(response).to have_http_status(:ok)
  533. expect(json_response).to be_a(Hash)
  534. expect(json_response['id']).to eq(user.id)
  535. expect(json_response['firstname']).to eq(user.firstname)
  536. expect(json_response['organization_id']).to eq(user.organization_id)
  537. expect(json_response['organization']).to eq(user.organization.name)
  538. expect(json_response['role_ids']).to eq(user.role_ids)
  539. expect(json_response['password']).to be_falsey
  540. expect(json_response['updated_by_id']).to eq(admin.id)
  541. expect(json_response['created_by_id']).to eq(admin.id)
  542. get "/api/v1/users/#{user.id}?expand=false", params: {}, as: :json
  543. expect(response).to have_http_status(:ok)
  544. expect(json_response).to be_a(Hash)
  545. expect(json_response['id']).to eq(user.id)
  546. expect(json_response['firstname']).to eq(user.firstname)
  547. expect(json_response['organization']).to be_falsey
  548. expect(json_response['organization_id']).to eq(user.organization_id)
  549. expect(json_response['password']).to be_falsey
  550. expect(json_response['role_ids']).to eq(user.role_ids)
  551. expect(json_response['updated_by_id']).to eq(admin.id)
  552. expect(json_response['created_by_id']).to eq(admin.id)
  553. get "/api/v1/users/#{user.id}?full=true", params: {}, as: :json
  554. expect(response).to have_http_status(:ok)
  555. expect(json_response).to be_a(Hash)
  556. expect(json_response['id']).to eq(user.id)
  557. expect(json_response['assets']).to be_truthy
  558. expect(json_response['assets']['User']).to be_truthy
  559. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  560. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  561. expect(json_response['assets']['User'][user.id.to_s]['firstname']).to eq(user.firstname)
  562. expect(json_response['assets']['User'][user.id.to_s]['organization_id']).to eq(user.organization_id)
  563. expect(json_response['assets']['User'][user.id.to_s]['role_ids']).to eq(user.role_ids)
  564. get "/api/v1/users/#{user.id}?full=false", params: {}, as: :json
  565. expect(response).to have_http_status(:ok)
  566. expect(json_response).to be_a(Hash)
  567. expect(json_response['id']).to eq(user.id)
  568. expect(json_response['firstname']).to eq(user.firstname)
  569. expect(json_response['organization']).to be_falsey
  570. expect(json_response['organization_id']).to eq(user.organization_id)
  571. expect(json_response['password']).to be_falsey
  572. expect(json_response['role_ids']).to eq(user.role_ids)
  573. expect(json_response['updated_by_id']).to eq(admin.id)
  574. expect(json_response['created_by_id']).to eq(admin.id)
  575. end
  576. it 'does user index and response format (04.02)' do
  577. user = create(
  578. :customer,
  579. login: 'rest-customer3@example.com',
  580. firstname: 'Rest',
  581. lastname: 'Customer3',
  582. email: 'rest-customer3@example.com',
  583. password: 'customer3pw',
  584. active: true,
  585. organization: organization,
  586. updated_by_id: admin.id,
  587. created_by_id: admin.id,
  588. )
  589. authenticated_as(admin)
  590. get '/api/v1/users', params: {}, as: :json
  591. expect(response).to have_http_status(:ok)
  592. expect(json_response).to be_a(Array)
  593. expect(json_response[0].class).to eq(Hash)
  594. expect(json_response.last['id']).to eq(user.id)
  595. expect(json_response.last['lastname']).to eq(user.lastname)
  596. expect(json_response.last['organization']).to be_falsey
  597. expect(json_response.last['role_ids']).to eq(user.role_ids)
  598. expect(json_response.last['organization_id']).to eq(user.organization_id)
  599. expect(json_response.last['password']).to be_falsey
  600. expect(json_response.last['updated_by_id']).to eq(admin.id)
  601. expect(json_response.last['created_by_id']).to eq(admin.id)
  602. get '/api/v1/users?expand=true', params: {}, as: :json
  603. expect(response).to have_http_status(:ok)
  604. expect(json_response).to be_a(Array)
  605. expect(json_response[0].class).to eq(Hash)
  606. expect(json_response.last['id']).to eq(user.id)
  607. expect(json_response.last['lastname']).to eq(user.lastname)
  608. expect(json_response.last['organization_id']).to eq(user.organization_id)
  609. expect(json_response.last['organization']).to eq(user.organization.name)
  610. expect(json_response.last['password']).to be_falsey
  611. expect(json_response.last['updated_by_id']).to eq(admin.id)
  612. expect(json_response.last['created_by_id']).to eq(admin.id)
  613. get '/api/v1/users?expand=false', params: {}, as: :json
  614. expect(response).to have_http_status(:ok)
  615. expect(json_response).to be_a(Array)
  616. expect(json_response[0].class).to eq(Hash)
  617. expect(json_response.last['id']).to eq(user.id)
  618. expect(json_response.last['lastname']).to eq(user.lastname)
  619. expect(json_response.last['organization']).to be_falsey
  620. expect(json_response.last['role_ids']).to eq(user.role_ids)
  621. expect(json_response.last['organization_id']).to eq(user.organization_id)
  622. expect(json_response.last['password']).to be_falsey
  623. expect(json_response.last['updated_by_id']).to eq(admin.id)
  624. expect(json_response.last['created_by_id']).to eq(admin.id)
  625. get '/api/v1/users?full=true', params: {}, as: :json
  626. expect(response).to have_http_status(:ok)
  627. expect(json_response).to be_a(Hash)
  628. expect(json_response['record_ids'].class).to eq(Array)
  629. expect(json_response['record_ids'][0]).to eq(1)
  630. expect(json_response['record_ids'].last).to eq(user.id)
  631. expect(json_response['assets']).to be_truthy
  632. expect(json_response['assets']['User']).to be_truthy
  633. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  634. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  635. expect(json_response['assets']['User'][user.id.to_s]['lastname']).to eq(user.lastname)
  636. expect(json_response['assets']['User'][user.id.to_s]['organization_id']).to eq(user.organization_id)
  637. expect(json_response['assets']['User'][user.id.to_s]['password']).to be_falsey
  638. get '/api/v1/users?full=false', params: {}, as: :json
  639. expect(response).to have_http_status(:ok)
  640. expect(json_response).to be_a(Array)
  641. expect(json_response[0].class).to eq(Hash)
  642. expect(json_response.last['id']).to eq(user.id)
  643. expect(json_response.last['lastname']).to eq(user.lastname)
  644. expect(json_response.last['organization']).to be_falsey
  645. expect(json_response.last['role_ids']).to eq(user.role_ids)
  646. expect(json_response.last['organization_id']).to eq(user.organization_id)
  647. expect(json_response.last['password']).to be_falsey
  648. expect(json_response.last['updated_by_id']).to eq(admin.id)
  649. expect(json_response.last['created_by_id']).to eq(admin.id)
  650. end
  651. it 'does ticket create and response format (04.03)' do
  652. organization = Organization.first
  653. params = {
  654. firstname: 'newfirstname123',
  655. note: 'some note',
  656. organization: organization.name,
  657. }
  658. authenticated_as(admin)
  659. post '/api/v1/users', params: params, as: :json
  660. expect(response).to have_http_status(:created)
  661. expect(json_response).to be_a(Hash)
  662. user = User.find(json_response['id'])
  663. expect(json_response['firstname']).to eq(user.firstname)
  664. expect(json_response['organization_id']).to eq(user.organization_id)
  665. expect(json_response['organization']).to be_falsey
  666. expect(json_response['password']).to be_falsey
  667. expect(json_response['updated_by_id']).to eq(admin.id)
  668. expect(json_response['created_by_id']).to eq(admin.id)
  669. post '/api/v1/users?expand=true', params: params, as: :json
  670. expect(response).to have_http_status(:created)
  671. expect(json_response).to be_a(Hash)
  672. user = User.find(json_response['id'])
  673. expect(json_response['firstname']).to eq(user.firstname)
  674. expect(json_response['organization_id']).to eq(user.organization_id)
  675. expect(json_response['organization']).to eq(user.organization.name)
  676. expect(json_response['password']).to be_falsey
  677. expect(json_response['updated_by_id']).to eq(admin.id)
  678. expect(json_response['created_by_id']).to eq(admin.id)
  679. post '/api/v1/users?full=true', params: params, as: :json
  680. expect(response).to have_http_status(:created)
  681. expect(json_response).to be_a(Hash)
  682. user = User.find(json_response['id'])
  683. expect(json_response['assets']).to be_truthy
  684. expect(json_response['assets']['User']).to be_truthy
  685. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  686. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  687. expect(json_response['assets']['User'][user.id.to_s]['firstname']).to eq(user.firstname)
  688. expect(json_response['assets']['User'][user.id.to_s]['lastname']).to eq(user.lastname)
  689. expect(json_response['assets']['User'][user.id.to_s]['password']).to be_falsey
  690. end
  691. it 'does ticket update and response formats (04.04)' do
  692. user = create(
  693. :customer,
  694. login: 'rest-customer3@example.com',
  695. firstname: 'Rest',
  696. lastname: 'Customer3',
  697. email: 'rest-customer3@example.com',
  698. password: 'customer3pw',
  699. active: true,
  700. organization: organization,
  701. updated_by_id: admin.id,
  702. created_by_id: admin.id,
  703. )
  704. authenticated_as(admin)
  705. params = {
  706. firstname: 'a update firstname #1',
  707. }
  708. put "/api/v1/users/#{user.id}", params: params, as: :json
  709. expect(response).to have_http_status(:ok)
  710. expect(json_response).to be_a(Hash)
  711. user = User.find(json_response['id'])
  712. expect(json_response['lastname']).to eq(user.lastname)
  713. expect(json_response['firstname']).to eq(params[:firstname])
  714. expect(json_response['organization_id']).to eq(user.organization_id)
  715. expect(json_response['organization']).to be_falsey
  716. expect(json_response['password']).to be_falsey
  717. expect(json_response['updated_by_id']).to eq(admin.id)
  718. expect(json_response['created_by_id']).to eq(admin.id)
  719. params = {
  720. firstname: 'a update firstname #2',
  721. }
  722. put "/api/v1/users/#{user.id}?expand=true", params: params, as: :json
  723. expect(response).to have_http_status(:ok)
  724. expect(json_response).to be_a(Hash)
  725. user = User.find(json_response['id'])
  726. expect(json_response['lastname']).to eq(user.lastname)
  727. expect(json_response['firstname']).to eq(params[:firstname])
  728. expect(json_response['organization_id']).to eq(user.organization_id)
  729. expect(json_response['organization']).to eq(user.organization.name)
  730. expect(json_response['password']).to be_falsey
  731. expect(json_response['updated_by_id']).to eq(admin.id)
  732. expect(json_response['created_by_id']).to eq(admin.id)
  733. params = {
  734. firstname: 'a update firstname #3',
  735. }
  736. put "/api/v1/users/#{user.id}?full=true", params: params, as: :json
  737. expect(response).to have_http_status(:ok)
  738. expect(json_response).to be_a(Hash)
  739. user = User.find(json_response['id'])
  740. expect(json_response['assets']).to be_truthy
  741. expect(json_response['assets']['User']).to be_truthy
  742. expect(json_response['assets']['User'][user.id.to_s]).to be_truthy
  743. expect(json_response['assets']['User'][user.id.to_s]['id']).to eq(user.id)
  744. expect(json_response['assets']['User'][user.id.to_s]['firstname']).to eq(params[:firstname])
  745. expect(json_response['assets']['User'][user.id.to_s]['lastname']).to eq(user.lastname)
  746. expect(json_response['assets']['User'][user.id.to_s]['password']).to be_falsey
  747. end
  748. it 'does csv example - customer no access (05.01)' do
  749. authenticated_as(customer)
  750. get '/api/v1/users/import_example', params: {}, as: :json
  751. expect(response).to have_http_status(:forbidden)
  752. expect(json_response['error']).to eq('Not authorized (user)!')
  753. end
  754. it 'does csv example - admin access (05.02)' do
  755. authenticated_as(admin)
  756. get '/api/v1/users/import_example', params: {}, as: :json
  757. expect(response).to have_http_status(:ok)
  758. rows = CSV.parse(@response.body)
  759. header = rows.shift
  760. expect(header[0]).to eq('id')
  761. expect(header[1]).to eq('login')
  762. expect(header[2]).to eq('firstname')
  763. expect(header[3]).to eq('lastname')
  764. expect(header[4]).to eq('email')
  765. expect(header).to include('organization')
  766. end
  767. it 'does csv import - admin access (05.03)' do
  768. # invalid file
  769. csv_file = fixture_file_upload('csv_import/user/simple_col_not_existing.csv', 'text/csv')
  770. authenticated_as(admin)
  771. post '/api/v1/users/import?try=true', params: { file: csv_file, col_sep: ';' }
  772. expect(response).to have_http_status(:ok)
  773. expect(json_response).to be_a(Hash)
  774. expect(json_response['try']).to be(true)
  775. expect(json_response['records']).to be_empty
  776. expect(json_response['result']).to eq('failed')
  777. expect(json_response['errors'].count).to eq(2)
  778. expect(json_response['errors'][0]).to eq("Line 1: Unable to create record - unknown attribute 'firstname2' for User.")
  779. expect(json_response['errors'][1]).to eq("Line 2: Unable to create record - unknown attribute 'firstname2' for User.")
  780. # valid file try
  781. csv_file = fixture_file_upload('csv_import/user/simple.csv', 'text/csv')
  782. post '/api/v1/users/import?try=true', params: { file: csv_file, col_sep: ';' }
  783. expect(response).to have_http_status(:ok)
  784. expect(json_response).to be_a(Hash)
  785. expect(json_response['try']).to be(true)
  786. expect(json_response['records'].count).to eq(2)
  787. expect(json_response['result']).to eq('success')
  788. expect(User.find_by(login: 'user-simple-import1')).to be_nil
  789. expect(User.find_by(login: 'user-simple-import2')).to be_nil
  790. # valid file
  791. csv_file = fixture_file_upload('csv_import/user/simple.csv', 'text/csv')
  792. post '/api/v1/users/import', params: { file: csv_file, col_sep: ';' }
  793. expect(response).to have_http_status(:ok)
  794. expect(json_response).to be_a(Hash)
  795. expect(json_response['try']).to be(false)
  796. expect(json_response['records'].count).to eq(2)
  797. expect(json_response['result']).to eq('success')
  798. user1 = User.find_by(login: 'user-simple-import1')
  799. expect(user1).to be_truthy
  800. expect(user1.login).to eq('user-simple-import1')
  801. expect(user1.firstname).to eq('firstname-simple-import1')
  802. expect(user1.lastname).to eq('lastname-simple-import1')
  803. expect(user1.email).to eq('user-simple-import1@example.com')
  804. expect(user1.active).to be(true)
  805. user2 = User.find_by(login: 'user-simple-import2')
  806. expect(user2).to be_truthy
  807. expect(user2.login).to eq('user-simple-import2')
  808. expect(user2.firstname).to eq('firstname-simple-import2')
  809. expect(user2.lastname).to eq('lastname-simple-import2')
  810. expect(user2.email).to eq('user-simple-import2@example.com')
  811. expect(user2.active).to be(false)
  812. user1.destroy!
  813. user2.destroy!
  814. end
  815. it 'does user history' do
  816. user1 = create(
  817. :customer,
  818. login: 'history@example.com',
  819. firstname: 'History',
  820. lastname: 'Customer1',
  821. email: 'history@example.com',
  822. )
  823. authenticated_as(agent)
  824. get "/api/v1/users/history/#{user1.id}", params: {}, as: :json
  825. expect(response).to have_http_status(:ok)
  826. expect(json_response).to be_a(Hash)
  827. expect(json_response['history'].class).to eq(Array)
  828. expect(json_response['assets'].class).to eq(Hash)
  829. expect(json_response['assets']['Ticket']).to be_nil
  830. expect(json_response['assets']['User'][user1.id.to_s]).not_to be_nil
  831. end
  832. it 'does user search sortable' do
  833. firstname = "user_search_sortable #{SecureRandom.uuid}"
  834. user1 = create(
  835. :customer,
  836. login: 'rest-user_search_sortableA@example.com',
  837. firstname: "#{firstname} A",
  838. lastname: 'user_search_sortableA',
  839. email: 'rest-user_search_sortableA@example.com',
  840. password: 'user_search_sortableA',
  841. active: true,
  842. organization_id: organization.id,
  843. out_of_office: false,
  844. created_at: '2016-02-05 17:42:00',
  845. )
  846. user2 = create(
  847. :customer,
  848. login: 'rest-user_search_sortableB@example.com',
  849. firstname: "#{firstname} B",
  850. lastname: 'user_search_sortableB',
  851. email: 'rest-user_search_sortableB@example.com',
  852. password: 'user_search_sortableB',
  853. active: true,
  854. organization_id: organization.id,
  855. out_of_office_start_at: '2016-02-06 19:42:00',
  856. out_of_office_end_at: '2016-02-07 19:42:00',
  857. out_of_office_replacement_id: 1,
  858. out_of_office: true,
  859. created_at: '2016-02-05 19:42:00',
  860. )
  861. perform_enqueued_jobs
  862. sleep 2 # let es time to come ready
  863. authenticated_as(admin)
  864. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'created_at', order_by: 'asc' }, as: :json
  865. expect(response).to have_http_status(:ok)
  866. expect(json_response).to be_a(Array)
  867. result = json_response
  868. result.collect! { |v| v['id'] }
  869. expect(result).to eq([user1.id, user2.id])
  870. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'firstname', order_by: 'asc' }, as: :json
  871. expect(response).to have_http_status(:ok)
  872. expect(json_response).to be_a(Array)
  873. result = json_response
  874. result.collect! { |v| v['id'] }
  875. expect(result).to eq([user1.id, user2.id])
  876. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'firstname', order_by: 'desc' }, as: :json
  877. expect(response).to have_http_status(:ok)
  878. expect(json_response).to be_a(Array)
  879. result = json_response
  880. result.collect! { |v| v['id'] }
  881. expect(result).to eq([user2.id, user1.id])
  882. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: %w[firstname created_at], order_by: %w[desc asc] }, as: :json
  883. expect(response).to have_http_status(:ok)
  884. expect(json_response).to be_a(Array)
  885. result = json_response
  886. result.collect! { |v| v['id'] }
  887. expect(result).to eq([user2.id, user1.id])
  888. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: %w[firstname created_at], order_by: %w[desc asc] }, as: :json
  889. expect(response).to have_http_status(:ok)
  890. expect(json_response).to be_a(Array)
  891. result = json_response
  892. result.collect! { |v| v['id'] }
  893. expect(result).to eq([user2.id, user1.id])
  894. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'out_of_office', order_by: 'asc' }, as: :json
  895. expect(response).to have_http_status(:ok)
  896. expect(json_response).to be_a(Array)
  897. result = json_response
  898. result.collect! { |v| v['id'] }
  899. expect(result).to eq([user1.id, user2.id])
  900. get "/api/v1/users/search?query=#{CGI.escape(firstname)}", params: { sort_by: 'out_of_office', order_by: 'desc' }, as: :json
  901. expect(response).to have_http_status(:ok)
  902. expect(json_response).to be_a(Array)
  903. result = json_response
  904. result.collect! { |v| v['id'] }
  905. expect(result).to eq([user2.id, user1.id])
  906. 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
  907. expect(response).to have_http_status(:ok)
  908. expect(json_response).to be_a(Array)
  909. result = json_response
  910. result.collect! { |v| v['id'] }
  911. expect(result).to eq([user1.id, user2.id])
  912. end
  913. context 'does password reset send work' do
  914. let(:user) { create(:customer, login: 'somebody', email: 'somebody@example.com') }
  915. context 'for user without email address' do
  916. let(:user) { create(:customer, login: 'somebody', email: '') }
  917. it 'return failed' do
  918. post '/api/v1/users/password_reset', params: { username: user.login }, as: :json
  919. expect(response).to have_http_status(:ok)
  920. expect(json_response).to be_a(Hash)
  921. expect(json_response['message']).to eq('failed')
  922. end
  923. end
  924. context 'for user with email address' do
  925. it 'return ok' do
  926. post '/api/v1/users/password_reset', params: { username: user.login }, as: :json
  927. expect(response).to have_http_status(:ok)
  928. expect(json_response).to be_a(Hash)
  929. expect(json_response['message']).to eq('ok')
  930. end
  931. end
  932. context 'for user with email address but disabled feature' do
  933. before { Setting.set('user_lost_password', false) }
  934. it 'raise 422' do
  935. post '/api/v1/users/password_reset', params: { username: user.login }, as: :json
  936. expect(response).to have_http_status(:unprocessable_entity)
  937. expect(json_response['error']).to be_truthy
  938. expect(json_response['error']).to eq('Feature not enabled!')
  939. end
  940. end
  941. end
  942. context 'does password reset by token work' do
  943. let(:user) { create(:customer, login: 'somebody', email: 'somebody@example.com') }
  944. let(:token) { create(:token, action: 'PasswordReset', user_id: user.id) }
  945. context 'for user without email address' do
  946. let(:user) { create(:customer, login: 'somebody', email: '') }
  947. it 'return failed' do
  948. post '/api/v1/users/password_reset_verify', params: { username: user.login, token: token.name, password: 'Test1234#.' }, as: :json
  949. expect(response).to have_http_status(:ok)
  950. expect(json_response).to be_a(Hash)
  951. expect(json_response['message']).to eq('failed')
  952. end
  953. end
  954. context 'for user with email address' do
  955. it 'return ok' do
  956. post '/api/v1/users/password_reset_verify', params: { username: user.login, token: token.name, password: 'TEst1234#.' }, as: :json
  957. expect(response).to have_http_status(:ok)
  958. expect(json_response).to be_a(Hash)
  959. expect(json_response['message']).to eq('ok')
  960. end
  961. end
  962. context 'for user with email address but disabled feature' do
  963. before { Setting.set('user_lost_password', false) }
  964. it 'raise 422' do
  965. post '/api/v1/users/password_reset_verify', params: { username: user.login, token: token.name, password: 'Test1234#.' }, as: :json
  966. expect(response).to have_http_status(:unprocessable_entity)
  967. expect(json_response['error']).to be_truthy
  968. expect(json_response['error']).to eq('Feature not enabled!')
  969. end
  970. end
  971. end
  972. context 'password change' do
  973. let(:user) { create(:customer, login: 'somebody', email: 'somebody@example.com', password: 'Test1234#.') }
  974. before { authenticated_as(user, login: 'somebody', password: 'Test1234#.') }
  975. context 'user without email address' do
  976. let(:user) { create(:customer, login: 'somebody', email: '', password: 'Test1234#.') }
  977. it 'return ok' do
  978. post '/api/v1/users/password_change', params: { password_old: 'Test1234#.', password_new: 'TEst12345#.' }, as: :json
  979. expect(response).to have_http_status(:ok)
  980. expect(json_response).to be_a(Hash)
  981. expect(json_response['message']).to eq('ok')
  982. end
  983. end
  984. context 'user with email address' do
  985. it 'return ok' do
  986. post '/api/v1/users/password_change', params: { password_old: 'Test1234#.', password_new: 'TEst12345#.' }, as: :json
  987. expect(response).to have_http_status(:ok)
  988. expect(json_response).to be_a(Hash)
  989. expect(json_response['message']).to eq('ok')
  990. end
  991. end
  992. end
  993. context 'ultra long password', authenticated_as: :user do
  994. let(:user) { create(:agent, :with_valid_password) }
  995. let(:long_string) { "asd1ASDasd!#{Faker::Lorem.characters(number: 1_000)}" }
  996. it 'does not reach verifying when old password is too long' do
  997. allow(PasswordHash).to receive(:verified?).and_call_original
  998. post '/api/v1/users/password_change', params: { password_old: long_string, password_new: long_string }, as: :json
  999. expect(PasswordHash).not_to have_received(:verified?).with(any_args, long_string)
  1000. expect(response).to have_http_status(:unprocessable_entity)
  1001. expect(json_response['message']).to eq('failed')
  1002. end
  1003. it 'does not reach hashing when saving' do
  1004. allow(PasswordHash).to receive(:crypt).and_call_original
  1005. post '/api/v1/users/password_change', params: { password_old: user.password_plain, password_new: long_string }, as: :json
  1006. expect(PasswordHash).not_to have_received(:crypt)
  1007. expect(response).to have_http_status(:unprocessable_entity)
  1008. expect(json_response['message']).to eq('failed')
  1009. end
  1010. end
  1011. end
  1012. describe 'POST /api/v1/users', authenticated_as: -> { create(:admin) } do
  1013. def make_request(params)
  1014. post '/api/v1/users', params: params, as: :json
  1015. end
  1016. let(:successful_params) { { email: attributes_for(:admin)[:email] } }
  1017. let(:params_with_role) { successful_params.merge({ role_ids: [Role.find_by(name: 'Admin').id] }) }
  1018. let(:params_with_invite) { successful_params.merge({ invite: true }) }
  1019. it 'succeeds' do
  1020. make_request successful_params
  1021. expect(response).to have_http_status(:created)
  1022. end
  1023. it 'returns user data' do
  1024. make_request successful_params
  1025. expect(json_response).to have_key('email').and(have_value(successful_params[:email]))
  1026. end
  1027. it 'no session treated as signup', authenticated_as: false do
  1028. make_request successful_params
  1029. expect(response).to have_http_status(:unprocessable_entity)
  1030. end
  1031. it 'does not accept requests from customers', authenticated_as: -> { create(:customer) } do
  1032. make_request successful_params
  1033. expect(response).to have_http_status(:forbidden)
  1034. end
  1035. it 'admins can give any role', authenticated_as: -> { create(:admin) } do
  1036. make_request params_with_role
  1037. expect(User.last).to be_role 'Admin'
  1038. end
  1039. it 'agents can not give roles', authenticated_as: -> { create(:agent) } do
  1040. make_request params_with_role
  1041. expect(User.last).not_to be_role 'Admin'
  1042. end
  1043. it 'does not send email verification notifications' do
  1044. allow(NotificationFactory::Mailer).to receive(:notification)
  1045. make_request successful_params
  1046. expect(NotificationFactory::Mailer).not_to have_received(:notification) { |arguments| arguments[:template] == 'signup' }
  1047. end
  1048. it 'does not send invitation notification by default' do
  1049. allow(NotificationFactory::Mailer).to receive(:notification)
  1050. make_request successful_params
  1051. expect(NotificationFactory::Mailer).not_to have_received(:notification) { |arguments| arguments[:template] == 'user_invite' }
  1052. end
  1053. it 'sends invitation notification when required' do
  1054. allow(NotificationFactory::Mailer).to receive(:notification)
  1055. make_request params_with_invite
  1056. expect(NotificationFactory::Mailer).to have_received(:notification) { |arguments| arguments[:template] == 'user_invite' }
  1057. end
  1058. it 'requires at least one identifier' do
  1059. make_request({ web: 'example.com' })
  1060. expect(json_response['error']).to start_with('At least one identifier')
  1061. end
  1062. it 'takes first name as identifier' do
  1063. make_request({ firstname: 'name' })
  1064. expect(response).to have_http_status(:created)
  1065. end
  1066. it 'takes last name as identifier' do
  1067. make_request({ lastname: 'name' })
  1068. expect(response).to have_http_status(:created)
  1069. end
  1070. it 'takes login as identifier' do
  1071. make_request({ login: 'name' })
  1072. expect(response).to have_http_status(:created)
  1073. end
  1074. it 'requires valid email if present' do
  1075. make_request({ email: 'not_valid_email' })
  1076. expect(response).to have_http_status(:unprocessable_entity)
  1077. end
  1078. end
  1079. describe 'POST /api/v1/users processed by #create_admin', authenticated_as: false do
  1080. before do
  1081. User.all[2...].each(&:destroy) # destroy previously created users
  1082. end
  1083. def make_request(params)
  1084. post '/api/v1/users', params: params, as: :json
  1085. end
  1086. let(:successful_params) do
  1087. email = attributes_for(:admin)[:email]
  1088. { firstname: 'Admin First', lastname: 'Admin Last', email: email, password: 'asd1ASDasd!' }
  1089. end
  1090. it 'succeds' do
  1091. make_request successful_params
  1092. expect(response).to have_http_status(:created)
  1093. end
  1094. it 'returns success message' do
  1095. make_request successful_params
  1096. expect(json_response).to have_key('message').and(have_value('ok'))
  1097. end
  1098. it 'does not allow to create 2nd administrator account' do
  1099. create(:admin)
  1100. make_request successful_params
  1101. expect(response).to have_http_status(:unprocessable_entity)
  1102. end
  1103. it 'requires email' do
  1104. make_request successful_params.merge(email: nil)
  1105. expect(response).to have_http_status(:unprocessable_entity)
  1106. end
  1107. it 'requires valid email' do
  1108. make_request successful_params.merge(email: 'invalid_email')
  1109. expect(response).to have_http_status(:unprocessable_entity)
  1110. end
  1111. it 'loads calendar' do
  1112. allow(Calendar).to receive(:init_setup)
  1113. make_request successful_params
  1114. expect(Calendar).to have_received(:init_setup)
  1115. end
  1116. it 'loads text module' do
  1117. allow(TextModule).to receive(:load)
  1118. make_request successful_params
  1119. expect(TextModule).to have_received(:load)
  1120. end
  1121. it 'does not send any notifications' do
  1122. allow(NotificationFactory::Mailer).to receive(:notification)
  1123. make_request successful_params
  1124. expect(NotificationFactory::Mailer).not_to have_received(:notification)
  1125. end
  1126. end
  1127. describe 'POST /api/v1/users processed by #create_signup', authenticated_as: false do
  1128. def make_request(params)
  1129. post '/api/v1/users', params: params, as: :json
  1130. end
  1131. let(:successful_params) do
  1132. email = attributes_for(:admin)[:email]
  1133. { firstname: 'Customer First', lastname: 'Customer Last', email: email, password: 'gsd1ASDasd!', signup: true }
  1134. end
  1135. before do
  1136. create(:admin) # simulate functional system with admin created
  1137. end
  1138. it 'succeeds' do
  1139. make_request successful_params
  1140. expect(response).to have_http_status(:created)
  1141. end
  1142. it 'requires csrf', allow_forgery_protection: true do
  1143. make_request successful_params
  1144. expect(response).to have_http_status(:unauthorized)
  1145. end
  1146. it 'requires honeypot attribute' do
  1147. params = successful_params.clone
  1148. params.delete :signup
  1149. make_request params
  1150. expect(response).to have_http_status(:unprocessable_entity)
  1151. end
  1152. it 'requires signup to be enabled' do
  1153. Setting.set('user_create_account', false)
  1154. make_request successful_params
  1155. expect(response).to have_http_status(:unprocessable_entity)
  1156. end
  1157. it 'requires email' do
  1158. make_request successful_params.merge(email: nil)
  1159. expect(response).to have_http_status(:unprocessable_entity)
  1160. end
  1161. it 'requires valid email' do
  1162. make_request successful_params.merge(email: 'not_valid_email')
  1163. expect(response).to have_http_status(:unprocessable_entity)
  1164. end
  1165. it 'returns false positive when email already used' do
  1166. create(:customer, email: successful_params[:email])
  1167. make_request successful_params
  1168. expect(response).to have_http_status(:created)
  1169. end
  1170. it 'sends email verification notifications' do
  1171. allow(NotificationFactory::Mailer).to receive(:notification)
  1172. make_request successful_params
  1173. expect(NotificationFactory::Mailer).to have_received(:notification) { |arguments| arguments[:template] == 'signup' }
  1174. end
  1175. it 'sends password reset notification when email already used' do
  1176. create(:customer, email: successful_params[:email])
  1177. allow(NotificationFactory::Mailer).to receive(:notification)
  1178. make_request successful_params
  1179. expect(NotificationFactory::Mailer).to have_received(:notification) { |arguments| arguments[:template] == 'signup_taken_reset' }
  1180. end
  1181. it 'sets role to Customer' do
  1182. make_request successful_params
  1183. expect(User.last).to be_role('Customer')
  1184. end
  1185. it 'ignores given Agent role' do
  1186. make_request successful_params.merge(role_ids: [Role.find_by(name: 'Agent').id])
  1187. expect(User.last).not_to be_role('Agent')
  1188. end
  1189. end
  1190. describe 'GET /api/v1/users/search group ids' do
  1191. let(:group1) { create(:group) }
  1192. let(:group2) { create(:group) }
  1193. let!(:agent1) { create(:agent, firstname: '9U7Z-agent1', groups: [group1]) }
  1194. let!(:agent2) { create(:agent, firstname: '9U7Z-agent2', groups: [group2]) }
  1195. def make_request(params)
  1196. authenticated_as(agent1)
  1197. get '/api/v1/users/search', params: params, as: :json
  1198. end
  1199. describe 'without searchindex' do
  1200. before do
  1201. Setting.set('es_url', nil)
  1202. end
  1203. it 'does find both users' do
  1204. make_request(query: '9U7Z')
  1205. expect(json_response.count).to eq(2)
  1206. end
  1207. it 'does find only agent 1' do
  1208. make_request(query: '9U7Z', group_ids: { group1.id => 'read' })
  1209. expect(json_response[0]['firstname']).to eq(agent1.firstname)
  1210. expect(json_response.count).to eq(1)
  1211. end
  1212. it 'does find only agent 2' do
  1213. make_request(query: '9U7Z', group_ids: { group2.id => 'read' })
  1214. expect(json_response[0]['firstname']).to eq(agent2.firstname)
  1215. expect(json_response.count).to eq(1)
  1216. end
  1217. it 'does find none' do
  1218. make_request(query: '9U7Z', group_ids: { 999 => 'read' })
  1219. expect(json_response.count).to eq(0)
  1220. end
  1221. it 'does not list user with id 1' do
  1222. make_request(query: '')
  1223. not_in_response = json_response.none? { |item| item['id'] == 1 }
  1224. expect(not_in_response).to be(true)
  1225. end
  1226. end
  1227. describe 'with searchindex', searchindex: true do
  1228. before do
  1229. searchindex_model_reload([::User])
  1230. end
  1231. it 'does find both users' do
  1232. make_request(query: '9U7Z')
  1233. expect(json_response.count).to eq(2)
  1234. end
  1235. it 'does find only agent 1' do
  1236. make_request(query: '9U7Z', group_ids: { group1.id => 'read' })
  1237. expect(json_response[0]['firstname']).to eq(agent1.firstname)
  1238. expect(json_response.count).to eq(1)
  1239. end
  1240. it 'does find only agent 2' do
  1241. make_request(query: '9U7Z', group_ids: { group2.id => 'read' })
  1242. expect(json_response[0]['firstname']).to eq(agent2.firstname)
  1243. expect(json_response.count).to eq(1)
  1244. end
  1245. it 'does find none' do
  1246. make_request(query: '9U7Z', group_ids: { 999 => 'read' })
  1247. expect(json_response.count).to eq(0)
  1248. end
  1249. it 'does not list user with id 1' do
  1250. make_request(query: '')
  1251. not_in_response = json_response.none? { |item| item['id'] == 1 }
  1252. expect(not_in_response).to be(true)
  1253. end
  1254. end
  1255. end
  1256. describe 'GET /api/v1/users/search, checks ES Usage', authenticated_as: :agent, searchindex: true do
  1257. let!(:agent) { create(:agent) }
  1258. def make_request(params)
  1259. get '/api/v1/users/search', params: params, as: :json
  1260. end
  1261. before do
  1262. # create some users that can be found
  1263. create(:agent, firstname: 'Test-Agent1')
  1264. create(:agent, firstname: 'Test-Agent2')
  1265. searchindex_model_reload([::User])
  1266. end
  1267. it 'uses elasticsearch when query is non empty' do
  1268. # Check if ES is used
  1269. allow(SearchIndexBackend).to receive(:search)
  1270. make_request(query: 'Test')
  1271. expect(SearchIndexBackend).to have_received(:search)
  1272. end
  1273. it 'does not uses elasticsearch when query is empty' do
  1274. allow(SearchIndexBackend).to receive(:search)
  1275. make_request(query: '')
  1276. expect(SearchIndexBackend).not_to have_received(:search)
  1277. end
  1278. end
  1279. describe 'POST /api/v1/users/avatar', authenticated_as: :user do
  1280. let(:user) { create(:user) }
  1281. let(:base64) { 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' }
  1282. def make_request(params)
  1283. post '/api/v1/users/avatar', params: params, as: :json
  1284. end
  1285. it 'returns verbose error when full image is missing' do
  1286. make_request(avatar_full: '')
  1287. expect(json_response).to include('error' => 'The image is invalid.')
  1288. end
  1289. it 'returns verbose error when resized image is missing' do
  1290. make_request(avatar_full: base64)
  1291. expect(json_response).to include('error' => 'The image is invalid.')
  1292. end
  1293. it 'successfully changes avatar' do
  1294. expect { make_request(avatar_full: base64, avatar_resize: base64) }
  1295. .to change { Avatar.list('User', user.id) }
  1296. end
  1297. context 'with a not allowed mime-type' do
  1298. let(:base64) { 'data:image/svg+xml;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' }
  1299. it 'returns verbose error for a not allowed mime-type' do
  1300. make_request(avatar_full: base64)
  1301. expect(json_response).to include('error' => 'The MIME type of the image is invalid.')
  1302. end
  1303. end
  1304. context 'with a not allowed resized image mime-type' do
  1305. let(:resized_base64) { 'data:image/svg+xml;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' }
  1306. it 'returns verbose error for a not allowed mime-type' do
  1307. make_request(avatar_full: base64, avatar_resize: resized_base64)
  1308. expect(json_response).to include('error' => 'The MIME type of the image is invalid.')
  1309. end
  1310. end
  1311. end
  1312. describe 'GET /api/v1/users/image/:hash', authenticated_as: :user do
  1313. let(:user) { create(:user) }
  1314. let(:avatar_mime_type) { 'image/png' }
  1315. let(:avatar) do
  1316. file = File.open('test/data/image/1000x1000.png', 'rb')
  1317. contents = file.read
  1318. Avatar.add(
  1319. object: 'User',
  1320. o_id: user.id,
  1321. default: true,
  1322. resize: {
  1323. content: contents,
  1324. mime_type: avatar_mime_type,
  1325. },
  1326. source: 'web',
  1327. deletable: true,
  1328. updated_by_id: 1,
  1329. created_by_id: 1,
  1330. )
  1331. end
  1332. let(:avatar_content) { Avatar.get_by_hash(avatar.store_hash).content }
  1333. before do
  1334. user.update!(image: avatar.store_hash)
  1335. end
  1336. def make_request(image_hash, params: {})
  1337. get "/api/v1/users/image/#{image_hash}", params: params, as: :json
  1338. end
  1339. it 'returns verbose error when full image is missing' do
  1340. make_request(avatar.store_hash)
  1341. expect(response.body).to eq(avatar_content)
  1342. end
  1343. context 'with a not allowed inline mime-type' do
  1344. let(:avatar_mime_type) { 'image/svg+xml' }
  1345. it 'returns the default image' do
  1346. make_request(avatar.store_hash)
  1347. expect(response.headers['Content-Type']).to include('image/gif')
  1348. end
  1349. end
  1350. end
  1351. describe 'GET /api/v1/users/search, checks usage of the ids parameter', authenticated_as: :agent do
  1352. let(:agent) { create(:agent) }
  1353. let!(:search_agents) { create_list(:agent, 3, firstname: 'Nick') }
  1354. shared_examples 'ids requests' do
  1355. before do
  1356. post '/api/v1/users/search', params: { query: 'Nick', ids: search_ids, sort_by: ['created_at'], order_by: ['ASC'] }, as: :json
  1357. end
  1358. shared_examples 'result check' do
  1359. it 'returns only agents matching search parameter ids' do
  1360. expect(json_response.map { |row| row['id'] }).to eq(search_ids)
  1361. end
  1362. end
  1363. context 'when searching for first two agents' do
  1364. let(:search_ids) { search_agents.first(2).map(&:id) }
  1365. include_examples 'result check'
  1366. end
  1367. context 'when searching for last two agents' do
  1368. let(:search_ids) { search_agents.last(2).map(&:id) }
  1369. include_examples 'result check'
  1370. end
  1371. end
  1372. context 'with elasticsearch', searchindex: true do
  1373. before do
  1374. searchindex_model_reload([::User])
  1375. end
  1376. include_examples 'ids requests'
  1377. end
  1378. context 'without elasticsearch' do
  1379. before do
  1380. Setting.set('es_url', nil)
  1381. end
  1382. include_examples 'ids requests'
  1383. end
  1384. end
  1385. describe 'PUT /api/v1/users/unlock/{id}' do
  1386. let(:admin) { create(:admin) }
  1387. let(:agent) { create(:agent) }
  1388. let(:customer) { create(:customer, login_failed: 2) }
  1389. def make_request(id)
  1390. put "/api/v1/users/unlock/#{id}", params: {}, as: :json
  1391. end
  1392. context 'with authenticated admin user', authenticated_as: :admin do
  1393. it 'returns success' do
  1394. make_request(customer.id)
  1395. expect(response).to have_http_status(:ok)
  1396. end
  1397. it 'check that login failed was reseted' do
  1398. expect { make_request(customer.id) }.to change { customer.reload.login_failed }.from(2).to(0)
  1399. end
  1400. it 'fail with not existing user id' do
  1401. make_request(99_999)
  1402. expect(response).to have_http_status(:not_found)
  1403. end
  1404. end
  1405. context 'with authenticated agent user', authenticated_as: :agent do
  1406. it 'fail without admin permission' do
  1407. make_request(customer.id)
  1408. expect(response).to have_http_status(:forbidden)
  1409. end
  1410. it 'check that login failed was not changed' do
  1411. expect { make_request(customer.id) }.not_to change { customer.reload.login_failed }
  1412. end
  1413. end
  1414. end
  1415. end