Commit 6368072f authored by Tiago Botelho's avatar Tiago Botelho

improves test settings for chat notification services for empty projects

parent c7680264
...@@ -22,7 +22,7 @@ class ChatNotificationService < Service ...@@ -22,7 +22,7 @@ class ChatNotificationService < Service
end end
def can_test? def can_test?
super && valid? valid?
end end
def self.supported_events def self.supported_events
......
...@@ -131,7 +131,7 @@ class Service < ActiveRecord::Base ...@@ -131,7 +131,7 @@ class Service < ActiveRecord::Base
end end
def can_test? def can_test?
!project.empty_repo? true
end end
# reason why service cannot be tested # reason why service cannot be tested
......
---
title: Improves test settings for chat notification services for empty projects
merge_request: 10886
author:
...@@ -41,7 +41,7 @@ module Gitlab ...@@ -41,7 +41,7 @@ module Gitlab
type = Gitlab::Git.tag_ref?(ref) ? 'tag_push' : 'push' type = Gitlab::Git.tag_ref?(ref) ? 'tag_push' : 'push'
# Hash to be passed as post_receive_data # Hash to be passed as post_receive_data
data = { {
object_kind: type, object_kind: type,
event_name: type, event_name: type,
before: oldrev, before: oldrev,
...@@ -61,16 +61,15 @@ module Gitlab ...@@ -61,16 +61,15 @@ module Gitlab
repository: project.hook_attrs.slice(:name, :url, :description, :homepage, repository: project.hook_attrs.slice(:name, :url, :description, :homepage,
:git_http_url, :git_ssh_url, :visibility_level) :git_http_url, :git_ssh_url, :visibility_level)
} }
data
end end
# This method provide a sample data generated with # This method provide a sample data generated with
# existing project and commits to test webhooks # existing project and commits to test webhooks
def build_sample(project, user) def build_sample(project, user)
commits = project.repository.commits(project.default_branch, limit: 3)
ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}" ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}"
build(project, user, commits.last.id, commits.first.id, ref, commits) commits = project.repository.commits(project.default_branch.to_s, limit: 3) rescue []
build(project, user, commits.last&.id, commits.first&.id, ref, commits)
end end
def checkout_sha(repository, newrev, ref) def checkout_sha(repository, newrev, ref)
......
...@@ -8,6 +8,7 @@ describe Projects::ServicesController do ...@@ -8,6 +8,7 @@ describe Projects::ServicesController do
before do before do
sign_in(user) sign_in(user)
project.team << [user, :master] project.team << [user, :master]
controller.instance_variable_set(:@project, project) controller.instance_variable_set(:@project, project)
controller.instance_variable_set(:@service, service) controller.instance_variable_set(:@service, service)
end end
...@@ -18,20 +19,60 @@ describe Projects::ServicesController do ...@@ -18,20 +19,60 @@ describe Projects::ServicesController do
end end
describe "#test" do describe "#test" do
context 'when can_test? returns false' do
it 'renders 404' do
allow_any_instance_of(Service).to receive(:can_test?).and_return(false)
get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html
expect(response).to have_http_status(404)
end
end
context 'success' do context 'success' do
context 'with empty project' do
let(:project) { create(:empty_project) }
context 'with chat notification service' do
let(:service) { project.create_microsoft_teams_service(webhook: 'http://webhook.com') }
it 'redirects and show success message' do
allow_any_instance_of(MicrosoftTeams::Notifier).to receive(:ping).and_return(true)
get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html
expect(response).to redirect_to(root_path)
expect(flash[:notice]).to eq('We sent a request to the provided URL')
end
end
it 'redirects and show success message' do
expect(service).to receive(:test).and_return(success: true, result: 'done')
get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html
expect(response).to redirect_to(root_path)
expect(flash[:notice]).to eq('We sent a request to the provided URL')
end
end
it "redirects and show success message" do it "redirects and show success message" do
expect(service).to receive(:test).and_return({ success: true, result: 'done' }) expect(service).to receive(:test).and_return(success: true, result: 'done')
get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html
expect(response.status).to redirect_to('/')
expect(response).to redirect_to(root_path)
expect(flash[:notice]).to eq('We sent a request to the provided URL') expect(flash[:notice]).to eq('We sent a request to the provided URL')
end end
end end
context 'failure' do context 'failure' do
it "redirects and show failure message" do it "redirects and show failure message" do
expect(service).to receive(:test).and_return({ success: false, result: 'Bad test' }) expect(service).to receive(:test).and_return(success: false, result: 'Bad test')
get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html
expect(response.status).to redirect_to('/')
expect(response).to redirect_to(root_path)
expect(flash[:alert]).to eq('We tried to send a request to the provided URL but an error occurred: Bad test') expect(flash[:alert]).to eq('We tried to send a request to the provided URL but an error occurred: Bad test')
end end
end end
......
...@@ -11,10 +11,10 @@ describe ChatNotificationService, models: true do ...@@ -11,10 +11,10 @@ describe ChatNotificationService, models: true do
describe '#can_test?' do describe '#can_test?' do
context 'with empty repository' do context 'with empty repository' do
it 'returns false' do it 'returns true' do
subject.project = create(:empty_project, :empty_repo) subject.project = create(:empty_project, :empty_repo)
expect(subject.can_test?).to be false expect(subject.can_test?).to be true
end end
end end
......
...@@ -7,43 +7,48 @@ describe Service, models: true do ...@@ -7,43 +7,48 @@ describe Service, models: true do
end end
describe "Test Button" do describe "Test Button" do
before do describe '#can_test?' do
@service = Service.new let(:service) { create(:service, project: project) }
end
describe "Testable" do context 'when repository is not empty' do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
before do it 'returns true' do
allow(@service).to receive(:project).and_return(project) expect(service.can_test?).to be true
@testable = @service.can_test? end
end end
describe '#can_test?' do context 'when repository is empty' do
it { expect(@testable).to eq(true) } let(:project) { create(:empty_project) }
it 'returns true' do
expect(service.can_test?).to be true
end
end end
end
describe '#test' do describe '#test' do
let(:data) { 'test' } let(:data) { 'test' }
let(:service) { create(:service, project: project) }
context 'when repository is not empty' do
let(:project) { create(:project, :repository) }
it 'test runs execute' do it 'test runs execute' do
expect(@service).to receive(:execute).with(data) expect(service).to receive(:execute).with(data)
@service.test(data) service.test(data)
end end
end end
end
describe "With commits" do context 'when repository is empty' do
let(:project) { create(:project, :repository) } let(:project) { create(:empty_project) }
before do it 'test runs execute' do
allow(@service).to receive(:project).and_return(project) expect(service).to receive(:execute).with(data)
@testable = @service.can_test?
end
describe '#can_test?' do service.test(data)
it { expect(@testable).to eq(true) } 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