Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
1521fd9f
Commit
1521fd9f
authored
Feb 20, 2018
by
Oswaldo Ferreira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unit tests to presenter methods
parent
d714073d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
315 additions
and
33 deletions
+315
-33
app/presenters/project_presenter.rb
app/presenters/project_presenter.rb
+41
-33
spec/presenters/project_presenter_spec.rb
spec/presenters/project_presenter_spec.rb
+274
-0
No files found.
app/presenters/project_presenter.rb
View file @
1521fd9f
...
...
@@ -100,6 +100,14 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
add_special_file_path
(
file_name:
'LICENSE'
)
end
def
add_changelog_path
add_special_file_path
(
file_name:
'CHANGELOG'
)
end
def
add_contribution_guide_path
add_special_file_path
(
file_name:
'CONTRIBUTING.md'
,
commit_message:
'Add contribution guide'
)
end
def
add_ci_yml_path
add_special_file_path
(
file_name:
'.gitlab-ci.yml'
)
end
...
...
@@ -150,36 +158,6 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
license
&
.
nickname
||
license
&
.
name
||
'LICENSE'
end
private
def
filename_path
(
filename
)
if
blob
=
repository
.
public_send
(
filename
)
# rubocop:disable GitlabSecurity/PublicSend
project_blob_path
(
project
,
tree_join
(
default_branch
,
blob
.
name
)
)
end
end
def
anonymous_project_view
if
!
project
.
empty_repo?
&&
can?
(
current_user
,
:download_code
,
project
)
'files'
else
'activity'
end
end
def
add_special_file_path
(
file_name
:,
commit_message:
nil
,
branch_name:
nil
)
commit_message
||=
s_
(
"CommitMessage|Add %{file_name}"
)
%
{
file_name:
file_name
}
project_new_blob_path
(
project
,
project
.
default_branch
||
'master'
,
file_name:
file_name
,
commit_message:
commit_message
,
branch_name:
branch_name
)
end
def
can_current_user_push_code?
if
empty_repo?
can?
(
current_user
,
:push_code
,
project
)
...
...
@@ -237,7 +215,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
if
current_user
&&
can_current_user_push_code?
&&
repository
.
changelog
.
blank?
OpenStruct
.
new
(
enabled:
false
,
label:
_
(
'Add Changelog'
),
link:
add_
special_file_path
(
file_name:
'CHANGELOG'
)
)
link:
add_
changelog_path
)
elsif
repository
.
changelog
.
present?
OpenStruct
.
new
(
enabled:
true
,
label:
_
(
'Changelog'
),
...
...
@@ -261,7 +239,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
if
current_user
&&
can_current_user_push_code?
&&
repository
.
contribution_guide
.
blank?
OpenStruct
.
new
(
enabled:
false
,
label:
_
(
'Add Contribution guide'
),
link:
add_
special_file_path
(
file_name:
'CONTRIBUTING.md'
,
commit_message:
'Add contribution guide'
)
)
link:
add_
contribution_guide_path
)
elsif
repository
.
contribution_guide
.
present?
OpenStruct
.
new
(
enabled:
true
,
label:
_
(
'Contribution guide'
),
...
...
@@ -283,7 +261,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
def
kubernetes_cluster_anchor_data
if
current_user
&&
can?
(
current_user
,
:create_cluster
,
project
)
cluster_link
=
clusters
.
size
==
1
?
project_cluster_path
(
project
,
clusters
.
first
)
:
project_clusters_path
(
project
)
cluster_link
=
clusters
.
count
==
1
?
project_cluster_path
(
project
,
clusters
.
first
)
:
project_clusters_path
(
project
)
if
clusters
.
empty?
cluster_link
=
new_project_cluster_path
(
project
)
...
...
@@ -315,6 +293,36 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
end
end
private
def
filename_path
(
filename
)
if
blob
=
repository
.
public_send
(
filename
)
# rubocop:disable GitlabSecurity/PublicSend
project_blob_path
(
project
,
tree_join
(
default_branch
,
blob
.
name
)
)
end
end
def
anonymous_project_view
if
!
project
.
empty_repo?
&&
can?
(
current_user
,
:download_code
,
project
)
'files'
else
'activity'
end
end
def
add_special_file_path
(
file_name
:,
commit_message:
nil
,
branch_name:
nil
)
commit_message
||=
s_
(
"CommitMessage|Add %{file_name}"
)
%
{
file_name:
file_name
}
project_new_blob_path
(
project
,
project
.
default_branch
||
'master'
,
file_name:
file_name
,
commit_message:
commit_message
,
branch_name:
branch_name
)
end
def
koding_enabled?
Gitlab
::
CurrentSettings
.
koding_enabled?
end
...
...
spec/presenters/project_presenter_spec.rb
View file @
1521fd9f
...
...
@@ -120,4 +120,278 @@ describe ProjectPresenter do
end
end
end
describe
'#can_current_user_push_code?'
do
let
(
:project
)
{
create
(
:project
,
:repository
)
}
let
(
:presenter
)
{
described_class
.
new
(
project
,
current_user:
user
)
}
context
'empty repo'
do
let
(
:project
)
{
create
(
:project
)
}
it
'returns true if user can push_code'
do
project
.
add_developer
(
user
)
expect
(
presenter
.
can_current_user_push_code?
).
to
be
(
true
)
end
it
'returns false if user cannot push_code'
do
project
.
add_reporter
(
user
)
expect
(
presenter
.
can_current_user_push_code?
).
to
be
(
false
)
end
end
context
'not empty repo'
do
let
(
:project
)
{
create
(
:project
,
:repository
)
}
it
'returns true if user can push to default branch'
do
project
.
add_developer
(
user
)
expect
(
presenter
.
can_current_user_push_code?
).
to
be
(
true
)
end
it
'returns false if default branch is protected'
do
project
.
add_developer
(
user
)
create
(
:protected_branch
,
project:
project
,
name:
project
.
default_branch
)
expect
(
presenter
.
can_current_user_push_code?
).
to
be
(
false
)
end
end
end
context
'statistics anchors'
do
let
(
:project
)
{
create
(
:project
,
:repository
)
}
let
(
:presenter
)
{
described_class
.
new
(
project
,
current_user:
user
)
}
describe
'#files_anchor_data'
do
it
'returns files data'
do
expect
(
presenter
.
files_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
true
,
label:
'Files (0 Bytes)'
,
link:
presenter
.
project_tree_path
(
project
)))
end
end
describe
'#commits_anchor_data'
do
it
'returns commits data'
do
expect
(
presenter
.
commits_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
true
,
label:
'Commits (0)'
,
link:
presenter
.
project_commits_path
(
project
,
project
.
repository
.
root_ref
)))
end
end
describe
'#branches_anchor_data'
do
it
'returns branches data'
do
expect
(
presenter
.
branches_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
true
,
label:
"Branches (
#{
project
.
repository
.
branches
.
size
}
)"
,
link:
presenter
.
project_branches_path
(
project
)))
end
end
describe
'#tags_anchor_data'
do
it
'returns tags data'
do
expect
(
presenter
.
tags_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
true
,
label:
"Tags (
#{
project
.
repository
.
tags
.
size
}
)"
,
link:
presenter
.
project_tags_path
(
project
)))
end
end
describe
'#new_file_anchor_data'
do
it
'returns new file data if user can push'
do
project
.
add_developer
(
user
)
expect
(
presenter
.
new_file_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
false
,
label:
"New file"
,
link:
presenter
.
project_new_blob_path
(
project
,
'master'
),
class_modifier:
'new'
))
end
it
'returns nil if user cannot push'
do
expect
(
presenter
.
new_file_anchor_data
).
to
be_nil
end
end
describe
'#readme_anchor_data'
do
context
'when user can push and README does not exists'
do
it
'returns anchor data'
do
project
.
add_developer
(
user
)
allow
(
project
.
repository
).
to
receive
(
:readme
).
and_return
(
nil
)
expect
(
presenter
.
readme_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
false
,
label:
'Add Readme'
,
link:
presenter
.
add_readme_path
))
end
end
context
'when README exists'
do
it
'returns anchor data'
do
allow
(
project
.
repository
).
to
receive
(
:readme
).
and_return
(
double
(
name:
'readme'
))
expect
(
presenter
.
readme_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
true
,
label:
'Readme'
,
link:
presenter
.
readme_path
))
end
end
end
describe
'#changelog_anchor_data'
do
context
'when user can push and CHANGELOG does not exists'
do
it
'returns anchor data'
do
project
.
add_developer
(
user
)
allow
(
project
.
repository
).
to
receive
(
:changelog
).
and_return
(
nil
)
expect
(
presenter
.
changelog_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
false
,
label:
'Add Changelog'
,
link:
presenter
.
add_changelog_path
))
end
end
context
'when CHANGELOG exists'
do
it
'returns anchor data'
do
allow
(
project
.
repository
).
to
receive
(
:changelog
).
and_return
(
double
(
name:
'foo'
))
expect
(
presenter
.
changelog_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
true
,
label:
'Changelog'
,
link:
presenter
.
changelog_path
))
end
end
end
describe
'#license_anchor_data'
do
context
'when user can push and LICENSE does not exists'
do
it
'returns anchor data'
do
project
.
add_developer
(
user
)
allow
(
project
.
repository
).
to
receive
(
:license_blob
).
and_return
(
nil
)
expect
(
presenter
.
license_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
false
,
label:
'Add License'
,
link:
presenter
.
add_license_path
))
end
end
context
'when LICENSE exists'
do
it
'returns anchor data'
do
allow
(
project
.
repository
).
to
receive
(
:license_blob
).
and_return
(
double
(
name:
'foo'
))
expect
(
presenter
.
license_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
true
,
label:
presenter
.
license_short_name
,
link:
presenter
.
license_path
))
end
end
end
describe
'#contribution_guide_anchor_data'
do
context
'when user can push and CONTRIBUTING does not exists'
do
it
'returns anchor data'
do
project
.
add_developer
(
user
)
allow
(
project
.
repository
).
to
receive
(
:contribution_guide
).
and_return
(
nil
)
expect
(
presenter
.
contribution_guide_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
false
,
label:
'Add Contribution guide'
,
link:
presenter
.
add_contribution_guide_path
))
end
end
context
'when CONTRIBUTING exists'
do
it
'returns anchor data'
do
allow
(
project
.
repository
).
to
receive
(
:contribution_guide
).
and_return
(
double
(
name:
'foo'
))
expect
(
presenter
.
contribution_guide_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
true
,
label:
'Contribution guide'
,
link:
presenter
.
contribution_guide_path
))
end
end
end
describe
'#autodevops_anchor_data'
do
context
'when Auto Devops is enabled'
do
it
'returns anchor data'
do
allow
(
project
).
to
receive
(
:auto_devops_enabled?
).
and_return
(
true
)
expect
(
presenter
.
autodevops_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
true
,
label:
'Auto DevOps enabled'
,
link:
nil
))
end
end
context
'when user can admin pipeline and CI yml does not exists'
do
it
'returns anchor data'
do
project
.
add_master
(
user
)
allow
(
project
).
to
receive
(
:auto_devops_enabled?
).
and_return
(
false
)
allow
(
project
.
repository
).
to
receive
(
:gitlab_ci_yml
).
and_return
(
nil
)
expect
(
presenter
.
autodevops_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
false
,
label:
'Enable Auto DevOps'
,
link:
presenter
.
project_settings_ci_cd_path
(
project
,
anchor:
'js-general-pipeline-settings'
)))
end
end
end
describe
'#kubernetes_cluster_anchor_data'
do
context
'when user can create Kubernetes cluster'
do
it
'returns link to cluster if only one exists'
do
project
.
add_master
(
user
)
cluster
=
create
(
:cluster
,
projects:
[
project
])
expect
(
presenter
.
kubernetes_cluster_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
true
,
label:
'Kubernetes cluster'
,
link:
presenter
.
project_cluster_path
(
project
,
cluster
)))
end
it
'returns link to clusters page if more than one exists'
do
project
.
add_master
(
user
)
cluster_1
=
create
(
:cluster
,
projects:
[
project
])
cluster_2
=
create
(
:cluster
,
projects:
[
project
])
expect
(
presenter
.
kubernetes_cluster_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
true
,
label:
'Kubernetes clusters'
,
link:
presenter
.
project_clusters_path
(
project
)))
end
it
'returns link to create a cluster if no cluster exists'
do
project
.
add_master
(
user
)
expect
(
presenter
.
kubernetes_cluster_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
false
,
label:
'Add Kubernetes cluster'
,
link:
presenter
.
new_project_cluster_path
(
project
)))
end
end
context
'when user cannot create Kubernetes cluster'
do
it
'returns nil'
do
expect
(
presenter
.
kubernetes_cluster_anchor_data
).
to
be_nil
end
end
end
describe
'#koding_anchor_data'
do
it
'returns link to setup Koding if user can push and no koding YML exists'
do
project
.
add_developer
(
user
)
allow
(
project
.
repository
).
to
receive
(
:koding_yml
).
and_return
(
nil
)
allow
(
Gitlab
::
CurrentSettings
).
to
receive
(
:koding_enabled?
).
and_return
(
true
)
expect
(
presenter
.
koding_anchor_data
).
to
eq
(
OpenStruct
.
new
(
enabled:
false
,
label:
'Set up Koding'
,
link:
presenter
.
add_koding_stack_path
))
end
it
'returns nil if user cannot push'
do
expect
(
presenter
.
koding_anchor_data
).
to
be_nil
end
it
'returns nil if koding is not enabled'
do
project
.
add_developer
(
user
)
allow
(
Gitlab
::
CurrentSettings
).
to
receive
(
:koding_enabled?
).
and_return
(
false
)
expect
(
presenter
.
koding_anchor_data
).
to
be_nil
end
it
'returns nil if koding YML already exists'
do
project
.
add_developer
(
user
)
allow
(
project
.
repository
).
to
receive
(
:koding_yml
).
and_return
(
double
)
allow
(
Gitlab
::
CurrentSettings
).
to
receive
(
:koding_enabled?
).
and_return
(
true
)
expect
(
presenter
.
koding_anchor_data
).
to
be_nil
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment