user_spec.rb 68 KB

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