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
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
...
@@ -30,8 +30,8 @@ class MattermostSlashCommandsService < ChatSlashCommandsService
def
list_teams
(
user
)
def
list_teams
(
user
)
Mattermost
::
Team
.
new
(
user
).
all
Mattermost
::
Team
.
new
(
user
).
all
rescue
Mattermost
::
Error
rescue
Mattermost
::
Error
=>
e
[]
[
[],
e
.
message
]
end
end
private
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
module
Mattermost
class
NoSessionError
<
Error
class
NoSessionError
<
Mattermost
::
Error
def
message
def
message
'No session could be set up, is Mattermost configured with Single Sign On?'
'No session could be set up, is Mattermost configured with Single Sign On?'
end
end
end
end
class
ConnectionError
<
Error
;
end
class
ConnectionError
<
Mattermost
::
Error
;
end
# This class' prime objective is to obtain a session token on a Mattermost
# This class' prime objective is to obtain a session token on a Mattermost
# instance with SSO configured where this GitLab instance is the provider.
# instance with SSO configured where this GitLab instance is the provider.
...
@@ -36,12 +36,12 @@ module Mattermost
...
@@ -36,12 +36,12 @@ module Mattermost
def
with_session
def
with_session
with_lease
do
with_lease
do
raise
NoSessionError
unless
create
raise
Mattermost
::
NoSessionError
unless
create
begin
begin
yield
self
yield
self
rescue
Errno
::
ECONNREFUSED
rescue
Errno
::
ECONNREFUSED
raise
NoSessionError
raise
Mattermost
::
NoSessionError
ensure
ensure
destroy
destroy
end
end
...
@@ -71,19 +71,15 @@ module Mattermost
...
@@ -71,19 +71,15 @@ module Mattermost
end
end
def
get
(
path
,
options
=
{})
def
get
(
path
,
options
=
{})
handle_exceptions
do
self
.
class
.
get
(
path
,
options
.
merge
(
headers:
@headers
))
self
.
class
.
get
(
path
,
options
.
merge
(
headers:
@headers
))
rescue
HTTParty
::
Error
=>
e
end
raise
Mattermost
::
ConnectionError
.
new
(
e
.
message
)
rescue
Errno
::
ECONNREFUSED
=>
e
raise
Mattermost
::
ConnectionError
.
new
(
e
.
message
)
end
end
def
post
(
path
,
options
=
{})
def
post
(
path
,
options
=
{})
handle_exceptions
do
self
.
class
.
post
(
path
,
options
.
merge
(
headers:
@headers
))
self
.
class
.
post
(
path
,
options
.
merge
(
headers:
@headers
))
rescue
HTTParty
::
Error
=>
e
end
raise
Mattermost
::
ConnectionError
.
new
(
e
.
message
)
rescue
Errno
::
ECONNREFUSED
raise
Mattermost
::
ConnectionError
.
new
(
e
.
message
)
end
end
private
private
...
@@ -152,5 +148,13 @@ module Mattermost
...
@@ -152,5 +148,13 @@ module Mattermost
lease
=
::
Gitlab
::
ExclusiveLease
.
new
(
lease_key
,
timeout:
LEASE_TIMEOUT
)
lease
=
::
Gitlab
::
ExclusiveLease
.
new
(
lease_key
,
timeout:
LEASE_TIMEOUT
)
lease
.
try_obtain
lease
.
try_obtain
end
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
end
end
spec/lib/mattermost/command_spec.rb
View file @
55c61d2e
...
@@ -2,21 +2,60 @@ require 'spec_helper'
...
@@ -2,21 +2,60 @@ require 'spec_helper'
describe
Mattermost
::
Command
do
describe
Mattermost
::
Command
do
let
(
:params
)
{
{
'token'
=>
'token'
,
team_id:
'abc'
}
}
let
(
:params
)
{
{
'token'
=>
'token'
,
team_id:
'abc'
}
}
let
(
:user
)
{
build
(
:user
)
}
before
do
before
do
Mattermost
::
Session
.
base_uri
(
"http://mattermost.example.com"
)
Mattermost
::
Session
.
base_uri
(
'http://mattermost.example.com'
)
end
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
describe
'#create'
do
it
'interpolates the team id'
do
let
(
:params
)
do
allow
(
subject
).
to
receive
(
:json_post
).
{
team_id:
'abc'
,
with
(
'/api/v3/teams/abc/commands/create'
,
body:
params
.
to_json
).
trigger:
'gitlab'
and_return
(
'token'
=>
'token'
)
}
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
end
end
end
spec/lib/mattermost/team_spec.rb
View file @
55c61d2e
require
'spec_helper'
require
'spec_helper'
describe
Mattermost
::
Team
do
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
describe
'#all'
do
let
(
:user
)
{
build
(
:user
)
}
subject
{
described_class
.
new
(
nil
).
all
}
context
'for valid request'
do
let
(
:response
)
do
let
(
:response
)
do
[{
[{
"id"
=>
"xiyro8huptfhdndadpz8r3wnbo"
,
"id"
=>
"xiyro8huptfhdndadpz8r3wnbo"
,
...
@@ -19,14 +28,39 @@ describe Mattermost::Team do
...
@@ -19,14 +28,39 @@ describe Mattermost::Team do
"allow_open_invite"
=>
false
}]
"allow_open_invite"
=>
false
}]
end
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
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
end
it
'gets the teams'
do
it
'raises an error with message'
do
expect
(
subject
.
all
.
count
).
to
be
(
1
)
expect
{
subject
}.
to
raise_error
(
Mattermost
::
Error
,
'Cannot list teams.'
)
end
end
end
end
end
end
end
spec/models/project_services/mattermost_slash_commands_service_spec.rb
View file @
55c61d2e
...
@@ -3,11 +3,19 @@ require 'spec_helper'
...
@@ -3,11 +3,19 @@ require 'spec_helper'
describe
MattermostSlashCommandsService
,
:models
do
describe
MattermostSlashCommandsService
,
:models
do
it_behaves_like
"chat slash commands service"
it_behaves_like
"chat slash commands service"
describe
'#configure
'
do
context
'Mattermost API
'
do
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:service
)
{
project
.
build_mattermost_slash_commands_service
}
let
(
:service
)
{
project
.
build_mattermost_slash_commands_service
}
let
(
:user
)
{
create
(
:user
)}
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
subject
do
service
.
configure
(
user
,
team_id:
'abc'
,
service
.
configure
(
user
,
team_id:
'abc'
,
trigger:
'gitlab'
,
url:
'http://trigger.url'
,
trigger:
'gitlab'
,
url:
'http://trigger.url'
,
...
@@ -16,8 +24,24 @@ describe MattermostSlashCommandsService, :models do
...
@@ -16,8 +24,24 @@ describe MattermostSlashCommandsService, :models do
context
'the requests succeeds'
do
context
'the requests succeeds'
do
before
do
before
do
allow_any_instance_of
(
Mattermost
::
Command
).
stub_request
(
:post
,
'http://mattermost.example.com/api/v3/teams/abc/commands/create'
).
to
receive
(
:json_post
).
and_return
(
'token'
=>
'token'
)
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
end
it
'saves the service'
do
it
'saves the service'
do
...
@@ -32,11 +56,68 @@ describe MattermostSlashCommandsService, :models do
...
@@ -32,11 +56,68 @@ describe MattermostSlashCommandsService, :models do
end
end
context
'an error is received'
do
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
it
'shows error messages'
do
succeeded
,
message
=
subject
succeeded
,
message
=
subject
expect
(
succeeded
).
to
be
(
false
)
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
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