user_device_spec.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. require 'rails_helper'
  2. RSpec.describe 'User Device', type: :request, sends_notification_emails: true do
  3. let!(:admin) do
  4. create(:admin, login: 'user-device-admin', password: 'adminpw', groups: Group.all)
  5. end
  6. let!(:agent) do
  7. create(:agent, login: 'user-device-agent', password: 'agentpw', groups: Group.all)
  8. end
  9. before do
  10. ENV['TEST_REMOTE_IP'] = '5.9.62.170' # de
  11. ENV['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0'
  12. ENV['SWITCHED_FROM_USER_ID'] = nil
  13. UserDevice.destroy_all
  14. end
  15. describe 'request handling' do
  16. it 'does index with nobody (01)' do
  17. get '/api/v1/signshow'
  18. expect(response).to have_http_status(:ok)
  19. expect(json_response).to be_a_kind_of(Hash)
  20. expect('no valid session').to eq(json_response['error'])
  21. expect(json_response['config']).to be_truthy
  22. expect(controller.session[:user_device_fingerprint]).to be_falsey
  23. Scheduler.worker(true)
  24. end
  25. it 'does login index with admin without fingerprint (02)' do
  26. params = { without_fingerprint: 'none', username: 'user-device-admin', password: 'adminpw' }
  27. post '/api/v1/signin', params: params, as: :json
  28. expect(response).to have_http_status(:unprocessable_entity)
  29. expect(json_response).to be_a_kind_of(Hash)
  30. expect(json_response['error']).to eq('Need fingerprint param!')
  31. expect(json_response['config']).to be_falsey
  32. expect(controller.session[:user_device_fingerprint]).to be_falsey
  33. check_notification do
  34. Scheduler.worker(true)
  35. not_sent(
  36. template: 'user_device_new',
  37. user: admin,
  38. )
  39. not_sent(
  40. template: 'user_device_new_location',
  41. user: admin,
  42. )
  43. end
  44. expect(UserDevice.where(user_id: admin.id).count).to eq(0)
  45. end
  46. it 'does login index with admin with fingerprint - I (03)' do
  47. params = { fingerprint: 'my_finger_print', username: 'user-device-admin', password: 'adminpw' }
  48. post '/api/v1/signin', params: params, as: :json
  49. expect(response).to have_http_status(:created)
  50. expect(json_response).to be_a_kind_of(Hash)
  51. expect(json_response['error']).to be_falsey
  52. expect(json_response['config']).to be_truthy
  53. expect(controller.session[:user_device_fingerprint]).to eq('my_finger_print')
  54. check_notification do
  55. Scheduler.worker(true)
  56. not_sent(
  57. template: 'user_device_new',
  58. user: admin,
  59. )
  60. not_sent(
  61. template: 'user_device_new_location',
  62. user: admin,
  63. )
  64. end
  65. expect(UserDevice.where(user_id: admin.id).count).to eq(1)
  66. user_device_first = UserDevice.last
  67. sleep 2
  68. params = {}
  69. get '/api/v1/users', params: params, as: :json
  70. expect(response).to have_http_status(:ok)
  71. expect(json_response).to be_a_kind_of(Array)
  72. expect(controller.session[:user_device_fingerprint]).to eq('my_finger_print')
  73. check_notification do
  74. Scheduler.worker(true)
  75. not_sent(
  76. template: 'user_device_new',
  77. user: admin,
  78. )
  79. not_sent(
  80. template: 'user_device_new_location',
  81. user: admin,
  82. )
  83. end
  84. expect(UserDevice.where(user_id: admin.id).count).to eq(1)
  85. user_device_last = UserDevice.last
  86. expect(user_device_first.updated_at.to_s).to eq(user_device_last.updated_at.to_s)
  87. params = { fingerprint: 'my_finger_print' }
  88. get '/api/v1/signshow', params: params, as: :json
  89. expect(response).to have_http_status(:ok)
  90. expect(json_response).to be_a_kind_of(Hash)
  91. expect(json_response['session']).to be_truthy
  92. expect('user-device-admin').to eq(json_response['session']['login'])
  93. expect(json_response['config']).to be_truthy
  94. check_notification do
  95. Scheduler.worker(true)
  96. not_sent(
  97. template: 'user_device_new',
  98. user: admin,
  99. )
  100. not_sent(
  101. template: 'user_device_new_location',
  102. user: admin,
  103. )
  104. end
  105. expect(UserDevice.where(user_id: admin.id).count).to eq(1)
  106. user_device_last = UserDevice.last
  107. expect(user_device_first.updated_at.to_s).to eq(user_device_last.updated_at.to_s)
  108. ENV['USER_DEVICE_UPDATED_AT'] = (Time.zone.now - 4.hours).to_s
  109. params = {}
  110. get '/api/v1/users', params: params, as: :json
  111. expect(response).to have_http_status(:ok)
  112. expect(json_response).to be_a_kind_of(Array)
  113. expect(controller.session[:user_device_fingerprint]).to eq('my_finger_print')
  114. check_notification do
  115. Scheduler.worker(true)
  116. not_sent(
  117. template: 'user_device_new',
  118. user: admin,
  119. )
  120. not_sent(
  121. template: 'user_device_new_location',
  122. user: admin,
  123. )
  124. end
  125. expect(UserDevice.where(user_id: admin.id).count).to eq(1)
  126. user_device_last = UserDevice.last
  127. expect(user_device_last.updated_at.to_s).not_to eq(user_device_first.updated_at.to_s)
  128. ENV['USER_DEVICE_UPDATED_AT'] = nil
  129. ENV['TEST_REMOTE_IP'] = '195.65.29.254' # ch
  130. #reset_notification_checks
  131. params = {}
  132. get '/api/v1/users', params: params, as: :json
  133. expect(response).to have_http_status(:ok)
  134. check_notification do
  135. Scheduler.worker(true)
  136. not_sent(
  137. template: 'user_device_new',
  138. user: admin,
  139. )
  140. sent(
  141. template: 'user_device_new_location',
  142. user: admin,
  143. )
  144. end
  145. expect(UserDevice.where(user_id: admin.id).count).to eq(2)
  146. # ip reset
  147. ENV['TEST_REMOTE_IP'] = '5.9.62.170' # de
  148. end
  149. it 'does login index with admin with fingerprint - II (04)' do
  150. create(
  151. :user_device,
  152. user_id: admin.id,
  153. fingerprint: 'fingerprintI',
  154. )
  155. params = { fingerprint: 'my_finger_print_II', username: 'user-device-admin', password: 'adminpw' }
  156. post '/api/v1/signin', params: params, as: :json
  157. expect(response).to have_http_status(:created)
  158. check_notification do
  159. Scheduler.worker(true)
  160. sent(
  161. template: 'user_device_new',
  162. user: admin,
  163. )
  164. not_sent(
  165. template: 'user_device_new_location',
  166. user: admin,
  167. )
  168. end
  169. expect(UserDevice.where(user_id: admin.id).count).to eq(2)
  170. expect(json_response).to be_a_kind_of(Hash)
  171. expect(json_response['error']).to be_falsey
  172. expect(json_response['config']).to be_truthy
  173. expect(controller.session[:user_device_fingerprint]).to be_truthy
  174. get '/api/v1/users', params: params, as: :json
  175. expect(response).to have_http_status(:ok)
  176. expect(json_response).to be_a_kind_of(Array)
  177. check_notification do
  178. Scheduler.worker(true)
  179. not_sent(
  180. template: 'user_device_new',
  181. user: admin,
  182. )
  183. not_sent(
  184. template: 'user_device_new_location',
  185. user: admin,
  186. )
  187. end
  188. expect(UserDevice.where(user_id: admin.id).count).to eq(2)
  189. params = { fingerprint: 'my_finger_print_II' }
  190. get '/api/v1/signshow', params: params, as: :json
  191. expect(response).to have_http_status(:ok)
  192. expect(json_response).to be_a_kind_of(Hash)
  193. expect(json_response['session']).to be_truthy
  194. expect('user-device-admin').to eq(json_response['session']['login'])
  195. expect(json_response['config']).to be_truthy
  196. check_notification do
  197. Scheduler.worker(true)
  198. not_sent(
  199. template: 'user_device_new',
  200. user: admin,
  201. )
  202. not_sent(
  203. template: 'user_device_new_location',
  204. user: admin,
  205. )
  206. end
  207. expect(UserDevice.where(user_id: admin.id).count).to eq(2)
  208. ENV['TEST_REMOTE_IP'] = '195.65.29.254' # ch
  209. params = {}
  210. get '/api/v1/users', params: params, as: :json
  211. expect(response).to have_http_status(:ok)
  212. check_notification do
  213. Scheduler.worker(true)
  214. not_sent(
  215. template: 'user_device_new',
  216. user: admin,
  217. )
  218. sent(
  219. template: 'user_device_new_location',
  220. user: admin,
  221. )
  222. end
  223. expect(UserDevice.where(user_id: admin.id).count).to eq(3)
  224. # ip reset
  225. ENV['TEST_REMOTE_IP'] = '5.9.62.170' # de
  226. end
  227. it 'does login index with admin with fingerprint - II (05)' do
  228. UserDevice.add(
  229. ENV['HTTP_USER_AGENT'],
  230. ENV['TEST_REMOTE_IP'],
  231. admin.id,
  232. 'my_finger_print_II',
  233. 'session', # session|basic_auth|token_auth|sso
  234. )
  235. expect(UserDevice.where(user_id: admin.id).count).to eq(1)
  236. params = { fingerprint: 'my_finger_print_II', username: 'user-device-admin', password: 'adminpw' }
  237. post '/api/v1/signin', params: params, as: :json
  238. expect(response).to have_http_status(:created)
  239. check_notification do
  240. Scheduler.worker(true)
  241. not_sent(
  242. template: 'user_device_new',
  243. user: admin,
  244. )
  245. not_sent(
  246. template: 'user_device_new_location',
  247. user: admin,
  248. )
  249. end
  250. expect(UserDevice.where(user_id: admin.id).count).to eq(1)
  251. expect(json_response).to be_a_kind_of(Hash)
  252. expect(json_response['error']).to be_falsey
  253. expect(json_response['config']).to be_truthy
  254. expect(controller.session[:user_device_fingerprint]).to be_truthy
  255. end
  256. it 'does login index with admin with basic auth (06)' do
  257. ENV['HTTP_USER_AGENT'] = 'curl 1.0.0'
  258. UserDevice.add(
  259. ENV['HTTP_USER_AGENT'],
  260. '127.0.0.1',
  261. admin.id,
  262. '',
  263. 'basic_auth', # session|basic_auth|token_auth|sso
  264. )
  265. expect(UserDevice.where(user_id: admin.id).count).to eq(1)
  266. ENV['HTTP_USER_AGENT'] = 'curl 1.2.3'
  267. params = {}
  268. authenticated_as(admin, password: 'adminpw')
  269. get '/api/v1/users', params: params, as: :json
  270. expect(response).to have_http_status(:ok)
  271. check_notification do
  272. Scheduler.worker(true)
  273. sent(
  274. template: 'user_device_new',
  275. user: admin,
  276. )
  277. not_sent(
  278. template: 'user_device_new_location',
  279. user: admin,
  280. )
  281. end
  282. expect(UserDevice.where(user_id: admin.id).count).to eq(2)
  283. expect(json_response).to be_a_kind_of(Array)
  284. user_device_first = UserDevice.last
  285. sleep 2
  286. params = {}
  287. get '/api/v1/users', params: params, as: :json
  288. expect(response).to have_http_status(:ok)
  289. check_notification do
  290. Scheduler.worker(true)
  291. not_sent(
  292. template: 'user_device_new',
  293. user: admin,
  294. )
  295. not_sent(
  296. template: 'user_device_new_location',
  297. user: admin,
  298. )
  299. end
  300. expect(UserDevice.where(user_id: admin.id).count).to eq(2)
  301. expect(json_response).to be_a_kind_of(Array)
  302. user_device_last = UserDevice.last
  303. expect(user_device_first.id).to eq(user_device_last.id)
  304. expect(user_device_first.updated_at.to_s).to eq(user_device_last.updated_at.to_s)
  305. user_device_last.updated_at = Time.zone.now - 4.hours
  306. user_device_last.save!
  307. params = {}
  308. get '/api/v1/users', params: params, as: :json
  309. expect(response).to have_http_status(:ok)
  310. check_notification do
  311. Scheduler.worker(true)
  312. not_sent(
  313. template: 'user_device_new',
  314. user: admin,
  315. )
  316. not_sent(
  317. template: 'user_device_new_location',
  318. user: admin,
  319. )
  320. end
  321. expect(UserDevice.where(user_id: admin.id).count).to eq(2)
  322. expect(json_response).to be_a_kind_of(Array)
  323. user_device_last = UserDevice.last
  324. expect(user_device_first.id).to eq(user_device_last.id)
  325. expect(user_device_last.updated_at > user_device_first.updated_at).to be_truthy
  326. end
  327. it 'does login index with admin with basic auth (07)' do
  328. ENV['HTTP_USER_AGENT'] = 'curl 1.2.3'
  329. UserDevice.add(
  330. ENV['HTTP_USER_AGENT'],
  331. ENV['TEST_REMOTE_IP'],
  332. admin.id,
  333. '',
  334. 'basic_auth', # session|basic_auth|token_auth|sso
  335. )
  336. expect(UserDevice.where(user_id: admin.id).count).to eq(1)
  337. params = {}
  338. authenticated_as(admin, password: 'adminpw')
  339. get '/api/v1/users', params: params, as: :json
  340. expect(response).to have_http_status(:ok)
  341. check_notification do
  342. Scheduler.worker(true)
  343. not_sent(
  344. template: 'user_device_new',
  345. user: admin,
  346. )
  347. not_sent(
  348. template: 'user_device_new_location',
  349. user: admin,
  350. )
  351. end
  352. expect(UserDevice.where(user_id: admin.id).count).to eq(1)
  353. expect(json_response).to be_a_kind_of(Array)
  354. end
  355. it 'does login index with agent with basic auth (08)' do
  356. ENV['HTTP_USER_AGENT'] = 'curl 1.2.3'
  357. params = {}
  358. authenticated_as(agent, password: 'agentpw')
  359. get '/api/v1/users', params: params, as: :json
  360. expect(response).to have_http_status(:ok)
  361. check_notification do
  362. Scheduler.worker(true)
  363. not_sent(
  364. template: 'user_device_new',
  365. user: agent,
  366. )
  367. not_sent(
  368. template: 'user_device_new_location',
  369. user: agent,
  370. )
  371. end
  372. expect(UserDevice.where(user_id: agent.id).count).to eq(1)
  373. expect(json_response).to be_a_kind_of(Array)
  374. end
  375. it 'does login index with agent with basic auth (09)' do
  376. ENV['HTTP_USER_AGENT'] = 'curl 1.2.3'
  377. UserDevice.add(
  378. ENV['HTTP_USER_AGENT'],
  379. ENV['TEST_REMOTE_IP'],
  380. agent.id,
  381. '',
  382. 'basic_auth', # session|basic_auth|token_auth|sso
  383. )
  384. expect(UserDevice.where(user_id: agent.id).count).to eq(1)
  385. params = {}
  386. authenticated_as(agent, password: 'agentpw')
  387. get '/api/v1/users', params: params, as: :json
  388. expect(response).to have_http_status(:ok)
  389. check_notification do
  390. Scheduler.worker(true)
  391. not_sent(
  392. template: 'user_device_new',
  393. user: agent,
  394. )
  395. not_sent(
  396. template: 'user_device_new_location',
  397. user: agent,
  398. )
  399. end
  400. expect(UserDevice.where(user_id: agent.id).count).to eq(1)
  401. expect(json_response).to be_a_kind_of(Array)
  402. end
  403. it 'does login with switched_from_user_id (10)' do
  404. expect(UserDevice.where(user_id: agent.id).count).to eq(0)
  405. ENV['SWITCHED_FROM_USER_ID'] = admin.id.to_s
  406. params = { fingerprint: 'my_finger_print_II', username: 'user-device-agent', password: 'agentpw' }
  407. post '/api/v1/signin', params: params, as: :json
  408. expect(response).to have_http_status(:created)
  409. check_notification do
  410. Scheduler.worker(true)
  411. not_sent(
  412. template: 'user_device_new',
  413. user: agent,
  414. )
  415. not_sent(
  416. template: 'user_device_new_location',
  417. user: agent,
  418. )
  419. end
  420. expect(UserDevice.where(user_id: agent.id).count).to eq(0)
  421. expect(json_response).to be_a_kind_of(Hash)
  422. expect(json_response['error']).to be_falsey
  423. expect(json_response['config']).to be_truthy
  424. check_notification do
  425. Scheduler.worker(true)
  426. not_sent(
  427. template: 'user_device_new',
  428. user: agent,
  429. )
  430. not_sent(
  431. template: 'user_device_new_location',
  432. user: agent,
  433. )
  434. end
  435. expect(UserDevice.where(user_id: agent.id).count).to eq(0)
  436. ENV['USER_DEVICE_UPDATED_AT'] = (Time.zone.now - 4.hours).to_s
  437. params = {}
  438. get '/api/v1/users', params: params, as: :json
  439. expect(response).to have_http_status(:ok)
  440. expect(json_response).to be_a_kind_of(Array)
  441. check_notification do
  442. Scheduler.worker(true)
  443. not_sent(
  444. template: 'user_device_new',
  445. user: agent,
  446. )
  447. not_sent(
  448. template: 'user_device_new_location',
  449. user: agent,
  450. )
  451. end
  452. expect(UserDevice.where(user_id: agent.id).count).to eq(0)
  453. ENV['USER_DEVICE_UPDATED_AT'] = nil
  454. ENV['TEST_REMOTE_IP'] = '195.65.29.254' # ch
  455. params = {}
  456. get '/api/v1/users', params: params, as: :json
  457. expect(response).to have_http_status(:ok)
  458. check_notification do
  459. Scheduler.worker(true)
  460. not_sent(
  461. template: 'user_device_new',
  462. user: agent,
  463. )
  464. not_sent(
  465. template: 'user_device_new_location',
  466. user: agent,
  467. )
  468. end
  469. # ip reset
  470. ENV['TEST_REMOTE_IP'] = '5.9.62.170' # de
  471. expect(UserDevice.where(user_id: agent.id).count).to eq(0)
  472. end
  473. it 'does login with invalid fingerprint (11)' do
  474. params = { fingerprint: 'to_long_1234567890to_long_1234567890to_long_1234567890to_long_1234567890to_long_1234567890to_long_1234567890to_long_1234567890to_long_1234567890to_long_1234567890to_long_1234567890to_long_1234567890', username: 'user-device-admin', password: 'adminpw' }
  475. post '/api/v1/signin', params: params, as: :json
  476. expect(response).to have_http_status(:unprocessable_entity)
  477. expect(json_response).to be_a_kind_of(Hash)
  478. expect(json_response['error']).to eq('fingerprint is 198 chars but can only be 160 chars!')
  479. expect(json_response['config']).to be_falsey
  480. expect(controller.session[:user_device_fingerprint]).to be_falsey
  481. check_notification do
  482. Scheduler.worker(true)
  483. not_sent(
  484. template: 'user_device_new',
  485. user: admin,
  486. )
  487. not_sent(
  488. template: 'user_device_new_location',
  489. user: admin,
  490. )
  491. end
  492. expect(UserDevice.where(user_id: admin.id).count).to eq(0)
  493. end
  494. it 'does login with integer as fingerprint (12)' do
  495. params = { fingerprint: 123_456_789, username: 'user-device-admin', password: 'adminpw' }
  496. post '/api/v1/signin', params: params, as: :json
  497. expect(response).to have_http_status(:created)
  498. expect(controller.session[:user_device_fingerprint]).to be_truthy
  499. check_notification do
  500. Scheduler.worker(true)
  501. not_sent(
  502. template: 'user_device_new',
  503. user: admin,
  504. )
  505. not_sent(
  506. template: 'user_device_new_location',
  507. user: admin,
  508. )
  509. end
  510. expect(UserDevice.where(user_id: admin.id).count).to eq(1)
  511. expect(json_response).to be_a_kind_of(Hash)
  512. expect(json_response['error']).to be_nil
  513. end
  514. it 'does login form controller - check no user device logging (13)' do
  515. Setting.set('form_ticket_create', true)
  516. params = {
  517. fingerprint: 'long_1234567890long_1234567890long_1234567890long_1234567890long_1234567890long_1234567890long_1234567890long_1234567890long_1234567890long_1234567890long_1234567890'
  518. }
  519. authenticated_as(admin, password: 'adminpw')
  520. post '/api/v1/form_config', params: params, as: :json
  521. expect(response).to have_http_status(:ok)
  522. expect(json_response).to be_a_kind_of(Hash)
  523. expect(json_response['error']).to be_falsey
  524. expect(json_response['endpoint']).to be_truthy
  525. expect(controller.session[:user_device_fingerprint]).to be_falsey
  526. check_notification do
  527. Scheduler.worker(true)
  528. not_sent(
  529. template: 'user_device_new',
  530. user: admin,
  531. )
  532. not_sent(
  533. template: 'user_device_new_location',
  534. user: admin,
  535. )
  536. end
  537. expect(UserDevice.where(user_id: admin.id).count).to eq(0)
  538. end
  539. end
  540. end