dawarich/spec/services/immich/import_geodata_spec.rb

92 lines
2.3 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Immich::ImportGeodata do
describe '#call' do
subject(:service) { described_class.new(user).call }
let(:user) do
create(:user, settings: { 'immich_url' => 'http://immich.app', 'immich_api_key' => '123456' })
end
let(:immich_data) do
[
{
"assets": [
{
"exifInfo": {
"dateTimeOriginal": '2022-12-31T23:17:06.170Z',
"latitude": 52.0000,
"longitude": 13.0000
}
},
{
"exifInfo": {
"dateTimeOriginal": '2022-12-31T23:21:53.140Z',
"latitude": 52.0000,
"longitude": 13.0000
}
}
],
"title": '1 year ago',
"yearsAgo": 1
}
].to_json
end
context 'when user has immich_url and immich_api_key' do
before do
stub_request(
:any,
%r{http://immich\.app/api/assets/memory-lane\?day=(1[0-9]|2[0-9]|3[01]|[1-9])&month=(1[0-2]|[1-9])}
).to_return(status: 200, body: immich_data, headers: {})
end
it 'creates import' do
expect { service }.to change { Import.count }.by(1)
end
it 'enqueues ImportJob' do
expect(ImportJob).to receive(:perform_later)
service
end
context 'when import already exists' do
before { service }
it 'does not create new import' do
expect { service }.not_to(change { Import.count })
end
it 'does not enqueue ImportJob' do
expect(ImportJob).to_not receive(:perform_later)
service
end
end
end
context 'when user has no immich_url' do
before do
user.settings['immich_url'] = nil
user.save
end
it 'raises ArgumentError' do
expect { service }.to raise_error(ArgumentError, 'Immich URL is missing')
end
end
context 'when user has no immich_api_key' do
before do
user.settings['immich_api_key'] = nil
user.save
end
it 'raises ArgumentError' do
expect { service }.to raise_error(ArgumentError, 'Immich API key is missing')
end
end
end
end