diff --git a/app/controllers/api/v1/photos_controller.rb b/app/controllers/api/v1/photos_controller.rb index b3bb0e6a..b2930888 100644 --- a/app/controllers/api/v1/photos_controller.rb +++ b/app/controllers/api/v1/photos_controller.rb @@ -1,6 +1,9 @@ # frozen_string_literal: true class Api::V1::PhotosController < ApiController + before_action :check_integration_configured, only: %i[index thumbnail] + before_action :check_source, only: %i[thumbnail] + def index @photos = Rails.cache.fetch("photos_#{params[:start_date]}_#{params[:end_date]}", expires_in: 1.day) do Photos::Request.new(current_api_user, start_date: params[:start_date], end_date: params[:end_date]).call @@ -10,8 +13,6 @@ class Api::V1::PhotosController < ApiController end def thumbnail - return unauthorized_integration unless integration_configured? - response = fetch_cached_thumbnail(params[:source]) handle_thumbnail_response(response) end @@ -33,8 +34,15 @@ class Api::V1::PhotosController < ApiController end def integration_configured? - (params[:source] == 'immich' && current_api_user.immich_integration_configured?) || - (params[:source] == 'photoprism' && current_api_user.photoprism_integration_configured?) + current_api_user.immich_integration_configured? || current_api_user.photoprism_integration_configured? + end + + def check_integration_configured + unauthorized_integration unless integration_configured? + end + + def check_source + unauthorized_integration unless params[:source] == 'immich' || params[:source] == 'photoprism' end def unauthorized_integration diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 2d4e654f..f3fe9d7b 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -23,7 +23,7 @@ FactoryBot.define do admin { true } end - trait :with_immich_credentials do + trait :with_immich_integration do settings do { immich_url: 'https://immich.example.com', @@ -31,5 +31,14 @@ FactoryBot.define do } end end + + trait :with_photoprism_integration do + settings do + { + photoprism_url: 'https://photoprism.example.com', + photoprism_api_key: '1234567890' + } + end + end end end diff --git a/spec/requests/api/v1/photos_spec.rb b/spec/requests/api/v1/photos_spec.rb index c24a5360..c1e440bc 100644 --- a/spec/requests/api/v1/photos_spec.rb +++ b/spec/requests/api/v1/photos_spec.rb @@ -4,50 +4,68 @@ require 'rails_helper' RSpec.describe 'Api::V1::Photos', type: :request do describe 'GET /index' do - let(:user) { create(:user) } + context 'when the integration is configured' do + let(:user) { create(:user, :with_photoprism_integration) } - let(:photo_data) do - [ - { - 'id' => 1, - 'latitude' => 35.6762, - 'longitude' => 139.6503, - 'localDateTime' => '2024-01-01T00:00:00.000Z', - 'originalFileName' => 'photo1.jpg', - 'city' => 'Tokyo', - 'state' => 'Tokyo', - 'country' => 'Japan', - 'type' => 'photo', - 'source' => 'photoprism' - }, - { - 'id' => 2, - 'latitude' => 40.7128, - 'longitude' => -74.0060, - 'localDateTime' => '2024-01-02T00:00:00.000Z', - 'originalFileName' => 'photo2.jpg', - 'city' => 'New York', - 'state' => 'New York', - 'country' => 'USA', - 'type' => 'photo', - 'source' => 'immich' - } - ] + let(:photo_data) do + [ + { + 'id' => 1, + 'latitude' => 35.6762, + 'longitude' => 139.6503, + 'localDateTime' => '2024-01-01T00:00:00.000Z', + 'originalFileName' => 'photo1.jpg', + 'city' => 'Tokyo', + 'state' => 'Tokyo', + 'country' => 'Japan', + 'type' => 'photo', + 'source' => 'photoprism' + }, + { + 'id' => 2, + 'latitude' => 40.7128, + 'longitude' => -74.0060, + 'localDateTime' => '2024-01-02T00:00:00.000Z', + 'originalFileName' => 'photo2.jpg', + 'city' => 'New York', + 'state' => 'New York', + 'country' => 'USA', + 'type' => 'photo', + 'source' => 'immich' + } + ] + end + + context 'when the request is successful' do + before do + allow_any_instance_of(Photos::Request).to receive(:call).and_return(photo_data) + + get '/api/v1/photos', params: { api_key: user.api_key } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'returns photos data as JSON' do + expect(JSON.parse(response.body)).to eq(photo_data) + end + end end - context 'when the request is successful' do + context 'when the integration is not configured' do + let(:user) { create(:user) } + before do - allow_any_instance_of(Photos::Request).to receive(:call).and_return(photo_data) - - get '/api/v1/photos', params: { api_key: user.api_key } + get '/api/v1/photos', params: { api_key: user.api_key, source: 'immich' } end - it 'returns http success' do - expect(response).to have_http_status(:success) + it 'returns http unauthorized' do + expect(response).to have_http_status(:unauthorized) end - it 'returns photos data as JSON' do - expect(JSON.parse(response.body)).to eq(photo_data) + it 'returns an error message' do + expect(JSON.parse(response.body)).to eq({ 'error' => 'Immich integration not configured' }) end end end diff --git a/spec/swagger/api/v1/photos_controller_spec.rb b/spec/swagger/api/v1/photos_controller_spec.rb index f441c307..eef5d9a5 100644 --- a/spec/swagger/api/v1/photos_controller_spec.rb +++ b/spec/swagger/api/v1/photos_controller_spec.rb @@ -3,7 +3,7 @@ require 'swagger_helper' RSpec.describe 'Api::V1::PhotosController', type: :request do - let(:user) { create(:user, :with_immich_credentials) } + let(:user) { create(:user, :with_immich_integration) } let(:api_key) { user.api_key } let(:start_date) { '2024-01-01' } let(:end_date) { '2024-01-02' }