2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
require 'spec_helper'
2021-06-08 01:23:25 +05:30
RSpec . describe Integrations :: ChatMessage :: DeploymentMessage do
2022-01-12 12:59:36 +05:30
subject { described_class . new ( args ) }
let_it_be ( :user ) { create ( :user , name : 'John Smith' , username : 'smith' ) }
let_it_be ( :namespace ) { create ( :namespace , name : 'myspace' ) }
let_it_be ( :project ) { create ( :project , :repository , namespace : namespace , name : 'myproject' ) }
let_it_be ( :commit ) { project . commit ( 'HEAD' ) }
let_it_be ( :ci_build ) { create ( :ci_build , project : project ) }
let_it_be ( :environment ) { create ( :environment , name : 'myenvironment' , project : project ) }
let_it_be ( :deployment ) { create ( :deployment , status : :success , deployable : ci_build , environment : environment , project : project , user : user , sha : commit . sha ) }
let ( :args ) do
2022-08-13 15:12:31 +05:30
Gitlab :: DataBuilder :: Deployment . build ( deployment , 'success' , Time . current )
2022-01-12 12:59:36 +05:30
end
2019-07-31 22:56:46 +05:30
2022-01-12 12:59:36 +05:30
it_behaves_like Integrations :: ChatMessage
2019-07-31 22:56:46 +05:30
2022-01-12 12:59:36 +05:30
describe '#pretext' do
it 'returns a message with the data returned by the deployment data builder' do
expect ( subject . pretext ) . to eq ( " Deploy to myenvironment succeeded " )
2019-07-31 22:56:46 +05:30
end
it 'returns a message for a successful deployment' do
2022-01-12 12:59:36 +05:30
args . merge! (
2019-07-31 22:56:46 +05:30
status : 'success' ,
environment : 'production'
2022-01-12 12:59:36 +05:30
)
2019-07-31 22:56:46 +05:30
2022-01-12 12:59:36 +05:30
expect ( subject . pretext ) . to eq ( 'Deploy to production succeeded' )
2019-07-31 22:56:46 +05:30
end
it 'returns a message for a failed deployment' do
2022-01-12 12:59:36 +05:30
args . merge! (
2019-07-31 22:56:46 +05:30
status : 'failed' ,
environment : 'production'
2022-01-12 12:59:36 +05:30
)
2019-07-31 22:56:46 +05:30
2022-01-12 12:59:36 +05:30
expect ( subject . pretext ) . to eq ( 'Deploy to production failed' )
2019-07-31 22:56:46 +05:30
end
it 'returns a message for a canceled deployment' do
2022-01-12 12:59:36 +05:30
args . merge! (
2019-07-31 22:56:46 +05:30
status : 'canceled' ,
environment : 'production'
2022-01-12 12:59:36 +05:30
)
2019-07-31 22:56:46 +05:30
2022-01-12 12:59:36 +05:30
expect ( subject . pretext ) . to eq ( 'Deploy to production canceled' )
2019-07-31 22:56:46 +05:30
end
it 'returns a message for a deployment to another environment' do
2022-01-12 12:59:36 +05:30
args . merge! (
2019-07-31 22:56:46 +05:30
status : 'success' ,
environment : 'staging'
2022-01-12 12:59:36 +05:30
)
2019-07-31 22:56:46 +05:30
2022-01-12 12:59:36 +05:30
expect ( subject . pretext ) . to eq ( 'Deploy to staging succeeded' )
2019-07-31 22:56:46 +05:30
end
it 'returns a message for a deployment with any other status' do
2022-01-12 12:59:36 +05:30
args . merge! (
2019-07-31 22:56:46 +05:30
status : 'unknown' ,
environment : 'staging'
2022-01-12 12:59:36 +05:30
)
2019-07-31 22:56:46 +05:30
2022-01-12 12:59:36 +05:30
expect ( subject . pretext ) . to eq ( 'Deploy to staging unknown' )
2019-07-31 22:56:46 +05:30
end
2021-01-03 14:25:43 +05:30
it 'returns a message for a running deployment' do
2022-01-12 12:59:36 +05:30
args . merge! (
status : 'running' ,
environment : 'production'
)
2021-01-03 14:25:43 +05:30
2022-01-12 12:59:36 +05:30
expect ( subject . pretext ) . to eq ( 'Starting deploy to production' )
2021-01-03 14:25:43 +05:30
end
2019-07-31 22:56:46 +05:30
end
describe '#attachments' do
def deployment_data ( params )
{
object_kind : " deployment " ,
status : " success " ,
deployable_id : 3 ,
deployable_url : " deployable_url " ,
environment : " sandbox " ,
project : {
name : " greatproject " ,
web_url : " project_web_url " ,
path_with_namespace : " project_path_with_namespace "
} ,
user : {
name : " Jane Person " ,
username : " jane "
} ,
user_url : " user_url " ,
short_sha : " 12345678 " ,
commit_url : " commit_url " ,
commit_title : " commit title text "
} . merge ( params )
end
it 'returns attachments with the data returned by the deployment data builder' do
job_url = Gitlab :: Routing . url_helpers . project_job_url ( project , ci_build )
commit_url = Gitlab :: UrlBuilder . build ( deployment . commit )
user_url = Gitlab :: Routing . url_helpers . user_url ( user )
2022-01-12 12:59:36 +05:30
expect ( subject . attachments ) . to eq ( [ {
2019-07-31 22:56:46 +05:30
text : " [myspace/myproject]( #{ project . web_url } ) with job [ # #{ ci_build . id } ]( #{ job_url } ) by [John Smith (smith)]( #{ user_url } ) \n [ #{ deployment . short_sha } ]( #{ commit_url } ): #{ commit . title } " ,
color : " good "
} ] )
end
it 'returns attachments for a failed deployment' do
data = deployment_data ( status : 'failed' )
message = described_class . new ( data )
expect ( message . attachments ) . to eq ( [ {
text : " [project_path_with_namespace](project_web_url) with job [ # 3](deployable_url) by [Jane Person (jane)](user_url) \n [12345678](commit_url): commit title text " ,
color : " danger "
} ] )
end
it 'returns attachments for a canceled deployment' do
data = deployment_data ( status : 'canceled' )
message = described_class . new ( data )
expect ( message . attachments ) . to eq ( [ {
text : " [project_path_with_namespace](project_web_url) with job [ # 3](deployable_url) by [Jane Person (jane)](user_url) \n [12345678](commit_url): commit title text " ,
color : " warning "
} ] )
end
it 'uses a neutral color for a deployment with any other status' do
data = deployment_data ( status : 'some-new-status-we-make-in-the-future' )
message = described_class . new ( data )
expect ( message . attachments ) . to eq ( [ {
text : " [project_path_with_namespace](project_web_url) with job [ # 3](deployable_url) by [Jane Person (jane)](user_url) \n [12345678](commit_url): commit title text " ,
color : " # 334455 "
} ] )
end
end
end