2013-07-24 05:58:26 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe StaffActionLogger do
|
|
|
|
|
2013-08-20 04:58:38 +08:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:logger) { described_class.new(admin) }
|
|
|
|
|
2013-07-24 05:58:26 +08:00
|
|
|
describe 'new' do
|
|
|
|
it 'raises an error when user is nil' do
|
|
|
|
expect { described_class.new(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when user is not a User' do
|
|
|
|
expect { described_class.new(5) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'log_user_deletion' do
|
|
|
|
let(:deleted_user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
subject(:log_user_deletion) { described_class.new(admin).log_user_deletion(deleted_user) }
|
|
|
|
|
|
|
|
it 'raises an error when user is nil' do
|
2013-08-20 04:58:38 +08:00
|
|
|
expect { logger.log_user_deletion(nil) }.to raise_error(Discourse::InvalidParameters)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when user is not a User' do
|
2013-08-20 04:58:38 +08:00
|
|
|
expect { logger.log_user_deletion(1) }.to raise_error(Discourse::InvalidParameters)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new StaffActionLog record' do
|
2013-09-11 09:21:16 +08:00
|
|
|
expect { log_user_deletion }.to change { UserHistory.count }.by(1)
|
|
|
|
UserHistory.last.target_user_id.should == deleted_user.id
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'log_trust_level_change' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
2013-08-14 00:04:28 +08:00
|
|
|
let(:old_trust_level) { TrustLevel.levels[:newuser] }
|
2013-07-24 05:58:26 +08:00
|
|
|
let(:new_trust_level) { TrustLevel.levels[:basic] }
|
|
|
|
|
2013-08-14 00:04:28 +08:00
|
|
|
subject(:log_trust_level_change) { described_class.new(admin).log_trust_level_change(user, old_trust_level, new_trust_level) }
|
2013-07-24 05:58:26 +08:00
|
|
|
|
|
|
|
it 'raises an error when user or trust level is nil' do
|
2013-08-20 04:58:38 +08:00
|
|
|
expect { logger.log_trust_level_change(nil, old_trust_level, new_trust_level) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
expect { logger.log_trust_level_change(user, nil, new_trust_level) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
expect { logger.log_trust_level_change(user, old_trust_level, nil) }.to raise_error(Discourse::InvalidParameters)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when user is not a User' do
|
2013-08-20 04:58:38 +08:00
|
|
|
expect { logger.log_trust_level_change(1, old_trust_level, new_trust_level) }.to raise_error(Discourse::InvalidParameters)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when new trust level is not a Trust Level' do
|
|
|
|
max_level = TrustLevel.levels.values.max
|
2013-08-20 04:58:38 +08:00
|
|
|
expect { logger.log_trust_level_change(user, old_trust_level, max_level + 1) }.to raise_error(Discourse::InvalidParameters)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new StaffActionLog record' do
|
2013-09-11 09:21:16 +08:00
|
|
|
expect { log_trust_level_change }.to change { UserHistory.count }.by(1)
|
|
|
|
UserHistory.last.details.should include "new trust level: #{new_trust_level}"
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
end
|
2013-08-20 04:58:38 +08:00
|
|
|
|
|
|
|
describe "log_site_setting_change" do
|
|
|
|
it "raises an error when params are invalid" do
|
|
|
|
SiteSetting.stubs(:respond_to?).with('abc').returns(false)
|
|
|
|
expect { logger.log_site_setting_change(nil, '1', '2') }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
expect { logger.log_site_setting_change('abc', '1', '2') }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new StaffActionLog record" do
|
2013-09-11 09:21:16 +08:00
|
|
|
expect { logger.log_site_setting_change('title', 'Discourse', 'My Site') }.to change { UserHistory.count }.by(1)
|
2013-08-20 04:58:38 +08:00
|
|
|
end
|
|
|
|
end
|
2013-08-21 22:49:35 +08:00
|
|
|
|
|
|
|
describe "log_site_customization_change" do
|
|
|
|
let(:valid_params) { {name: 'Cool Theme', stylesheet: "body {\n background-color: blue;\n}\n", header: "h1 {color: white;}"} }
|
|
|
|
|
|
|
|
it "raises an error when params are invalid" do
|
|
|
|
expect { logger.log_site_customization_change(nil, nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "logs new site customizations" do
|
|
|
|
log_record = logger.log_site_customization_change(nil, valid_params)
|
|
|
|
log_record.subject.should == valid_params[:name]
|
|
|
|
log_record.previous_value.should be_nil
|
|
|
|
log_record.new_value.should be_present
|
|
|
|
json = ::JSON.parse(log_record.new_value)
|
|
|
|
json['stylesheet'].should be_present
|
|
|
|
json['header'].should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "logs updated site customizations" do
|
|
|
|
existing = SiteCustomization.new(name: 'Banana', stylesheet: "body {color: yellow;}", header: "h1 {color: brown;}")
|
|
|
|
log_record = logger.log_site_customization_change(existing, valid_params)
|
|
|
|
log_record.previous_value.should be_present
|
|
|
|
json = ::JSON.parse(log_record.previous_value)
|
|
|
|
json['stylesheet'].should == existing.stylesheet
|
|
|
|
json['header'].should == existing.header
|
|
|
|
end
|
|
|
|
end
|
2013-08-22 00:03:21 +08:00
|
|
|
|
|
|
|
describe "log_site_customization_destroy" do
|
|
|
|
it "raises an error when params are invalid" do
|
|
|
|
expect { logger.log_site_customization_destroy(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new StaffActionLog record" do
|
|
|
|
site_customization = SiteCustomization.new(name: 'Banana', stylesheet: "body {color: yellow;}", header: "h1 {color: brown;}")
|
|
|
|
log_record = logger.log_site_customization_destroy(site_customization)
|
|
|
|
log_record.previous_value.should be_present
|
|
|
|
log_record.new_value.should be_nil
|
|
|
|
json = ::JSON.parse(log_record.previous_value)
|
|
|
|
json['stylesheet'].should == site_customization.stylesheet
|
|
|
|
json['header'].should == site_customization.header
|
|
|
|
end
|
|
|
|
end
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|