mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-14 03:01:39 -05:00
Add spec for Photos::Thumbnail
This commit is contained in:
parent
1030bd5c37
commit
d2bffdf1f1
2 changed files with 82 additions and 8 deletions
|
|
@ -8,7 +8,7 @@ class Photos::Thumbnail
|
|||
end
|
||||
|
||||
def call
|
||||
fetch_thumbnail_from_source
|
||||
HTTParty.get(request_url, headers: headers)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
@ -35,6 +35,10 @@ class Photos::Thumbnail
|
|||
end
|
||||
end
|
||||
|
||||
def request_url
|
||||
"#{source_url}#{source_path}"
|
||||
end
|
||||
|
||||
def headers
|
||||
request_headers = {
|
||||
'accept' => 'application/octet-stream'
|
||||
|
|
@ -44,11 +48,4 @@ class Photos::Thumbnail
|
|||
|
||||
request_headers
|
||||
end
|
||||
|
||||
def fetch_thumbnail_from_source
|
||||
url = "#{source_url}#{source_path}"
|
||||
a = HTTParty.get(url, headers: headers)
|
||||
pp url
|
||||
a
|
||||
end
|
||||
end
|
||||
|
|
|
|||
77
spec/services/photos/thumbnail_spec.rb
Normal file
77
spec/services/photos/thumbnail_spec.rb
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Photos::Thumbnail do
|
||||
let(:user) { create(:user) }
|
||||
let(:id) { 'photo123' }
|
||||
|
||||
describe '#call' do
|
||||
subject { described_class.new(user, source, id).call }
|
||||
|
||||
context 'with immich source' do
|
||||
let(:source) { 'immich' }
|
||||
let(:api_key) { 'immich_key_123' }
|
||||
let(:base_url) { 'https://photos.example.com' }
|
||||
let(:expected_url) { "#{base_url}/api/assets/#{id}/thumbnail?size=preview" }
|
||||
let(:expected_headers) do
|
||||
{
|
||||
'accept' => 'application/octet-stream',
|
||||
'X-Api-Key' => api_key
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
allow(user).to receive(:settings).and_return(
|
||||
'immich_url' => base_url,
|
||||
'immich_api_key' => api_key
|
||||
)
|
||||
end
|
||||
|
||||
it 'fetches thumbnail with correct parameters' do
|
||||
expect(HTTParty).to receive(:get)
|
||||
.with(expected_url, headers: expected_headers)
|
||||
.and_return('thumbnail_data')
|
||||
|
||||
expect(subject).to eq('thumbnail_data')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with photoprism source' do
|
||||
let(:source) { 'photoprism' }
|
||||
let(:base_url) { 'https://photoprism.example.com' }
|
||||
let(:preview_token) { 'preview_token_123' }
|
||||
let(:expected_url) { "#{base_url}/api/v1/t/#{id}/#{preview_token}/tile_500" }
|
||||
let(:expected_headers) do
|
||||
{
|
||||
'accept' => 'application/octet-stream'
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
allow(user).to receive(:settings).and_return(
|
||||
'photoprism_url' => base_url
|
||||
)
|
||||
allow(Rails.cache).to receive(:read)
|
||||
.with("#{Photoprism::CachePreviewToken::TOKEN_CACHE_KEY}_#{user.id}")
|
||||
.and_return(preview_token)
|
||||
end
|
||||
|
||||
it 'fetches thumbnail with correct parameters' do
|
||||
expect(HTTParty).to receive(:get)
|
||||
.with(expected_url, headers: expected_headers)
|
||||
.and_return('thumbnail_data')
|
||||
|
||||
expect(subject).to eq('thumbnail_data')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with unsupported source' do
|
||||
let(:source) { 'unsupported' }
|
||||
|
||||
it 'raises an error' do
|
||||
expect { subject }.to raise_error(RuntimeError, 'Unsupported source: unsupported')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue