user_device_test.rb 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. require 'test_helper'
  2. class UserDeviceTest < ActiveSupport::TestCase
  3. setup do
  4. UserInfo.current_user_id = 1
  5. groups = Group.all
  6. roles = Role.where(name: 'Agent')
  7. @agent = User.create_or_update(
  8. login: 'user-device-agent@example.com',
  9. firstname: 'UserDevice',
  10. lastname: 'Agent',
  11. email: 'user-device-agent@example.com',
  12. password: 'agentpw',
  13. active: true,
  14. roles: roles,
  15. groups: groups,
  16. )
  17. roles = Role.where(name: 'Customer')
  18. @customer = User.create_or_update(
  19. login: 'user-device-customer@example.com',
  20. firstname: 'UserDevice',
  21. lastname: 'Customer',
  22. email: 'user-device-customer@example.com',
  23. password: 'customerpw',
  24. active: true,
  25. roles: roles,
  26. )
  27. end
  28. test 'aaa - session test' do
  29. # signin with fingerprint A from country A via session -> new device #1
  30. user_device1 = UserDevice.add(
  31. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  32. '91.115.248.231',
  33. @agent.id,
  34. 'fingerprint1234',
  35. 'session',
  36. )
  37. # signin with fingerprint A from country B via session -> new device #2
  38. user_device2 = UserDevice.add(
  39. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  40. '176.198.137.254',
  41. @agent.id,
  42. 'fingerprint1234',
  43. 'session',
  44. )
  45. assert_not_equal(user_device1.id, user_device2.id)
  46. # signin with fingerprint B from country A via session -> new device #3
  47. user_device3 = UserDevice.add(
  48. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  49. '91.115.248.231',
  50. @agent.id,
  51. 'fingerprintABC',
  52. 'session',
  53. )
  54. assert_not_equal(user_device2.id, user_device3.id)
  55. # signin with fingerprint A from country A via session -> new device #1
  56. user_device4 = UserDevice.add(
  57. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  58. '91.115.248.231',
  59. @agent.id,
  60. 'fingerprint1234',
  61. 'session',
  62. )
  63. assert_equal(user_device1.id, user_device4.id)
  64. # signin with fingerprint A from country B via session -> new device #2
  65. user_device5 = UserDevice.add(
  66. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  67. '176.198.137.254',
  68. @agent.id,
  69. 'fingerprint1234',
  70. 'session',
  71. )
  72. assert_equal(user_device2.id, user_device5.id)
  73. # signin with fingerprint B from country A via session -> new device #3
  74. user_device6 = UserDevice.add(
  75. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  76. '91.115.248.231',
  77. @agent.id,
  78. 'fingerprintABC',
  79. 'session',
  80. )
  81. assert_equal(user_device3.id, user_device6.id)
  82. end
  83. test 'bbb - session test - user agent (unknown)' do
  84. # known user agent
  85. user_device1 = UserDevice.add(
  86. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  87. '91.115.248.231',
  88. @agent.id,
  89. nil,
  90. 'session',
  91. )
  92. assert_equal('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36', user_device1.user_agent)
  93. assert_equal('Mac, Chrome', user_device1.name)
  94. # unknown user agent
  95. user_device2 = UserDevice.add(
  96. 'ABC 123',
  97. '91.115.248.231',
  98. @agent.id,
  99. nil,
  100. 'session',
  101. )
  102. assert_equal('ABC 123', user_device2.user_agent)
  103. assert_equal('ABC 123', user_device2.browser)
  104. assert_equal('ABC 123', user_device2.name)
  105. # partently known
  106. user_device3 = UserDevice.add(
  107. 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4',
  108. '91.115.248.231',
  109. @agent.id,
  110. nil,
  111. 'session',
  112. )
  113. assert_equal('Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4', user_device3.user_agent)
  114. assert_equal('Safari', user_device3.browser)
  115. assert_equal('Ios, Safari', user_device3.name)
  116. user_device4 = UserDevice.add(
  117. 'Mac+OS+X/10.10.5 (14F27) CalendarAgent/316.1',
  118. '91.115.248.231',
  119. @agent.id,
  120. nil,
  121. 'session',
  122. )
  123. assert_equal('Mac+OS+X/10.10.5 (14F27) CalendarAgent/316.1', user_device4.user_agent)
  124. assert_equal('Mac+OS+X/10.10.5 (14F27) CalendarAgent/316.1', user_device4.browser)
  125. assert_equal('Mac, Mac+OS+X/10.10.5 (14F27) CalendarAgent/316.1', user_device4.name)
  126. end
  127. test 'ccc - api test' do
  128. # signin with ua from country A via basic auth -> new device #1
  129. user_device1 = UserDevice.add(
  130. 'curl/7.43.0',
  131. '91.115.248.231',
  132. @agent.id,
  133. nil,
  134. 'basic_auth',
  135. )
  136. # signin with ua from country B via basic auth -> new device #2
  137. user_device2 = UserDevice.add(
  138. 'curl/7.43.0',
  139. '176.198.137.254',
  140. @agent.id,
  141. nil,
  142. 'basic_auth',
  143. )
  144. assert_not_equal(user_device1.id, user_device2.id)
  145. # signin with ua from country A via basic auth -> new device #1
  146. user_device3 = UserDevice.add(
  147. 'curl/7.43.0',
  148. '91.115.248.231',
  149. @agent.id,
  150. nil,
  151. 'basic_auth',
  152. )
  153. assert_equal(user_device1.id, user_device3.id)
  154. # signin with ua from country B via basic auth -> new device #2
  155. user_device4 = UserDevice.add(
  156. 'curl/7.43.0',
  157. '176.198.137.254',
  158. @agent.id,
  159. nil,
  160. 'basic_auth',
  161. )
  162. assert_equal(user_device2.id, user_device4.id)
  163. # signin with ua from country A via token auth -> new device #1
  164. user_device5 = UserDevice.add(
  165. 'curl/7.43.0',
  166. '91.115.248.231',
  167. @agent.id,
  168. nil,
  169. 'token_auth',
  170. )
  171. assert_equal(user_device1.id, user_device5.id)
  172. # signin with ua from country B via token auth -> new device #2
  173. user_device6 = UserDevice.add(
  174. 'curl/7.43.0',
  175. '176.198.137.254',
  176. @agent.id,
  177. nil,
  178. 'token_auth',
  179. )
  180. assert_equal(user_device2.id, user_device6.id)
  181. end
  182. test 'ddd - api test' do
  183. # signin with fingerprint A from country A via session -> new device #1
  184. user_device1 = UserDevice.add(
  185. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  186. '91.115.248.231',
  187. @agent.id,
  188. 'fingerprint1234',
  189. 'session',
  190. )
  191. # action with same fingerprint -> same device
  192. user_device1_1 = UserDevice.action(
  193. user_device1.id,
  194. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  195. '91.115.248.231',
  196. @agent.id,
  197. 'session',
  198. )
  199. assert_equal(user_device1.id, user_device1_1.id)
  200. # signin with same fingerprint -> same device
  201. user_device1_2 = UserDevice.add(
  202. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  203. '91.115.248.231',
  204. @agent.id,
  205. 'fingerprint1234',
  206. 'session',
  207. )
  208. assert_equal(user_device1.id, user_device1_2.id)
  209. # action with different fingerprint -> new device
  210. user_device1_3 = UserDevice.add(
  211. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  212. '91.115.248.231',
  213. @agent.id,
  214. 'fingerprintABC',
  215. 'session',
  216. )
  217. assert_not_equal(user_device1.id, user_device1_3.id)
  218. # signin with without accessable location -> new device
  219. user_device1_4 = UserDevice.add(
  220. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  221. 'not_existing_ip',
  222. @agent.id,
  223. 'fingerprintABC',
  224. 'session',
  225. )
  226. assert_not_equal(user_device1.id, user_device1_4.id)
  227. # action with fingerprint A from country B via session -> new device #2
  228. user_device2 = UserDevice.action(
  229. user_device1.id,
  230. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  231. '176.198.137.254',
  232. @agent.id,
  233. 'session',
  234. )
  235. assert_not_equal(user_device1.id, user_device2.id)
  236. # action with fingerprint A without accessable location -> use current device #2
  237. user_device3 = UserDevice.action(
  238. user_device2.id,
  239. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  240. 'not_existing_ip',
  241. @agent.id,
  242. 'session',
  243. )
  244. assert_equal(user_device2.id, user_device3.id)
  245. end
  246. end