diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-12-20 12:02:37 +0100 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-12-20 12:02:37 +0100 |
commit | 0cf23fde7c666b64e6c18a92d29e632f51b00059 (patch) | |
tree | 943171ba614758895e88bd1b69d2add1a517da91 | |
parent | d305d15b6dde99b0de7fc78242aaa095fc79b5ca (diff) | |
download | gitlab-ce-0cf23fde7c666b64e6c18a92d29e632f51b00059.tar.gz |
Work on tests for mattermost
-rw-r--r-- | app/helpers/projects_helper.rb | 8 | ||||
-rw-r--r-- | lib/mattermost/client.rb | 6 | ||||
-rw-r--r-- | lib/mattermost/session.rb | 14 | ||||
-rw-r--r-- | spec/models/project_services/mattermost_slash_commands_service_spec.rb | 35 |
4 files changed, 56 insertions, 7 deletions
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index b7731ab4be2..bd2dcb08b3e 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -150,10 +150,10 @@ module ProjectsHelper def mattermost_teams_options(teams) teams_options = teams.map do |id, options| - return nil unless id && options['display_name'] - [options['display_name'], id] - end.compact - teams_options.unshift(['Select team...', '0']) + [options['display_name'] || options['name'], id] + end + + teams_options.compact.unshift(['Select team...', '0']) end private diff --git a/lib/mattermost/client.rb b/lib/mattermost/client.rb index 8bbb2038772..d6759eac0d0 100644 --- a/lib/mattermost/client.rb +++ b/lib/mattermost/client.rb @@ -1,4 +1,6 @@ module Mattermost + class ClientError < Mattermost::Error; end + class Client attr_reader :user @@ -30,9 +32,9 @@ module Mattermost if response.success? json_response elsif json_response['message'] - raise json_response['message'] + raise ClientError(json_response['message']) else - raise 'Undefined error' + raise ClientError('Undefined error') end end end diff --git a/lib/mattermost/session.rb b/lib/mattermost/session.rb index f0ce51d6a71..e36500d24a3 100644 --- a/lib/mattermost/session.rb +++ b/lib/mattermost/session.rb @@ -1,10 +1,18 @@ module Mattermost - class NoSessionError < StandardError + class Error < StandardError; end + + class NoSessionError < Error def message 'No session could be set up, is Mattermost configured with Single Sign on?' end end + class ConnectionError < Error + def message + 'Could not connect. Is Mattermost up?' + end + 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. # @@ -66,10 +74,14 @@ module Mattermost def get(path, options = {}) self.class.get(path, options.merge(headers: @headers)) + rescue Errno::ECONNREFUSED + raise ConnectionError end def post(path, options = {}) self.class.post(path, options.merge(headers: @headers)) + rescue Errno::ECONNREFUSED + raise ConnectionError end private diff --git a/spec/models/project_services/mattermost_slash_commands_service_spec.rb b/spec/models/project_services/mattermost_slash_commands_service_spec.rb index 1ae1483e2a4..9fb6132d171 100644 --- a/spec/models/project_services/mattermost_slash_commands_service_spec.rb +++ b/spec/models/project_services/mattermost_slash_commands_service_spec.rb @@ -2,4 +2,39 @@ require 'spec_helper' describe MattermostSlashCommandsService, :models do it_behaves_like "chat slash commands service" + + describe '#configure!' do + let(:project) { create(:empty_project) } + let(:service) { project.build_mattermost_slash_commands_service } + let(:user) { create(:user)} + + before do + allow_any_instance_of(Mattermost::Session).to + receive(:with_session).and_yield + end + + subject do + service.configure!(user, team_id: 'abc', + trigger: 'gitlab', url: 'http://trigger.url', + icon_url: 'http://icon.url/icon.png') + end + + context 'the requests succeeds' do + 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('mynewtoken') + end + end + + context 'an error is received' do + it 'shows error messages' do + expect(subject).to raise_error("Error") + end + end + end end |