2015-10-11 05:41:23 -04:00
|
|
|
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
|
|
|
|
|
2014-02-13 13:41:46 -05:00
|
|
|
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", "{}")
|
|
|
|
|
2014-02-13 13:41:46 -05:00
|
|
|
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 ".cancel" do
|
|
|
|
|
|
|
|
it "cancels an export" do
|
|
|
|
BackupRestore.expects(:cancel!)
|
|
|
|
|
|
|
|
xhr :delete, :cancel
|
|
|
|
|
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
|
2014-09-22 19:55:20 -04:00
|
|
|
FileUtils.mkdir_p Backup.base_directory
|
2014-09-22 19:25:53 -04:00
|
|
|
File.open(Backup.base_directory << "/" << backup_filename, "w") do |f|
|
|
|
|
f.write("hello")
|
|
|
|
end
|
2014-02-12 23:33:21 -05:00
|
|
|
|
2014-09-22 19:25:53 -04:00
|
|
|
Backup.create_from_filename(backup_filename)
|
2014-02-12 23:33:21 -05:00
|
|
|
|
|
|
|
get :show, id: backup_filename
|
2014-09-22 19:25:53 -04:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.headers['Content-Length']).to eq(5)
|
|
|
|
expect(response.headers['Content-Disposition']).to match(/attachment; filename/)
|
2014-02-12 23:33:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when the backup does not exist" do
|
|
|
|
Backup.expects(:[]).returns(nil)
|
|
|
|
|
|
|
|
get :show, id: backup_filename
|
|
|
|
|
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
|
|
|
|
|
2014-03-11 17:28:12 -04:00
|
|
|
let(:b) { Backup.new(backup_filename) }
|
2014-02-12 23:33:21 -05:00
|
|
|
|
2014-03-11 17:28:12 -04:00
|
|
|
it "removes the backup if found" do
|
|
|
|
Backup.expects(:[]).with(backup_filename).returns(b)
|
|
|
|
b.expects(:remove)
|
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
|
|
|
|
|
2014-03-11 17:28:12 -04:00
|
|
|
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
|
2014-03-11 17:28:12 -04:00
|
|
|
end
|
|
|
|
|
2014-02-12 23:33:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".logs" do
|
|
|
|
|
2014-02-13 13:41:46 -05:00
|
|
|
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", "{}")
|
|
|
|
|
2014-02-13 13:41:46 -05:00
|
|
|
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
|
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
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-02-12 23:33:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".rollback" do
|
|
|
|
|
|
|
|
it "rolls back to previous working state" do
|
|
|
|
BackupRestore.expects(:rollback!)
|
|
|
|
|
|
|
|
xhr :get, :rollback
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
xhr :put, :readonly, enable: true
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-02-12 23:33:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "disables readonly mode" do
|
|
|
|
Discourse.expects(:disable_readonly_mode)
|
|
|
|
|
|
|
|
xhr :put, :readonly, enable: false
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-02-12 23:33:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|