discourse/spec/models/site_customization_spec.rb

105 lines
3.3 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require 'spec_helper'
describe SiteCustomization do
let :user do
Fabricate(:user)
end
2013-02-26 00:42:20 +08:00
let :customization do
2013-02-06 03:16:51 +08:00
SiteCustomization.create!(name: 'my name', user_id: user.id, header: "my awesome header", stylesheet: "my awesome css")
end
2013-02-26 00:42:20 +08:00
it 'should set default key when creating a new customization' do
2013-02-06 03:16:51 +08:00
s = SiteCustomization.create!(name: 'my name', user_id: user.id)
s.key.should_not == nil
end
context 'caching' do
2013-02-26 00:42:20 +08:00
context 'enabled style' do
before do
@customization = customization
end
2013-02-26 00:42:20 +08:00
it 'finds no style when none enabled' do
SiteCustomization.enabled_style_key.should be_nil
end
2013-02-26 00:42:20 +08:00
it 'finds the enabled style' do
@customization.enabled = true
@customization.save
SiteCustomization.enabled_style_key.should == @customization.key
end
2013-02-26 00:42:20 +08:00
it 'finds no enabled style on other sites' do
@customization.enabled = true
@customization.save
2013-02-26 00:42:20 +08:00
RailsMultisite::ConnectionManagement.expects(:current_db).returns("foo").twice
# the mocking is tricky, lets remove the record so we can properly pretend we are on another db
# this bypasses the before / after stuff
SiteCustomization.exec_sql('delete from site_customizations')
SiteCustomization.enabled_style_key.should be_nil
end
end
2013-02-26 00:42:20 +08:00
it 'ensure stylesheet is on disk on first fetch' do
c = customization
c.remove_from_cache!
File.delete(c.stylesheet_fullpath)
SiteCustomization.custom_stylesheet(c.key)
File.exists?(c.stylesheet_fullpath).should == true
end
2013-02-06 03:16:51 +08:00
it 'should allow me to lookup a filename containing my preview stylesheet' do
2013-02-26 00:42:20 +08:00
SiteCustomization.custom_stylesheet(customization.key).should ==
"<link class=\"custom-css\" rel=\"stylesheet\" href=\"/uploads/stylesheet-cache/#{customization.key}.css?#{customization.stylesheet_hash}\" type=\"text/css\" media=\"screen\">"
2013-02-06 03:16:51 +08:00
end
2013-02-26 00:42:20 +08:00
it 'should fix stylesheet files after changing the stylesheet' do
old_file = customization.stylesheet_fullpath
2013-02-06 03:16:51 +08:00
original = SiteCustomization.custom_stylesheet(customization.key)
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
File.exists?(old_file).should == true
customization.stylesheet = "div { clear:both; }"
customization.save
SiteCustomization.custom_stylesheet(customization.key).should_not == original
end
it 'should delete old stylesheet files after deleting' do
2013-02-26 00:42:20 +08:00
old_file = customization.stylesheet_fullpath
2013-02-06 03:16:51 +08:00
customization.ensure_stylesheet_on_disk!
customization.destroy
File.exists?(old_file).should == false
end
2013-02-26 00:42:20 +08:00
it 'should nuke old revs out of the cache' do
2013-02-06 03:16:51 +08:00
old_style = SiteCustomization.custom_stylesheet(customization.key)
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
customization.stylesheet = "hello worldz"
customization.save
SiteCustomization.custom_stylesheet(customization.key).should_not == old_style
end
2013-02-26 00:42:20 +08:00
it 'should compile scss' do
2013-02-06 03:16:51 +08:00
c = SiteCustomization.create!(user_id: user.id, name: "test", stylesheet: '$black: #000; #a { color: $black; }', header: '')
c.stylesheet_baked.should == "#a {\n color: black; }\n"
end
2013-02-26 00:42:20 +08:00
it 'should provide an awesome error on failure' do
2013-02-06 03:16:51 +08:00
c = SiteCustomization.create!(user_id: user.id, name: "test", stylesheet: "$black: #000; #a { color: $black; }\n\n\nboom", header: '')
c.stylesheet_baked.should =~ /Syntax error/
end
end
end