summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZ.J. van de Weg <git@zjvandeweg.nl>2016-11-18 10:00:40 +0100
committerZ.J. van de Weg <git@zjvandeweg.nl>2016-11-18 10:00:40 +0100
commit0d04724fa1cd670124b8ad9a3860bfa476c50f99 (patch)
treefc8b96d32eda2bfe9633fa0eff1569237e4d454e
parent778b5a5a04c4861c84408c944fa8dc01411cbf55 (diff)
downloadgitlab-ce-0d04724fa1cd670124b8ad9a3860bfa476c50f99.tar.gz
More coverage on service level
-rw-r--r--lib/api/helpers.rb9
-rw-r--r--lib/api/services.rb7
-rw-r--r--lib/mattermost/presenter.rb6
-rw-r--r--spec/lib/gitlab/chat_commands/command_spec.rb2
-rw-r--r--spec/models/project_services/chat_service_spec.rb15
-rw-r--r--spec/models/project_services/mattermost_command_service_spec.rb99
-rw-r--r--spec/requests/api/services_spec.rb46
-rw-r--r--spec/services/chat_names/find_user_service_spec.rb2
8 files changed, 177 insertions, 9 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index 84cc9200d1b..d6526ec4fdc 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -90,6 +90,15 @@ module API
@project_service || not_found!("Service")
end
+ def service_by_slug(project, slug)
+ underscored_service = slug.underscore
+
+ not_found!('Service') unless Service.available_services_names.include?(underscored_service)
+ service_method = "#{underscored_service}_service"
+
+ service = project.public_send(service_method)
+ end
+
def service_attributes
@service_attributes ||= project_service.fields.inject([]) do |arr, hash|
arr << hash[:name].to_sym
diff --git a/lib/api/services.rb b/lib/api/services.rb
index b0a94508d10..163187d450d 100644
--- a/lib/api/services.rb
+++ b/lib/api/services.rb
@@ -67,12 +67,7 @@ module API
post ':id/services/:service_slug/trigger' do
project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id])
- underscored_service = params[:service_slug].underscore
-
- not_found!('Service') unless Service.available_services_names.include?(underscored_service)
- service_method = "#{underscored_service}_service"
-
- service = project.public_send(service_method)
+ service = service_by_slug(project, params[:service_slug])
result = service.try(:active?) && service.try(:trigger, params)
diff --git a/lib/mattermost/presenter.rb b/lib/mattermost/presenter.rb
index 7722022c658..d7455d39bce 100644
--- a/lib/mattermost/presenter.rb
+++ b/lib/mattermost/presenter.rb
@@ -4,7 +4,11 @@ module Mattermost
include Rails.application.routes.url_helpers
def authorize_chat_name(url)
- message = ":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{url})."
+ message = if url
+ ":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{url})."
+ else
+ ":sweat_smile: Couldn't identify you, nor can I autorize you!"
+ end
ephemeral_response(message)
end
diff --git a/spec/lib/gitlab/chat_commands/command_spec.rb b/spec/lib/gitlab/chat_commands/command_spec.rb
index 528e690d234..11b607a6843 100644
--- a/spec/lib/gitlab/chat_commands/command_spec.rb
+++ b/spec/lib/gitlab/chat_commands/command_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'
describe Gitlab::ChatCommands::Command, service: true do
- let(:project) { create(:project) }
+ let(:project) { create(:empty_project) }
let(:user) { create(:user) }
subject { described_class.new(project, user, params).execute }
diff --git a/spec/models/project_services/chat_service_spec.rb b/spec/models/project_services/chat_service_spec.rb
new file mode 100644
index 00000000000..c6a45a3e1be
--- /dev/null
+++ b/spec/models/project_services/chat_service_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe ChatService, models: true do
+ describe "Associations" do
+ it { is_expected.to have_many :chat_names }
+ end
+
+ describe '#valid_token?' do
+ subject { described_class.new }
+
+ it 'is false as it has no token' do
+ expect(subject.valid_token?('wer')).to be_falsey
+ end
+ end
+end
diff --git a/spec/models/project_services/mattermost_command_service_spec.rb b/spec/models/project_services/mattermost_command_service_spec.rb
new file mode 100644
index 00000000000..757ad687bc4
--- /dev/null
+++ b/spec/models/project_services/mattermost_command_service_spec.rb
@@ -0,0 +1,99 @@
+require 'spec_helper'
+
+describe MattermostCommandService, models: true do
+ describe "Associations" do
+ it { is_expected.to respond_to :token }
+ end
+
+ describe '#valid_token?' do
+ subject { described_class.new }
+
+ context 'when the token is empty' do
+ it 'is false' do
+ expect(subject.valid_token?('wer')).to be_falsey
+ end
+ end
+
+ context 'when there is a token' do
+ before do
+ subject.token = '123'
+ end
+
+ it 'accepts equal tokens' do
+ expect(subject.valid_token?('123')).to be_truthy
+ end
+ end
+ end
+
+ describe '#trigger' do
+ subject { described_class.new }
+
+ context 'no token is passed' do
+ let(:params) { Hash.new }
+
+ it 'returns nil' do
+ expect(subject.trigger(params)).to be_nil
+ end
+ end
+
+ context 'with a token passed' do
+ let(:project) { create(:empty_project) }
+ let(:params) { { token: 'token' } }
+
+ before do
+ allow(subject).to receive(:token).and_return('token')
+ end
+
+ context 'no user can be found' do
+ context 'when no url can be generated' do
+ it 'responds with the authorize url' do
+ response = subject.trigger(params)
+
+ expect(response[:response_type]).to eq :ephemeral
+ expect(response[:text]).to start_with ":sweat_smile: Couldn't identify you"
+ end
+ end
+
+ context 'when an auth url can be generated' do
+ let(:params) do
+ {
+ team_domain: 'http://domain.tld',
+ team_id: 'T3423423',
+ user_id: 'U234234',
+ user_name: 'mepmep',
+ token: 'token'
+ }
+ end
+
+ let(:service) do
+ project.create_mattermost_command_service(
+ properties: { token: 'token' }
+ )
+ end
+
+ it 'generates the url' do
+ response = service.trigger(params)
+
+ expect(response[:text]).to start_with(':wave: Hi there!')
+ end
+ end
+ end
+
+ context 'when the user is authenticated' do
+ let!(:chat_name) { create(:chat_name, service: service) }
+ let(:service) do
+ project.create_mattermost_command_service(
+ properties: { token: 'token' }
+ )
+ end
+ let(:params) { { token: 'token', team_id: chat_name.team_id, user_id: chat_name.chat_id } }
+
+ it 'triggers the command' do
+ expect_any_instance_of(Gitlab::ChatCommands::Command).to receive(:execute)
+
+ service.trigger(params)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/requests/api/services_spec.rb b/spec/requests/api/services_spec.rb
index 2aadab3cbe1..fb234ab8ed1 100644
--- a/spec/requests/api/services_spec.rb
+++ b/spec/requests/api/services_spec.rb
@@ -88,4 +88,50 @@ describe API::API, api: true do
end
end
end
+
+ describe 'POST /projects/:id/services/:slug/trigger' do
+ let!(:project) { create(:empty_project) }
+ let(:service_name) { 'mattermost_command' }
+
+ context 'no service is available' do
+ it 'returns a not found message' do
+ post api("/projects/#{project.id}/services/mattermost_command/trigger")
+
+ expect(response).to have_http_status(404)
+ end
+ end
+
+ context 'the service exists' do
+ context 'the service is not active' do
+ let!(:inactive_service) do
+ project.create_mattermost_command_service(
+ active: false,
+ properties: { token: 'token' }
+ )
+ end
+
+ it 'when the service is inactive' do
+ post api("/projects/#{project.id}/services/mattermost_command/trigger")
+
+ expect(response).to have_http_status(404)
+ end
+ end
+
+ context 'the service is active' do
+ let!(:active_service) do
+ project.create_mattermost_command_service(
+ active: true,
+ properties: { token: 'token' }
+ )
+ end
+ let(:params) { { token: 'token' } }
+
+ it 'retusn status 200' do
+ post api("/projects/#{project.id}/services/mattermost_command/trigger"), params
+
+ expect(response).to have_http_status(200)
+ end
+ end
+ end
+ end
end
diff --git a/spec/services/chat_names/find_user_service_spec.rb b/spec/services/chat_names/find_user_service_spec.rb
index 5b885b2c657..51441e8f3be 100644
--- a/spec/services/chat_names/find_user_service_spec.rb
+++ b/spec/services/chat_names/find_user_service_spec.rb
@@ -13,7 +13,7 @@ describe ChatNames::FindUserService, services: true do
context 'when existing user is requested' do
let(:params) { { team_id: chat_name.team_id, user_id: chat_name.chat_id } }
- it 'returns existing user' do
+ it 'returns the existing user' do
is_expected.to eq(user)
end