dawarich/spec/services/users/import_data/places_spec.rb
Evgenii Burmakin b1393ee674
0.36.0 (#1952)
* Implement OmniAuth GitHub authentication

* Fix omniauth GitHub scope to include user email access

* Remove margin-bottom

* Implement Google OAuth2 authentication

* Implement OIDC authentication for Dawarich using omniauth_openid_connect gem.

* Add patreon account linking and patron checking service

* Update docker-compose.yml to use boolean values instead of strings

* Add support for KML files

* Add tests

* Update changelog

* Remove patreon OAuth integration

* Move omniauthable to a concern

* Update an icon in integrations

* Update changelog

* Update app version

* Fix family location sharing toggle

* Move family location sharing to its own controller

* Update changelog

* Implement basic tagging functionality for places, allowing users to categorize and label places with custom tags.

* Add places management API and tags feature

* Add some changes related to places management feature

* Fix some tests

* Fix sometests

* Add places layer

* Update places layer to use Leaflet.Control.Layers.Tree for hierarchical layer control

* Rework tag form

* Add hashtag

* Add privacy zones to tags

* Add notes to places and manage place tags

* Update changelog

* Update e2e tests

* Extract tag serializer to its own file

* Fix some tests

* Fix tags request specs

* Fix some tests

* Fix rest of the tests

* Revert some changes

* Add missing specs

* Revert changes in place export/import code

* Fix some specs

* Fix PlaceFinder to only consider global places when finding existing places

* Fix few more specs

* Fix visits creator spec

* Fix last tests

* Update place creating modal

* Add home location based on "Home" tagged place

* Save enabled tag layers

* Some fixes

* Fix bug where enabling place tag layers would trigger saving enabled layers, overwriting with incomplete data

* Update migration to use disable_ddl_transaction! and add up/down methods

* Fix tag layers restoration and filtering logic

* Update OIDC auto-registration and email/password registration settings

* Fix potential xss
2025-11-24 19:45:09 +01:00

210 lines
6.4 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Users::ImportData::Places, type: :service do
let(:user) { create(:user) }
let(:places_data) do
[
{
'name' => 'Home',
'latitude' => '40.7128',
'longitude' => '-74.0060',
'source' => 'manual',
'geodata' => { 'address' => '123 Main St' },
'created_at' => '2024-01-01T00:00:00Z',
'updated_at' => '2024-01-01T00:00:00Z'
},
{
'name' => 'Office',
'latitude' => '40.7589',
'longitude' => '-73.9851',
'source' => 'photon',
'geodata' => { 'properties' => { 'name' => 'Office Building' } },
'created_at' => '2024-01-02T00:00:00Z',
'updated_at' => '2024-01-02T00:00:00Z'
}
]
end
let(:service) { described_class.new(user, places_data) }
describe '#call' do
context 'with valid places data' do
it 'creates new places' do
expect { service.call }.to change { Place.count }.by(2)
end
it 'creates places with correct attributes' do
service.call
home_place = Place.find_by(name: 'Home')
expect(home_place).to have_attributes(
name: 'Home',
source: 'manual'
)
expect(home_place.lat).to be_within(0.0001).of(40.7128)
expect(home_place.lon).to be_within(0.0001).of(-74.0060)
expect(home_place.geodata).to eq('address' => '123 Main St')
office_place = Place.find_by(name: 'Office')
expect(office_place).to have_attributes(
name: 'Office',
source: 'photon'
)
expect(office_place.lat).to be_within(0.0001).of(40.7589)
expect(office_place.lon).to be_within(0.0001).of(-73.9851)
expect(office_place.geodata).to eq('properties' => { 'name' => 'Office Building' })
end
it 'returns the number of places created' do
result = service.call
expect(result).to eq(2)
end
end
context 'with duplicate places (same name)' do
before do
# Create an existing place with same name but different coordinates
create(:place, name: 'Home',
latitude: 41.0000, longitude: -75.0000,
lonlat: 'POINT(-75.0000 41.0000)')
end
it 'creates the place since coordinates are different' do
expect { service.call }.to change { Place.count }.by(2)
end
it 'creates both places with different coordinates' do
service.call
home_places = Place.where(name: 'Home')
expect(home_places.count).to eq(2)
imported_home = home_places.find_by(latitude: 40.7128, longitude: -74.0060)
expect(imported_home).to be_present
end
end
context 'with exact duplicate places (same name and coordinates)' do
before do
# Create an existing place with exact same name and coordinates
create(:place, name: 'Home',
latitude: 40.7128, longitude: -74.0060,
lonlat: 'POINT(-74.0060 40.7128)')
end
it 'skips exact duplicate places' do
expect { service.call }.to change { Place.count }.by(1)
end
it 'returns only the count of newly created places' do
result = service.call
expect(result).to eq(1)
end
end
context 'with duplicate places (same coordinates)' do
before do
# Create an existing place with same coordinates but different name
create(:place, name: 'Different Name',
latitude: 40.7128, longitude: -74.0060,
lonlat: 'POINT(-74.0060 40.7128)')
end
it 'creates the place since name is different' do
expect { service.call }.to change { Place.global.count }.by(2)
end
it 'creates both places with different names' do
service.call
places_at_location = Place.where(latitude: 40.7128, longitude: -74.0060, user_id: nil)
expect(places_at_location.count).to eq(2)
expect(places_at_location.pluck(:name)).to contain_exactly('Home', 'Different Name')
end
end
context 'with places having same name but different coordinates' do
before do
create(:place, name: 'Different Place',
latitude: 41.0000, longitude: -75.0000,
lonlat: 'POINT(-75.0000 41.0000)')
end
it 'creates both places since coordinates and names differ' do
expect { service.call }.to change { Place.count }.by(2)
end
end
context 'with invalid place data' do
let(:places_data) do
[
{ 'name' => 'Valid Place', 'latitude' => '40.7128', 'longitude' => '-74.0060' },
'invalid_data',
{ 'name' => 'Another Valid Place', 'latitude' => '40.7589', 'longitude' => '-73.9851' }
]
end
it 'skips invalid entries and imports valid ones' do
expect { service.call }.to change { Place.count }.by(2)
end
it 'returns the count of valid places created' do
result = service.call
expect(result).to eq(2)
end
end
context 'with missing required fields' do
let(:places_data) do
[
{ 'name' => 'Valid Place', 'latitude' => '40.7128', 'longitude' => '-74.0060' },
{ 'latitude' => '40.7589', 'longitude' => '-73.9851' }, # missing name
{ 'name' => 'Invalid Place', 'longitude' => '-73.9851' }, # missing latitude
{ 'name' => 'Another Invalid Place', 'latitude' => '40.7589' } # missing longitude
]
end
it 'only creates places with all required fields' do
expect { service.call }.to change { Place.count }.by(1)
end
end
context 'with nil places data' do
let(:places_data) { nil }
it 'does not create any places' do
expect { service.call }.not_to change { Place.count }
end
it 'returns 0' do
result = service.call
expect(result).to eq(0)
end
end
context 'with non-array places data' do
let(:places_data) { 'invalid_data' }
it 'does not create any places' do
expect { service.call }.not_to change { Place.count }
end
it 'returns 0' do
result = service.call
expect(result).to eq(0)
end
end
context 'with empty places data' do
let(:places_data) { [] }
it 'does not create any places' do
expect { service.call }.not_to change { Place.count }
end
it 'returns 0' do
result = service.call
expect(result).to eq(0)
end
end
end
end