From 1f83f3b35813efc06ca27ac784cec4fe42bb7cc4 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 29 Sep 2017 18:03:09 +0200 Subject: [PATCH] Moved to mock api call for geo_location (for stable tests). --- app/models/observer/user/geo.rb | 8 +++-- lib/service/image/zammad.rb | 9 ++++++ test/integration/geo_location_test.rb | 46 +++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/app/models/observer/user/geo.rb b/app/models/observer/user/geo.rb index f78327f10..b4bbd97c3 100644 --- a/app/models/observer/user/geo.rb +++ b/app/models/observer/user/geo.rb @@ -47,13 +47,15 @@ class Observer::User::Geo < ActiveRecord::Observer address = '' location = %w(address street zip city country) location.each { |item| - if record[item] && record[item] != '' - address = address + ',' + record[item] + next if record[item].blank? + if address.present? + address += ', ' end + address += record[item] } # return if no address is given - return if address == '' + return if address.blank? # lookup latlng = Service::GeoLocation.geocode(address) diff --git a/lib/service/image/zammad.rb b/lib/service/image/zammad.rb index 69003529b..22c93bcd5 100644 --- a/lib/service/image/zammad.rb +++ b/lib/service/image/zammad.rb @@ -9,6 +9,11 @@ class Service::Image::Zammad @@total_timeout = 6 def self.user(email) + raise Exceptions::UnprocessableEntity, 'no email given' if email.blank? + + email.downcase! + + return if email =~ /@example.com$/ # fetch image response = UserAgent.post( @@ -35,10 +40,14 @@ class Service::Image::Zammad end def self.organization(domain) + raise Exceptions::UnprocessableEntity, 'no domain given' if domain.blank? # strip, just use domain name domain = domain.sub(/^.+?@(.+?)$/, '\1') + domain.downcase! + return if domain == 'example.com' + # fetch org logo response = UserAgent.post( "#{@@api_host}/api/v1/organization/image", diff --git a/test/integration/geo_location_test.rb b/test/integration/geo_location_test.rb index 8bd148a9a..88e6d1152 100644 --- a/test/integration/geo_location_test.rb +++ b/test/integration/geo_location_test.rb @@ -1,26 +1,52 @@ # encoding: utf-8 require 'integration_test_helper' +require 'webmock/minitest' class GeoLocationTest < ActiveSupport::TestCase + setup do + @mock = true + #WebMock.allow_net_connect! + end + # check 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') assert(result) assert_equal(52.5219143, result[0]) 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') assert(result) assert_equal(52.5219143, result[0]) 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') assert(result) assert_equal(47.4366557, result[0]) 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') assert(result) assert_equal(47.4366557, result[0]) @@ -30,6 +56,11 @@ class GeoLocationTest < ActiveSupport::TestCase 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( login: 'some_geo_login1', firstname: 'First', @@ -47,6 +78,11 @@ class GeoLocationTest < ActiveSupport::TestCase assert_equal(52.5219143, user1.preferences['lat']) 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( login: 'some_geo_login2', firstname: 'First', @@ -66,6 +102,11 @@ class GeoLocationTest < ActiveSupport::TestCase assert_equal(52.5219143, user2.preferences['lat']) 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( login: 'some_geo_login3', firstname: 'First', @@ -83,6 +124,11 @@ class GeoLocationTest < ActiveSupport::TestCase assert_equal(47.4366557, user3.preferences['lat']) 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( login: 'some_geo_login4', firstname: 'First',