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
Boxiang Sun
gitlab-ce
Commits
0d04724f
Commit
0d04724f
authored
Nov 18, 2016
by
Z.J. van de Weg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More coverage on service level
parent
778b5a5a
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
177 additions
and
9 deletions
+177
-9
lib/api/helpers.rb
lib/api/helpers.rb
+9
-0
lib/api/services.rb
lib/api/services.rb
+1
-6
lib/mattermost/presenter.rb
lib/mattermost/presenter.rb
+5
-1
spec/lib/gitlab/chat_commands/command_spec.rb
spec/lib/gitlab/chat_commands/command_spec.rb
+1
-1
spec/models/project_services/chat_service_spec.rb
spec/models/project_services/chat_service_spec.rb
+15
-0
spec/models/project_services/mattermost_command_service_spec.rb
...odels/project_services/mattermost_command_service_spec.rb
+99
-0
spec/requests/api/services_spec.rb
spec/requests/api/services_spec.rb
+46
-0
spec/services/chat_names/find_user_service_spec.rb
spec/services/chat_names/find_user_service_spec.rb
+1
-1
No files found.
lib/api/helpers.rb
View file @
0d04724f
...
...
@@ -90,6 +90,15 @@ module API
@project_service
||
not_found!
(
"Service"
)
end
def
service_by_slug
(
project
,
slug
)
underscored_service
=
slug
.
underscore
not_found!
(
'Service'
)
unless
Service
.
available_services_names
.
include?
(
underscored_service
)
service_method
=
"
#{
underscored_service
}
_service"
service
=
project
.
public_send
(
service_method
)
end
def
service_attributes
@service_attributes
||=
project_service
.
fields
.
inject
([])
do
|
arr
,
hash
|
arr
<<
hash
[
:name
].
to_sym
...
...
lib/api/services.rb
View file @
0d04724f
...
...
@@ -67,12 +67,7 @@ module API
post
':id/services/:service_slug/trigger'
do
project
=
Project
.
find_with_namespace
(
params
[
:id
])
||
Project
.
find_by
(
id:
params
[
:id
])
underscored_service
=
params
[
:service_slug
].
underscore
not_found!
(
'Service'
)
unless
Service
.
available_services_names
.
include?
(
underscored_service
)
service_method
=
"
#{
underscored_service
}
_service"
service
=
project
.
public_send
(
service_method
)
service
=
service_by_slug
(
project
,
params
[
:service_slug
])
result
=
service
.
try
(
:active?
)
&&
service
.
try
(
:trigger
,
params
)
...
...
lib/mattermost/presenter.rb
View file @
0d04724f
...
...
@@ -4,7 +4,11 @@ module Mattermost
include
Rails
.
application
.
routes
.
url_helpers
def
authorize_chat_name
(
url
)
message
=
":wave: Hi there! Before I do anything for you, please [connect your GitLab account](
#{
url
}
)."
message
=
if
url
":wave: Hi there! Before I do anything for you, please [connect your GitLab account](
#{
url
}
)."
else
":sweat_smile: Couldn't identify you, nor can I autorize you!"
end
ephemeral_response
(
message
)
end
...
...
spec/lib/gitlab/chat_commands/command_spec.rb
View file @
0d04724f
require
'spec_helper'
describe
Gitlab
::
ChatCommands
::
Command
,
service:
true
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:project
)
{
create
(
:
empty_
project
)
}
let
(
:user
)
{
create
(
:user
)
}
subject
{
described_class
.
new
(
project
,
user
,
params
).
execute
}
...
...
spec/models/project_services/chat_service_spec.rb
0 → 100644
View file @
0d04724f
require
'spec_helper'
describe
ChatService
,
models:
true
do
describe
"Associations"
do
it
{
is_expected
.
to
have_many
:chat_names
}
end
describe
'#valid_token?'
do
subject
{
described_class
.
new
}
it
'is false as it has no token'
do
expect
(
subject
.
valid_token?
(
'wer'
)).
to
be_falsey
end
end
end
spec/models/project_services/mattermost_command_service_spec.rb
0 → 100644
View file @
0d04724f
require
'spec_helper'
describe
MattermostCommandService
,
models:
true
do
describe
"Associations"
do
it
{
is_expected
.
to
respond_to
:token
}
end
describe
'#valid_token?'
do
subject
{
described_class
.
new
}
context
'when the token is empty'
do
it
'is false'
do
expect
(
subject
.
valid_token?
(
'wer'
)).
to
be_falsey
end
end
context
'when there is a token'
do
before
do
subject
.
token
=
'123'
end
it
'accepts equal tokens'
do
expect
(
subject
.
valid_token?
(
'123'
)).
to
be_truthy
end
end
end
describe
'#trigger'
do
subject
{
described_class
.
new
}
context
'no token is passed'
do
let
(
:params
)
{
Hash
.
new
}
it
'returns nil'
do
expect
(
subject
.
trigger
(
params
)).
to
be_nil
end
end
context
'with a token passed'
do
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:params
)
{
{
token:
'token'
}
}
before
do
allow
(
subject
).
to
receive
(
:token
).
and_return
(
'token'
)
end
context
'no user can be found'
do
context
'when no url can be generated'
do
it
'responds with the authorize url'
do
response
=
subject
.
trigger
(
params
)
expect
(
response
[
:response_type
]).
to
eq
:ephemeral
expect
(
response
[
:text
]).
to
start_with
":sweat_smile: Couldn't identify you"
end
end
context
'when an auth url can be generated'
do
let
(
:params
)
do
{
team_domain:
'http://domain.tld'
,
team_id:
'T3423423'
,
user_id:
'U234234'
,
user_name:
'mepmep'
,
token:
'token'
}
end
let
(
:service
)
do
project
.
create_mattermost_command_service
(
properties:
{
token:
'token'
}
)
end
it
'generates the url'
do
response
=
service
.
trigger
(
params
)
expect
(
response
[
:text
]).
to
start_with
(
':wave: Hi there!'
)
end
end
end
context
'when the user is authenticated'
do
let!
(
:chat_name
)
{
create
(
:chat_name
,
service:
service
)
}
let
(
:service
)
do
project
.
create_mattermost_command_service
(
properties:
{
token:
'token'
}
)
end
let
(
:params
)
{
{
token:
'token'
,
team_id:
chat_name
.
team_id
,
user_id:
chat_name
.
chat_id
}
}
it
'triggers the command'
do
expect_any_instance_of
(
Gitlab
::
ChatCommands
::
Command
).
to
receive
(
:execute
)
service
.
trigger
(
params
)
end
end
end
end
end
spec/requests/api/services_spec.rb
View file @
0d04724f
...
...
@@ -88,4 +88,50 @@ describe API::API, api: true do
end
end
end
describe
'POST /projects/:id/services/:slug/trigger'
do
let!
(
:project
)
{
create
(
:empty_project
)
}
let
(
:service_name
)
{
'mattermost_command'
}
context
'no service is available'
do
it
'returns a not found message'
do
post
api
(
"/projects/
#{
project
.
id
}
/services/mattermost_command/trigger"
)
expect
(
response
).
to
have_http_status
(
404
)
end
end
context
'the service exists'
do
context
'the service is not active'
do
let!
(
:inactive_service
)
do
project
.
create_mattermost_command_service
(
active:
false
,
properties:
{
token:
'token'
}
)
end
it
'when the service is inactive'
do
post
api
(
"/projects/
#{
project
.
id
}
/services/mattermost_command/trigger"
)
expect
(
response
).
to
have_http_status
(
404
)
end
end
context
'the service is active'
do
let!
(
:active_service
)
do
project
.
create_mattermost_command_service
(
active:
true
,
properties:
{
token:
'token'
}
)
end
let
(
:params
)
{
{
token:
'token'
}
}
it
'retusn status 200'
do
post
api
(
"/projects/
#{
project
.
id
}
/services/mattermost_command/trigger"
),
params
expect
(
response
).
to
have_http_status
(
200
)
end
end
end
end
end
spec/services/chat_names/find_user_service_spec.rb
View file @
0d04724f
...
...
@@ -13,7 +13,7 @@ describe ChatNames::FindUserService, services: true do
context
'when existing user is requested'
do
let
(
:params
)
{
{
team_id:
chat_name
.
team_id
,
user_id:
chat_name
.
chat_id
}
}
it
'returns existing user'
do
it
'returns
the
existing user'
do
is_expected
.
to
eq
(
user
)
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