discourse/spec/controllers/admin/backups_controller_spec.rb

275 lines
7.4 KiB
Ruby
Raw Normal View History

require "rails_helper"
2014-02-12 23:33:21 -05:00
describe Admin::BackupsController do
it "is a subclass of AdminController" do
2015-01-09 12:04:02 -05:00
expect(Admin::BackupsController < Admin::AdminController).to eq(true)
2014-02-12 23:33:21 -05:00
end
let(:backup_filename) { "2014-02-10-065935.tar.gz" }
context "while logged in as an admin" do
before { @admin = log_in(:admin) }
describe ".index" do
context "html format" do
it "preloads important data" do
2014-02-12 23:33:21 -05:00
Backup.expects(:all).returns([])
subject.expects(:store_preloaded).with("backups", "[]")
BackupRestore.expects(:operations_status).returns({})
subject.expects(:store_preloaded).with("operations_status", "{}")
BackupRestore.expects(:logs).returns([])
subject.expects(:store_preloaded).with("logs", "[]")
2014-02-12 23:33:21 -05:00
xhr :get, :index, format: :html
2015-01-09 12:04:02 -05:00
expect(response).to be_success
2014-02-12 23:33:21 -05:00
end
end
context "json format" do
it "returns a list of all the backups" do
Backup.expects(:all).returns([Backup.new("backup1"), Backup.new("backup2")])
xhr :get, :index, format: :json
2015-01-09 12:04:02 -05:00
expect(response).to be_success
2014-02-12 23:33:21 -05:00
json = JSON.parse(response.body)
2015-01-09 12:04:02 -05:00
expect(json[0]["filename"]).to eq("backup1")
expect(json[1]["filename"]).to eq("backup2")
2014-02-12 23:33:21 -05:00
end
end
end
describe ".status" do
it "returns the current backups status" do
BackupRestore.expects(:operations_status)
xhr :get, :status
2015-01-09 12:04:02 -05:00
expect(response).to be_success
2014-02-12 23:33:21 -05:00
end
end
describe ".create" do
it "starts a backup" do
2015-08-27 16:46:30 -04:00
BackupRestore.expects(:backup!).with(@admin.id, publish_to_message_bus: true, with_uploads: false, client_id: "foo")
2014-02-12 23:33:21 -05:00
2015-08-27 16:46:30 -04:00
xhr :post, :create, with_uploads: false, client_id: "foo"
2014-02-12 23:33:21 -05:00
2015-01-09 12:04:02 -05:00
expect(response).to be_success
2014-02-12 23:33:21 -05:00
end
end
describe ".show" do
it "uses send_file to transmit the backup" do
begin
token = EmailBackupToken.set(@admin.id)
path = File.join(Backup.base_directory, backup_filename)
File.open(path, "w") { |f| f.write("hello") }
2014-02-12 23:33:21 -05:00
Backup.create_from_filename(backup_filename)
2014-02-12 23:33:21 -05:00
StaffActionLogger.any_instance.expects(:log_backup_download).once
get :show, id: backup_filename, token: token
2014-09-22 19:25:53 -04:00
expect(response.headers['Content-Length']).to eq("5")
expect(response.headers['Content-Disposition']).to match(/attachment; filename/)
ensure
File.delete(path)
EmailBackupToken.del(@admin.id)
end
end
it "returns 422 when token is bad" do
begin
path = File.join(Backup.base_directory, backup_filename)
File.open(path, "w") { |f| f.write("hello") }
Backup.create_from_filename(backup_filename)
get :show, id: backup_filename, token: "bad_value"
expect(response.status).to eq(422)
ensure
File.delete(path)
end
2014-02-12 23:33:21 -05:00
end
it "returns 404 when the backup does not exist" do
token = EmailBackupToken.set(@admin.id)
2014-02-12 23:33:21 -05:00
Backup.expects(:[]).returns(nil)
get :show, id: backup_filename, token: token
EmailBackupToken.del(@admin.id)
expect(response).to be_not_found
end
end
describe ".email" do
let(:b) { Backup.new(backup_filename) }
it "enqueues email job" do
Backup.expects(:[]).with(backup_filename).returns(b)
Jobs.expects(:enqueue).with(:download_backup_email, has_entries(to_address: @admin.email))
xhr :put, :email, id: backup_filename
expect(response).to be_success
end
it "returns 404 when the backup does not exist" do
xhr :put, :email, id: backup_filename
2014-02-12 23:33:21 -05:00
2015-01-09 12:04:02 -05:00
expect(response).to be_not_found
2014-02-12 23:33:21 -05:00
end
end
describe ".destroy" do
let(:b) { Backup.new(backup_filename) }
2014-02-12 23:33:21 -05:00
it "removes the backup if found" do
Backup.expects(:[]).with(backup_filename).returns(b)
b.expects(:remove)
StaffActionLogger.any_instance.expects(:log_backup_destroy).with(b).once
2014-02-12 23:33:21 -05:00
xhr :delete, :destroy, id: backup_filename
2015-01-09 12:04:02 -05:00
expect(response).to be_success
2014-02-12 23:33:21 -05:00
end
it "doesn't remove the backup if not found" do
Backup.expects(:[]).with(backup_filename).returns(nil)
b.expects(:remove).never
xhr :delete, :destroy, id: backup_filename
2015-01-09 12:04:02 -05:00
expect(response).not_to be_success
end
2014-02-12 23:33:21 -05:00
end
describe ".logs" do
it "preloads important data" do
2014-02-12 23:33:21 -05:00
BackupRestore.expects(:operations_status).returns({})
subject.expects(:store_preloaded).with("operations_status", "{}")
BackupRestore.expects(:logs).returns([])
subject.expects(:store_preloaded).with("logs", "[]")
2014-02-12 23:33:21 -05:00
xhr :get, :logs, format: :html
2015-01-09 12:04:02 -05:00
expect(response).to be_success
2014-02-12 23:33:21 -05:00
end
end
describe ".restore" do
it "starts a restore" do
expect(SiteSetting.disable_emails).to eq(false)
2015-08-27 16:46:30 -04:00
BackupRestore.expects(:restore!).with(@admin.id, filename: backup_filename, publish_to_message_bus: true, client_id: "foo")
2014-02-12 23:33:21 -05:00
2015-08-27 16:46:30 -04:00
xhr :post, :restore, id: backup_filename, client_id: "foo"
2014-02-12 23:33:21 -05:00
expect(SiteSetting.disable_emails).to eq(true)
2015-01-09 12:04:02 -05:00
expect(response).to be_success
2014-02-12 23:33:21 -05:00
end
end
describe ".readonly" do
it "enables readonly mode" do
Discourse.expects(:enable_readonly_mode)
expect { xhr :put, :readonly, enable: true }
.to change { UserHistory.count }.by(1)
2014-02-12 23:33:21 -05:00
2015-01-09 12:04:02 -05:00
expect(response).to be_success
user_history = UserHistory.last
expect(UserHistory.last.action).to eq(UserHistory.actions[:change_readonly_mode])
expect(UserHistory.last.new_value).to eq('t')
2014-02-12 23:33:21 -05:00
end
it "disables readonly mode" do
Discourse.expects(:disable_readonly_mode)
expect { xhr :put, :readonly, enable: false }
.to change { UserHistory.count }.by(1)
2014-02-12 23:33:21 -05:00
2015-01-09 12:04:02 -05:00
expect(response).to be_success
user_history = UserHistory.last
expect(UserHistory.last.action).to eq(UserHistory.actions[:change_readonly_mode])
expect(UserHistory.last.new_value).to eq('f')
2014-02-12 23:33:21 -05:00
end
end
describe "#upload_backup_chunk" do
describe "when filename contains invalid characters" do
it "should raise an error" do
['灰色.tar.gz', '; echo \'haha\'.tar.gz'].each do |invalid_filename|
2016-09-16 03:10:37 -04:00
described_class.any_instance.expects(:has_enough_space_on_disk?).returns(true)
xhr :post, :upload_backup_chunk, resumableFilename: invalid_filename, resumableTotalSize: 1
expect(response.status).to eq(415)
expect(response.body).to eq(I18n.t('backup.invalid_filename'))
end
end
end
describe "when filename is valid" do
it "should upload the file successfully" do
begin
described_class.any_instance.expects(:has_enough_space_on_disk?).returns(true)
filename = 'test_Site-0123456789.tar.gz'
xhr :post, :upload_backup_chunk,
resumableFilename: filename,
resumableTotalSize: 1,
resumableIdentifier: 'test',
resumableChunkNumber: '1',
resumableChunkSize: '1',
resumableCurrentChunkSize: '1',
file: fixture_file_upload(Tempfile.new)
expect(response.status).to eq(200)
expect(response.body).to eq("")
ensure
File.delete(File.join(Backup.base_directory, filename))
end
end
end
end
2014-02-12 23:33:21 -05:00
end
end