2024-04-26 12:59:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-04-06 16:31:33 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
2024-04-25 16:46:20 -04:00
|
|
|
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
|
2024-04-06 16:31:33 -04:00
|
|
|
context 'when user is logged in' do
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
sign_in user
|
|
|
|
|
end
|
|
|
|
|
|
2024-04-25 16:46:20 -04:00
|
|
|
it 'returns http success' do
|
2024-04-06 16:31:33 -04:00
|
|
|
get imports_path
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when user has imports' do
|
2024-04-26 12:59:58 -04:00
|
|
|
let!(:import) { create(:import, user:) }
|
2024-04-06 16:31:33 -04:00
|
|
|
|
|
|
|
|
it 'displays imports' do
|
|
|
|
|
get imports_path
|
|
|
|
|
|
|
|
|
|
expect(response.body).to include(import.name)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-04-25 16:46:20 -04:00
|
|
|
describe 'POST /imports' do
|
2024-04-06 16:31:33 -04:00
|
|
|
context 'when user is logged in' do
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
|
|
before { sign_in user }
|
|
|
|
|
|
2024-05-31 17:18:57 -04:00
|
|
|
context 'when importing owntracks data' do
|
2024-10-15 16:41:49 -04:00
|
|
|
let(:file) { fixture_file_upload('owntracks/2024-03.rec', 'text/plain') }
|
2025-05-03 15:35:02 -04:00
|
|
|
let(:blob) { create_blob_for_file(file) }
|
|
|
|
|
let(:signed_id) { generate_signed_id_for_blob(blob) }
|
2024-05-31 17:18:57 -04:00
|
|
|
|
|
|
|
|
it 'queues import job' do
|
2025-05-03 15:35:02 -04:00
|
|
|
allow(ActiveStorage::Blob).to receive(:find_signed).with(signed_id).and_return(blob)
|
|
|
|
|
|
2024-05-31 17:18:57 -04:00
|
|
|
expect do
|
2025-05-03 15:35:02 -04:00
|
|
|
post imports_path, params: { import: { source: 'owntracks', files: [signed_id] } }
|
2025-03-23 13:37:10 -04:00
|
|
|
end.to have_enqueued_job(Import::ProcessJob).on_queue('imports').at_least(1).times
|
2024-05-31 17:18:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'creates a new import' do
|
2025-05-03 15:35:02 -04:00
|
|
|
allow(ActiveStorage::Blob).to receive(:find_signed).with(signed_id).and_return(blob)
|
|
|
|
|
|
2024-05-31 17:18:57 -04:00
|
|
|
expect do
|
2025-05-03 15:35:02 -04:00
|
|
|
post imports_path, params: { import: { source: 'owntracks', files: [signed_id] } }
|
2024-05-31 17:18:57 -04:00
|
|
|
end.to change(user.imports, :count).by(1)
|
|
|
|
|
|
|
|
|
|
expect(response).to redirect_to(imports_path)
|
|
|
|
|
end
|
2024-04-06 16:31:33 -04:00
|
|
|
end
|
|
|
|
|
|
2024-05-31 17:18:57 -04:00
|
|
|
context 'when importing gpx data' do
|
2024-06-19 15:16:06 -04:00
|
|
|
let(:file) { fixture_file_upload('gpx/gpx_track_single_segment.gpx', 'application/gpx+xml') }
|
2025-05-03 15:35:02 -04:00
|
|
|
let(:blob) { create_blob_for_file(file) }
|
|
|
|
|
let(:signed_id) { generate_signed_id_for_blob(blob) }
|
2024-04-06 16:31:33 -04:00
|
|
|
|
2024-05-31 17:18:57 -04:00
|
|
|
it 'queues import job' do
|
2025-05-03 15:35:02 -04:00
|
|
|
allow(ActiveStorage::Blob).to receive(:find_signed).with(signed_id).and_return(blob)
|
|
|
|
|
|
2024-05-31 17:18:57 -04:00
|
|
|
expect do
|
2025-05-03 15:35:02 -04:00
|
|
|
post imports_path, params: { import: { source: 'gpx', files: [signed_id] } }
|
2025-03-23 13:37:10 -04:00
|
|
|
end.to have_enqueued_job(Import::ProcessJob).on_queue('imports').at_least(1).times
|
2024-05-31 17:18:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'creates a new import' do
|
2025-05-03 15:35:02 -04:00
|
|
|
allow(ActiveStorage::Blob).to receive(:find_signed).with(signed_id).and_return(blob)
|
|
|
|
|
|
2024-05-31 17:18:57 -04:00
|
|
|
expect do
|
2025-05-03 15:35:02 -04:00
|
|
|
post imports_path, params: { import: { source: 'gpx', files: [signed_id] } }
|
2024-05-31 17:18:57 -04:00
|
|
|
end.to change(user.imports, :count).by(1)
|
|
|
|
|
|
|
|
|
|
expect(response).to redirect_to(imports_path)
|
|
|
|
|
end
|
2024-04-06 16:31:33 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2025-04-18 13:48:02 -04:00
|
|
|
|
|
|
|
|
describe 'GET /imports/new' do
|
|
|
|
|
context 'when user is logged in' do
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
|
|
before { sign_in user }
|
|
|
|
|
|
|
|
|
|
it 'returns http success' do
|
|
|
|
|
get new_import_path
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'DELETE /imports/:id' do
|
|
|
|
|
context 'when user is logged in' do
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
let!(:import) { create(:import, user:) }
|
|
|
|
|
|
|
|
|
|
before { sign_in user }
|
|
|
|
|
|
|
|
|
|
it 'deletes the import' do
|
|
|
|
|
expect do
|
|
|
|
|
delete import_path(import)
|
|
|
|
|
end.to change(user.imports, :count).by(-1)
|
|
|
|
|
|
|
|
|
|
expect(response).to redirect_to(imports_path)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'GET /imports/:id/edit' do
|
|
|
|
|
context 'when user is logged in' do
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
let(:import) { create(:import, user:) }
|
|
|
|
|
|
|
|
|
|
before { sign_in user }
|
|
|
|
|
|
|
|
|
|
it 'returns http success' do
|
|
|
|
|
get edit_import_path(import)
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'PATCH /imports/:id' do
|
|
|
|
|
context 'when user is logged in' do
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
let(:import) { create(:import, user:) }
|
|
|
|
|
|
|
|
|
|
before { sign_in user }
|
|
|
|
|
|
|
|
|
|
it 'updates the import' do
|
|
|
|
|
patch import_path(import), params: { import: { name: 'New Name' } }
|
|
|
|
|
|
|
|
|
|
expect(response).to redirect_to(imports_path)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2025-05-03 15:35:02 -04:00
|
|
|
|
|
|
|
|
# Helper methods for creating ActiveStorage blobs and signed IDs in tests
|
|
|
|
|
def create_blob_for_file(file)
|
|
|
|
|
ActiveStorage::Blob.create_and_upload!(
|
|
|
|
|
io: file.open,
|
|
|
|
|
filename: file.original_filename,
|
|
|
|
|
content_type: file.content_type
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def generate_signed_id_for_blob(blob)
|
|
|
|
|
blob.signed_id
|
|
|
|
|
end
|
2024-04-06 16:31:33 -04:00
|
|
|
end
|