2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe StaticController do
|
|
|
|
|
2016-12-05 00:08:36 -05:00
|
|
|
context 'brotli_asset' do
|
2017-03-20 15:59:06 -04:00
|
|
|
it 'returns a non brotli encoded 404 if asset is missing' do
|
2016-12-15 00:05:20 -05:00
|
|
|
|
|
|
|
get :brotli_asset, path: 'missing.js'
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.headers['Content-Encoding']).not_to eq('br')
|
2017-03-20 15:59:06 -04:00
|
|
|
expect(response.headers["Cache-Control"]).to match(/max-age=1/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can handle fallback brotli assets' do
|
|
|
|
begin
|
|
|
|
assets_path = Rails.root.join("tmp/backup_assets")
|
|
|
|
|
|
|
|
GlobalSetting.stubs(:fallback_assets_path).returns(assets_path.to_s)
|
|
|
|
|
|
|
|
FileUtils.mkdir_p(assets_path)
|
|
|
|
|
|
|
|
file_path = assets_path.join("test.js.br")
|
|
|
|
File.write(file_path, 'fake brotli file')
|
|
|
|
|
|
|
|
get :brotli_asset, path: 'test.js'
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["Cache-Control"]).to match(/public/)
|
|
|
|
ensure
|
|
|
|
File.delete(file_path)
|
|
|
|
end
|
2016-12-15 00:05:20 -05:00
|
|
|
end
|
|
|
|
|
2016-12-05 00:08:36 -05:00
|
|
|
it 'has correct headers for brotli assets' do
|
2016-12-05 00:37:33 -05:00
|
|
|
begin
|
|
|
|
assets_path = Rails.root.join("public/assets")
|
2016-12-05 00:08:36 -05:00
|
|
|
|
2016-12-05 00:37:33 -05:00
|
|
|
FileUtils.mkdir_p(assets_path)
|
2016-12-05 00:08:36 -05:00
|
|
|
|
2016-12-05 00:37:33 -05:00
|
|
|
file_path = assets_path.join("test.js.br")
|
|
|
|
File.write(file_path, 'fake brotli file')
|
|
|
|
|
|
|
|
get :brotli_asset, path: 'test.js'
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["Cache-Control"]).to match(/public/)
|
|
|
|
ensure
|
|
|
|
File.delete(file_path)
|
|
|
|
end
|
2016-12-05 00:08:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-30 16:37:22 -04:00
|
|
|
context 'show' do
|
2014-07-24 14:27:34 -04:00
|
|
|
before do
|
|
|
|
post = create_post
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.tos_topic_id = post.topic.id
|
|
|
|
SiteSetting.guidelines_topic_id = post.topic.id
|
|
|
|
SiteSetting.privacy_topic_id = post.topic.id
|
2014-07-24 14:27:34 -04:00
|
|
|
end
|
|
|
|
|
2013-10-30 16:37:22 -04:00
|
|
|
context "with a static file that's present" do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-10-30 16:37:22 -04:00
|
|
|
before do
|
|
|
|
xhr :get, :show, id: 'faq'
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-10-30 16:37:22 -04:00
|
|
|
it 'renders the static file if present' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-10-30 16:37:22 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-10-30 16:37:22 -04:00
|
|
|
it "renders the file" do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to render_template('static/show')
|
|
|
|
expect(assigns(:page)).to eq('faq')
|
2013-07-23 14:42:52 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-10-30 16:37:22 -04:00
|
|
|
[ ['tos', :tos_url], ['privacy', :privacy_policy_url] ].each do |id, setting_name|
|
|
|
|
context "#{id}" do
|
|
|
|
subject { xhr :get, :show, id: id }
|
2013-06-18 10:52:04 -04:00
|
|
|
|
2013-10-30 16:37:22 -04:00
|
|
|
context "when #{setting_name} site setting is NOT set" do
|
|
|
|
it "renders the #{id} page" do
|
2014-07-24 14:27:34 -04:00
|
|
|
expect(subject).to render_template("static/show")
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(assigns(:page)).to eq(id)
|
2013-07-23 14:42:52 -04:00
|
|
|
end
|
2013-06-18 10:52:04 -04:00
|
|
|
end
|
|
|
|
|
2013-10-30 16:37:22 -04:00
|
|
|
context "when #{setting_name} site setting is set" do
|
2017-07-07 02:09:14 -04:00
|
|
|
before { SiteSetting.public_send("#{setting_name}=", 'http://example.com/page') }
|
2013-06-18 10:52:04 -04:00
|
|
|
|
2013-10-30 16:37:22 -04:00
|
|
|
it "redirects to the #{setting_name}" do
|
|
|
|
expect(subject).to redirect_to('http://example.com/page')
|
|
|
|
end
|
2013-06-18 10:52:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-30 16:37:22 -04:00
|
|
|
context "with a missing file" do
|
|
|
|
it "should respond 404" do
|
|
|
|
xhr :get, :show, id: 'does-not-exist'
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.response_code).to eq(404)
|
2013-10-30 16:37:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should redirect to / when logged in and path is /login' do
|
|
|
|
log_in
|
|
|
|
xhr :get, :show, id: 'login'
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to redirect_to '/'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-26 17:16:08 -04:00
|
|
|
it "should display the login template when login is required" do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.login_required = true
|
2014-07-26 17:16:08 -04:00
|
|
|
xhr :get, :show, id: 'login'
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-07-26 17:16:08 -04:00
|
|
|
end
|
2017-03-08 05:30:49 -05:00
|
|
|
|
|
|
|
context "when login_required is enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.login_required = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'faq page redirects to login page for anon' do
|
|
|
|
xhr :get, :show, id: 'faq'
|
|
|
|
expect(response).to redirect_to '/login'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'guidelines page redirects to login page for anon' do
|
|
|
|
xhr :get, :show, id: 'guidelines'
|
|
|
|
expect(response).to redirect_to '/login'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'faq page loads for logged in user' do
|
|
|
|
log_in
|
|
|
|
xhr :get, :show, id: 'faq'
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(response).to render_template('static/show')
|
|
|
|
expect(assigns(:page)).to eq('faq')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'guidelines page loads for logged in user' do
|
|
|
|
log_in
|
|
|
|
xhr :get, :show, id: 'guidelines'
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(response).to render_template('static/show')
|
|
|
|
expect(assigns(:page)).to eq('faq')
|
|
|
|
end
|
|
|
|
end
|
2014-07-26 17:16:08 -04:00
|
|
|
end
|
2013-10-30 16:37:22 -04:00
|
|
|
|
2013-06-04 18:34:54 -04:00
|
|
|
describe '#enter' do
|
|
|
|
context 'without a redirect path' do
|
|
|
|
it 'redirects to the root url' do
|
|
|
|
xhr :post, :enter
|
2013-07-01 14:00:06 -04:00
|
|
|
expect(response).to redirect_to '/'
|
2013-06-04 18:34:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a redirect path' do
|
|
|
|
it 'redirects to the redirect path' do
|
|
|
|
xhr :post, :enter, redirect: '/foo'
|
|
|
|
expect(response).to redirect_to '/foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-28 17:45:13 -04:00
|
|
|
context 'with a full url' do
|
|
|
|
it 'redirects to the correct path' do
|
|
|
|
xhr :post, :enter, redirect: "#{Discourse.base_url}/foo"
|
|
|
|
expect(response).to redirect_to '/foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-30 11:31:44 -04:00
|
|
|
context 'with a period to force a new host' do
|
|
|
|
it 'redirects to the root path' do
|
|
|
|
xhr :post, :enter, redirect: ".org/foo"
|
|
|
|
expect(response).to redirect_to '/'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-28 17:45:13 -04:00
|
|
|
context 'with a full url to someone else' do
|
|
|
|
it 'redirects to the root path' do
|
|
|
|
xhr :post, :enter, redirect: "http://eviltrout.com/foo"
|
|
|
|
expect(response).to redirect_to '/'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an invalid URL' do
|
|
|
|
it "redirects to the root" do
|
|
|
|
xhr :post, :enter, redirect: "javascript:alert('trout')"
|
|
|
|
expect(response).to redirect_to '/'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-04 18:34:54 -04:00
|
|
|
context 'when the redirect path is the login page' do
|
|
|
|
it 'redirects to the root url' do
|
|
|
|
xhr :post, :enter, redirect: login_path
|
2013-07-01 14:00:06 -04:00
|
|
|
expect(response).to redirect_to '/'
|
2013-06-04 18:34:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|