mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-09 08:47:11 -05:00
Fix some tests
This commit is contained in:
parent
01df22d080
commit
ebd0f8d6bc
22 changed files with 120 additions and 75 deletions
|
|
@ -15,7 +15,6 @@ class Place < ApplicationRecord
|
|||
before_validation :build_lonlat, if: -> { latitude.present? && longitude.present? }
|
||||
|
||||
validates :name, presence: true
|
||||
validates :latitude, :longitude, presence: true
|
||||
validates :lonlat, presence: true
|
||||
|
||||
enum :source, { manual: 0, photon: 1 }
|
||||
|
|
|
|||
27
app/serializers/api/place_serializer.rb
Normal file
27
app/serializers/api/place_serializer.rb
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Api::PlaceSerializer
|
||||
def initialize(place)
|
||||
@place = place
|
||||
end
|
||||
|
||||
def call
|
||||
{
|
||||
id: place.id,
|
||||
name: place.name,
|
||||
longitude: place.lon,
|
||||
latitude: place.lat,
|
||||
city: place.city,
|
||||
country: place.country,
|
||||
source: place.source,
|
||||
geodata: place.geodata,
|
||||
created_at: place.created_at,
|
||||
updated_at: place.updated_at,
|
||||
reverse_geocoded_at: place.reverse_geocoded_at
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :place
|
||||
end
|
||||
|
|
@ -174,7 +174,6 @@ module Visits
|
|||
# Create new place
|
||||
place = user.places.build(
|
||||
name: name,
|
||||
lonlat: "POINT(#{result.longitude} #{result.latitude})",
|
||||
latitude: result.latitude,
|
||||
longitude: result.longitude,
|
||||
city: properties['city'],
|
||||
|
|
|
|||
|
|
@ -5,10 +5,7 @@ require 'rails_helper'
|
|||
RSpec.describe 'Api::V1::Maps::Hexagons', type: :request do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
describe 'GET /api/v1/maps/hexagons' do
|
||||
let(:valid_params) do
|
||||
|
|
|
|||
|
|
@ -5,13 +5,6 @@ require 'rails_helper'
|
|||
RSpec.describe 'Authentication', type: :request do
|
||||
let(:user) { create(:user, password: 'password123') }
|
||||
|
||||
before do
|
||||
stub_request(:get, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => /.*/,
|
||||
'Host' => 'api.github.com', 'User-Agent' => /.*/ })
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
describe 'Route Protection' do
|
||||
it 'redirects to sign in page when accessing protected routes while signed out' do
|
||||
get map_path
|
||||
|
|
|
|||
|
|
@ -6,11 +6,6 @@ RSpec.describe '/exports', type: :request do
|
|||
let(:user) { create(:user) }
|
||||
let(:params) { { start_at: 1.day.ago, end_at: Time.zone.now } }
|
||||
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
describe 'GET /index' do
|
||||
context 'when user is not logged in' do
|
||||
it 'redirects to the login page' do
|
||||
|
|
|
|||
|
|
@ -8,10 +8,7 @@ RSpec.describe 'Family::Invitations', type: :request do
|
|||
let!(:membership) { create(:family_membership, user: user, family: family, role: :owner) }
|
||||
let(:invitation) { create(:family_invitation, family: family, invited_by: user) }
|
||||
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
describe 'GET /family/invitations' do
|
||||
before { sign_in user }
|
||||
|
|
|
|||
|
|
@ -7,10 +7,7 @@ RSpec.describe 'Family Workflows', type: :request do
|
|||
let(:user2) { create(:user, email: 'bob@example.com') }
|
||||
let(:user3) { create(:user, email: 'charlie@example.com') }
|
||||
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
describe 'Complete family creation and management workflow' do
|
||||
it 'allows creating a family, inviting members, and managing the family' do
|
||||
|
|
|
|||
|
|
@ -3,10 +3,7 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Imports', type: :request do
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
describe 'GET /imports' do
|
||||
context 'when user is logged in' do
|
||||
|
|
@ -63,7 +60,7 @@ RSpec.describe 'Imports', type: :request do
|
|||
|
||||
it 'prevents viewing other users import' do
|
||||
get import_path(other_import)
|
||||
|
||||
|
||||
expect(response).to redirect_to(root_path)
|
||||
expect(flash[:alert]).to eq('You are not authorized to perform this action.')
|
||||
end
|
||||
|
|
@ -100,7 +97,7 @@ RSpec.describe 'Imports', type: :request do
|
|||
|
||||
it 'prevents access to new import form' do
|
||||
get new_import_path
|
||||
|
||||
|
||||
expect(response).to redirect_to(root_path)
|
||||
expect(flash[:alert]).to eq('You are not authorized to perform this action.')
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,10 +3,7 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Map', type: :request do
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
describe 'GET /index' do
|
||||
context 'when user signed in' do
|
||||
|
|
|
|||
|
|
@ -3,10 +3,7 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe '/notifications', type: :request do
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
context 'when user is not logged in' do
|
||||
it 'redirects to the login page' do
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ RSpec.describe '/places', type: :request do
|
|||
end
|
||||
|
||||
describe 'DELETE /destroy' do
|
||||
let!(:place) { create(:place) }
|
||||
let!(:place) { create(:place, user:) }
|
||||
let!(:visit) { create(:visit, place:, user:) }
|
||||
|
||||
it 'destroys the requested place' do
|
||||
|
|
|
|||
|
|
@ -3,10 +3,7 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe '/settings/background_jobs', type: :request do
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
context 'when Dawarich is in self-hosted mode' do
|
||||
before do
|
||||
|
|
|
|||
|
|
@ -3,10 +3,7 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'settings/maps', type: :request do
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
context 'when user is authenticated' do
|
||||
let!(:user) { create(:user) }
|
||||
|
|
|
|||
|
|
@ -3,10 +3,7 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Shared::Stats', type: :request do
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
context 'public sharing' do
|
||||
let(:user) { create(:user) }
|
||||
|
|
|
|||
|
|
@ -3,10 +3,7 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe '/stats', type: :request do
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
context 'when user is not signed in' do
|
||||
describe 'GET /index' do
|
||||
|
|
|
|||
|
|
@ -10,10 +10,7 @@ RSpec.describe 'Users::Registrations', type: :request do
|
|||
create(:family_invitation, family: family, invited_by: family_owner, email: 'invited@example.com')
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
describe 'Family Invitation Registration Flow' do
|
||||
context 'when accessing registration with a valid invitation token' do
|
||||
|
|
|
|||
|
|
@ -3,10 +3,7 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Users', type: :request do
|
||||
before do
|
||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||
end
|
||||
|
||||
|
||||
describe 'GET /users/sign_up' do
|
||||
context 'when self-hosted' do
|
||||
|
|
|
|||
67
spec/serializers/api/place_serializer_spec.rb
Normal file
67
spec/serializers/api/place_serializer_spec.rb
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Api::PlaceSerializer do
|
||||
describe '#call' do
|
||||
let(:place) do
|
||||
create(
|
||||
:place,
|
||||
:with_geodata,
|
||||
name: 'Central Park',
|
||||
longitude: -73.9665,
|
||||
latitude: 40.7812,
|
||||
city: 'New York',
|
||||
country: 'United States',
|
||||
source: 'photon',
|
||||
geodata: { 'amenity' => 'park', 'leisure' => 'park' },
|
||||
reverse_geocoded_at: Time.zone.parse('2023-01-15T12:00:00Z')
|
||||
)
|
||||
end
|
||||
|
||||
subject(:serializer) { described_class.new(place) }
|
||||
|
||||
it 'serializes a place into a hash with all attributes' do
|
||||
result = serializer.call
|
||||
|
||||
expect(result).to be_a(Hash)
|
||||
expect(result[:id]).to eq(place.id)
|
||||
expect(result[:name]).to eq('Central Park')
|
||||
expect(result[:longitude]).to eq(-73.9665)
|
||||
expect(result[:latitude]).to eq(40.7812)
|
||||
expect(result[:city]).to eq('New York')
|
||||
expect(result[:country]).to eq('United States')
|
||||
expect(result[:source]).to eq('photon')
|
||||
expect(result[:geodata]).to eq({ 'amenity' => 'park', 'leisure' => 'park' })
|
||||
expect(result[:reverse_geocoded_at]).to eq(Time.zone.parse('2023-01-15T12:00:00Z'))
|
||||
end
|
||||
|
||||
context 'with nil values' do
|
||||
let(:place_with_nils) do
|
||||
create(
|
||||
:place,
|
||||
name: 'Unknown Place',
|
||||
city: nil,
|
||||
country: nil,
|
||||
source: nil,
|
||||
geodata: {},
|
||||
reverse_geocoded_at: nil
|
||||
)
|
||||
end
|
||||
|
||||
subject(:serializer_with_nils) { described_class.new(place_with_nils) }
|
||||
|
||||
it 'handles nil values correctly' do
|
||||
result = serializer_with_nils.call
|
||||
|
||||
expect(result[:id]).to eq(place_with_nils.id)
|
||||
expect(result[:name]).to eq('Unknown Place')
|
||||
expect(result[:city]).to be_nil
|
||||
expect(result[:country]).to be_nil
|
||||
expect(result[:source]).to be_nil
|
||||
expect(result[:geodata]).to eq({})
|
||||
expect(result[:reverse_geocoded_at]).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -20,7 +20,7 @@ RSpec.describe Visits::PlaceFinder do
|
|||
end
|
||||
|
||||
context 'when an existing place is found' do
|
||||
let!(:existing_place) { create(:place, latitude: latitude, longitude: longitude) }
|
||||
let!(:existing_place) { create(:place, user: user, latitude: latitude, longitude: longitude) }
|
||||
|
||||
it 'returns the existing place as main_place' do
|
||||
result = subject.find_or_create_place(visit_data)
|
||||
|
|
@ -38,6 +38,7 @@ RSpec.describe Visits::PlaceFinder do
|
|||
|
||||
it 'finds an existing place by name within search radius' do
|
||||
similar_named_place = create(:place,
|
||||
user: user,
|
||||
name: 'Test Place',
|
||||
latitude: latitude + 0.0001,
|
||||
longitude: longitude + 0.0001)
|
||||
|
|
@ -183,9 +184,9 @@ RSpec.describe Visits::PlaceFinder do
|
|||
end
|
||||
|
||||
context 'with multiple potential places' do
|
||||
let!(:place1) { create(:place, name: 'Place 1', latitude: latitude, longitude: longitude) }
|
||||
let!(:place2) { create(:place, name: 'Place 2', latitude: latitude + 0.0005, longitude: longitude + 0.0005) }
|
||||
let!(:place3) { create(:place, name: 'Place 3', latitude: latitude + 0.001, longitude: longitude + 0.001) }
|
||||
let!(:place1) { create(:place, user: user, name: 'Place 1', latitude: latitude, longitude: longitude) }
|
||||
let!(:place2) { create(:place, user: user, name: 'Place 2', latitude: latitude + 0.0005, longitude: longitude + 0.0005) }
|
||||
let!(:place3) { create(:place, user: user, name: 'Place 3', latitude: latitude + 0.001, longitude: longitude + 0.001) }
|
||||
|
||||
it 'selects the closest place as main_place' do
|
||||
result = subject.find_or_create_place(visit_data)
|
||||
|
|
@ -201,7 +202,7 @@ RSpec.describe Visits::PlaceFinder do
|
|||
end
|
||||
|
||||
it 'may include places with the same name' do
|
||||
dup_place = create(:place, name: 'Place 1', latitude: latitude + 0.0002, longitude: longitude + 0.0002)
|
||||
dup_place = create(:place, user: user, name: 'Place 1', latitude: latitude + 0.0002, longitude: longitude + 0.0002)
|
||||
|
||||
allow(subject).to receive(:place_name_exists?).and_return(false)
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,9 @@ RSpec.configure do |config|
|
|||
'state' => 'New York',
|
||||
'name' => 'Test Location'
|
||||
}
|
||||
}
|
||||
},
|
||||
latitude: 40.7128,
|
||||
longitude: -74.0060
|
||||
)
|
||||
]
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Stub GitHub API requests in tests
|
||||
RSpec.configure do |config|
|
||||
config.before(:each) do
|
||||
# Stub GitHub API version checking
|
||||
stub_request(:get, "https://api.github.com/repos/Freika/dawarich/tags")
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: [{ name: "v0.1.0" }].to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
body: '[{"name": "1.0.0"}]',
|
||||
headers: {}
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue