user_spec.rb 63 KB

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