Commit c445ef53 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Move default_project_view tests to presenter

parent e0e3f1c2
...@@ -52,7 +52,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated ...@@ -52,7 +52,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
].compact.reject { |i| i[:enabled] } ].compact.reject { |i| i[:enabled] }
end end
def default_project_view def default_view
return anonymous_project_view unless current_user return anonymous_project_view unless current_user
user_view = current_user.project_view user_view = current_user.project_view
...@@ -242,7 +242,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated ...@@ -242,7 +242,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
{ {
enabled: true, enabled: true,
label: _('Readme'), label: _('Readme'),
link: default_project_view != 'readme' ? readme_path : '#readme' link: default_view != 'readme' ? readme_path : '#readme'
} }
end end
end end
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
= icon("exclamation-triangle fw") = icon("exclamation-triangle fw")
#{ _('Archived project! Repository is read-only') } #{ _('Archived project! Repository is read-only') }
- view_path = @project.default_project_view - view_path = @project.default_view
- if show_auto_devops_callout?(@project) - if show_auto_devops_callout?(@project)
= render 'shared/auto_devops_callout' = render 'shared/auto_devops_callout'
......
...@@ -77,103 +77,6 @@ describe PreferencesHelper do ...@@ -77,103 +77,6 @@ describe PreferencesHelper do
end end
end end
describe '#default_project_view' do
context 'user not signed in' do
before do
helper.instance_variable_set(:@project, project)
stub_user
end
context 'when repository is empty' do
let(:project) { create(:project_empty_repo, :public) }
it 'returns activity if user has repository access' do
allow(helper).to receive(:can?).with(nil, :download_code, project).and_return(true)
expect(helper.default_project_view).to eq('activity')
end
it 'returns activity if user does not have repository access' do
allow(helper).to receive(:can?).with(nil, :download_code, project).and_return(false)
expect(helper.default_project_view).to eq('activity')
end
end
context 'when repository is not empty' do
let(:project) { create(:project, :public, :repository) }
it 'returns files and readme if user has repository access' do
allow(helper).to receive(:can?).with(nil, :download_code, project).and_return(true)
expect(helper.default_project_view).to eq('files')
end
it 'returns activity if user does not have repository access' do
allow(helper).to receive(:can?).with(nil, :download_code, project).and_return(false)
expect(helper.default_project_view).to eq('activity')
end
end
end
context 'user signed in' do
let(:user) { create(:user, :readme) }
let(:project) { create(:project, :public, :repository) }
before do
helper.instance_variable_set(:@project, project)
allow(helper).to receive(:current_user).and_return(user)
end
context 'when the user is allowed to see the code' do
it 'returns the project view' do
allow(helper).to receive(:can?).with(user, :download_code, project).and_return(true)
expect(helper.default_project_view).to eq('readme')
end
end
context 'with wikis enabled and the right policy for the user' do
before do
project.project_feature.update_attribute(:issues_access_level, 0)
allow(helper).to receive(:can?).with(user, :download_code, project).and_return(false)
end
it 'returns wiki if the user has the right policy' do
allow(helper).to receive(:can?).with(user, :read_wiki, project).and_return(true)
expect(helper.default_project_view).to eq('wiki')
end
it 'returns customize_workflow if the user does not have the right policy' do
allow(helper).to receive(:can?).with(user, :read_wiki, project).and_return(false)
expect(helper.default_project_view).to eq('customize_workflow')
end
end
context 'with issues as a feature available' do
it 'return issues' do
allow(helper).to receive(:can?).with(user, :download_code, project).and_return(false)
allow(helper).to receive(:can?).with(user, :read_wiki, project).and_return(false)
expect(helper.default_project_view).to eq('projects/issues/issues')
end
end
context 'with no activity, no wikies and no issues' do
it 'returns customize_workflow as default' do
project.project_feature.update_attribute(:issues_access_level, 0)
allow(helper).to receive(:can?).with(user, :download_code, project).and_return(false)
allow(helper).to receive(:can?).with(user, :read_wiki, project).and_return(false)
expect(helper.default_project_view).to eq('customize_workflow')
end
end
end
end
def stub_user(messages = {}) def stub_user(messages = {})
if messages.empty? if messages.empty?
allow(helper).to receive(:current_user).and_return(nil) allow(helper).to receive(:current_user).and_return(nil)
......
...@@ -2,10 +2,11 @@ require 'spec_helper' ...@@ -2,10 +2,11 @@ require 'spec_helper'
describe ProjectPresenter do describe ProjectPresenter do
let(:user) { create(:user) } let(:user) { create(:user) }
describe '#license_short_name' do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:presenter) { described_class.new(project, current_user: user) } let(:presenter) { described_class.new(project, current_user: user) }
describe '#license_short_name' do
context 'when project.repository has a license_key' do context 'when project.repository has a license_key' do
it 'returns the nickname of the license if present' do it 'returns the nickname of the license if present' do
allow(project.repository).to receive(:license_key).and_return('agpl-3.0') allow(project.repository).to receive(:license_key).and_return('agpl-3.0')
...@@ -28,4 +29,95 @@ describe ProjectPresenter do ...@@ -28,4 +29,95 @@ describe ProjectPresenter do
end end
end end
end end
describe '#default_view' do
let(:presenter) { described_class.new(project, current_user: user) }
context 'user not signed in' do
let(:user) { nil }
context 'when repository is empty' do
let(:project) { create(:project_empty_repo, :public) }
it 'returns activity if user has repository access' do
allow(presenter).to receive(:can?).with(nil, :download_code, project).and_return(true)
expect(presenter.default_view).to eq('activity')
end
it 'returns activity if user does not have repository access' do
allow(project).to receive(:can?).with(nil, :download_code, project).and_return(false)
expect(presenter.default_view).to eq('activity')
end
end
context 'when repository is not empty' do
let(:project) { create(:project, :public, :repository) }
it 'returns files and readme if user has repository access' do
allow(presenter).to receive(:can?).with(nil, :download_code, project).and_return(true)
expect(presenter.default_view).to eq('files')
end
it 'returns activity if user does not have repository access' do
allow(presenter).to receive(:can?).with(nil, :download_code, project).and_return(false)
expect(presenter.default_view).to eq('activity')
end
end
end
context 'user signed in' do
let(:user) { create(:user, :readme) }
let(:project) { create(:project, :public, :repository) }
context 'when the user is allowed to see the code' do
it 'returns the project view' do
allow(presenter).to receive(:can?).with(user, :download_code, project).and_return(true)
expect(presenter.default_view).to eq('readme')
end
end
context 'with wikis enabled and the right policy for the user' do
before do
project.project_feature.update_attribute(:issues_access_level, 0)
allow(presenter).to receive(:can?).with(user, :download_code, project).and_return(false)
end
it 'returns wiki if the user has the right policy' do
allow(presenter).to receive(:can?).with(user, :read_wiki, project).and_return(true)
expect(presenter.default_view).to eq('wiki')
end
it 'returns customize_workflow if the user does not have the right policy' do
allow(presenter).to receive(:can?).with(user, :read_wiki, project).and_return(false)
expect(presenter.default_view).to eq('customize_workflow')
end
end
context 'with issues as a feature available' do
it 'return issues' do
allow(presenter).to receive(:can?).with(user, :download_code, project).and_return(false)
allow(presenter).to receive(:can?).with(user, :read_wiki, project).and_return(false)
expect(presenter.default_view).to eq('projects/issues/issues')
end
end
context 'with no activity, no wikies and no issues' do
it 'returns customize_workflow as default' do
project.project_feature.update_attribute(:issues_access_level, 0)
allow(presenter).to receive(:can?).with(user, :download_code, project).and_return(false)
allow(presenter).to receive(:can?).with(user, :read_wiki, project).and_return(false)
expect(presenter.default_view).to eq('customize_workflow')
end
end
end
end
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