geo_location_spec.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::GeoLocation do
  4. describe '#geocode' do
  5. subject(:geocode) { described_class.geocode(address) }
  6. context 'when checking simple results' do
  7. let(:address) { 'Marienstrasse 13, 10117 Berlin' }
  8. let(:latitude) { 52.5220514 }
  9. let(:longitude) { 13.3832091 }
  10. let(:result) { [latitude, longitude] }
  11. before do
  12. allow(Service::GeoLocation::Osm).to receive(:geocode).and_return(result)
  13. end
  14. it { is_expected.to eq(result) }
  15. context 'when address field in user preferences is filled' do
  16. let(:user) { create(:user, address: address) }
  17. it 'stores correct values for latitude + longitude' do
  18. expect(user.preferences).to include({ 'lat' => latitude, 'lng' => longitude })
  19. end
  20. end
  21. context 'when street, city and zip fields in user preferences are filled' do
  22. let(:address) { 'Marienstrasse 13, 10117, Berlin' }
  23. let(:address_parts) { address.split(%r{\.?\s+}, 4) }
  24. let(:street) { "#{address_parts.first} #{address_parts[1].chop}" }
  25. let(:zip) { address_parts[2].chop }
  26. let(:city) { address_parts.last }
  27. let(:user) { create(:user, street: street, zip: zip, city: city) }
  28. it 'stores correct values for latitude + longitude' do
  29. expect(user.preferences).to include({ 'lat' => latitude, 'lng' => longitude })
  30. end
  31. end
  32. end
  33. end
  34. describe '#reverse_geocode' do
  35. subject(:reverse_geocode) { described_class.reverse_geocode(latitude, longitude) }
  36. context 'when checking simple results' do
  37. let(:latitude) { 52.5220514 }
  38. let(:longitude) { 13.3832091 }
  39. let(:result) { '13, Marienstraße, Dorotheenstadt, Mitte, Berlin, 10117, Deutschland' }
  40. before do
  41. allow(Service::GeoLocation::Osm).to receive(:reverse_geocode).and_return(result)
  42. end
  43. it { is_expected.to eq(result) }
  44. end
  45. end
  46. end