mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 09:22:42 +08:00
FEATURE: Allow theme tests to be run in production (take 2) (#12845)
This commit allows site admins to run theme tests in production via a new `/theme-qunit` route. When you visit `/theme-qunit`, you'll see a list of the themes/components installed on your site that have tests, and from there you can select a theme or component that you run its tests. We also have a new rake task `themes:install_and_test` that can be used to install a list of themes/components on a temporary database and run the tests of the themes/components that are installed. This rake task can be useful when upgrading/deploying a Discourse instance to make sure that the installed themes/components are compatible with the new Discourse version being deployed, and if the tests fail you can abort the build/deploy process so you don't end up with a broken site.
This commit is contained in:
@ -2,9 +2,19 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe ThemeJavascriptsController do
|
||||
include ActiveSupport::Testing::TimeHelpers
|
||||
|
||||
def clear_disk_cache
|
||||
if Dir.exist?(ThemeJavascriptsController::DISK_CACHE_PATH)
|
||||
`rm -rf #{ThemeJavascriptsController::DISK_CACHE_PATH}`
|
||||
end
|
||||
end
|
||||
|
||||
let!(:theme) { Fabricate(:theme) }
|
||||
let(:theme_field) { ThemeField.create!(theme: theme, target_id: 0, name: "header", value: "<a>html</a>") }
|
||||
let(:javascript_cache) { JavascriptCache.create!(content: 'console.log("hello");', theme_field: theme_field) }
|
||||
before { clear_disk_cache }
|
||||
after { clear_disk_cache }
|
||||
|
||||
describe '#show' do
|
||||
def update_digest_and_get(digest)
|
||||
@ -47,31 +57,83 @@ describe ThemeJavascriptsController do
|
||||
get "/theme-javascripts/#{javascript_cache.digest}.js"
|
||||
expect(response.status).to eq(404)
|
||||
end
|
||||
|
||||
def clear_disk_cache
|
||||
if Dir.exist?(ThemeJavascriptsController::DISK_CACHE_PATH)
|
||||
`rm #{ThemeJavascriptsController::DISK_CACHE_PATH}/*`
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#show_tests" do
|
||||
context "theme settings" do
|
||||
let(:component) { Fabricate(:theme, component: true, name: 'enabled-component') }
|
||||
let(:component) { Fabricate(:theme, component: true, name: 'enabled-component') }
|
||||
let!(:tests_field) do
|
||||
field = component.set_field(
|
||||
target: :tests_js,
|
||||
type: :js,
|
||||
name: "acceptance/some-test.js",
|
||||
value: "assert.ok(true);"
|
||||
)
|
||||
component.save!
|
||||
field
|
||||
end
|
||||
|
||||
it "forces default values" do
|
||||
ThemeField.create!(
|
||||
theme: component,
|
||||
target_id: Theme.targets[:settings],
|
||||
name: "yaml",
|
||||
value: "num_setting: 5"
|
||||
)
|
||||
component.reload
|
||||
component.update_setting(:num_setting, 643)
|
||||
before do
|
||||
ThemeField.create!(
|
||||
theme: component,
|
||||
target_id: Theme.targets[:settings],
|
||||
name: "yaml",
|
||||
value: "num_setting: 5"
|
||||
)
|
||||
component.save!
|
||||
end
|
||||
|
||||
get "/theme-javascripts/tests/#{component.id}.js"
|
||||
expect(response.body).to include("require(\"discourse/lib/theme-settings-store\").registerSettings(#{component.id}, {\"num_setting\":5}, { force: true });")
|
||||
end
|
||||
it "forces theme settings default values" do
|
||||
component.update_setting(:num_setting, 643)
|
||||
_, digest = component.baked_js_tests_with_digest
|
||||
|
||||
get "/theme-javascripts/tests/#{component.id}-#{digest}.js"
|
||||
expect(response.body).to include("require(\"discourse/lib/theme-settings-store\").registerSettings(#{component.id}, {\"num_setting\":5}, { force: true });")
|
||||
expect(response.body).to include("assert.ok(true);")
|
||||
end
|
||||
|
||||
it "responds with 404 if digest is not a 40 chars hex" do
|
||||
digest = Rack::Utils.escape('../../../../../../../../../../etc/passwd').gsub('.', '%2E')
|
||||
get "/theme-javascripts/tests/#{component.id}-#{digest}.js"
|
||||
expect(response.status).to eq(404)
|
||||
|
||||
get "/theme-javascripts/tests/#{component.id}-abc123.js"
|
||||
expect(response.status).to eq(404)
|
||||
end
|
||||
|
||||
it "responds with 404 if theme does not exist" do
|
||||
get "/theme-javascripts/tests/#{Theme.maximum(:id) + 1}-#{SecureRandom.hex(20)}.js"
|
||||
expect(response.status).to eq(404)
|
||||
end
|
||||
|
||||
it "responds with 304 if tests digest has not changed" do
|
||||
content, digest = component.baked_js_tests_with_digest
|
||||
get "/theme-javascripts/tests/#{component.id}-#{digest}.js"
|
||||
last_modified = Time.rfc2822(response.headers["Last-Modified"])
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.headers["Content-Length"].to_i).to eq(content.size)
|
||||
|
||||
get "/theme-javascripts/tests/#{component.id}-#{digest}.js",
|
||||
headers: { "If-Modified-Since" => (last_modified + 10.seconds).rfc2822 }
|
||||
expect(response.status).to eq(304)
|
||||
end
|
||||
|
||||
it "responds with 404 to requests with old digests" do
|
||||
_, old_digest = component.baked_js_tests_with_digest
|
||||
get "/theme-javascripts/tests/#{component.id}-#{old_digest}.js"
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.body).to include("assert.ok(true);")
|
||||
|
||||
tests_field.update!(value: "assert.ok(343434);")
|
||||
tests_field.invalidate_baked!
|
||||
_, digest = component.baked_js_tests_with_digest
|
||||
expect(old_digest).not_to eq(digest)
|
||||
|
||||
get "/theme-javascripts/tests/#{component.id}-#{old_digest}.js"
|
||||
expect(response.status).to eq(404)
|
||||
|
||||
get "/theme-javascripts/tests/#{component.id}-#{digest}.js"
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.body).to include("assert.ok(343434);")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user