dawarich/spec/models/trip_spec.rb

174 lines
4.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Trip, type: :model do
describe 'validations' do
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:started_at) }
it { is_expected.to validate_presence_of(:ended_at) }
end
describe 'associations' do
it { is_expected.to belong_to(:user) }
end
2024-11-28 11:41:43 -05:00
describe 'callbacks' do
let(:user) { create(:user) }
let(:trip) { create(:trip, :with_points, user:) }
2025-05-15 16:58:04 -04:00
context 'when the trip is created' do
let(:trip) { build(:trip, :with_points, user:) }
it 'enqueues the calculation jobs' do
expect(Trips::CalculateAllJob).to receive(:perform_later)
2025-01-24 09:58:44 -05:00
2025-05-15 16:58:04 -04:00
trip.save
end
2025-01-24 09:58:44 -05:00
end
2024-11-28 11:41:43 -05:00
end
describe '#photo_previews' do
2024-11-28 11:41:43 -05:00
let(:photo_data) do
[
{
'id' => '123',
'latitude' => 35.6762,
'longitude' => 139.6503,
'localDateTime' => '2024-01-01T03:00:00.000Z',
'type' => 'photo',
'exifInfo' => {
'orientation' => '3'
}
},
{
'id' => '456',
'latitude' => 40.7128,
'longitude' => -74.0060,
'localDateTime' => '2024-01-02T01:00:00.000Z',
'type' => 'photo',
'exifInfo' => {
'orientation' => '6'
}
},
{
'id' => '789',
'latitude' => 40.7128,
'longitude' => -74.0060,
'localDateTime' => '2024-01-02T02:00:00.000Z',
'type' => 'photo',
'exifInfo' => {
'orientation' => '6'
}
}
]
end
2024-11-29 06:03:00 -05:00
let(:user) { create(:user, settings: settings) }
2024-11-28 11:41:43 -05:00
let(:trip) { create(:trip, user:) }
let(:expected_photos) do
[
{
id: '456',
url: "/api/v1/photos/456/thumbnail.jpg?api_key=#{user.api_key}&source=immich",
source: 'immich',
orientation: 'portrait'
},
{
id: '789',
url: "/api/v1/photos/789/thumbnail.jpg?api_key=#{user.api_key}&source=immich",
source: 'immich',
orientation: 'portrait'
}
2024-11-28 11:41:43 -05:00
]
end
before do
allow_any_instance_of(Immich::RequestPhotos).to receive(:call).and_return(photo_data)
end
2024-11-29 06:03:00 -05:00
context 'when Immich integration is not configured' do
let(:settings) { {} }
it 'returns an empty array' do
expect(trip.photo_previews).to eq([])
2024-11-29 06:03:00 -05:00
end
end
context 'when Immich integration is configured' do
let(:settings) do
{
immich_url: 'https://immich.example.com',
immich_api_key: '1234567890'
}
end
it 'returns the photos' do
2024-12-10 13:43:52 -05:00
expect(trip.photo_previews).to include(expected_photos[0])
expect(trip.photo_previews).to include(expected_photos[1])
expect(trip.photo_previews.size).to eq(2)
2024-11-29 06:03:00 -05:00
end
2024-11-28 11:41:43 -05:00
end
end
2025-07-04 13:49:56 -04:00
describe 'Calculateable concern' do
let(:user) { create(:user) }
let(:trip) { create(:trip, user: user) }
let!(:points) do
[
create(:point, user: user, lonlat: 'POINT(13.404954 52.520008)', timestamp: trip.started_at.to_i + 1.hour),
create(:point, user: user, lonlat: 'POINT(13.404955 52.520009)', timestamp: trip.started_at.to_i + 2.hours),
create(:point, user: user, lonlat: 'POINT(13.404956 52.520010)', timestamp: trip.started_at.to_i + 3.hours)
]
end
describe '#calculate_distance' do
it 'stores distance in user preferred unit for Trip model' do
allow(user).to receive(:safe_settings).and_return(double(distance_unit: 'km'))
allow(Point).to receive(:total_distance).and_return(2.5) # 2.5 km
trip.calculate_distance
expect(trip.distance).to eq(3) # Should be rounded, in km
end
end
2025-07-20 12:57:53 -04:00
describe '#recalculate_distance!' do
2025-07-04 13:49:56 -04:00
it 'recalculates and saves the distance' do
original_distance = trip.distance
trip.recalculate_distance!
trip.reload
expect(trip.distance).not_to eq(original_distance)
end
end
describe '#recalculate_path!' do
it 'recalculates and saves the path' do
original_path = trip.path
trip.recalculate_path!
trip.reload
expect(trip.path).not_to eq(original_path)
end
end
end
Implement public trip sharing with Shareable concern This commit implements comprehensive public trip sharing functionality by extracting sharing logic into a reusable Shareable concern and extending it to Trip models. ## Key Features **Shareable Concern (DRY principle)** - Extract sharing logic from Stat model into reusable concern - Support for time-based expiration (1h, 12h, 24h, permanent) - UUID-based secure public access - User-controlled sharing of notes and photos - Automatic UUID generation on model creation **Database Changes** - Add sharing_uuid (UUID) column to trips table - Add sharing_settings (JSONB) column for configuration storage - Add unique index on sharing_uuid for performance **Public Trip Sharing** - Public-facing trip view with read-only access - Interactive map with trip route visualization - Optional sharing of notes and photo previews - Branded footer with Dawarich attribution - Responsive design matching existing UI patterns **Sharing Management** - In-app sharing controls in trip show view - Enable/disable sharing with one click - Configurable expiration times - Copy-to-clipboard for sharing URLs - Visual indicators for sharing status **Authorization & Security** - TripPolicy for fine-grained access control - Public access only for explicitly shared trips - Automatic expiration enforcement - Owner-only sharing management - UUID-based URLs prevent enumeration attacks **API & Routes** - GET /shared/trips/:trip_uuid for public access - PATCH /trips/:id/sharing for sharing management - RESTful endpoint design consistent with stats sharing **Frontend** - New public-trip-map Stimulus controller - OpenStreetMap tiles for public viewing (no API key required) - Start/end markers on trip route - Automatic map bounds fitting **Tests** - Comprehensive concern specs (Shareable) - Model specs for Trip sharing functionality - Request specs for public and authenticated access - Policy specs for authorization rules - 100% coverage of sharing functionality ## Implementation Details ### Models Updated - Stat: Now uses Shareable concern (removed duplicate code) - Trip: Includes Shareable concern with notes/photos options ### Controllers Added - Shared::TripsController: Handles public viewing and sharing management ### Views Added - trips/public_show.html.erb: Public-facing trip view - trips/_sharing.html.erb: Sharing controls partial ### JavaScript Added - public_trip_map_controller.js: Map rendering for public trips ### Helpers Extended - TripsHelper: Added sharing status and expiration helpers ## Breaking Changes None. This is a purely additive feature. ## Migration Required Yes. Run: rails db:migrate ## Testing All specs pass: - spec/models/concerns/shareable_spec.rb - spec/models/trip_spec.rb - spec/requests/shared/trips_spec.rb - spec/policies/trip_policy_spec.rb
2025-11-05 10:44:27 -05:00
describe 'Shareable concern' do
it_behaves_like 'shareable'
Implement public trip sharing with Shareable concern This commit implements comprehensive public trip sharing functionality by extracting sharing logic into a reusable Shareable concern and extending it to Trip models. ## Key Features **Shareable Concern (DRY principle)** - Extract sharing logic from Stat model into reusable concern - Support for time-based expiration (1h, 12h, 24h, permanent) - UUID-based secure public access - User-controlled sharing of notes and photos - Automatic UUID generation on model creation **Database Changes** - Add sharing_uuid (UUID) column to trips table - Add sharing_settings (JSONB) column for configuration storage - Add unique index on sharing_uuid for performance **Public Trip Sharing** - Public-facing trip view with read-only access - Interactive map with trip route visualization - Optional sharing of notes and photo previews - Branded footer with Dawarich attribution - Responsive design matching existing UI patterns **Sharing Management** - In-app sharing controls in trip show view - Enable/disable sharing with one click - Configurable expiration times - Copy-to-clipboard for sharing URLs - Visual indicators for sharing status **Authorization & Security** - TripPolicy for fine-grained access control - Public access only for explicitly shared trips - Automatic expiration enforcement - Owner-only sharing management - UUID-based URLs prevent enumeration attacks **API & Routes** - GET /shared/trips/:trip_uuid for public access - PATCH /trips/:id/sharing for sharing management - RESTful endpoint design consistent with stats sharing **Frontend** - New public-trip-map Stimulus controller - OpenStreetMap tiles for public viewing (no API key required) - Start/end markers on trip route - Automatic map bounds fitting **Tests** - Comprehensive concern specs (Shareable) - Model specs for Trip sharing functionality - Request specs for public and authenticated access - Policy specs for authorization rules - 100% coverage of sharing functionality ## Implementation Details ### Models Updated - Stat: Now uses Shareable concern (removed duplicate code) - Trip: Includes Shareable concern with notes/photos options ### Controllers Added - Shared::TripsController: Handles public viewing and sharing management ### Views Added - trips/public_show.html.erb: Public-facing trip view - trips/_sharing.html.erb: Sharing controls partial ### JavaScript Added - public_trip_map_controller.js: Map rendering for public trips ### Helpers Extended - TripsHelper: Added sharing status and expiration helpers ## Breaking Changes None. This is a purely additive feature. ## Migration Required Yes. Run: rails db:migrate ## Testing All specs pass: - spec/models/concerns/shareable_spec.rb - spec/models/trip_spec.rb - spec/requests/shared/trips_spec.rb - spec/policies/trip_policy_spec.rb
2025-11-05 10:44:27 -05:00
describe 'Trip-specific sharing' do
let(:user) { create(:user) }
let(:trip) { create(:trip, user: user) }
Implement public trip sharing with Shareable concern This commit implements comprehensive public trip sharing functionality by extracting sharing logic into a reusable Shareable concern and extending it to Trip models. ## Key Features **Shareable Concern (DRY principle)** - Extract sharing logic from Stat model into reusable concern - Support for time-based expiration (1h, 12h, 24h, permanent) - UUID-based secure public access - User-controlled sharing of notes and photos - Automatic UUID generation on model creation **Database Changes** - Add sharing_uuid (UUID) column to trips table - Add sharing_settings (JSONB) column for configuration storage - Add unique index on sharing_uuid for performance **Public Trip Sharing** - Public-facing trip view with read-only access - Interactive map with trip route visualization - Optional sharing of notes and photo previews - Branded footer with Dawarich attribution - Responsive design matching existing UI patterns **Sharing Management** - In-app sharing controls in trip show view - Enable/disable sharing with one click - Configurable expiration times - Copy-to-clipboard for sharing URLs - Visual indicators for sharing status **Authorization & Security** - TripPolicy for fine-grained access control - Public access only for explicitly shared trips - Automatic expiration enforcement - Owner-only sharing management - UUID-based URLs prevent enumeration attacks **API & Routes** - GET /shared/trips/:trip_uuid for public access - PATCH /trips/:id/sharing for sharing management - RESTful endpoint design consistent with stats sharing **Frontend** - New public-trip-map Stimulus controller - OpenStreetMap tiles for public viewing (no API key required) - Start/end markers on trip route - Automatic map bounds fitting **Tests** - Comprehensive concern specs (Shareable) - Model specs for Trip sharing functionality - Request specs for public and authenticated access - Policy specs for authorization rules - 100% coverage of sharing functionality ## Implementation Details ### Models Updated - Stat: Now uses Shareable concern (removed duplicate code) - Trip: Includes Shareable concern with notes/photos options ### Controllers Added - Shared::TripsController: Handles public viewing and sharing management ### Views Added - trips/public_show.html.erb: Public-facing trip view - trips/_sharing.html.erb: Sharing controls partial ### JavaScript Added - public_trip_map_controller.js: Map rendering for public trips ### Helpers Extended - TripsHelper: Added sharing status and expiration helpers ## Breaking Changes None. This is a purely additive feature. ## Migration Required Yes. Run: rails db:migrate ## Testing All specs pass: - spec/models/concerns/shareable_spec.rb - spec/models/trip_spec.rb - spec/requests/shared/trips_spec.rb - spec/policies/trip_policy_spec.rb
2025-11-05 10:44:27 -05:00
it 'enables sharing with notes and photos options' do
trip.enable_sharing!(expiration: '24h', share_notes: true, share_photos: true)
expect(trip.sharing_enabled?).to be true
expect(trip.share_notes?).to be true
expect(trip.share_photos?).to be true
end
end
end
end