Commit 0d04724f authored by Z.J. van de Weg's avatar Z.J. van de Weg

More coverage on service level

parent 778b5a5a
...@@ -90,6 +90,15 @@ module API ...@@ -90,6 +90,15 @@ module API
@project_service || not_found!("Service") @project_service || not_found!("Service")
end 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 def service_attributes
@service_attributes ||= project_service.fields.inject([]) do |arr, hash| @service_attributes ||= project_service.fields.inject([]) do |arr, hash|
arr << hash[:name].to_sym arr << hash[:name].to_sym
......
...@@ -67,12 +67,7 @@ module API ...@@ -67,12 +67,7 @@ module API
post ':id/services/:service_slug/trigger' do post ':id/services/:service_slug/trigger' do
project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id]) project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id])
underscored_service = params[:service_slug].underscore service = service_by_slug(project, params[:service_slug])
not_found!('Service') unless Service.available_services_names.include?(underscored_service)
service_method = "#{underscored_service}_service"
service = project.public_send(service_method)
result = service.try(:active?) && service.try(:trigger, params) result = service.try(:active?) && service.try(:trigger, params)
......
...@@ -4,7 +4,11 @@ module Mattermost ...@@ -4,7 +4,11 @@ module Mattermost
include Rails.application.routes.url_helpers include Rails.application.routes.url_helpers
def authorize_chat_name(url) 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) ephemeral_response(message)
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::ChatCommands::Command, service: true do describe Gitlab::ChatCommands::Command, service: true do
let(:project) { create(:project) } let(:project) { create(:empty_project) }
let(:user) { create(:user) } let(:user) { create(:user) }
subject { described_class.new(project, user, params).execute } subject { described_class.new(project, user, params).execute }
......
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
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
...@@ -88,4 +88,50 @@ describe API::API, api: true do ...@@ -88,4 +88,50 @@ describe API::API, api: true do
end end
end 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 end
...@@ -13,7 +13,7 @@ describe ChatNames::FindUserService, services: true do ...@@ -13,7 +13,7 @@ describe ChatNames::FindUserService, services: true do
context 'when existing user is requested' do context 'when existing user is requested' do
let(:params) { { team_id: chat_name.team_id, user_id: chat_name.chat_id } } 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) is_expected.to eq(user)
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