Commit 0af73977 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'rs-fix-application-helper-specs' into 'master'

Fix ApplicationHelper specs

On current `master` if you run these specs by themselves, they fail. I have no idea why they work when run with other specs, but now they work all the time.

- Use the `helper` object, as per RSpec 3 standards
- Use `assign` to assign instance variables that helpers expect
- Add `StubConfiguration` support module

See merge request !906
parents a373422a 42b643f0
...@@ -2,157 +2,177 @@ require 'spec_helper' ...@@ -2,157 +2,177 @@ require 'spec_helper'
describe ApplicationHelper do describe ApplicationHelper do
describe 'current_controller?' do describe 'current_controller?' do
before do
allow(controller).to receive(:controller_name).and_return('foo')
end
it 'returns true when controller matches argument' do it 'returns true when controller matches argument' do
expect(current_controller?(:foo)).to be_truthy stub_controller_name('foo')
expect(helper.current_controller?(:foo)).to eq true
end end
it 'returns false when controller does not match argument' do it 'returns false when controller does not match argument' do
expect(current_controller?(:bar)).not_to be_truthy stub_controller_name('foo')
expect(helper.current_controller?(:bar)).to eq false
end end
it 'should take any number of arguments' do it 'takes any number of arguments' do
expect(current_controller?(:baz, :bar)).not_to be_truthy stub_controller_name('foo')
expect(current_controller?(:baz, :bar, :foo)).to be_truthy
expect(helper.current_controller?(:baz, :bar)).to eq false
expect(helper.current_controller?(:baz, :bar, :foo)).to eq true
end
def stub_controller_name(value)
allow(helper.controller).to receive(:controller_name).and_return(value)
end end
end end
describe 'current_action?' do describe 'current_action?' do
before do it 'returns true when action matches' do
allow(self).to receive(:action_name).and_return('foo') stub_action_name('foo')
expect(helper.current_action?(:foo)).to eq true
end end
it 'returns true when action matches argument' do it 'returns false when action does not match' do
expect(current_action?(:foo)).to be_truthy stub_action_name('foo')
expect(helper.current_action?(:bar)).to eq false
end end
it 'returns false when action does not match argument' do it 'takes any number of arguments' do
expect(current_action?(:bar)).not_to be_truthy stub_action_name('foo')
expect(helper.current_action?(:baz, :bar)).to eq false
expect(helper.current_action?(:baz, :bar, :foo)).to eq true
end end
it 'should take any number of arguments' do def stub_action_name(value)
expect(current_action?(:baz, :bar)).not_to be_truthy allow(helper).to receive(:action_name).and_return(value)
expect(current_action?(:baz, :bar, :foo)).to be_truthy
end end
end end
describe 'project_icon' do describe 'project_icon' do
avatar_file_path = File.join(Rails.root, 'spec', 'fixtures', 'banana_sample.gif') let(:avatar_file_path) { File.join(Rails.root, 'spec', 'fixtures', 'banana_sample.gif') }
it 'should return an url for the avatar' do it 'should return an url for the avatar' do
project = create(:project) project = create(:project, avatar: File.open(avatar_file_path))
project.avatar = File.open(avatar_file_path)
project.save! avatar_url = "http://localhost/uploads/project/avatar/#{project.id}/banana_sample.gif"
avatar_url = "http://localhost/uploads/project/avatar/#{ project.id }/banana_sample.gif" expect(helper.project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).
expect(project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).to eq( to eq "<img alt=\"Banana sample\" src=\"#{avatar_url}\" />"
"<img alt=\"Banana sample\" src=\"#{avatar_url}\" />"
)
end end
it 'should give uploaded icon when present' do it 'should give uploaded icon when present' do
project = create(:project) project = create(:project)
project.save!
allow_any_instance_of(Project).to receive(:avatar_in_git).and_return(true) allow_any_instance_of(Project).to receive(:avatar_in_git).and_return(true)
avatar_url = 'http://localhost' + namespace_project_avatar_path(project.namespace, project) avatar_url = 'http://localhost' + namespace_project_avatar_path(project.namespace, project)
expect(project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).to match( expect(helper.project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).to match(
image_tag(avatar_url)) image_tag(avatar_url))
end end
end end
describe 'avatar_icon' do describe 'avatar_icon' do
avatar_file_path = File.join(Rails.root, 'spec', 'fixtures', 'banana_sample.gif') let(:avatar_file_path) { File.join(Rails.root, 'spec', 'fixtures', 'banana_sample.gif') }
it 'should return an url for the avatar' do it 'should return an url for the avatar' do
user = create(:user) user = create(:user, avatar: File.open(avatar_file_path))
user.avatar = File.open(avatar_file_path)
user.save! expect(helper.avatar_icon(user.email).to_s).
expect(avatar_icon(user.email).to_s). to match("/uploads/user/avatar/#{user.id}/banana_sample.gif")
to match("/uploads/user/avatar/#{ user.id }/banana_sample.gif")
end end
it 'should return an url for the avatar with relative url' do it 'should return an url for the avatar with relative url' do
allow(Gitlab.config.gitlab).to receive(:relative_url_root).and_return('/gitlab') stub_config_setting(relative_url_root: '/gitlab')
allow(Gitlab.config.gitlab).to receive(:url).and_return(Settings.send(:build_gitlab_url)) # Must be stubbed after the stub above, and separately
stub_config_setting(url: Settings.send(:build_gitlab_url))
user = create(:user) user = create(:user, avatar: File.open(avatar_file_path))
user.avatar = File.open(avatar_file_path)
user.save! expect(helper.avatar_icon(user.email).to_s).
expect(avatar_icon(user.email).to_s). to match("/gitlab/uploads/user/avatar/#{user.id}/banana_sample.gif")
to match("/gitlab/uploads/user/avatar/#{ user.id }/banana_sample.gif")
end end
it 'should call gravatar_icon when no avatar is present' do it 'should call gravatar_icon when no User exists with the given email' do
user = create(:user, email: 'test@example.com') expect(helper).to receive(:gravatar_icon).with('foo@example.com', 20)
user.save!
expect(avatar_icon(user.email).to_s).to eq('http://www.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?s=40&d=identicon') helper.avatar_icon('foo@example.com', 20)
end end
end end
describe 'gravatar_icon' do describe 'gravatar_icon' do
let(:user_email) { 'user@email.com' } let(:user_email) { 'user@email.com' }
it 'should return a generic avatar path when Gravatar is disabled' do context 'with Gravatar disabled' do
allow_any_instance_of(ApplicationSetting).to receive(:gravatar_enabled?).and_return(false) before do
expect(gravatar_icon(user_email)).to match('no_avatar.png') stub_application_setting(gravatar_enabled?: false)
end end
it 'should return a generic avatar path when email is blank' do it 'returns a generic avatar' do
expect(gravatar_icon('')).to match('no_avatar.png') expect(helper.gravatar_icon(user_email)).to match('no_avatar.png')
end
end end
it 'should return default gravatar url' do context 'with Gravatar enabled' do
allow(Gitlab.config.gitlab).to receive(:https).and_return(false) before do
url = 'http://www.gravatar.com/avatar/b58c6f14d292556214bd64909bcdb118' stub_application_setting(gravatar_enabled?: true)
expect(gravatar_icon(user_email)).to match(url) end
end
it 'should use SSL when appropriate' do it 'returns a generic avatar when email is blank' do
allow(Gitlab.config.gitlab).to receive(:https).and_return(true) expect(helper.gravatar_icon('')).to match('no_avatar.png')
expect(gravatar_icon(user_email)).to match('https://secure.gravatar.com') end
end
it 'should return custom gravatar path when gravatar_url is set' do it 'returns a valid Gravatar URL' do
allow(self).to receive(:request).and_return(double(:ssl? => false)) stub_config_setting(https: false)
allow(Gitlab.config.gravatar).
to receive(:plain_url).
and_return('http://example.local/?s=%{size}&hash=%{hash}')
url = 'http://example.local/?s=20&hash=b58c6f14d292556214bd64909bcdb118'
expect(gravatar_icon(user_email, 20)).to eq(url)
end
it 'should accept a custom size' do expect(helper.gravatar_icon(user_email)).
allow(self).to receive(:request).and_return(double(:ssl? => false)) to match('http://www.gravatar.com/avatar/b58c6f14d292556214bd64909bcdb118')
expect(gravatar_icon(user_email, 64)).to match(/\?s=64/) end
end
it 'should use default size when size is wrong' do it 'uses HTTPs when configured' do
allow(self).to receive(:request).and_return(double(:ssl? => false)) stub_config_setting(https: true)
expect(gravatar_icon(user_email, nil)).to match(/\?s=40/)
end expect(helper.gravatar_icon(user_email)).
to match('https://secure.gravatar.com')
end
it 'should return custom gravatar path when gravatar_url is set' do
stub_gravatar_setting(plain_url: 'http://example.local/?s=%{size}&hash=%{hash}')
it 'should be case insensitive' do expect(gravatar_icon(user_email, 20)).
allow(self).to receive(:request).and_return(double(:ssl? => false)) to eq('http://example.local/?s=20&hash=b58c6f14d292556214bd64909bcdb118')
expect(gravatar_icon(user_email)). end
to eq(gravatar_icon(user_email.upcase + ' '))
it 'accepts a custom size argument' do
expect(helper.gravatar_icon(user_email, 64)).to include '?s=64'
end
it 'defaults size to 40 when given an invalid size' do
expect(helper.gravatar_icon(user_email, nil)).to include '?s=40'
end
it 'ignores case and surrounding whitespace' do
normal = helper.gravatar_icon('foo@example.com')
upcase = helper.gravatar_icon(' FOO@EXAMPLE.COM ')
expect(normal).to eq upcase
end
end end
end end
describe 'grouped_options_refs' do describe 'grouped_options_refs' do
# Override Rails' grouped_options_for_select helper since HTML is harder to work with let(:options) { helper.grouped_options_refs }
def grouped_options_for_select(options, *args) let(:project) { create(:project) }
options
end
let(:options) { grouped_options_refs }
before do before do
# Must be an instance variable assign(:project, project)
@project = create(:project)
# Override Rails' grouped_options_for_select helper to just return the
# first argument (`options`), since it's easier to work with than the
# generated HTML.
allow(helper).to receive(:grouped_options_for_select).
and_wrap_original { |_, *args| args.first }
end end
it 'includes a list of branch names' do it 'includes a list of branch names' do
...@@ -167,15 +187,16 @@ describe ApplicationHelper do ...@@ -167,15 +187,16 @@ describe ApplicationHelper do
it 'includes a specific commit ref if defined' do it 'includes a specific commit ref if defined' do
# Must be an instance variable # Must be an instance variable
@ref = '2ed06dc41dbb5936af845b87d79e05bbf24c73b8' ref = '2ed06dc41dbb5936af845b87d79e05bbf24c73b8'
assign(:ref, ref)
expect(options[2][0]).to eq('Commit') expect(options[2][0]).to eq('Commit')
expect(options[2][1]).to eq([@ref]) expect(options[2][1]).to eq([ref])
end end
it 'sorts tags in a natural order' do it 'sorts tags in a natural order' do
# Stub repository.tag_names to make sure we get some valid testing data # Stub repository.tag_names to make sure we get some valid testing data
expect(@project.repository).to receive(:tag_names). expect(project.repository).to receive(:tag_names).
and_return(['v1.0.9', 'v1.0.10', 'v2.0', 'v3.1.4.2', 'v2.0rc1¿', and_return(['v1.0.9', 'v1.0.10', 'v2.0', 'v3.1.4.2', 'v2.0rc1¿',
'v1.0.9a', 'v2.0-rc1', 'v2.0rc2']) 'v1.0.9a', 'v2.0-rc1', 'v2.0rc2'])
...@@ -189,17 +210,17 @@ describe ApplicationHelper do ...@@ -189,17 +210,17 @@ describe ApplicationHelper do
let(:a_tag) { '<a href="#">Foo</a>' } let(:a_tag) { '<a href="#">Foo</a>' }
it 'allows the a tag' do it 'allows the a tag' do
expect(simple_sanitize(a_tag)).to eq(a_tag) expect(helper.simple_sanitize(a_tag)).to eq(a_tag)
end end
it 'allows the span tag' do it 'allows the span tag' do
input = '<span class="foo">Bar</span>' input = '<span class="foo">Bar</span>'
expect(simple_sanitize(input)).to eq(input) expect(helper.simple_sanitize(input)).to eq(input)
end end
it 'disallows other tags' do it 'disallows other tags' do
input = "<strike><b>#{a_tag}</b></strike>" input = "<strike><b>#{a_tag}</b></strike>"
expect(simple_sanitize(input)).to eq(a_tag) expect(helper.simple_sanitize(input)).to eq(a_tag)
end end
end end
...@@ -207,7 +228,7 @@ describe ApplicationHelper do ...@@ -207,7 +228,7 @@ describe ApplicationHelper do
def element(*arguments) def element(*arguments)
Time.zone = 'UTC' Time.zone = 'UTC'
time = Time.zone.parse('2015-07-02 08:00') time = Time.zone.parse('2015-07-02 08:00')
element = time_ago_with_tooltip(time, *arguments) element = helper.time_ago_with_tooltip(time, *arguments)
Nokogiri::HTML::DocumentFragment.parse(element).first_element_child Nokogiri::HTML::DocumentFragment.parse(element).first_element_child
end end
...@@ -257,21 +278,21 @@ describe ApplicationHelper do ...@@ -257,21 +278,21 @@ describe ApplicationHelper do
it 'should preserve encoding' do it 'should preserve encoding' do
expect(content.encoding.name).to eq('UTF-8') expect(content.encoding.name).to eq('UTF-8')
expect(render_markup('foo.rst', content).encoding.name).to eq('UTF-8') expect(helper.render_markup('foo.rst', content).encoding.name).to eq('UTF-8')
end end
it "should delegate to #markdown when file name corresponds to Markdown" do it "should delegate to #markdown when file name corresponds to Markdown" do
expect(self).to receive(:gitlab_markdown?).with('foo.md').and_return(true) expect(helper).to receive(:gitlab_markdown?).with('foo.md').and_return(true)
expect(self).to receive(:markdown).and_return('NOEL') expect(helper).to receive(:markdown).and_return('NOEL')
expect(render_markup('foo.md', content)).to eq('NOEL') expect(helper.render_markup('foo.md', content)).to eq('NOEL')
end end
it "should delegate to #asciidoc when file name corresponds to AsciiDoc" do it "should delegate to #asciidoc when file name corresponds to AsciiDoc" do
expect(self).to receive(:asciidoc?).with('foo.adoc').and_return(true) expect(helper).to receive(:asciidoc?).with('foo.adoc').and_return(true)
expect(self).to receive(:asciidoc).and_return('NOEL') expect(helper).to receive(:asciidoc).and_return('NOEL')
expect(render_markup('foo.adoc', content)).to eq('NOEL') expect(helper.render_markup('foo.adoc', content)).to eq('NOEL')
end end
end end
end end
...@@ -13,12 +13,12 @@ RSpec.configure do |config| ...@@ -13,12 +13,12 @@ RSpec.configure do |config|
config.use_instantiated_fixtures = false config.use_instantiated_fixtures = false
config.mock_with :rspec config.mock_with :rspec
config.include LoginHelpers, type: :feature
config.include LoginHelpers, type: :request
config.include FactoryGirl::Syntax::Methods
config.include Devise::TestHelpers, type: :controller config.include Devise::TestHelpers, type: :controller
config.include LoginHelpers, type: :feature
config.include LoginHelpers, type: :request
config.include StubConfiguration
config.include TestEnv config.include TestEnv
config.infer_spec_type_from_file_location! config.infer_spec_type_from_file_location!
config.raise_errors_for_deprecations! config.raise_errors_for_deprecations!
......
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
module StubConfiguration
def stub_application_setting(messages)
allow(Gitlab::CurrentSettings.current_application_settings).
to receive_messages(messages)
end
def stub_config_setting(messages)
allow(Gitlab.config.gitlab).to receive_messages(messages)
end
def stub_gravatar_setting(messages)
allow(Gitlab.config.gravatar).to receive_messages(messages)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment