Moved to mock api call for geo_location (for stable tests).

This commit is contained in:
Martin Edenhofer 2017-09-29 18:03:09 +02:00
parent d5033b99bd
commit 1f83f3b358
3 changed files with 60 additions and 3 deletions

View file

@ -47,13 +47,15 @@ class Observer::User::Geo < ActiveRecord::Observer
address = '' address = ''
location = %w(address street zip city country) location = %w(address street zip city country)
location.each { |item| location.each { |item|
if record[item] && record[item] != '' next if record[item].blank?
address = address + ',' + record[item] if address.present?
address += ', '
end end
address += record[item]
} }
# return if no address is given # return if no address is given
return if address == '' return if address.blank?
# lookup # lookup
latlng = Service::GeoLocation.geocode(address) latlng = Service::GeoLocation.geocode(address)

View file

@ -9,6 +9,11 @@ class Service::Image::Zammad
@@total_timeout = 6 @@total_timeout = 6
def self.user(email) def self.user(email)
raise Exceptions::UnprocessableEntity, 'no email given' if email.blank?
email.downcase!
return if email =~ /@example.com$/
# fetch image # fetch image
response = UserAgent.post( response = UserAgent.post(
@ -35,10 +40,14 @@ class Service::Image::Zammad
end end
def self.organization(domain) def self.organization(domain)
raise Exceptions::UnprocessableEntity, 'no domain given' if domain.blank?
# strip, just use domain name # strip, just use domain name
domain = domain.sub(/^.+?@(.+?)$/, '\1') domain = domain.sub(/^.+?@(.+?)$/, '\1')
domain.downcase!
return if domain == 'example.com'
# fetch org logo # fetch org logo
response = UserAgent.post( response = UserAgent.post(
"#{@@api_host}/api/v1/organization/image", "#{@@api_host}/api/v1/organization/image",

View file

@ -1,26 +1,52 @@
# encoding: utf-8 # encoding: utf-8
require 'integration_test_helper' require 'integration_test_helper'
require 'webmock/minitest'
class GeoLocationTest < ActiveSupport::TestCase class GeoLocationTest < ActiveSupport::TestCase
setup do
@mock = true
#WebMock.allow_net_connect!
end
# check # check
test 'check simple results' do test 'check simple results' do
if @mock
stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013,%2010117%20Berlin&sensor=true')
.to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
end
result = Service::GeoLocation.geocode('Marienstrasse 13, 10117 Berlin') result = Service::GeoLocation.geocode('Marienstrasse 13, 10117 Berlin')
assert(result) assert(result)
assert_equal(52.5219143, result[0]) assert_equal(52.5219143, result[0])
assert_equal(13.3832647, result[1]) assert_equal(13.3832647, result[1])
if @mock
stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013%2010117%20Berlin&sensor=true')
.to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
end
result = Service::GeoLocation.geocode('Marienstrasse 13 10117 Berlin') result = Service::GeoLocation.geocode('Marienstrasse 13 10117 Berlin')
assert(result) assert(result)
assert_equal(52.5219143, result[0]) assert_equal(52.5219143, result[0])
assert_equal(13.3832647, result[1]) assert_equal(13.3832647, result[1])
if @mock
stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035,%209016%20St.%20Gallen&sensor=true')
.to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
end
result = Service::GeoLocation.geocode('Martinsbruggstrasse 35, 9016 St. Gallen') result = Service::GeoLocation.geocode('Martinsbruggstrasse 35, 9016 St. Gallen')
assert(result) assert(result)
assert_equal(47.4366557, result[0]) assert_equal(47.4366557, result[0])
assert_equal(9.4098904, result[1]) assert_equal(9.4098904, result[1])
if @mock
stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035%209016%20St.%20Gallen&sensor=true')
.to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
end
result = Service::GeoLocation.geocode('Martinsbruggstrasse 35 9016 St. Gallen') result = Service::GeoLocation.geocode('Martinsbruggstrasse 35 9016 St. Gallen')
assert(result) assert(result)
assert_equal(47.4366557, result[0]) assert_equal(47.4366557, result[0])
@ -30,6 +56,11 @@ class GeoLocationTest < ActiveSupport::TestCase
test 'check user results' do test 'check user results' do
if @mock
stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013%2010117%20Berlin&sensor=true')
.to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
end
user1 = User.create( user1 = User.create(
login: 'some_geo_login1', login: 'some_geo_login1',
firstname: 'First', firstname: 'First',
@ -47,6 +78,11 @@ class GeoLocationTest < ActiveSupport::TestCase
assert_equal(52.5219143, user1.preferences['lat']) assert_equal(52.5219143, user1.preferences['lat'])
assert_equal(13.3832647, user1.preferences['lng']) assert_equal(13.3832647, user1.preferences['lng'])
if @mock
stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013,%2010117,%20Berlin&sensor=true')
.to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
end
user2 = User.create( user2 = User.create(
login: 'some_geo_login2', login: 'some_geo_login2',
firstname: 'First', firstname: 'First',
@ -66,6 +102,11 @@ class GeoLocationTest < ActiveSupport::TestCase
assert_equal(52.5219143, user2.preferences['lat']) assert_equal(52.5219143, user2.preferences['lat'])
assert_equal(13.3832647, user2.preferences['lng']) assert_equal(13.3832647, user2.preferences['lng'])
if @mock
stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035,%209016%20St.%20Gallen&sensor=true')
.to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
end
user3 = User.create( user3 = User.create(
login: 'some_geo_login3', login: 'some_geo_login3',
firstname: 'First', firstname: 'First',
@ -83,6 +124,11 @@ class GeoLocationTest < ActiveSupport::TestCase
assert_equal(47.4366557, user3.preferences['lat']) assert_equal(47.4366557, user3.preferences['lat'])
assert_equal(9.4098904, user3.preferences['lng']) assert_equal(9.4098904, user3.preferences['lng'])
if @mock
stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035,%209016,%20St.%20Gallen&sensor=true')
.to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
end
user4 = User.create( user4 = User.create(
login: 'some_geo_login4', login: 'some_geo_login4',
firstname: 'First', firstname: 'First',