Commit 55c61d2e authored by Kamil Trzcinski's avatar Kamil Trzcinski

Improve API specs

parent 22a05678
...@@ -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
...@@ -44,7 +44,7 @@ class MattermostSlashCommandsService < ChatSlashCommandsService ...@@ -44,7 +44,7 @@ class MattermostSlashCommandsService < ChatSlashCommandsService
auto_complete_desc: "Perform common operations on: #{pretty_project_name}", auto_complete_desc: "Perform common operations on: #{pretty_project_name}",
auto_complete_hint: '[help]', auto_complete_hint: '[help]',
description: "Perform common operations on: #{pretty_project_name}", description: "Perform common operations on: #{pretty_project_name}",
display_name: "GitLab / #{pretty_project_name}", display_name: "GitLab / #{pretty_project_name}",
method: 'P', method: 'P',
user_name: 'GitLab') user_name: 'GitLab')
end end
......
---
title: Allow to auto-configure Mattermost
merge_request: 8070
author:
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 = {})
self.class.get(path, options.merge(headers: @headers)) handle_exceptions do
rescue HTTParty::Error => e self.class.get(path, options.merge(headers: @headers))
raise Mattermost::ConnectionError.new(e.message) end
rescue Errno::ECONNREFUSED => e
raise Mattermost::ConnectionError.new(e.message)
end end
def post(path, options = {}) def post(path, options = {})
self.class.post(path, options.merge(headers: @headers)) handle_exceptions do
rescue HTTParty::Error => e self.class.post(path, options.merge(headers: @headers))
raise Mattermost::ConnectionError.new(e.message) end
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
...@@ -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
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
subject.create(params) 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
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 }
let(:response) do
[{ context 'for valid request' do
"id" => "xiyro8huptfhdndadpz8r3wnbo", let(:response) do
"create_at" => 1482174222155, [{
"update_at" => 1482174222155, "id" => "xiyro8huptfhdndadpz8r3wnbo",
"delete_at" => 0, "create_at" => 1482174222155,
"display_name" => "chatops", "update_at" => 1482174222155,
"name" => "chatops", "delete_at" => 0,
"email" => "admin@example.com", "display_name" => "chatops",
"type" => "O", "name" => "chatops",
"company_name" => "", "email" => "admin@example.com",
"allowed_domains" => "", "type" => "O",
"invite_id" => "o4utakb9jtb7imctdfzbf9r5ro", "company_name" => "",
"allow_open_invite" => false }] "allowed_domains" => "",
end "invite_id" => "o4utakb9jtb7imctdfzbf9r5ro",
"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
before do it 'returns a token' do
allow(subject).to receive(:json_get).and_return(response) is_expected.to eq(response)
end
end end
it 'gets the teams' do context 'for error message' do
expect(subject.all.count).to be(1) before do
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 'raises an error with message' do
expect { subject }.to raise_error(Mattermost::Error, 'Cannot list teams.')
end
end end
end end
end end
...@@ -3,40 +3,121 @@ require 'spec_helper' ...@@ -3,40 +3,121 @@ 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)}
subject do before do
service.configure(user, team_id: 'abc', Mattermost::Session.base_uri("http://mattermost.example.com")
trigger: 'gitlab', url: 'http://trigger.url',
icon_url: 'http://icon.url/icon.png') allow_any_instance_of(Mattermost::Client).to receive(:with_session).
and_yield(Mattermost::Session.new(nil))
end end
context 'the requests succeeds' do describe '#configure' do
before do subject do
allow_any_instance_of(Mattermost::Command). service.configure(user, team_id: 'abc',
to receive(:json_post).and_return('token' => 'token') trigger: 'gitlab', url: 'http://trigger.url',
icon_url: 'http://icon.url/icon.png')
end end
it 'saves the service' do context 'the requests succeeds' do
expect { subject }.to change { project.services.count }.by(1) before do
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
expect { subject }.to change { project.services.count }.by(1)
end
it 'saves the token' do
subject
expect(service.reload.token).to eq('token')
end
end end
it 'saves the token' do context 'an error is received' do
subject 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(service.reload.token).to eq('token') expect(succeeded).to be(false)
expect(message).to eq('This trigger word is already in use. Please choose another word.')
end
end end
end end
context 'an error is received' do describe '#list_teams' do
it 'shows error messages' do subject do
succeeded, message = subject 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(succeeded).to be(false) expect(teams).to be_empty
expect(message).to start_with("Failed to open TCP connection to") expect(message).to eq('Failed to get team list.')
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment