geo_location_test.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'integration_test_helper'
  3. require 'webmock/minitest'
  4. class GeoLocationTest < ActiveSupport::TestCase
  5. setup do
  6. @mock = true
  7. # WebMock.allow_net_connect!
  8. end
  9. # check
  10. test 'check simple results' do
  11. if @mock
  12. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013,%2010117%20Berlin&sensor=true')
  13. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
  14. end
  15. result = Service::GeoLocation.geocode('Marienstrasse 13, 10117 Berlin')
  16. assert(result)
  17. assert_equal(52.5219143, result[0])
  18. assert_equal(13.3832647, result[1])
  19. if @mock
  20. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013%2010117%20Berlin&sensor=true')
  21. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
  22. end
  23. result = Service::GeoLocation.geocode('Marienstrasse 13 10117 Berlin')
  24. assert(result)
  25. assert_equal(52.5219143, result[0])
  26. assert_equal(13.3832647, result[1])
  27. if @mock
  28. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035,%209016%20St.%20Gallen&sensor=true')
  29. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
  30. end
  31. result = Service::GeoLocation.geocode('Martinsbruggstrasse 35, 9016 St. Gallen')
  32. assert(result)
  33. assert_equal(47.4366557, result[0])
  34. assert_equal(9.4098904, result[1])
  35. if @mock
  36. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035%209016%20St.%20Gallen&sensor=true')
  37. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
  38. end
  39. result = Service::GeoLocation.geocode('Martinsbruggstrasse 35 9016 St. Gallen')
  40. assert(result)
  41. assert_equal(47.4366557, result[0])
  42. assert_equal(9.4098904, result[1])
  43. end
  44. test 'check user results' do
  45. if @mock
  46. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013%2010117%20Berlin&sensor=true')
  47. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
  48. end
  49. user1 = User.create(
  50. login: 'some_geo_login1',
  51. firstname: 'First',
  52. lastname: 'Last',
  53. email: 'some_geo_login1@example.com',
  54. password: 'test',
  55. address: 'Marienstrasse 13 10117 Berlin',
  56. active: false,
  57. updated_by_id: 1,
  58. created_by_id: 1
  59. )
  60. assert(user1.preferences)
  61. assert(user1.preferences['lat'])
  62. assert(user1.preferences['lng'])
  63. assert_equal(52.5219143, user1.preferences['lat'])
  64. assert_equal(13.3832647, user1.preferences['lng'])
  65. if @mock
  66. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013,%2010117,%20Berlin&sensor=true')
  67. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
  68. end
  69. user2 = User.create(
  70. login: 'some_geo_login2',
  71. firstname: 'First',
  72. lastname: 'Last',
  73. email: 'some_geo_login2@example.com',
  74. password: 'test',
  75. street: 'Marienstrasse 13',
  76. city: 'Berlin',
  77. zip: '10117',
  78. active: false,
  79. updated_by_id: 1,
  80. created_by_id: 1
  81. )
  82. assert(user2.preferences)
  83. assert(user2.preferences['lat'])
  84. assert(user2.preferences['lng'])
  85. assert_equal(52.5219143, user2.preferences['lat'])
  86. assert_equal(13.3832647, user2.preferences['lng'])
  87. if @mock
  88. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035,%209016%20St.%20Gallen&sensor=true')
  89. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
  90. end
  91. user3 = User.create(
  92. login: 'some_geo_login3',
  93. firstname: 'First',
  94. lastname: 'Last',
  95. email: 'some_geo_login3@example.com',
  96. password: 'test',
  97. address: 'Martinsbruggstrasse 35, 9016 St. Gallen',
  98. active: false,
  99. updated_by_id: 1,
  100. created_by_id: 1
  101. )
  102. assert(user3.preferences)
  103. assert(user3.preferences['lat'])
  104. assert(user3.preferences['lng'])
  105. assert_equal(47.4366557, user3.preferences['lat'])
  106. assert_equal(9.4098904, user3.preferences['lng'])
  107. if @mock
  108. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035,%209016,%20St.%20Gallen&sensor=true')
  109. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
  110. end
  111. user4 = User.create(
  112. login: 'some_geo_login4',
  113. firstname: 'First',
  114. lastname: 'Last',
  115. email: 'some_geo_login4@example.com',
  116. password: 'test',
  117. street: 'Martinsbruggstrasse 35',
  118. city: 'St. Gallen',
  119. zip: '9016',
  120. active: false,
  121. updated_by_id: 1,
  122. created_by_id: 1
  123. )
  124. assert(user4.preferences)
  125. assert(user4.preferences['lat'])
  126. assert(user4.preferences['lng'])
  127. assert_equal(47.4366557, user4.preferences['lat'])
  128. assert_equal(9.4098904, user4.preferences['lng'])
  129. end
  130. end