2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-09 15:45:49 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::View::Presenter::Base do
|
2017-01-09 15:45:49 -05:00
|
|
|
let(:project) { double(:project) }
|
|
|
|
let(:presenter_class) do
|
|
|
|
Struct.new(:subject).include(described_class)
|
|
|
|
end
|
|
|
|
|
2017-01-10 17:41:04 -05:00
|
|
|
describe '.presenter?' do
|
|
|
|
it 'returns true' do
|
|
|
|
presenter = presenter_class.new(project)
|
|
|
|
|
|
|
|
expect(presenter.class).to be_presenter
|
|
|
|
end
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.presents' do
|
|
|
|
it 'exposes #subject with the given keyword' do
|
|
|
|
presenter_class.presents(:foo)
|
2017-01-10 17:41:04 -05:00
|
|
|
presenter = presenter_class.new(project)
|
2017-01-09 15:45:49 -05:00
|
|
|
|
2017-01-10 17:41:04 -05:00
|
|
|
expect(presenter.foo).to eq(project)
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#can?' do
|
|
|
|
context 'user is not allowed' do
|
|
|
|
it 'returns false' do
|
2017-08-02 15:55:11 -04:00
|
|
|
presenter = presenter_class.new(build_stubbed(:project))
|
2017-01-10 17:41:04 -05:00
|
|
|
|
|
|
|
expect(presenter.can?(nil, :read_project)).to be_falsy
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user is allowed' do
|
2017-01-10 17:41:04 -05:00
|
|
|
it 'returns true' do
|
2017-08-02 15:55:11 -04:00
|
|
|
presenter = presenter_class.new(build_stubbed(:project, :public))
|
2017-01-09 15:45:49 -05:00
|
|
|
|
2017-01-10 17:41:04 -05:00
|
|
|
expect(presenter.can?(nil, :read_project)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-30 06:53:01 -04:00
|
|
|
context 'subject is overridden' do
|
2017-01-09 15:45:49 -05:00
|
|
|
it 'returns true' do
|
2017-08-02 15:55:11 -04:00
|
|
|
presenter = presenter_class.new(build_stubbed(:project, :public))
|
2017-01-10 17:41:04 -05:00
|
|
|
|
2017-08-02 15:55:11 -04:00
|
|
|
expect(presenter.can?(nil, :read_project, build_stubbed(:project))).to be_falsy
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-19 03:20:53 -04:00
|
|
|
|
|
|
|
describe '#present' do
|
|
|
|
it 'returns self' do
|
|
|
|
presenter = presenter_class.new(build_stubbed(:project))
|
|
|
|
expect(presenter.present).to eq(presenter)
|
|
|
|
end
|
|
|
|
end
|
2020-07-29 14:09:50 -04:00
|
|
|
|
|
|
|
describe '#url_builder' do
|
|
|
|
it 'returns the UrlBuilder instance' do
|
|
|
|
presenter = presenter_class.new(project)
|
|
|
|
|
|
|
|
expect(presenter.url_builder).to eq(Gitlab::UrlBuilder.instance)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#web_url' do
|
|
|
|
it 'delegates to the UrlBuilder' do
|
|
|
|
presenter = presenter_class.new(project)
|
|
|
|
|
|
|
|
expect(presenter.url_builder).to receive(:build).with(project)
|
|
|
|
|
|
|
|
presenter.web_url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#web_path' do
|
|
|
|
it 'delegates to the UrlBuilder' do
|
|
|
|
presenter = presenter_class.new(project)
|
|
|
|
|
|
|
|
expect(presenter.url_builder).to receive(:build).with(project, only_path: true)
|
|
|
|
|
|
|
|
presenter.web_path
|
|
|
|
end
|
|
|
|
end
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|