2016-07-29 16:59:54 +00:00
require 'spec_helper'
2016-11-25 19:36:37 +00:00
describe ChatMessage :: PipelineMessage do
subject { described_class . new ( args ) }
2016-07-29 16:59:54 +00:00
2017-10-06 20:00:50 +00:00
let ( :user ) { { name : " The Hacker " , username : 'hacker' } }
2017-05-03 07:25:13 +00:00
let ( :duration ) { 7210 }
2016-07-29 16:59:54 +00:00
let ( :args ) do
{
2016-08-01 09:09:26 +00:00
object_attributes : {
id : 123 ,
sha : '97de212e80737a608d939f648d959671fb0a0142' ,
tag : false ,
ref : 'develop' ,
status : status ,
duration : duration
} ,
2017-04-04 15:10:21 +00:00
project : {
path_with_namespace : 'project_name' ,
web_url : 'http://example.gitlab.com'
} ,
2016-12-13 11:58:26 +00:00
user : user
2016-07-29 16:59:54 +00:00
}
end
2017-10-06 20:00:50 +00:00
let ( :combined_name ) { " The Hacker (hacker) " }
2016-07-29 16:59:54 +00:00
2017-04-04 15:10:21 +00:00
context 'without markdown' do
context 'pipeline succeeded' do
let ( :status ) { 'success' }
let ( :color ) { 'good' }
2017-10-06 20:00:50 +00:00
let ( :message ) { build_message ( 'passed' , combined_name ) }
2017-04-04 15:10:21 +00:00
it 'returns a message with information about succeeded build' do
expect ( subject . pretext ) . to be_empty
expect ( subject . fallback ) . to eq ( message )
expect ( subject . attachments ) . to eq ( [ text : message , color : color ] )
end
end
2016-08-29 07:33:36 +00:00
2017-04-04 15:10:21 +00:00
context 'pipeline failed' do
let ( :status ) { 'failed' }
let ( :color ) { 'danger' }
2017-10-06 20:00:50 +00:00
let ( :message ) { build_message ( status , combined_name ) }
2016-07-29 16:59:54 +00:00
2017-04-04 15:10:21 +00:00
it 'returns a message with information about failed build' do
expect ( subject . pretext ) . to be_empty
expect ( subject . fallback ) . to eq ( message )
expect ( subject . attachments ) . to eq ( [ text : message , color : color ] )
end
context 'when triggered by API therefore lacking user' do
let ( :user ) { nil }
let ( :message ) { build_message ( status , 'API' ) }
it 'returns a message stating it is by API' do
expect ( subject . pretext ) . to be_empty
expect ( subject . fallback ) . to eq ( message )
expect ( subject . attachments ) . to eq ( [ text : message , color : color ] )
end
end
2016-07-29 16:59:54 +00:00
end
2017-04-04 15:10:21 +00:00
def build_message ( status_text = status , name = user [ :name ] )
" <http://example.gitlab.com|project_name>: " \
" Pipeline <http://example.gitlab.com/pipelines/123| # 123> " \
2017-06-19 16:57:03 +00:00
" of branch <http://example.gitlab.com/commits/develop|develop> " \
2017-05-03 07:25:13 +00:00
" by #{ name } #{ status_text } in 02:00:10 "
2017-04-04 15:10:21 +00:00
end
end
2016-07-29 16:59:54 +00:00
2017-04-04 15:10:21 +00:00
context 'with markdown' do
before do
args [ :markdown ] = true
2016-07-29 16:59:54 +00:00
end
2016-12-13 11:58:26 +00:00
2017-04-04 15:10:21 +00:00
context 'pipeline succeeded' do
let ( :status ) { 'success' }
let ( :color ) { 'good' }
2017-10-06 20:00:50 +00:00
let ( :message ) { build_markdown_message ( 'passed' , combined_name ) }
2016-12-13 11:58:26 +00:00
2017-04-04 15:10:21 +00:00
it 'returns a message with information about succeeded build' do
expect ( subject . pretext ) . to be_empty
expect ( subject . attachments ) . to eq ( message )
expect ( subject . activity ) . to eq ( {
2017-10-06 20:00:50 +00:00
title : 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of branch [develop](http://example.gitlab.com/commits/develop) by The Hacker (hacker) passed' ,
2017-04-04 15:10:21 +00:00
subtitle : 'in [project_name](http://example.gitlab.com)' ,
2017-05-03 07:25:13 +00:00
text : 'in 02:00:10' ,
2017-04-04 15:10:21 +00:00
image : ''
} )
2016-12-13 11:58:26 +00:00
end
end
2017-04-04 15:10:21 +00:00
context 'pipeline failed' do
let ( :status ) { 'failed' }
let ( :color ) { 'danger' }
2017-10-06 20:00:50 +00:00
let ( :message ) { build_markdown_message ( status , combined_name ) }
2017-04-04 15:10:21 +00:00
it 'returns a message with information about failed build' do
expect ( subject . pretext ) . to be_empty
expect ( subject . attachments ) . to eq ( message )
expect ( subject . activity ) . to eq ( {
2017-10-06 20:00:50 +00:00
title : 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of branch [develop](http://example.gitlab.com/commits/develop) by The Hacker (hacker) failed' ,
2017-04-04 15:10:21 +00:00
subtitle : 'in [project_name](http://example.gitlab.com)' ,
2017-05-03 07:25:13 +00:00
text : 'in 02:00:10' ,
2017-04-04 15:10:21 +00:00
image : ''
} )
end
2016-07-29 16:59:54 +00:00
2017-04-04 15:10:21 +00:00
context 'when triggered by API therefore lacking user' do
let ( :user ) { nil }
let ( :message ) { build_markdown_message ( status , 'API' ) }
it 'returns a message stating it is by API' do
expect ( subject . pretext ) . to be_empty
expect ( subject . attachments ) . to eq ( message )
expect ( subject . activity ) . to eq ( {
2017-06-19 16:57:03 +00:00
title : 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of branch [develop](http://example.gitlab.com/commits/develop) by API failed' ,
2017-04-04 15:10:21 +00:00
subtitle : 'in [project_name](http://example.gitlab.com)' ,
2017-05-03 07:25:13 +00:00
text : 'in 02:00:10' ,
2017-04-04 15:10:21 +00:00
image : ''
} )
end
end
end
def build_markdown_message ( status_text = status , name = user [ :name ] )
" [project_name](http://example.gitlab.com): " \
" Pipeline [ # 123](http://example.gitlab.com/pipelines/123) " \
2017-06-19 16:57:03 +00:00
" of branch [develop](http://example.gitlab.com/commits/develop) " \
2017-05-11 15:47:44 +00:00
" by #{ name } #{ status_text } in 02:00:10 "
2017-04-04 15:10:21 +00:00
end
2016-08-29 07:33:36 +00:00
end
2016-07-29 16:59:54 +00:00
end