fix and test purge cache

This commit is contained in:
Kyle Drake 2014-08-19 17:05:43 -07:00
parent 445ec92226
commit 3e4e85ceee
2 changed files with 12 additions and 2 deletions

View file

@ -202,7 +202,7 @@ class Site < Sequel::Model
%w{index not_found}.each do |name| %w{index not_found}.each do |name|
File.write files_path("#{name}.html"), render_template("#{name}.erb") File.write files_path("#{name}.html"), render_template("#{name}.erb")
purge_cache "#{name}.html" purge_cache "/#{name}.html"
ScreenshotWorker.perform_async values[:username], "#{name}.html" ScreenshotWorker.perform_async values[:username], "#{name}.html"
end end
@ -327,7 +327,8 @@ class Site < Sequel::Model
end end
def purge_cache(path) def purge_cache(path)
payload = {site: username, path: path} relative_path = path.gsub(base_files_path, '')
payload = {site: username, path: relative_path}
payload[:domain] = domain if !domain.empty? payload[:domain] = domain if !domain.empty?
PurgeCacheWorker.perform_async payload PurgeCacheWorker.perform_async payload
end end

View file

@ -11,16 +11,22 @@ describe 'site_files' do
describe 'upload' do describe 'upload' do
it 'succeeds with valid file' do it 'succeeds with valid file' do
site = Fabricate :site site = Fabricate :site
PurgeCacheWorker.jobs.clear
post '/site_files/upload', { post '/site_files/upload', {
'files[]' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg'), 'files[]' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg'),
'csrf_token' => 'abcd' 'csrf_token' => 'abcd'
}, {'rack.session' => { 'id' => site.id, '_csrf_token' => 'abcd' }} }, {'rack.session' => { 'id' => site.id, '_csrf_token' => 'abcd' }}
last_response.body.must_match /successfully uploaded/i last_response.body.must_match /successfully uploaded/i
File.exists?(site.files_path('test.jpg')).must_equal true File.exists?(site.files_path('test.jpg')).must_equal true
queue_args = PurgeCacheWorker.jobs.first['args'].first
queue_args['site'].must_equal site.username
queue_args['path'].must_equal '/test.jpg'
end end
it 'works with directory path' do it 'works with directory path' do
site = Fabricate :site site = Fabricate :site
PurgeCacheWorker.jobs.clear
post '/site_files/upload', { post '/site_files/upload', {
'dir' => 'derpie/derptest', 'dir' => 'derpie/derptest',
'files[]' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg'), 'files[]' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg'),
@ -28,6 +34,9 @@ describe 'site_files' do
}, {'rack.session' => { 'id' => site.id, '_csrf_token' => 'abcd' }} }, {'rack.session' => { 'id' => site.id, '_csrf_token' => 'abcd' }}
last_response.body.must_match /successfully uploaded/i last_response.body.must_match /successfully uploaded/i
File.exists?(site.files_path('derpie/derptest/test.jpg')).must_equal true File.exists?(site.files_path('derpie/derptest/test.jpg')).must_equal true
queue_args = PurgeCacheWorker.jobs.first['args'].first
queue_args['path'].must_equal '/derpie/derptest/test.jpg'
end end
end end
end end