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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
1a0e54b3
Commit
1a0e54b3
authored
Feb 03, 2017
by
Z.J. van de Weg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for Mattermost team creation
parent
297dc701
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
72 additions
and
31 deletions
+72
-31
app/controllers/groups_controller.rb
app/controllers/groups_controller.rb
+1
-0
app/models/chat_team.rb
app/models/chat_team.rb
+5
-0
app/services/groups/create_service.rb
app/services/groups/create_service.rb
+3
-1
app/workers/mattermost/create_team_worker.rb
app/workers/mattermost/create_team_worker.rb
+10
-15
lib/mattermost/client.rb
lib/mattermost/client.rb
+1
-1
lib/mattermost/team.rb
lib/mattermost/team.rb
+16
-2
spec/models/chat_team_spec.rb
spec/models/chat_team_spec.rb
+1
-1
spec/services/groups/create_service_spec.rb
spec/services/groups/create_service_spec.rb
+13
-4
spec/workers/mattermost/create_team_worker_spec.rb
spec/workers/mattermost/create_team_worker_spec.rb
+22
-7
No files found.
app/controllers/groups_controller.rb
View file @
1a0e54b3
...
@@ -143,6 +143,7 @@ class GroupsController < Groups::ApplicationController
...
@@ -143,6 +143,7 @@ class GroupsController < Groups::ApplicationController
:share_with_group_lock
,
:share_with_group_lock
,
:visibility_level
,
:visibility_level
,
:parent_id
:parent_id
:create_chat_team
]
]
end
end
...
...
app/models/chat_team.rb
0 → 100644
View file @
1a0e54b3
class
ChatTeam
<
ActiveRecord
::
Base
validates
:team_id
,
presence:
true
belongs_to
:namespace
end
app/services/groups/create_service.rb
View file @
1a0e54b3
...
@@ -5,6 +5,8 @@ module Groups
...
@@ -5,6 +5,8 @@ module Groups
end
end
def
execute
def
execute
create_chat_team
=
params
.
delete
(
:create_chat_team
)
@group
=
Group
.
new
(
params
)
@group
=
Group
.
new
(
params
)
unless
Gitlab
::
VisibilityLevel
.
allowed_for?
(
current_user
,
params
[
:visibility_level
])
unless
Gitlab
::
VisibilityLevel
.
allowed_for?
(
current_user
,
params
[
:visibility_level
])
...
@@ -23,7 +25,7 @@ module Groups
...
@@ -23,7 +25,7 @@ module Groups
@group
.
save
@group
.
save
@group
.
add_owner
(
current_user
)
@group
.
add_owner
(
current_user
)
if
params
[
:create_chat_team
]
&&
Gitlab
.
config
.
mattermost
.
enabled
if
create_chat_team
&&
Gitlab
.
config
.
mattermost
.
enabled
Mattermost
::
CreateTeamWorker
.
perform_async
(
@group
.
id
,
current_user
.
id
)
Mattermost
::
CreateTeamWorker
.
perform_async
(
@group
.
id
,
current_user
.
id
)
end
end
...
...
app/workers/mattermost/create_team_worker.rb
View file @
1a0e54b3
...
@@ -3,26 +3,21 @@ module Mattermost
...
@@ -3,26 +3,21 @@ module Mattermost
include
Sidekiq
::
Worker
include
Sidekiq
::
Worker
include
DedicatedSidekiqQueue
include
DedicatedSidekiqQueue
sidekiq_options
retry:
5
# Add 5 seconds so the first retry isn't 1 second later
sidekiq_retry_in
do
|
count
|
5
+
5
**
n
end
def
perform
(
group_id
,
current_user_id
,
options
=
{})
def
perform
(
group_id
,
current_user_id
,
options
=
{})
@
group
=
Group
.
find
(
group_id
)
group
=
Group
.
find
(
group_id
)
current_user
=
User
.
find
(
current_user_id
)
current_user
=
User
.
find
(
current_user_id
)
options
=
team_params
.
merge
(
options
)
# The user that creates the team will be Team Admin
# The user that creates the team will be Team Admin
response
=
Mattermost
::
Team
.
new
(
current_user
).
create
(
options
)
response
=
Mattermost
::
Team
.
new
(
current_user
).
create
(
group
,
options
)
ChatTeam
.
create!
(
namespace:
@group
,
name:
response
[
'name'
],
team_id:
response
[
'id'
])
end
private
def
team_params
ChatTeam
.
create
(
namespace:
group
,
name:
response
[
'name'
],
team_id:
response
[
'id'
])
{
name:
@group
.
path
[
0
..
59
],
display_name:
@group
.
name
[
0
..
59
],
type:
@group
.
public?
?
'O'
:
'I'
# Open vs Invite-only
}
end
end
end
end
end
end
lib/mattermost/client.rb
View file @
1a0e54b3
...
@@ -26,7 +26,7 @@ module Mattermost
...
@@ -26,7 +26,7 @@ module Mattermost
def
session_get
(
path
,
options
=
{})
def
session_get
(
path
,
options
=
{})
with_session
do
|
session
|
with_session
do
|
session
|
get
(
session
,
path
,
options
)
get
(
session
,
path
,
options
)
end
end
end
end
...
...
lib/mattermost/team.rb
View file @
1a0e54b3
...
@@ -5,8 +5,22 @@ module Mattermost
...
@@ -5,8 +5,22 @@ module Mattermost
session_get
(
'/api/v3/teams/all'
)
session_get
(
'/api/v3/teams/all'
)
end
end
def
create
(
params
)
# Creates a team on the linked Mattermost instance, the team admin will be the
session_post
(
'/api/v3/teams/create'
,
body:
params
.
to_json
)
# `current_user` passed to the Mattermost::Client instance
def
create
(
group
,
params
)
session_post
(
'/api/v3/teams/create'
,
body:
new_team_params
(
group
,
params
).
to_json
)
end
private
MATTERMOST_TEAM_LENGTH_MAX
=
59
def
new_team_params
(
group
,
options
)
{
name:
group
.
path
[
0
..
MATTERMOST_TEAM_LENGTH_MAX
],
display_name:
group
.
name
[
0
..
MATTERMOST_TEAM_LENGTH_MAX
],
type:
group
.
public?
?
'O'
:
'I'
# Open vs Invite-only
}.
merge
(
options
)
end
end
end
end
end
end
spec/models/chat_team_spec.rb
View file @
1a0e54b3
...
@@ -2,7 +2,7 @@ require 'spec_helper'
...
@@ -2,7 +2,7 @@ require 'spec_helper'
describe
ChatTeam
,
type: :model
do
describe
ChatTeam
,
type: :model
do
# Associations
# Associations
it
{
is_expected
.
to
belong_to
(
:
group
)
}
it
{
is_expected
.
to
belong_to
(
:
namespace
)
}
# Fields
# Fields
it
{
is_expected
.
to
respond_to
(
:name
)
}
it
{
is_expected
.
to
respond_to
(
:name
)
}
...
...
spec/services/groups/create_service_spec.rb
View file @
1a0e54b3
...
@@ -4,11 +4,11 @@ describe Groups::CreateService, '#execute', services: true do
...
@@ -4,11 +4,11 @@ describe Groups::CreateService, '#execute', services: true do
let!
(
:user
)
{
create
(
:user
)
}
let!
(
:user
)
{
create
(
:user
)
}
let!
(
:group_params
)
{
{
path:
"group_path"
,
visibility_level:
Gitlab
::
VisibilityLevel
::
PUBLIC
}
}
let!
(
:group_params
)
{
{
path:
"group_path"
,
visibility_level:
Gitlab
::
VisibilityLevel
::
PUBLIC
}
}
subject
{
service
.
execute
}
describe
'visibility level restrictions'
do
describe
'visibility level restrictions'
do
let!
(
:service
)
{
described_class
.
new
(
user
,
group_params
)
}
let!
(
:service
)
{
described_class
.
new
(
user
,
group_params
)
}
subject
{
service
.
execute
}
context
"create groups without restricted visibility level"
do
context
"create groups without restricted visibility level"
do
it
{
is_expected
.
to
be_persisted
}
it
{
is_expected
.
to
be_persisted
}
end
end
...
@@ -24,8 +24,6 @@ describe Groups::CreateService, '#execute', services: true do
...
@@ -24,8 +24,6 @@ describe Groups::CreateService, '#execute', services: true do
let!
(
:group
)
{
create
(
:group
)
}
let!
(
:group
)
{
create
(
:group
)
}
let!
(
:service
)
{
described_class
.
new
(
user
,
group_params
.
merge
(
parent_id:
group
.
id
))
}
let!
(
:service
)
{
described_class
.
new
(
user
,
group_params
.
merge
(
parent_id:
group
.
id
))
}
subject
{
service
.
execute
}
context
'as group owner'
do
context
'as group owner'
do
before
{
group
.
add_owner
(
user
)
}
before
{
group
.
add_owner
(
user
)
}
...
@@ -40,4 +38,15 @@ describe Groups::CreateService, '#execute', services: true do
...
@@ -40,4 +38,15 @@ describe Groups::CreateService, '#execute', services: true do
end
end
end
end
end
end
describe
'creating a mattermost team'
do
let!
(
:params
)
{
group_params
.
merge
(
create_chat_team:
true
)
}
let!
(
:service
)
{
described_class
.
new
(
user
,
params
)
}
it
'queues a background job'
do
expect
(
Mattermost
::
CreateTeamWorker
).
to
receive
(
:perform_async
)
subject
end
end
end
end
spec/workers/mattermost/create_team_worker_spec.rb
View file @
1a0e54b3
...
@@ -7,15 +7,30 @@ describe Mattermost::CreateTeamWorker do
...
@@ -7,15 +7,30 @@ describe Mattermost::CreateTeamWorker do
describe
'.perform'
do
describe
'.perform'
do
subject
{
described_class
.
new
.
perform
(
group
.
id
,
admin
.
id
)
}
subject
{
described_class
.
new
.
perform
(
group
.
id
,
admin
.
id
)
}
before
do
context
'succesfull request to mattermost'
do
allow_any_instance_of
(
Mattermost
::
Team
).
before
do
to
receive
(
:create
).
allow_any_instance_of
(
Mattermost
::
Team
).
with
(
name:
"path"
,
display_name:
"name"
,
type:
"O"
).
to
receive
(
:create
).
and_return
(
'name'
=>
'my team'
,
'id'
=>
'sjfkdlwkdjfwlkfjwf'
)
with
(
group
,
{}).
and_return
(
'name'
=>
'my team'
,
'id'
=>
'sjfkdlwkdjfwlkfjwf'
)
end
it
'creates a new chat team'
do
expect
{
subject
}.
to
change
{
ChatTeam
.
count
}.
from
(
0
).
to
(
1
)
end
end
end
it
'creates a new chat team'
do
context
'connection trouble'
do
expect
{
subject
}.
to
change
{
ChatTeam
.
count
}.
from
(
0
).
to
(
1
)
before
do
allow_any_instance_of
(
Mattermost
::
Team
).
to
receive
(
:create
).
with
(
group
,
{}).
and_raise
(
Mattermost
::
ClientError
.
new
(
'Undefined error'
))
end
it
'does not rescue the error'
do
expect
{
subject
}.
to
raise_error
(
Mattermost
::
ClientError
)
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