DEV: Apply syntax_tree formatting to spec/*

This commit is contained in:
David Taylor
2023-01-09 11:18:21 +00:00
parent 0cf6421716
commit cb932d6ee1
907 changed files with 58693 additions and 45909 deletions

View File

@@ -3,79 +3,70 @@
RSpec.describe StaticController do
fab!(:upload) { Fabricate(:upload) }
describe '#favicon' do
let(:filename) { 'smallest.png' }
describe "#favicon" do
let(:filename) { "smallest.png" }
let(:file) { file_from_fixtures(filename) }
let(:upload) do
UploadCreator.new(file, filename).create_for(Discourse.system_user.id)
end
let(:upload) { UploadCreator.new(file, filename).create_for(Discourse.system_user.id) }
after do
Discourse.redis.scan_each(match: "memoize_*").each do |key|
Discourse.redis.del(key)
end
end
after { Discourse.redis.scan_each(match: "memoize_*").each { |key| Discourse.redis.del(key) } }
context 'with local store' do
it 'returns the default favicon if favicon has not been configured' do
get '/favicon/proxied'
context "with local store" do
it "returns the default favicon if favicon has not been configured" do
get "/favicon/proxied"
expect(response.status).to eq(200)
expect(response.media_type).to eq('image/png')
expect(response.media_type).to eq("image/png")
expect(response.body.bytesize).to eq(SiteIconManager.favicon.filesize)
end
it 'returns the configured favicon' do
it "returns the configured favicon" do
SiteSetting.favicon = upload
get '/favicon/proxied'
get "/favicon/proxied"
expect(response.status).to eq(200)
expect(response.media_type).to eq('image/png')
expect(response.media_type).to eq("image/png")
expect(response.body.bytesize).to eq(upload.filesize)
end
end
context 'with external store' do
context "with external store" do
let(:upload) do
Upload.create!(
url: '//s3-upload-bucket.s3-us-east-1.amazonaws.com/somewhere/a.png',
url: "//s3-upload-bucket.s3-us-east-1.amazonaws.com/somewhere/a.png",
original_filename: filename,
filesize: file.size,
user_id: Discourse.system_user.id
user_id: Discourse.system_user.id,
)
end
before do
setup_s3
end
before { setup_s3 }
it 'can proxy a favicon correctly' do
it "can proxy a favicon correctly" do
SiteSetting.favicon = upload
stub_request(:get, "https:/#{upload.url}")
.to_return(status: 200, body: file)
stub_request(:get, "https:/#{upload.url}").to_return(status: 200, body: file)
get '/favicon/proxied'
get "/favicon/proxied"
expect(response.status).to eq(200)
expect(response.media_type).to eq('image/png')
expect(response.media_type).to eq("image/png")
expect(response.body.bytesize).to eq(upload.filesize)
end
end
end
describe '#brotli_asset' do
it 'returns a non brotli encoded 404 if asset is missing' do
describe "#brotli_asset" do
it "returns a non brotli encoded 404 if asset is missing" do
get "/brotli_asset/missing.js"
expect(response.status).to eq(404)
expect(response.headers['Content-Encoding']).not_to eq('br')
expect(response.headers['Cache-Control']).to match(/max-age=1/)
expect(response.headers["Content-Encoding"]).not_to eq("br")
expect(response.headers["Cache-Control"]).to match(/max-age=1/)
end
it 'can handle fallback brotli assets' do
it "can handle fallback brotli assets" do
begin
assets_path = Rails.root.join("tmp/backup_assets")
@@ -84,7 +75,7 @@ RSpec.describe StaticController do
FileUtils.mkdir_p(assets_path)
file_path = assets_path.join("test.js.br")
File.write(file_path, 'fake brotli file')
File.write(file_path, "fake brotli file")
get "/brotli_asset/test.js"
@@ -95,14 +86,14 @@ RSpec.describe StaticController do
end
end
it 'has correct headers for brotli assets' do
it "has correct headers for brotli assets" do
begin
assets_path = Rails.root.join("public/assets")
FileUtils.mkdir_p(assets_path)
file_path = assets_path.join("test.js.br")
File.write(file_path, 'fake brotli file')
File.write(file_path, "fake brotli file")
get "/brotli_asset/test.js"
@@ -113,14 +104,14 @@ RSpec.describe StaticController do
end
end
it 'has correct cors headers for brotli assets' do
it "has correct cors headers for brotli assets" do
begin
assets_path = Rails.root.join("public/assets")
FileUtils.mkdir_p(assets_path)
file_path = assets_path.join("test.js.br")
File.write(file_path, 'fake brotli file')
File.write(file_path, "fake brotli file")
GlobalSetting.stubs(:cdn_url).returns("https://www.example.com/")
get "/brotli_asset/test.js"
@@ -133,17 +124,19 @@ RSpec.describe StaticController do
end
end
describe '#cdn_asset' do
let (:site) { RailsMultisite::ConnectionManagement.current_db }
describe "#cdn_asset" do
let (:site) {
RailsMultisite::ConnectionManagement.current_db
}
it 'can serve assets' do
it "can serve assets" do
begin
assets_path = Rails.root.join("public/assets")
FileUtils.mkdir_p(assets_path)
file_path = assets_path.join("test.js.br")
File.write(file_path, 'fake brotli file')
File.write(file_path, "fake brotli file")
get "/cdn_asset/#{site}/test.js.br"
@@ -155,7 +148,7 @@ RSpec.describe StaticController do
end
end
describe '#show' do
describe "#show" do
before do
post = create_post
SiteSetting.tos_topic_id = post.topic.id
@@ -168,16 +161,15 @@ RSpec.describe StaticController do
get "/faq"
expect(response.status).to eq(200)
expect(response.body).to include(I18n.t('js.faq'))
expect(response.body).to include(I18n.t("js.faq"))
expect(response.body).to include("<title>FAQ - Discourse</title>")
end
end
[
['tos', :tos_url, I18n.t('js.tos')],
['privacy', :privacy_policy_url, I18n.t('js.privacy')]
["tos", :tos_url, I18n.t("js.tos")],
["privacy", :privacy_policy_url, I18n.t("js.privacy")],
].each do |id, setting_name, text|
context "#{id}" do
context "when #{setting_name} site setting is NOT set" do
it "renders the #{id} page" do
@@ -189,14 +181,12 @@ RSpec.describe StaticController do
end
context "when #{setting_name} site setting is set" do
before do
SiteSetting.set(setting_name, 'http://example.com/page')
end
before { SiteSetting.set(setting_name, "http://example.com/page") }
it "redirects to the #{setting_name}" do
get "/#{id}"
expect(response).to redirect_to('http://example.com/page')
expect(response).to redirect_to("http://example.com/page")
end
end
end
@@ -221,10 +211,10 @@ RSpec.describe StaticController do
end
end
it 'should redirect to / when logged in and path is /login' do
it "should redirect to / when logged in and path is /login" do
sign_in(Fabricate(:user))
get "/login"
expect(response).to redirect_to('/')
expect(response).to redirect_to("/")
end
it "should display the login template when login is required" do
@@ -234,25 +224,23 @@ RSpec.describe StaticController do
expect(response.status).to eq(200)
expect(response.body).to include(PrettyText.cook(I18n.t(
'login_required.welcome_message', title: SiteSetting.title
)))
expect(response.body).to include(
PrettyText.cook(I18n.t("login_required.welcome_message", title: SiteSetting.title)),
)
end
context "when login_required is enabled" do
before do
SiteSetting.login_required = true
end
before { SiteSetting.login_required = true }
['faq', 'guidelines', 'rules', 'conduct'].each do |page_name|
%w[faq guidelines rules conduct].each do |page_name|
it "#{page_name} page redirects to login page for anon" do
get "/#{page_name}"
expect(response).to redirect_to '/login'
expect(response).to redirect_to "/login"
end
it "#{page_name} page redirects to login page for anon" do
get "/#{page_name}"
expect(response).to redirect_to '/login'
expect(response).to redirect_to "/login"
end
it "#{page_name} page loads for logged in user" do
@@ -261,14 +249,14 @@ RSpec.describe StaticController do
get "/#{page_name}"
expect(response.status).to eq(200)
expect(response.body).to include(I18n.t('js.guidelines'))
expect(response.body).to include(I18n.t("js.guidelines"))
end
end
end
context "with crawler view" do
it "should include correct title" do
get '/faq', headers: { 'HTTP_USER_AGENT' => 'Googlebot' }
get "/faq", headers: { "HTTP_USER_AGENT" => "Googlebot" }
expect(response.status).to eq(200)
expect(response.body).to include("<title>FAQ - Discourse</title>")
end
@@ -281,9 +269,7 @@ RSpec.describe StaticController do
end
it "adds new topic-backed pages" do
routes = Proc.new do
get "contact" => "static#show", id: "contact"
end
routes = Proc.new { get "contact" => "static#show", :id => "contact" }
Discourse::Application.routes.send(:eval_block, routes)
topic_id = Fabricate(:post, cooked: "contact info").topic_id
@@ -303,9 +289,11 @@ RSpec.describe StaticController do
polish_topic_id = Fabricate(:post, cooked: "Polish FAQ").topic_id
SiteSetting.setting(:test_polish_faq_topic_id, polish_topic_id)
Plugin::Instance.new.add_topic_static_page("faq") do
current_user&.locale == "pl" ? "test_polish_faq_topic_id" : "test_faq_topic_id"
end
Plugin::Instance
.new
.add_topic_static_page("faq") do
current_user&.locale == "pl" ? "test_polish_faq_topic_id" : "test_faq_topic_id"
end
get "/faq"
@@ -331,72 +319,70 @@ RSpec.describe StaticController do
end
end
describe '#enter' do
context 'without a redirect path' do
it 'redirects to the root url' do
describe "#enter" do
context "without a redirect path" do
it "redirects to the root url" do
post "/login.json"
expect(response).to redirect_to('/')
expect(response).to redirect_to("/")
end
end
context 'with a redirect path' do
it 'redirects to the redirect path' do
post "/login.json", params: { redirect: '/foo' }
expect(response).to redirect_to('/foo')
context "with a redirect path" do
it "redirects to the redirect path" do
post "/login.json", params: { redirect: "/foo" }
expect(response).to redirect_to("/foo")
end
end
context 'with a full url' do
it 'redirects to the correct path' do
context "with a full url" do
it "redirects to the correct path" do
post "/login.json", params: { redirect: "#{Discourse.base_url}/foo" }
expect(response).to redirect_to('/foo')
expect(response).to redirect_to("/foo")
end
end
context 'with a redirect path with query params' do
it 'redirects to the redirect path and preserves query params' do
post "/login.json", params: { redirect: '/foo?bar=1' }
expect(response).to redirect_to('/foo?bar=1')
context "with a redirect path with query params" do
it "redirects to the redirect path and preserves query params" do
post "/login.json", params: { redirect: "/foo?bar=1" }
expect(response).to redirect_to("/foo?bar=1")
end
end
context 'with a period to force a new host' do
it 'redirects to the root path' do
context "with a period to force a new host" do
it "redirects to the root path" do
post "/login.json", params: { redirect: ".org/foo" }
expect(response).to redirect_to('/')
expect(response).to redirect_to("/")
end
end
context 'with a full url to someone else' do
it 'redirects to the root path' do
context "with a full url to someone else" do
it "redirects to the root path" do
post "/login.json", params: { redirect: "http://eviltrout.com/foo" }
expect(response).to redirect_to('/')
expect(response).to redirect_to("/")
end
end
context 'with an invalid URL' do
context "with an invalid URL" do
it "redirects to the root" do
post "/login.json", params: { redirect: "javascript:alert('trout')" }
expect(response).to redirect_to('/')
expect(response).to redirect_to("/")
end
end
context 'with an array' do
context "with an array" do
it "redirects to the root" do
post "/login.json", params: { redirect: ["/foo"] }
expect(response.status).to eq(400)
json = response.parsed_body
expect(json["errors"]).to be_present
expect(json["errors"]).to include(
I18n.t("invalid_params", message: "redirect")
)
expect(json["errors"]).to include(I18n.t("invalid_params", message: "redirect"))
end
end
context 'when the redirect path is the login page' do
it 'redirects to the root url' do
context "when the redirect path is the login page" do
it "redirects to the root url" do
post "/login.json", params: { redirect: login_path }
expect(response).to redirect_to('/')
expect(response).to redirect_to("/")
end
end
end
@@ -410,23 +396,32 @@ RSpec.describe StaticController do
end
it "replaces sourcemap URL" do
Rails.application.assets_manifest.stubs(:find_sources).with("service-worker.js").returns([
<<~JS
Rails
.application
.assets_manifest
.stubs(:find_sources)
.with("service-worker.js")
.returns([<<~JS])
someFakeServiceWorkerSource();
//# sourceMappingURL=service-worker-abcde.js.map
JS
])
{
'/assets/service-worker.js' => '/assets/service-worker-abcde.js.map',
'/assets/service-worker.js.br' => '/assets/service-worker-abcde.js.map',
'/assets/service-worker.br.js' => '/assets/service-worker-abcde.js.map',
'/assets/service-worker.js.gz' => '/assets/service-worker-abcde.js.map',
'/assets/service-worker.gz.js' => '/assets/service-worker-abcde.js.map',
'https://example.com/assets/service-worker.js' => 'https://example.com/assets/service-worker-abcde.js.map',
'https://example.com/subfolder/assets/service-worker.js' => 'https://example.com/subfolder/assets/service-worker-abcde.js.map',
"/assets/service-worker.js" => "/assets/service-worker-abcde.js.map",
"/assets/service-worker.js.br" => "/assets/service-worker-abcde.js.map",
"/assets/service-worker.br.js" => "/assets/service-worker-abcde.js.map",
"/assets/service-worker.js.gz" => "/assets/service-worker-abcde.js.map",
"/assets/service-worker.gz.js" => "/assets/service-worker-abcde.js.map",
"https://example.com/assets/service-worker.js" =>
"https://example.com/assets/service-worker-abcde.js.map",
"https://example.com/subfolder/assets/service-worker.js" =>
"https://example.com/subfolder/assets/service-worker-abcde.js.map",
}.each do |asset_path, expected_map_url|
ActionController::Base.helpers.stubs(:asset_path).with("service-worker.js").returns(asset_path)
ActionController::Base
.helpers
.stubs(:asset_path)
.with("service-worker.js")
.returns(asset_path)
get "/service-worker.js"
expect(response.status).to eq(200)