Commit 7363a7d3 authored by Z.J. van de Weg's avatar Z.J. van de Weg

Add tests for auto configure slash commands

parent 475a4dc3
...@@ -44,35 +44,9 @@ class Projects::ServicesController < Projects::ApplicationController ...@@ -44,35 +44,9 @@ class Projects::ServicesController < Projects::ApplicationController
redirect_back_or_default(options: message) redirect_back_or_default(options: message)
end end
def configure
host = Gitlab.config.mattermost.host
if @service.auto_config? && host
@service.configure(host, current_user, params)
redirect_to(
edit_namespace_project_service_path(@project.namespace, @project, @service.to_param),
notice: 'This service is now configured.'
)
else
redirect_to(
edit_namespace_project_service_path(@project.namespace, @project, @service.to_param),
alert: 'This service can not be automatticly configured.'
)
end
rescue Mattermost::NoSessionError
redirect_to(
edit_namespace_project_service_path(@project.namespace, @project, @service.to_param),
alert: 'An error occurred, is Mattermost configured with Single Sign on?'
)
end
private private
def service def service
@service ||= @project.find_or_initialize_service(params[:id]) @service ||= @project.find_or_initialize_service(params[:id])
end end
def configure_params
params.require(:auto_configure).permit(:trigger, :team_id)
end
end end
module MattermostHelper
def mattermost_teams_for(current_user)
return unless Gitlab.config.mattermost.enabled
# Hack to make frontend work better
return [{"id"=>"qz8gdr1fopncueb8n9on8ohk3h", "create_at"=>1479992105904, "update_at"=>1479992105904, "delete_at"=>0, "display_name"=>"chatops", "name"=>"chatops", "email"=>"admin@example.com", "type"=>"O", "company_name"=>"", "allowed_domains"=>"", "invite_id"=>"gthxi47gj7rxtcx6zama63zd1w", "allow_open_invite"=>false}]
host = Gitlab.config.mattermost.host
Mattermost::Mattermost.new(host, current_user).with_session do
Mattermost::Team.all
end
end
end
...@@ -6,12 +6,16 @@ ...@@ -6,12 +6,16 @@
%p= @service.description %p= @service.description
.col-lg-9 .col-lg-9
- # This returns an array of hashes, could you make a fancy dropdown :D = form_for(@service, as: :service, url: namespace_project_service_path(@project.namespace, @project, @service.to_param), method: :put, html: { class: 'form-horizontal' }) do |form|
- # Also, this is mocked for now, checkout the MattermostHelper to edit the data = render 'shared/service_settings', form: form, subject: @service
= mattermost_teams_for(current_user)
= form_for(:auto_configure, method: :post, url: configure_namespace_project_service_path(@project.namespace, @project, @service.to_param)) do |f|
= "Team ID"
= f.text_field(:team_id)
= "Team ID"
= f.submit 'Save changes', class: 'btn btn-save'
.footer-block.row-content-block
= form.submit 'Save changes', class: 'btn btn-save'
&nbsp;
- if @service.valid? && @service.activated?
- unless @service.can_test?
- disabled_class = 'disabled'
- disabled_title = @service.disabled_title
= link_to 'Test settings', test_namespace_project_service_path(@project.namespace, @project, @service), class: "btn #{disabled_class}", title: disabled_title
= link_to "Cancel", namespace_project_services_path(@project.namespace, @project), class: "btn btn-cancel"
- teams = Mattermost::Mattermost.new(Gitlab.config.mattermost.host, current_user).with_session do
Mattermost::Mattermost::Team.all
end
...@@ -13,9 +13,6 @@ ...@@ -13,9 +13,6 @@
.col-sm-10 .col-sm-10
= form.check_box :active = form.check_box :active
- if @service.auto_config?
- else
- if @service.supported_events.present? - if @service.supported_events.present?
.form-group .form-group
= form.label :url, "Trigger", class: 'control-label' = form.label :url, "Trigger", class: 'control-label'
......
...@@ -61,7 +61,6 @@ constraints(ProjectUrlConstrainer.new) do ...@@ -61,7 +61,6 @@ constraints(ProjectUrlConstrainer.new) do
resources :services, constraints: { id: /[^\/]+/ }, only: [:index, :edit, :update] do resources :services, constraints: { id: /[^\/]+/ }, only: [:index, :edit, :update] do
member do member do
get :test get :test
post :configure
end end
end end
......
...@@ -14,9 +14,13 @@ module Mattermost ...@@ -14,9 +14,13 @@ module Mattermost
icon_url: icon_url icon_url: icon_url
} }
response = post( "/teams/#{team_id}/commands/create", body: command.to_json) post_command(command)['token']
end
private
response.parsed_response['token'] def post_command(command)
post( "/teams/#{team_id}/commands/create", body: command.to_json).parsed_response
end end
end end
end end
module Mattermost module Mattermost
class Team < Session class Team < Session
def self.team_admin def self.team_admin
body = get('/users/initial_load').parsed_response return [] unless initial_load['team_members']
return [] unless body['team_members'] team_ids = initial_load['team_members'].map do |team|
team_ids = body['team_members'].map do |team|
team['team_id'] if team['roles'].split.include?('team_admin') team['team_id'] if team['roles'].split.include?('team_admin')
end.compact end.compact
body['teams'].select do |team| initial_load['teams'].select do |team|
team_ids.include?(team['id']) team_ids.include?(team['id'])
end end
end end
private
def initial_load
@initial_load ||= get('/users/initial_load').parsed_response
end
end end
end end
{"user":{"id":"78nm4euoc7dypergdc13ekxgpo","create_at":1481826051672,"update_at":1481835484207,"delete_at":0,"username":"root","auth_data":"","auth_service":"gitlab","email":"admin@example.com","email_verified":true,"nickname":"","first_name":"Administrator","last_name":"","roles":"system_admin system_user","notify_props":{"channel":"true","desktop":"all","desktop_sound":"true","email":"true","first_name":"true","mention_keys":"root,@root","push":"mention"},"last_password_update":1481826051672,"locale":"en"},"team_members":[{"team_id":"w59qt5a817f69jkxdz6xe7y4ir","user_id":"78nm4euoc7dypergdc13ekxgpo","roles":"team_user team_admin","delete_at":0},{"team_id":"my9oujxf5jy1zqdgu9rihd66do","user_id":"78nm4euoc7dypergdc13ekxgpo","roles":"team_user team_admin","delete_at":0}],"teams":[{"id":"w59qt5a817f69jkxdz6xe7y4ir","create_at":1481835484179,"update_at":1481835484179,"delete_at":0,"display_name":"new_team","name":"new-team","email":"","type":"O","company_name":"","allowed_domains":"","invite_id":"mfgsqnmpiby18eepo6jd6pq3oh","allow_open_invite":false},{"id":"my9oujxf5jy1zqdgu9rihd66do","create_at":1481826062406,"update_at":1481826062406,"delete_at":0,"display_name":"chatops","name":"chatops","email":"","type":"O","company_name":"","allowed_domains":"","invite_id":"s7c1phenmi8udkybcyytc3pxuh","allow_open_invite":false}],"preferences":[{"user_id":"78nm4euoc7dypergdc13ekxgpo","category":"last","name":"channel","value":"u4j58zgjyt8zd8nwwhaqjkyqzw"},{"user_id":"78nm4euoc7dypergdc13ekxgpo","category":"tutorial_step","name":"78nm4euoc7dypergdc13ekxgpo","value":"999"}],"client_cfg":{"AboutLink":"/static/help/about.html","AndroidAppDownloadLink":"https://about.mattermost.com/mattermost-android-app/","AppDownloadLink":"https://about.mattermost.com/downloads/","AvailableLocales":"","BuildDate":"Wed Nov 23 19:43:58 UTC 2016","BuildEnterpriseReady":"false","BuildHash":"36f62c9e82350f58c902f64a5d3304872431ad41","BuildHashEnterprise":"none","BuildNumber":"3.5.1","DefaultClientLocale":"en","EnableCommands":"true","EnableCustomEmoji":"false","EnableDeveloper":"false","EnableDiagnostics":"true","EnableEmailBatching":"false","EnableIncomingWebhooks":"false","EnableOAuthServiceProvider":"false","EnableOnlyAdminIntegrations":"true","EnableOpenServer":"false","EnableOutgoingWebhooks":"false","EnablePostIconOverride":"true","EnablePostUsernameOverride":"true","EnablePublicLink":"true","EnableSignInWithEmail":"true","EnableSignInWithUsername":"false","EnableSignUpWithEmail":"false","EnableSignUpWithGitLab":"true","EnableTeamCreation":"true","EnableTesting":"false","EnableUserCreation":"true","EnableWebrtc":"false","GoogleDeveloperKey":"","HelpLink":"","IosAppDownloadLink":"https://about.mattermost.com/mattermost-ios-app/","MaxFileSize":"52428800","PrivacyPolicyLink":"/static/help/privacy.html","ProfileHeight":"128","ProfileWidth":"128","ReportAProblemLink":"/static/help/report_problem.html","RequireEmailVerification":"false","RestrictCustomEmojiCreation":"all","RestrictDirectMessage":"any","RestrictPrivateChannelManagement":"all","RestrictPublicChannelManagement":"all","RestrictTeamInvite":"all","SQLDriverName":"postgres","SegmentDeveloperKey":"","SendEmailNotifications":"false","SendPushNotifications":"false","ShowEmailAddress":"true","SiteName":"GitLab Mattermost","SiteURL":"","SupportEmail":"support@example.com","TermsOfServiceLink":"/static/help/terms.html","Version":"3.5.0","WebsocketPort":"80","WebsocketSecurePort":"443"},"license_cfg":{"IsLicensed":"false"},"no_accounts":false}
{"id":"y8j1nexrdirj5nubq5uzdwwidr","token":"pzajm5hfbtni3r49ujpt8betpc","create_at":1481897117122,"update_at":1481897117122,"delete_at":0,"creator_id":"78nm4euoc7dypergdc13ekxgpo","team_id":"w59qt5a817f69jkxdz6xe7y4ir","trigger":"display","method":"P","username":"GitLab","icon_url":"","auto_complete":false,"auto_complete_desc":"","auto_complete_hint":"","display_name":"Display name","description":"the description","url":"http://trigger.url/trigger"}
require 'spec_helper'
describe Mattermost::Command do
describe '.create' do
let(:new_command) do
JSON.parse(File.read(Rails.root.join('spec/fixtures/', 'mattermost_new_command.json')))
end
it 'gets the teams' do
allow(described_class).to receive(:post_command).and_return(new_command)
token = described_class.create('abc', url: 'http://trigger.url/trigger', icon_url: 'http://myicon.com/icon.png')
expect(token).to eq('pzajm5hfbtni3r49ujpt8betpc')
end
end
end
require 'spec_helper' require 'spec_helper'
describe Mattermost::Team do describe Mattermost::Team do
let(:session) { Mattermost::Session.new('http://localhost:8065/', nil) } describe '.team_admin' do
let(:init_load) do
JSON.parse(File.read(Rails.root.join('spec/fixtures/', 'mattermost_initial_load.json')))
end
describe '.all' do
let(:result) { {id: 'abc', display_name: 'team'} }
before do before do
WebMock.stub_request(:get, 'http://localhost:8065/api/v3/teams/all'). allow(described_class).to receive(:initial_load).and_return(init_load)
and_return({ abc: result }.to_json)
end end
xit 'gets the teams' do it 'gets the teams' do
allow(session).to receive(:with_session) { yield } expect(described_class.team_admin.count).to be(2)
end
expect(described_class.all).to eq(result) it 'filters on being team admin' do
ids = described_class.team_admin.map { |team| team['id'] }
expect(ids).to include("w59qt5a817f69jkxdz6xe7y4ir", "my9oujxf5jy1zqdgu9rihd66do")
end end
end end
end end
...@@ -96,4 +96,35 @@ describe MattermostSlashCommandsService, models: true do ...@@ -96,4 +96,35 @@ describe MattermostSlashCommandsService, models: true do
end end
end end
end end
describe '#configure' do
let(:project) { create(:empty_project) }
let(:service) { project.build_mattermost_slash_commands_service }
subject do
service.configure('http://localhost:8065', nil, team_id: 'abc', trigger: 'gitlab', url: 'http://trigger.url', icon_url: 'http://icon.url/icon.png')
end
it 'creates a new Mattermost session' do
expect_any_instance_of(Mattermost::Session).to receive(:with_session)
subject
end
it 'saves the service' do
allow_any_instance_of(Mattermost::Session).to receive(:with_session).
and_return('mynewtoken')
expect { subject }.to change { project.services.count }.by(1)
end
it 'saves the token' do
allow_any_instance_of(Mattermost::Session).to receive(:with_session).
and_return('mynewtoken')
subject
expect(service.reload.token).to eq('mynewtoken')
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