Fix export file deletion

This commit is contained in:
Eugene Burmakin 2025-02-06 22:52:19 +01:00
parent 344d0c7ec1
commit 085ceb16e2
3 changed files with 24 additions and 13 deletions

View file

@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- After deleting one point from the map, other points can now be deleted as well. #723 #678
- Fixed a bug where export file was not being deleted from the server after it was deleted. #808
### Added
- `X-Dawarich-Version` header to the `GET /api/v1/health` endpoint response. #800

View file

@ -23,7 +23,11 @@ class ExportsController < ApplicationController
end
def destroy
@export.destroy
ActiveRecord::Base.transaction do
@export.destroy
File.delete(Rails.root.join('public', 'exports', @export.name))
end
redirect_to exports_url, notice: 'Export was successfully destroyed.', status: :see_other
end

View file

@ -76,9 +76,25 @@ RSpec.describe '/exports', type: :request do
end
describe 'DELETE /destroy' do
let!(:export) { create(:export, user:, url: 'exports/export.json') }
let!(:export) { create(:export, user:, url: 'exports/export.json', name: 'export.json') }
let(:export_file) { Rails.root.join('public', 'exports', export.name) }
before { sign_in user }
before do
sign_in user
FileUtils.mkdir_p(File.dirname(export_file))
File.write(export_file, '{"some": "data"}')
end
after { FileUtils.rm_f(export_file) }
it 'removes the export file from disk' do
expect(File.exist?(export_file)).to be true
delete export_url(export)
expect(File.exist?(export_file)).to be false
end
it 'destroys the requested export' do
expect { delete export_url(export) }.to change(Export, :count).by(-1)
@ -89,14 +105,5 @@ RSpec.describe '/exports', type: :request do
expect(response).to redirect_to(exports_url)
end
it 'remove the export file from the disk' do
export_file = Rails.root.join('public', export.url)
FileUtils.touch(export_file)
delete export_url(export)
expect(File.exist?(export_file)).to be_falsey
end
end
end