user_device_controller_test.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class UserDeviceControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. # create agent
  8. roles = Role.where( name: %w(Admin Agent) )
  9. groups = Group.all
  10. UserInfo.current_user_id = 1
  11. @admin = User.create_or_update(
  12. login: 'user-device-admin',
  13. firstname: 'UserDevice',
  14. lastname: 'Admin',
  15. email: 'user-device-admin@example.com',
  16. password: 'adminpw',
  17. active: true,
  18. roles: roles,
  19. groups: groups,
  20. )
  21. # create agent
  22. roles = Role.where( name: 'Agent' )
  23. @agent = User.create_or_update(
  24. login: 'user-device-agent',
  25. firstname: 'UserDevice',
  26. lastname: 'Agent',
  27. email: 'user-device-agent@example.com',
  28. password: 'agentpw',
  29. active: true,
  30. roles: roles,
  31. groups: groups,
  32. )
  33. ENV['TEST_REMOTE_IP'] = '5.9.62.170' # de
  34. ENV['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0'
  35. end
  36. test '01 - index with nobody' do
  37. get '/api/v1/signshow'
  38. assert_response(200)
  39. result = JSON.parse(@response.body)
  40. assert_equal(result.class, Hash)
  41. assert_equal(result['error'], 'no valid session')
  42. assert(result['config'])
  43. assert_not(controller.session[:user_device_fingerprint])
  44. Scheduler.worker(true)
  45. end
  46. test '02 - login index with admin without fingerprint' do
  47. assert_equal(0, UserDevice.where(user_id: @admin.id).count)
  48. assert_equal(0, email_notification_count('user_device_new', @admin.email))
  49. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  50. params = { without_fingerprint: 'none', username: 'user-device-admin', password: 'adminpw' }
  51. post '/api/v1/signin', params.to_json, @headers
  52. assert_response(422)
  53. result = JSON.parse(@response.body)
  54. assert_equal(result.class, Hash)
  55. assert_equal('Need fingerprint param!', result['error'])
  56. assert_not(result['config'])
  57. assert_not(controller.session[:user_device_fingerprint])
  58. Scheduler.worker(true)
  59. assert_equal(0, UserDevice.where(user_id: @admin.id).count)
  60. assert_equal(0, email_notification_count('user_device_new', @admin.email))
  61. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  62. end
  63. test '03 - login index with admin with fingerprint - I' do
  64. assert_equal(0, UserDevice.where(user_id: @admin.id).count)
  65. assert_equal(0, email_notification_count('user_device_new', @admin.email))
  66. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  67. params = { fingerprint: 'my_finger_print', username: 'user-device-admin', password: 'adminpw' }
  68. post '/api/v1/signin', params.to_json, @headers
  69. assert_response(201)
  70. result = JSON.parse(@response.body)
  71. assert_equal(result.class, Hash)
  72. assert_not(result['error'])
  73. assert(result['config'])
  74. assert('my_finger_print', controller.session[:user_device_fingerprint])
  75. Scheduler.worker(true)
  76. assert_equal(1, UserDevice.where(user_id: @admin.id).count)
  77. assert_equal(0, email_notification_count('user_device_new', @admin.email))
  78. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  79. user_device_first = UserDevice.last
  80. sleep 2
  81. params = {}
  82. get '/api/v1/users', params.to_json, @headers
  83. assert_response(200)
  84. result = JSON.parse(@response.body)
  85. assert_equal(result.class, Array)
  86. assert('my_finger_print', controller.session[:user_device_fingerprint])
  87. Scheduler.worker(true)
  88. assert_equal(1, UserDevice.where(user_id: @admin.id).count)
  89. assert_equal(0, email_notification_count('user_device_new', @admin.email))
  90. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  91. user_device_last = UserDevice.last
  92. assert_equal(user_device_last.updated_at.to_s, user_device_first.updated_at.to_s)
  93. params = { fingerprint: 'my_finger_print' }
  94. get '/api/v1/signshow', params, @headers
  95. assert_response(200)
  96. result = JSON.parse(@response.body)
  97. assert_equal(result.class, Hash)
  98. assert(result['session'])
  99. assert_equal(result['session']['login'], 'user-device-admin')
  100. assert(result['config'])
  101. Scheduler.worker(true)
  102. assert_equal(1, UserDevice.where(user_id: @admin.id).count)
  103. assert_equal(0, email_notification_count('user_device_new', @admin.email))
  104. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  105. user_device_last = UserDevice.last
  106. assert_equal(user_device_last.updated_at.to_s, user_device_first.updated_at.to_s)
  107. ENV['USER_DEVICE_UPDATED_AT'] = (Time.zone.now - 4.hours).to_s
  108. params = {}
  109. get '/api/v1/users', params.to_json, @headers
  110. assert_response(200)
  111. result = JSON.parse(@response.body)
  112. assert_equal(result.class, Array)
  113. assert('my_finger_print', controller.session[:user_device_fingerprint])
  114. Scheduler.worker(true)
  115. assert_equal(1, UserDevice.where(user_id: @admin.id).count)
  116. assert_equal(0, email_notification_count('user_device_new', @admin.email))
  117. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  118. user_device_last = UserDevice.last
  119. assert_not_equal(user_device_last.updated_at.to_s, user_device_first.updated_at.to_s)
  120. ENV['USER_DEVICE_UPDATED_AT'] = nil
  121. ENV['TEST_REMOTE_IP'] = '195.65.29.254' # ch
  122. params = {}
  123. get '/api/v1/users', params.to_json, @headers
  124. assert_response(200)
  125. result = JSON.parse(@response.body)
  126. Scheduler.worker(true)
  127. assert_equal(2, UserDevice.where(user_id: @admin.id).count)
  128. assert_equal(0, email_notification_count('user_device_new', @admin.email))
  129. assert_equal(1, email_notification_count('user_device_new_location', @admin.email))
  130. # ip reset
  131. ENV['TEST_REMOTE_IP'] = '5.9.62.170' # de
  132. end
  133. test '04 - login index with admin with fingerprint - II' do
  134. params = { fingerprint: 'my_finger_print_II', username: 'user-device-admin', password: 'adminpw' }
  135. post '/api/v1/signin', params.to_json, @headers
  136. assert_response(201)
  137. result = JSON.parse(@response.body)
  138. Scheduler.worker(true)
  139. assert_equal(3, UserDevice.where(user_id: @admin.id).count)
  140. assert_equal(1, email_notification_count('user_device_new', @admin.email))
  141. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  142. assert_equal(result.class, Hash)
  143. assert_not(result['error'])
  144. assert(result['config'])
  145. assert('my_finger_print_II', controller.session[:user_device_fingerprint])
  146. get '/api/v1/users', params.to_json, @headers
  147. assert_response(200)
  148. result = JSON.parse(@response.body)
  149. assert_equal(result.class, Array)
  150. Scheduler.worker(true)
  151. assert_equal(3, UserDevice.where(user_id: @admin.id).count)
  152. assert_equal(1, email_notification_count('user_device_new', @admin.email))
  153. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  154. params = { fingerprint: 'my_finger_print_II' }
  155. get '/api/v1/signshow', params, @headers
  156. assert_response(200)
  157. result = JSON.parse(@response.body)
  158. assert_equal(result.class, Hash)
  159. assert(result['session'])
  160. assert_equal(result['session']['login'], 'user-device-admin')
  161. assert(result['config'])
  162. Scheduler.worker(true)
  163. assert_equal(3, UserDevice.where(user_id: @admin.id).count)
  164. assert_equal(1, email_notification_count('user_device_new', @admin.email))
  165. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  166. ENV['TEST_REMOTE_IP'] = '195.65.29.254' # ch
  167. params = {}
  168. get '/api/v1/users', params.to_json, @headers
  169. assert_response(200)
  170. result = JSON.parse(@response.body)
  171. Scheduler.worker(true)
  172. assert_equal(4, UserDevice.where(user_id: @admin.id).count)
  173. assert_equal(1, email_notification_count('user_device_new', @admin.email))
  174. assert_equal(1, email_notification_count('user_device_new_location', @admin.email))
  175. # ip reset
  176. ENV['TEST_REMOTE_IP'] = '5.9.62.170' # de
  177. end
  178. test '05 - login index with admin with fingerprint - II' do
  179. params = { fingerprint: 'my_finger_print_II', username: 'user-device-admin', password: 'adminpw' }
  180. post '/api/v1/signin', params.to_json, @headers
  181. assert_response(201)
  182. result = JSON.parse(@response.body)
  183. Scheduler.worker(true)
  184. assert_equal(4, UserDevice.where(user_id: @admin.id).count)
  185. assert_equal(0, email_notification_count('user_device_new', @admin.email))
  186. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  187. assert_equal(result.class, Hash)
  188. assert_not(result['error'])
  189. assert(result['config'])
  190. assert('my_finger_print_II', controller.session[:user_device_fingerprint])
  191. end
  192. test '06 - login index with admin with basic auth' do
  193. ENV['HTTP_USER_AGENT'] = 'curl 1.2.3'
  194. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('user-device-admin', 'adminpw')
  195. params = {}
  196. get '/api/v1/users', params.to_json, @headers.merge('Authorization' => credentials)
  197. assert_response(200)
  198. result = JSON.parse(@response.body)
  199. Scheduler.worker(true)
  200. assert_equal(5, UserDevice.where(user_id: @admin.id).count)
  201. assert_equal(1, email_notification_count('user_device_new', @admin.email))
  202. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  203. assert_equal(result.class, Array)
  204. user_device_first = UserDevice.last
  205. sleep 2
  206. params = {}
  207. get '/api/v1/users', params.to_json, @headers.merge('Authorization' => credentials)
  208. assert_response(200)
  209. result = JSON.parse(@response.body)
  210. Scheduler.worker(true)
  211. assert_equal(5, UserDevice.where(user_id: @admin.id).count)
  212. assert_equal(1, email_notification_count('user_device_new', @admin.email))
  213. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  214. assert_equal(result.class, Array)
  215. user_device_last = UserDevice.last
  216. assert_equal(user_device_last.id, user_device_first.id)
  217. assert_equal(user_device_last.updated_at.to_s, user_device_first.updated_at.to_s)
  218. user_device_last.updated_at = Time.zone.now - 4.hours
  219. user_device_last.save!
  220. params = {}
  221. get '/api/v1/users', params, @headers.merge('Authorization' => credentials)
  222. assert_response(200)
  223. result = JSON.parse(@response.body)
  224. Scheduler.worker(true)
  225. assert_equal(5, UserDevice.where(user_id: @admin.id).count)
  226. assert_equal(1, email_notification_count('user_device_new', @admin.email))
  227. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  228. assert_equal(result.class, Array)
  229. user_device_last = UserDevice.last
  230. assert_equal(user_device_last.id, user_device_first.id)
  231. assert(user_device_last.updated_at > user_device_first.updated_at)
  232. end
  233. test '07 - login index with admin with basic auth' do
  234. ENV['HTTP_USER_AGENT'] = 'curl 1.2.3'
  235. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('user-device-admin', 'adminpw')
  236. params = {}
  237. get '/api/v1/users', params.to_json, @headers.merge('Authorization' => credentials)
  238. assert_response(200)
  239. result = JSON.parse(@response.body)
  240. Scheduler.worker(true)
  241. assert_equal(5, UserDevice.where(user_id: @admin.id).count)
  242. assert_equal(0, email_notification_count('user_device_new', @admin.email))
  243. assert_equal(0, email_notification_count('user_device_new_location', @admin.email))
  244. assert_equal(result.class, Array)
  245. end
  246. test '08 - login index with agent with basic auth' do
  247. assert_equal(0, UserDevice.where(user_id: @agent.id).count)
  248. assert_equal(0, email_notification_count('user_device_new', @agent.email))
  249. assert_equal(0, email_notification_count('user_device_new_location', @agent.email))
  250. ENV['HTTP_USER_AGENT'] = 'curl 1.2.3'
  251. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('user-device-agent', 'agentpw')
  252. params = {}
  253. get '/api/v1/users', params.to_json, @headers.merge('Authorization' => credentials)
  254. assert_response(200)
  255. result = JSON.parse(@response.body)
  256. Scheduler.worker(true)
  257. assert_equal(1, UserDevice.where(user_id: @agent.id).count)
  258. assert_equal(0, email_notification_count('user_device_new', @agent.email))
  259. assert_equal(0, email_notification_count('user_device_new_location', @agent.email))
  260. assert_equal(result.class, Array)
  261. end
  262. test '09 - login index with agent with basic auth' do
  263. assert_equal(1, UserDevice.where(user_id: @agent.id).count)
  264. assert_equal(0, email_notification_count('user_device_new', @agent.email))
  265. assert_equal(0, email_notification_count('user_device_new_location', @agent.email))
  266. ENV['HTTP_USER_AGENT'] = 'curl 1.2.3'
  267. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('user-device-agent', 'agentpw')
  268. params = {}
  269. get '/api/v1/users', params.to_json, @headers.merge('Authorization' => credentials)
  270. assert_response(200)
  271. result = JSON.parse(@response.body)
  272. Scheduler.worker(true)
  273. assert_equal(1, UserDevice.where(user_id: @agent.id).count)
  274. assert_equal(0, email_notification_count('user_device_new', @agent.email))
  275. assert_equal(0, email_notification_count('user_device_new_location', @agent.email))
  276. assert_equal(result.class, Array)
  277. end
  278. test '10 - login with switched_from_user_id' do
  279. assert_equal(1, UserDevice.where(user_id: @agent.id).count)
  280. assert_equal(0, email_notification_count('user_device_new', @agent.email))
  281. assert_equal(0, email_notification_count('user_device_new_location', @agent.email))
  282. ENV['SWITCHED_FROM_USER_ID'] = @admin.id.to_s
  283. params = { fingerprint: 'my_finger_print_II', username: 'user-device-agent', password: 'agentpw' }
  284. post '/api/v1/signin', params.to_json, @headers
  285. assert_response(201)
  286. result = JSON.parse(@response.body)
  287. Scheduler.worker(true)
  288. assert_equal(1, UserDevice.where(user_id: @agent.id).count)
  289. assert_equal(0, email_notification_count('user_device_new', @agent.email))
  290. assert_equal(0, email_notification_count('user_device_new_location', @agent.email))
  291. assert_equal(result.class, Hash)
  292. assert_not(result['error'])
  293. assert(result['config'])
  294. assert('my_finger_print_II', controller.session[:user_device_fingerprint])
  295. Scheduler.worker(true)
  296. assert_equal(1, UserDevice.where(user_id: @agent.id).count)
  297. assert_equal(0, email_notification_count('user_device_new', @agent.email))
  298. assert_equal(0, email_notification_count('user_device_new_location', @agent.email))
  299. ENV['USER_DEVICE_UPDATED_AT'] = (Time.zone.now - 4.hours).to_s
  300. params = {}
  301. get '/api/v1/users', params.to_json, @headers
  302. assert_response(200)
  303. result = JSON.parse(@response.body)
  304. assert_equal(result.class, Array)
  305. assert('my_finger_print_II', controller.session[:user_device_fingerprint])
  306. Scheduler.worker(true)
  307. assert_equal(1, UserDevice.where(user_id: @agent.id).count)
  308. assert_equal(0, email_notification_count('user_device_new', @agent.email))
  309. assert_equal(0, email_notification_count('user_device_new_location', @agent.email))
  310. ENV['USER_DEVICE_UPDATED_AT'] = nil
  311. ENV['TEST_REMOTE_IP'] = '195.65.29.254' # ch
  312. params = {}
  313. get '/api/v1/users', params.to_json, @headers
  314. assert_response(200)
  315. result = JSON.parse(@response.body)
  316. Scheduler.worker(true)
  317. # ip reset
  318. ENV['TEST_REMOTE_IP'] = '5.9.62.170' # de
  319. assert_equal(1, UserDevice.where(user_id: @agent.id).count)
  320. assert_equal(0, email_notification_count('user_device_new', @agent.email))
  321. assert_equal(0, email_notification_count('user_device_new_location', @agent.email))
  322. end
  323. end