diff options
-rw-r--r-- | app/helpers/preferences_helper.rb | 6 | ||||
-rw-r--r-- | changelogs/unreleased/23990-project-show-error-when-empty-repo.yml | 4 | ||||
-rw-r--r-- | spec/helpers/preferences_helper_spec.rb | 41 |
3 files changed, 50 insertions, 1 deletions
diff --git a/app/helpers/preferences_helper.rb b/app/helpers/preferences_helper.rb index a46f2c6e17d..6e68aad4cb7 100644 --- a/app/helpers/preferences_helper.rb +++ b/app/helpers/preferences_helper.rb @@ -50,7 +50,7 @@ module PreferencesHelper end def default_project_view - return 'readme' unless current_user + return anonymous_project_view unless current_user user_view = current_user.project_view @@ -66,4 +66,8 @@ module PreferencesHelper "customize_workflow" end end + + def anonymous_project_view + @project.empty_repo? || !can?(current_user, :download_code, @project) ? 'activity' : 'readme' + end end diff --git a/changelogs/unreleased/23990-project-show-error-when-empty-repo.yml b/changelogs/unreleased/23990-project-show-error-when-empty-repo.yml new file mode 100644 index 00000000000..8d4593d4df7 --- /dev/null +++ b/changelogs/unreleased/23990-project-show-error-when-empty-repo.yml @@ -0,0 +1,4 @@ +--- +title: fixes 500 error on project show when user is not logged in and project is still empty +merge_request: 7376 +author: diff --git a/spec/helpers/preferences_helper_spec.rb b/spec/helpers/preferences_helper_spec.rb index 2f9291afc3f..77841e85223 100644 --- a/spec/helpers/preferences_helper_spec.rb +++ b/spec/helpers/preferences_helper_spec.rb @@ -85,4 +85,45 @@ describe PreferencesHelper do and_return(double('user', messages)) 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) } + + it 'returns 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('readme') + 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 + end end |