2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe StylesheetsController do
|
2015-05-21 20:21:16 -05:00
|
|
|
it "can survive cache miss" do
|
|
|
|
StylesheetCache.destroy_all
|
2021-06-15 01:57:17 -05:00
|
|
|
manager = Stylesheet::Manager.new(theme_id: nil)
|
|
|
|
builder = Stylesheet::Manager::Builder.new(target: "desktop_rtl", manager: manager, theme: nil)
|
2015-05-23 00:25:05 -05:00
|
|
|
builder.compile
|
|
|
|
|
|
|
|
digest = StylesheetCache.first.digest
|
|
|
|
StylesheetCache.destroy_all
|
2015-05-21 20:21:16 -05:00
|
|
|
|
2018-06-05 22:53:53 -05:00
|
|
|
get "/stylesheets/desktop_rtl_#{digest}.css"
|
|
|
|
expect(response.status).to eq(200)
|
2015-05-23 00:25:05 -05:00
|
|
|
|
|
|
|
cached = StylesheetCache.first
|
|
|
|
expect(cached.target).to eq "desktop_rtl"
|
|
|
|
expect(cached.digest).to eq digest
|
|
|
|
|
2015-05-21 20:21:16 -05:00
|
|
|
# tmp folder destruction and cached
|
2024-01-02 23:15:35 -06:00
|
|
|
Stylesheet::Manager.rm_cache_folder
|
2015-05-21 20:21:16 -05:00
|
|
|
|
2018-06-05 22:53:53 -05:00
|
|
|
get "/stylesheets/desktop_rtl_#{digest}.css"
|
|
|
|
expect(response.status).to eq(200)
|
2015-05-23 00:25:05 -05:00
|
|
|
|
2015-05-21 20:21:16 -05:00
|
|
|
# there is an edge case which is ... disk and db cache is nuked, very unlikely to happen
|
|
|
|
end
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
it "can lookup theme specific css" do
|
|
|
|
scheme = ColorScheme.create_from_base(name: "testing", colors: [])
|
2018-08-07 23:46:34 -05:00
|
|
|
theme = Fabricate(:theme, color_scheme_id: scheme.id)
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
manager = Stylesheet::Manager.new(theme_id: theme.id)
|
|
|
|
|
|
|
|
builder = Stylesheet::Manager::Builder.new(target: :desktop, theme: theme, manager: manager)
|
2017-04-12 09:52:52 -05:00
|
|
|
builder.compile
|
|
|
|
|
2024-01-02 23:15:35 -06:00
|
|
|
Stylesheet::Manager.rm_cache_folder
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2018-06-05 22:53:53 -05:00
|
|
|
get "/stylesheets/#{builder.stylesheet_filename.sub(".css", "")}.css"
|
2017-08-30 23:06:56 -05:00
|
|
|
|
2018-06-05 22:53:53 -05:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2018-06-05 22:53:53 -05:00
|
|
|
get "/stylesheets/#{builder.stylesheet_filename_no_digest.sub(".css", "")}.css"
|
2017-08-30 23:06:56 -05:00
|
|
|
|
2018-06-05 22:53:53 -05:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
builder =
|
|
|
|
Stylesheet::Manager::Builder.new(target: :desktop_theme, theme: theme, manager: manager)
|
2017-04-12 09:52:52 -05:00
|
|
|
builder.compile
|
|
|
|
|
2024-01-02 23:15:35 -06:00
|
|
|
Stylesheet::Manager.rm_cache_folder
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2018-06-05 22:53:53 -05:00
|
|
|
get "/stylesheets/#{builder.stylesheet_filename.sub(".css", "")}.css"
|
2017-08-30 23:06:56 -05:00
|
|
|
|
2018-06-05 22:53:53 -05:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2018-06-05 22:53:53 -05:00
|
|
|
get "/stylesheets/#{builder.stylesheet_filename_no_digest.sub(".css", "")}.css"
|
2017-08-30 23:06:56 -05:00
|
|
|
|
2018-06-05 22:53:53 -05:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
2020-08-28 09:36:52 -05:00
|
|
|
|
2023-05-31 21:27:11 -05:00
|
|
|
context "when there are enabled plugins" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:user)
|
2023-05-31 21:27:11 -05:00
|
|
|
|
|
|
|
let(:plugin) do
|
2023-06-04 19:06:00 -05:00
|
|
|
plugin = plugin_from_fixtures("my_plugin")
|
2023-05-31 21:27:11 -05:00
|
|
|
plugin.register_css "body { padding: 1px 2px 3px 4px; }"
|
|
|
|
plugin
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
Discourse.plugins << plugin
|
|
|
|
plugin.activate!
|
|
|
|
Stylesheet::Importer.register_imports!
|
|
|
|
StylesheetCache.destroy_all
|
|
|
|
SiteSetting.has_login_hint = false
|
|
|
|
SiteSetting.allow_user_locale = true
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Discourse.plugins.delete(plugin)
|
|
|
|
Stylesheet::Importer.register_imports!
|
|
|
|
DiscoursePluginRegistry.reset!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can lookup plugin specific css" do
|
|
|
|
get "/"
|
|
|
|
|
|
|
|
html = Nokogiri::HTML5.fragment(response.body)
|
|
|
|
expect(html.at("link[data-target=my_plugin_rtl]")).to eq(nil)
|
|
|
|
|
|
|
|
href = html.at("link[data-target=my_plugin]").attribute("href").value
|
|
|
|
get href
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["Content-Type"]).to eq("text/css")
|
|
|
|
expect(response.body).to include("body{padding:1px 2px 3px 4px}")
|
|
|
|
expect(response.body).not_to include("body{padding:1px 4px 3px 2px}")
|
|
|
|
|
|
|
|
user.locale = "ar" # RTL locale
|
|
|
|
user.save!
|
|
|
|
get "/"
|
|
|
|
|
|
|
|
html = Nokogiri::HTML5.fragment(response.body)
|
|
|
|
expect(html.at("link[data-target=my_plugin]")).to eq(nil)
|
|
|
|
|
|
|
|
href = html.at("link[data-target=my_plugin_rtl]").attribute("href").value
|
|
|
|
get href
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["Content-Type"]).to eq("text/css")
|
|
|
|
expect(response.body).to include("body{padding:1px 4px 3px 2px}")
|
|
|
|
expect(response.body).not_to include("body{padding:1px 2px 3px 4px}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-07 09:46:35 -06:00
|
|
|
it "ignores Accept header and does not include Vary header" do
|
|
|
|
StylesheetCache.destroy_all
|
|
|
|
manager = Stylesheet::Manager.new(theme_id: nil)
|
|
|
|
builder = Stylesheet::Manager::Builder.new(target: "desktop", manager: manager, theme: nil)
|
|
|
|
builder.compile
|
|
|
|
|
|
|
|
digest = StylesheetCache.first.digest
|
|
|
|
|
|
|
|
get "/stylesheets/desktop_#{digest}.css"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["Content-Type"]).to eq("text/css")
|
|
|
|
expect(response.headers["Vary"]).to eq(nil)
|
|
|
|
|
|
|
|
get "/stylesheets/desktop_#{digest}.css", headers: { "Accept" => "text/html" }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["Content-Type"]).to eq("text/css")
|
|
|
|
expect(response.headers["Vary"]).to eq(nil)
|
|
|
|
|
|
|
|
get "/stylesheets/desktop_#{digest}.css", headers: { "Accept" => "invalidcontenttype" }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["Content-Type"]).to eq("text/css")
|
|
|
|
expect(response.headers["Vary"]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2022-07-27 05:21:10 -05:00
|
|
|
describe "#color_scheme" do
|
2020-08-28 09:36:52 -05:00
|
|
|
it "works as expected" do
|
|
|
|
scheme = ColorScheme.last
|
|
|
|
get "/color-scheme-stylesheet/#{scheme.id}.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["color_scheme_id"]).to eq(scheme.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works with a theme parameter" do
|
|
|
|
scheme = ColorScheme.last
|
|
|
|
theme = Theme.last
|
|
|
|
get "/color-scheme-stylesheet/#{scheme.id}/#{theme.id}.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["color_scheme_id"]).to eq(scheme.id)
|
|
|
|
end
|
|
|
|
end
|
2015-05-21 20:21:16 -05:00
|
|
|
end
|