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
Jérome Perrin
gitlab-ce
Commits
55c61d2e
Commit
55c61d2e
authored
Dec 21, 2016
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve API specs
parent
22a05678
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
228 additions
and
66 deletions
+228
-66
app/models/project_services/mattermost_slash_commands_service.rb
...els/project_services/mattermost_slash_commands_service.rb
+3
-3
changelogs/unreleased/mattermost-slash-auto-config.yml
changelogs/unreleased/mattermost-slash-auto-config.yml
+4
-0
lib/mattermost/session.rb
lib/mattermost/session.rb
+18
-14
spec/lib/mattermost/command_spec.rb
spec/lib/mattermost/command_spec.rb
+48
-9
spec/lib/mattermost/team_spec.rb
spec/lib/mattermost/team_spec.rb
+55
-21
spec/models/project_services/mattermost_slash_commands_service_spec.rb
...roject_services/mattermost_slash_commands_service_spec.rb
+100
-19
No files found.
app/models/project_services/mattermost_slash_commands_service.rb
View file @
55c61d2e
...
...
@@ -30,8 +30,8 @@ class MattermostSlashCommandsService < ChatSlashCommandsService
def
list_teams
(
user
)
Mattermost
::
Team
.
new
(
user
).
all
rescue
Mattermost
::
Error
[]
rescue
Mattermost
::
Error
=>
e
[
[],
e
.
message
]
end
private
...
...
changelogs/unreleased/mattermost-slash-auto-config.yml
0 → 100644
View file @
55c61d2e
---
title
:
Allow to auto-configure Mattermost
merge_request
:
8070
author
:
lib/mattermost/session.rb
View file @
55c61d2e
module
Mattermost
class
NoSessionError
<
Error
class
NoSessionError
<
Mattermost
::
Error
def
message
'No session could be set up, is Mattermost configured with Single Sign On?'
end
end
class
ConnectionError
<
Error
;
end
class
ConnectionError
<
Mattermost
::
Error
;
end
# This class' prime objective is to obtain a session token on a Mattermost
# instance with SSO configured where this GitLab instance is the provider.
...
...
@@ -36,12 +36,12 @@ module Mattermost
def
with_session
with_lease
do
raise
NoSessionError
unless
create
raise
Mattermost
::
NoSessionError
unless
create
begin
yield
self
rescue
Errno
::
ECONNREFUSED
raise
NoSessionError
raise
Mattermost
::
NoSessionError
ensure
destroy
end
...
...
@@ -71,19 +71,15 @@ module Mattermost
end
def
get
(
path
,
options
=
{})
handle_exceptions
do
self
.
class
.
get
(
path
,
options
.
merge
(
headers:
@headers
))
rescue
HTTParty
::
Error
=>
e
raise
Mattermost
::
ConnectionError
.
new
(
e
.
message
)
rescue
Errno
::
ECONNREFUSED
=>
e
raise
Mattermost
::
ConnectionError
.
new
(
e
.
message
)
end
end
def
post
(
path
,
options
=
{})
handle_exceptions
do
self
.
class
.
post
(
path
,
options
.
merge
(
headers:
@headers
))
rescue
HTTParty
::
Error
=>
e
raise
Mattermost
::
ConnectionError
.
new
(
e
.
message
)
rescue
Errno
::
ECONNREFUSED
raise
Mattermost
::
ConnectionError
.
new
(
e
.
message
)
end
end
private
...
...
@@ -152,5 +148,13 @@ module Mattermost
lease
=
::
Gitlab
::
ExclusiveLease
.
new
(
lease_key
,
timeout:
LEASE_TIMEOUT
)
lease
.
try_obtain
end
def
handle_exceptions
yield
rescue
HTTParty
::
Error
=>
e
raise
Mattermost
::
ConnectionError
.
new
(
e
.
message
)
rescue
Errno
::
ECONNREFUSED
raise
Mattermost
::
ConnectionError
.
new
(
e
.
message
)
end
end
end
spec/lib/mattermost/command_spec.rb
View file @
55c61d2e
...
...
@@ -2,21 +2,60 @@ require 'spec_helper'
describe
Mattermost
::
Command
do
let
(
:params
)
{
{
'token'
=>
'token'
,
team_id:
'abc'
}
}
let
(
:user
)
{
build
(
:user
)
}
before
do
Mattermost
::
Session
.
base_uri
(
"http://mattermost.example.com"
)
end
Mattermost
::
Session
.
base_uri
(
'http://mattermost.example.com'
)
subject
{
described_class
.
new
(
user
)
}
allow_any_instance_of
(
Mattermost
::
Client
).
to
receive
(
:with_session
).
and_yield
(
Mattermost
::
Session
.
new
(
nil
))
end
describe
'#create'
do
it
'interpolates the team id'
do
allow
(
subject
).
to
receive
(
:json_post
).
with
(
'/api/v3/teams/abc/commands/create'
,
body:
params
.
to_json
).
and_return
(
'token'
=>
'token'
)
let
(
:params
)
do
{
team_id:
'abc'
,
trigger:
'gitlab'
}
end
subject
{
described_class
.
new
(
nil
).
create
(
params
)
}
context
'for valid trigger word'
do
before
do
stub_request
(
:post
,
'http://mattermost.example.com/api/v3/teams/abc/commands/create'
).
with
(
body:
{
team_id:
'abc'
,
trigger:
'gitlab'
}.
to_json
).
to_return
(
status:
200
,
headers:
{
'Content-Type'
=>
'application/json'
},
body:
{
token:
'token'
}.
to_json
)
end
subject
.
create
(
params
)
it
'returns a token'
do
is_expected
.
to
eq
(
'token'
)
end
end
context
'for error message'
do
before
do
stub_request
(
:post
,
'http://mattermost.example.com/api/v3/teams/abc/commands/create'
).
to_return
(
status:
500
,
headers:
{
'Content-Type'
=>
'application/json'
},
body:
{
id:
'api.command.duplicate_trigger.app_error'
,
message:
'This trigger word is already in use. Please choose another word.'
,
detailed_error:
''
,
request_id:
'obc374man7bx5r3dbc1q5qhf3r'
,
status_code:
500
}.
to_json
)
end
it
'raises an error with message'
do
expect
{
subject
}.
to
raise_error
(
Mattermost
::
Error
,
'This trigger word is already in use. Please choose another word.'
)
end
end
end
end
spec/lib/mattermost/team_spec.rb
View file @
55c61d2e
require
'spec_helper'
describe
Mattermost
::
Team
do
before
do
Mattermost
::
Session
.
base_uri
(
'http://mattermost.example.com'
)
allow_any_instance_of
(
Mattermost
::
Client
).
to
receive
(
:with_session
).
and_yield
(
Mattermost
::
Session
.
new
(
nil
))
end
describe
'#all'
do
let
(
:user
)
{
build
(
:user
)
}
subject
{
described_class
.
new
(
nil
).
all
}
context
'for valid request'
do
let
(
:response
)
do
[{
"id"
=>
"xiyro8huptfhdndadpz8r3wnbo"
,
...
...
@@ -19,14 +28,39 @@ describe Mattermost::Team do
"allow_open_invite"
=>
false
}]
end
subject
{
described_class
.
new
(
user
)
}
before
do
stub_request
(
:get
,
'http://mattermost.example.com/api/v3/teams/all'
).
to_return
(
status:
200
,
headers:
{
'Content-Type'
=>
'application/json'
},
body:
response
.
to_json
)
end
it
'returns a token'
do
is_expected
.
to
eq
(
response
)
end
end
context
'for error message'
do
before
do
allow
(
subject
).
to
receive
(
:json_get
).
and_return
(
response
)
stub_request
(
:get
,
'http://mattermost.example.com/api/v3/teams/all'
).
to_return
(
status:
500
,
headers:
{
'Content-Type'
=>
'application/json'
},
body:
{
id:
'api.team.list.app_error'
,
message:
'Cannot list teams.'
,
detailed_error:
''
,
request_id:
'obc374man7bx5r3dbc1q5qhf3r'
,
status_code:
500
}.
to_json
)
end
it
'gets the teams'
do
expect
(
subject
.
all
.
count
).
to
be
(
1
)
it
'raises an error with message'
do
expect
{
subject
}.
to
raise_error
(
Mattermost
::
Error
,
'Cannot list teams.'
)
end
end
end
end
spec/models/project_services/mattermost_slash_commands_service_spec.rb
View file @
55c61d2e
...
...
@@ -3,11 +3,19 @@ require 'spec_helper'
describe
MattermostSlashCommandsService
,
:models
do
it_behaves_like
"chat slash commands service"
describe
'#configure
'
do
context
'Mattermost API
'
do
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:service
)
{
project
.
build_mattermost_slash_commands_service
}
let
(
:user
)
{
create
(
:user
)}
before
do
Mattermost
::
Session
.
base_uri
(
"http://mattermost.example.com"
)
allow_any_instance_of
(
Mattermost
::
Client
).
to
receive
(
:with_session
).
and_yield
(
Mattermost
::
Session
.
new
(
nil
))
end
describe
'#configure'
do
subject
do
service
.
configure
(
user
,
team_id:
'abc'
,
trigger:
'gitlab'
,
url:
'http://trigger.url'
,
...
...
@@ -16,8 +24,24 @@ describe MattermostSlashCommandsService, :models do
context
'the requests succeeds'
do
before
do
allow_any_instance_of
(
Mattermost
::
Command
).
to
receive
(
:json_post
).
and_return
(
'token'
=>
'token'
)
stub_request
(
:post
,
'http://mattermost.example.com/api/v3/teams/abc/commands/create'
).
with
(
body:
{
team_id:
'abc'
,
trigger:
'gitlab'
,
url:
'http://trigger.url'
,
icon_url:
'http://icon.url/icon.png'
,
auto_complete:
true
,
auto_complete_desc:
"Perform common operations on:
#{
project
.
name_with_namespace
}
"
,
auto_complete_hint:
'[help]'
,
description:
"Perform common operations on:
#{
project
.
name_with_namespace
}
"
,
display_name:
"GitLab /
#{
project
.
name_with_namespace
}
"
,
method:
'P'
,
user_name:
'GitLab'
}.
to_json
).
to_return
(
status:
200
,
headers:
{
'Content-Type'
=>
'application/json'
},
body:
{
token:
'token'
}.
to_json
)
end
it
'saves the service'
do
...
...
@@ -32,11 +56,68 @@ describe MattermostSlashCommandsService, :models do
end
context
'an error is received'
do
before
do
stub_request
(
:post
,
'http://mattermost.example.com/api/v3/teams/abc/commands/create'
).
to_return
(
status:
500
,
headers:
{
'Content-Type'
=>
'application/json'
},
body:
{
id:
'api.command.duplicate_trigger.app_error'
,
message:
'This trigger word is already in use. Please choose another word.'
,
detailed_error:
''
,
request_id:
'obc374man7bx5r3dbc1q5qhf3r'
,
status_code:
500
}.
to_json
)
end
it
'shows error messages'
do
succeeded
,
message
=
subject
expect
(
succeeded
).
to
be
(
false
)
expect
(
message
).
to
start_with
(
"Failed to open TCP connection to"
)
expect
(
message
).
to
eq
(
'This trigger word is already in use. Please choose another word.'
)
end
end
end
describe
'#list_teams'
do
subject
do
service
.
list_teams
(
user
)
end
context
'the requests succeeds'
do
before
do
stub_request
(
:get
,
'http://mattermost.example.com/api/v3/teams/all'
).
to_return
(
status:
200
,
headers:
{
'Content-Type'
=>
'application/json'
},
body:
[
'list'
].
to_json
)
end
it
'returns a list of teams'
do
expect
(
subject
).
not_to
be_empty
end
end
context
'an error is received'
do
before
do
stub_request
(
:get
,
'http://mattermost.example.com/api/v3/teams/all'
).
to_return
(
status:
500
,
headers:
{
'Content-Type'
=>
'application/json'
},
body:
{
message:
'Failed to get team list.'
}.
to_json
)
end
it
'shows error messages'
do
teams
,
message
=
subject
expect
(
teams
).
to
be_empty
expect
(
message
).
to
eq
(
'Failed to get team list.'
)
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