DEV: improve usability of subfolder specs

Previously people were not consistent about mocking which left internals in
a fragile state when running subfolder specs.

This introduces a simple helper `set_subfolder` which you can use to set
the subfolder for the spec. It takes care of proper configuration of subfolder
and teardown.

```
# usage
set_subfolder "/my_amazing_subfolder"
```

You should no longer stub base_uri or global_settings
This commit is contained in:
Sam Saffron
2019-11-15 16:48:24 +11:00
parent 40d14ba166
commit e7cf4579a8
18 changed files with 38 additions and 68 deletions

View File

@@ -558,14 +558,12 @@ describe CookedPostProcessor do
end end
let(:cpp) { CookedPostProcessor.new(post, disable_loading_image: true) } let(:cpp) { CookedPostProcessor.new(post, disable_loading_image: true) }
let(:base_url) { "http://test.localhost/subfolder" }
let(:base_uri) { "/subfolder" }
before do before do
set_subfolder "/subfolder"
SiteSetting.max_image_height = 2000 SiteSetting.max_image_height = 2000
SiteSetting.create_thumbnails = true SiteSetting.create_thumbnails = true
Discourse.stubs(:base_url).returns(base_url)
Discourse.stubs(:base_uri).returns(base_uri)
FastImage.expects(:size).returns([1750, 2000]) FastImage.expects(:size).returns([1750, 2000])
OptimizedImage.expects(:resize).returns(true) OptimizedImage.expects(:resize).returns(true)

View File

@@ -114,11 +114,6 @@ describe FileStore::LocalStore do
end end
def stub_for_subfolder
GlobalSetting.stubs(:relative_url_root).returns('/forum')
Discourse.stubs(:base_uri).returns("/forum")
end
describe "#absolute_base_url" do describe "#absolute_base_url" do
it "is present" do it "is present" do
@@ -126,7 +121,7 @@ describe FileStore::LocalStore do
end end
it "supports subfolder" do it "supports subfolder" do
stub_for_subfolder set_subfolder "/forum"
expect(store.absolute_base_url).to eq("http://test.localhost/forum/uploads/default") expect(store.absolute_base_url).to eq("http://test.localhost/forum/uploads/default")
end end
@@ -139,7 +134,7 @@ describe FileStore::LocalStore do
end end
it "supports subfolder" do it "supports subfolder" do
stub_for_subfolder set_subfolder "/forum"
expect(store.relative_base_url).to eq("/forum/uploads/default") expect(store.relative_base_url).to eq("/forum/uploads/default")
end end

View File

@@ -386,8 +386,7 @@ describe FileStore::S3Store do
# none of this should matter at all # none of this should matter at all
# subfolder should not leak into uploads # subfolder should not leak into uploads
global_setting :relative_url_root, '/community' set_subfolder "/community"
Discourse.stubs(:base_uri).returns("/community")
url = "//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com/livechat/original/gif.png" url = "//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com/livechat/original/gif.png"

View File

@@ -269,12 +269,8 @@ describe PrettyText do
fab!(:user) { Fabricate(:user) } fab!(:user) { Fabricate(:user) }
context "subfolder" do context "subfolder" do
before do
GlobalSetting.stubs(:relative_url_root).returns("/forum")
Discourse.stubs(:base_uri).returns("/forum")
end
it "should have correct avatar url" do it "should have correct avatar url" do
set_subfolder "/forum"
md = <<~MD md = <<~MD
[quote="#{user.username}, post:123, topic:456, full:true"] [quote="#{user.username}, post:123, topic:456, full:true"]
ddd ddd
@@ -331,12 +327,9 @@ describe PrettyText do
end end
context 'subfolder' do context 'subfolder' do
before do
GlobalSetting.stubs(:relative_url_root).returns('/forum')
Discourse.stubs(:base_uri).returns("/forum")
end
it "handles user and group mentions correctly" do it "handles user and group mentions correctly" do
set_subfolder "/forum"
Fabricate(:user, username: 'user1') Fabricate(:user, username: 'user1')
Fabricate(:group, name: 'groupA', mentionable_level: Group::ALIAS_LEVELS[:everyone]) Fabricate(:group, name: 'groupA', mentionable_level: Group::ALIAS_LEVELS[:everyone])

View File

@@ -214,12 +214,8 @@ describe TopicView do
end end
context 'subfolder' do context 'subfolder' do
before do
GlobalSetting.stubs(:relative_url_root).returns('/forum')
Discourse.stubs(:base_uri).returns("/forum")
end
it "provides the correct absolute url" do it "provides the correct absolute url" do
set_subfolder "/forum"
expect(topic_view.absolute_url).to eq("http://test.localhost/forum/t/#{topic.slug}/#{topic.id}") expect(topic_view.absolute_url).to eq("http://test.localhost/forum/t/#{topic.slug}/#{topic.id}")
end end
end end

View File

@@ -33,7 +33,8 @@ describe UrlHelper do
store = stub store = stub
store.expects(:has_been_uploaded?).returns(false) store.expects(:has_been_uploaded?).returns(false)
Discourse.stubs(:store).returns(store) Discourse.stubs(:store).returns(store)
Discourse.stubs(:base_uri).returns("/subpath")
set_subfolder "/subpath"
expect(UrlHelper.is_local("/subpath/assets/javascripts/all.js")).to eq(true) expect(UrlHelper.is_local("/subpath/assets/javascripts/all.js")).to eq(true)
end end

View File

@@ -31,19 +31,15 @@ describe ApplicationHelper do
global_setting :s3_cdn_url, 'https://s3cdn.com' global_setting :s3_cdn_url, 'https://s3cdn.com'
end end
after do
ActionController::Base.config.relative_url_root = nil
end
it "deals correctly with subfolder" do it "deals correctly with subfolder" do
ActionController::Base.config.relative_url_root = "/community" set_subfolder "/community"
expect(helper.preload_script("application")).to include('https://s3cdn.com/assets/application.js') expect(helper.preload_script("application")).to include('https://s3cdn.com/assets/application.js')
end end
it "replaces cdn URLs with s3 cdn subfolder paths" do it "replaces cdn URLs with s3 cdn subfolder paths" do
global_setting :s3_cdn_url, 'https://s3cdn.com/s3_subpath' global_setting :s3_cdn_url, 'https://s3cdn.com/s3_subpath'
set_cdn_url "https://awesome.com" set_cdn_url "https://awesome.com"
ActionController::Base.config.relative_url_root = "/community" set_subfolder "/community"
expect(helper.preload_script("application")).to include('https://s3cdn.com/s3_subpath/assets/application.js') expect(helper.preload_script("application")).to include('https://s3cdn.com/s3_subpath/assets/application.js')
end end

View File

@@ -225,8 +225,7 @@ describe UserNotifications do
end end
it "supports subfolder" do it "supports subfolder" do
GlobalSetting.stubs(:relative_url_root).returns('/forum') set_subfolder "/forum"
Discourse.stubs(:base_uri).returns("/forum")
html = subject.html_part.body.to_s html = subject.html_part.body.to_s
text = subject.text_part.body.to_s text = subject.text_part.body.to_s
expect(html).to be_present expect(html).to be_present

View File

@@ -446,8 +446,7 @@ describe Category do
end end
it "correctly creates permalink when category slug is changed in subfolder install" do it "correctly creates permalink when category slug is changed in subfolder install" do
GlobalSetting.stubs(:relative_url_root).returns('/forum') set_subfolder '/forum'
Discourse.stubs(:base_uri).returns("/forum")
old_url = @category.url old_url = @category.url
@category.update(slug: 'new-category') @category.update(slug: 'new-category')
permalink = Permalink.last permalink = Permalink.last

View File

@@ -204,8 +204,7 @@ RSpec.describe ApplicationController do
end end
it 'supports subfolder with permalinks' do it 'supports subfolder with permalinks' do
GlobalSetting.stubs(:relative_url_root).returns('/forum') set_subfolder "/forum"
Discourse.stubs(:base_uri).returns("/forum")
trashed_topic = create_post.topic trashed_topic = create_post.topic
trashed_topic.trash! trashed_topic.trash!

View File

@@ -21,8 +21,7 @@ describe CategoriesController do
end end
it 'web crawler view has correct urls for subfolder install' do it 'web crawler view has correct urls for subfolder install' do
GlobalSetting.stubs(:relative_url_root).returns('/forum') set_subfolder "/forum"
Discourse.stubs(:base_uri).returns("/forum")
get '/categories', headers: { 'HTTP_USER_AGENT' => 'Googlebot' } get '/categories', headers: { 'HTTP_USER_AGENT' => 'Googlebot' }
html = Nokogiri::HTML(response.body) html = Nokogiri::HTML(response.body)
expect(html.css('body.crawler')).to be_present expect(html.css('body.crawler')).to be_present

View File

@@ -339,8 +339,7 @@ RSpec.describe ListController do
end end
it 'renders links correctly with subfolder' do it 'renders links correctly with subfolder' do
GlobalSetting.stubs(:relative_url_root).returns('/forum') set_subfolder "/forum"
Discourse.stubs(:base_uri).returns("/forum")
post = Fabricate(:post, topic: topic, user: user) post = Fabricate(:post, topic: topic, user: user)
get "/latest.rss" get "/latest.rss"
expect(response.status).to eq(200) expect(response.status).to eq(200)
@@ -458,8 +457,7 @@ RSpec.describe ListController do
end end
it "renders RSS in subfolder correctly" do it "renders RSS in subfolder correctly" do
GlobalSetting.stubs(:relative_url_root).returns('/forum') set_subfolder "/forum"
Discourse.stubs(:base_uri).returns("/forum")
get "/c/#{category.slug}.rss" get "/c/#{category.slug}.rss"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(response.body).to_not include("/forum/forum") expect(response.body).to_not include("/forum/forum")

View File

@@ -18,8 +18,7 @@ describe PermalinksController do
it "should work for subfolder installs too" do it "should work for subfolder installs too" do
permalink.update!(topic_id: topic.id) permalink.update!(topic_id: topic.id)
GlobalSetting.stubs(:relative_url_root).returns('/forum') set_subfolder "/forum"
Discourse.stubs(:base_uri).returns("/forum")
get "/#{permalink.url}" get "/#{permalink.url}"

View File

@@ -49,7 +49,8 @@ RSpec.describe RobotsTxtController do
context 'subfolder' do context 'subfolder' do
it 'prefixes the rules with the directory' do it 'prefixes the rules with the directory' do
Discourse.stubs(:base_uri).returns('/forum') set_subfolder "/forum"
get '/robots.txt' get '/robots.txt'
expect(response.body).to include("\nDisallow: /forum/admin") expect(response.body).to include("\nDisallow: /forum/admin")
end end

View File

@@ -1861,7 +1861,8 @@ RSpec.describe TopicsController do
describe 'clear_notifications' do describe 'clear_notifications' do
it 'correctly clears notifications if specified via cookie' do it 'correctly clears notifications if specified via cookie' do
Discourse.stubs(:base_uri).returns("/eviltrout") set_subfolder "/eviltrout"
notification = Fabricate(:notification) notification = Fabricate(:notification)
sign_in(notification.user) sign_in(notification.user)
@@ -2146,8 +2147,7 @@ RSpec.describe TopicsController do
end end
it 'renders rss of the topic correctly with subfolder' do it 'renders rss of the topic correctly with subfolder' do
GlobalSetting.stubs(:relative_url_root).returns('/forum') set_subfolder "/forum"
Discourse.stubs(:base_uri).returns("/forum")
get "/t/foo/#{topic.id}.rss" get "/t/foo/#{topic.id}.rss"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(response.body).to_not include("/forum/forum") expect(response.body).to_not include("/forum/forum")

View File

@@ -192,16 +192,9 @@ describe UserBadgesController do
end end
describe 'with relative_url_root' do describe 'with relative_url_root' do
before do
@orig_relative_url_root = ActionController::Base.config.relative_url_root
ActionController::Base.config.relative_url_root = "/discuss"
end
after do
ActionController::Base.config.relative_url_root = @orig_relative_url_root
end
it 'grants badge when valid post/topic link is given in reason' do it 'grants badge when valid post/topic link is given in reason' do
set_subfolder "/discuss"
admin = Fabricate(:admin) admin = Fabricate(:admin)
post = create_post post = create_post

View File

@@ -248,12 +248,7 @@ RSpec.describe InlineUploads do
context "subfolder" do context "subfolder" do
before do before do
global_setting :relative_url_root, "/community" set_subfolder "/community"
ActionController::Base.config.relative_url_root = "/community"
end
after do
ActionController::Base.config.relative_url_root = nil
end end
it "should correct subfolder images" do it "should correct subfolder images" do

View File

@@ -129,4 +129,14 @@ module Helpers
ensure ensure
$stdout = old_stdout $stdout = old_stdout
end end
def set_subfolder(f)
global_setting :relative_url_root, f
old_root = ActionController::Base.config.relative_url_root
ActionController::Base.config.relative_url_root = f
before_next_spec do
ActionController::Base.config.relative_url_root = old_root
end
end
end end