260 lines
9.1 KiB
PowerShell
260 lines
9.1 KiB
PowerShell
|
. $PSScriptRoot\..\..\Load-PesterModules.ps1
|
||
|
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||
|
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.'
|
||
|
$functionPath = Join-Path -Path $here -ChildPath $sut
|
||
|
Write-Host "Overriding SUT: $functionPath"
|
||
|
Import-Module $functionPath -Force
|
||
|
$moduleForMock = ""
|
||
|
|
||
|
Describe "Publish-MessageToSlack" {
|
||
|
Context "When Sending Messages To Multiple Channels " {
|
||
|
It "Calls Invoke-RestMethod multiple times" {
|
||
|
$channels = @(
|
||
|
"#testChannel1",
|
||
|
"#testChannel2"
|
||
|
)
|
||
|
|
||
|
$params = @{
|
||
|
"username" = "@user1"
|
||
|
"icon_emoji" = ":smiley:"
|
||
|
"text" = "sampletext"
|
||
|
}
|
||
|
Mock -ModuleName $moduleForMock Invoke-RestMethod { return $true }
|
||
|
|
||
|
Publish-MessageToSlack -channels $channels -slackHookUrl "www.dummyurl.com" -MessageBody $params
|
||
|
|
||
|
Assert-MockCalled Invoke-RestMethod -Times 2 -ModuleName $moduleForMock
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Context "When Message Body Does Not Contain Required Values" {
|
||
|
It "Should Throw" {
|
||
|
$channels = @(
|
||
|
"#testChannel1",
|
||
|
"#testChannel2"
|
||
|
)
|
||
|
|
||
|
# No text value, which is required
|
||
|
$params = @{
|
||
|
"username" = "@user1"
|
||
|
"icon_emoji" = ":smiley:"
|
||
|
}
|
||
|
|
||
|
Mock -ModuleName $moduleForMock Invoke-RestMethod { return $true }
|
||
|
|
||
|
{ Publish-MessageToSlack -channels $channels -slackHookUrl "www.dummyurl.com" -slackParameters $params } | Should -Throw "Missing required parameter key 'text' on 'SlackParameters'."
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Context "When Message Body And Message Text Are Both Empty" {
|
||
|
It "Should Throw" {
|
||
|
$channels = @(
|
||
|
"#testChannel1",
|
||
|
"#testChannel2"
|
||
|
)
|
||
|
|
||
|
Mock -ModuleName $moduleForMock Invoke-RestMethod { return $true }
|
||
|
|
||
|
{ Publish-MessageToSlack -channels $channels -slackHookUrl "www.dummyurl.com" } | Should -Throw "SlackParameters or MessageText must be populated to send a message!"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Context "When Attachment List Is Invalid" {
|
||
|
It "Should Throw" {
|
||
|
$channels = @(
|
||
|
"#testChannel1",
|
||
|
"#testChannel2"
|
||
|
)
|
||
|
|
||
|
# Actions.url must be in a valid http format. Can't just add "sometext".
|
||
|
$actions = @(
|
||
|
@{
|
||
|
"type" = "button";
|
||
|
"text" = "Nginx Server";
|
||
|
"url" = "http://google.com"
|
||
|
}
|
||
|
)
|
||
|
|
||
|
# Attachment is missing fallback, which is required.
|
||
|
$params = @{
|
||
|
"username" = "@user1"
|
||
|
"icon_emoji" = ":smiley:"
|
||
|
"text" = "I am a test with an invalid attachment"
|
||
|
"attachments" = @(
|
||
|
@{
|
||
|
color = "green";
|
||
|
actions = $actions
|
||
|
}
|
||
|
)
|
||
|
}
|
||
|
|
||
|
Mock -ModuleName $moduleForMock Invoke-RestMethod { return $true }
|
||
|
|
||
|
{ Publish-MessageToSlack -channels $channels -slackHookUrl "www.dummyurl.com" -slackParameters $params } | Should -Throw "Missing required parameter key 'fallback' on 'SlackAttachments'."
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Context "When Sending Message is Successful" {
|
||
|
It "Should Not Throw" {
|
||
|
$channels = @(
|
||
|
"#testChannel1",
|
||
|
"#testChannel2"
|
||
|
)
|
||
|
|
||
|
$params = @{
|
||
|
"username" = "@user1"
|
||
|
"icon_emoji" = ":smiley:"
|
||
|
"text" = "sampletext"
|
||
|
}
|
||
|
# Actions.url must be in a valid http format. Can't just add "sometext".
|
||
|
$buildLog = "https://google.com"
|
||
|
$fallback = "View the Build: $buildLog"
|
||
|
$actions = @(
|
||
|
@{
|
||
|
"type" = "button"
|
||
|
"text" = "Build Log"
|
||
|
"url" = $buildLog
|
||
|
}
|
||
|
)
|
||
|
$attachments = @{
|
||
|
fallback = $fallback
|
||
|
color = "#009EFF"
|
||
|
actions = $actions
|
||
|
}
|
||
|
Mock -ModuleName $moduleForMock Invoke-RestMethod { return $true }
|
||
|
Mock Write-Host -ModuleName $moduleForMock `
|
||
|
-ParameterFilter { $Object -like "*Both SlackParameters and MessageText parameters are not null. Choosing SlackParameters as message body." } `
|
||
|
-MockWith { }
|
||
|
|
||
|
{ Publish-MessageToSlack -channels $channels -slackHookUrl "www.dummyurl.com" -MessageBody $params -SlackAttachments $attachments } | Should -Not -Throw
|
||
|
|
||
|
Assert-MockCalled Write-Host -Exactly 0 -ModuleName $moduleForMock -Scope It
|
||
|
}
|
||
|
|
||
|
It "Should Not Throw" {
|
||
|
$channels = @(
|
||
|
"#testChannel1",
|
||
|
"#testChannel2"
|
||
|
)
|
||
|
Mock -ModuleName $moduleForMock Invoke-RestMethod { return $true }
|
||
|
Mock Write-Host -ModuleName $moduleForMock `
|
||
|
-ParameterFilter { $Object -like "*Both SlackParameters and MessageText parameters are not null. Choosing SlackParameters as message body." } `
|
||
|
-MockWith { }
|
||
|
|
||
|
{ Publish-MessageToSlack -channels $channels -slackHookUrl "www.dummyurl.com" -MessageText "sample text" -IconEmoji ":slack:" -Username "someuser" } | Should -Not -Throw
|
||
|
|
||
|
Assert-MockCalled Write-Host -Exactly 0 -ModuleName $moduleForMock -Scope It
|
||
|
}
|
||
|
|
||
|
It "Should Not Throw" {
|
||
|
$channels = @(
|
||
|
"#testChannel1",
|
||
|
"#testChannel2"
|
||
|
)
|
||
|
|
||
|
$params = @{
|
||
|
"username" = "@user1"
|
||
|
"icon_emoji" = ":smiley:"
|
||
|
"text" = "sampletext"
|
||
|
}
|
||
|
Mock -ModuleName $moduleForMock Invoke-RestMethod { return $true }
|
||
|
Mock Write-Host -ModuleName $moduleForMock `
|
||
|
-ParameterFilter { $Object -like "*Both SlackParameters and MessageText parameters are not null. Choosing SlackParameters as message body." } `
|
||
|
-MockWith { }
|
||
|
|
||
|
{ Publish-MessageToSlack -channels $channels -slackHookUrl "www.dummyurl.com" -MessageBody $params -MessageText "sample text" -IconEmoji ":slack:" -Username "someuser" } | Should -Not -Throw
|
||
|
|
||
|
Assert-MockCalled Write-Host -Exactly 1 -ModuleName $moduleForMock -Scope It
|
||
|
}
|
||
|
|
||
|
It "Should Call Invoke-RestMethod" {
|
||
|
$channels = @(
|
||
|
"#testChannel1",
|
||
|
"#testChannel2"
|
||
|
)
|
||
|
|
||
|
$params = @{
|
||
|
"username" = "@user1"
|
||
|
"icon_emoji" = ":smiley:"
|
||
|
"text" = "sampletext"
|
||
|
}
|
||
|
Mock -ModuleName $moduleForMock Invoke-RestMethod { return $true }
|
||
|
|
||
|
Publish-MessageToSlack -channels $channels -slackHookUrl "www.dummyurl.com" -MessageBody $params
|
||
|
|
||
|
Assert-MockCalled Invoke-RestMethod -ModuleName $moduleForMock
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Context "When Setting Both MessageBody And MessageText" {
|
||
|
It "Should Not Throw" {
|
||
|
$channels = @(
|
||
|
"#testChannel1",
|
||
|
"#testChannel2"
|
||
|
)
|
||
|
|
||
|
$params = @{
|
||
|
"username" = "@user1"
|
||
|
"icon_emoji" = ":smiley:"
|
||
|
"text" = "sampletext"
|
||
|
}
|
||
|
|
||
|
$messageText = "sampletext"
|
||
|
|
||
|
Mock -ModuleName $moduleForMock Invoke-RestMethod { return $true }
|
||
|
Mock Write-Host -ModuleName $moduleForMock `
|
||
|
-ParameterFilter { $Object -like "*Both SlackParameters and MessageText parameters are not null. Choosing SlackParameters as message body." } `
|
||
|
-MockWith { }
|
||
|
|
||
|
{ Publish-MessageToSlack -channels $channels -slackHookUrl "www.dummyurl.com" -MessageBody $params -MessageText $messageText } | Should -Not -Throw
|
||
|
}
|
||
|
|
||
|
It "Should Write Override Output" {
|
||
|
$channels = @(
|
||
|
"#testChannel1",
|
||
|
"#testChannel2"
|
||
|
)
|
||
|
|
||
|
$params = @{
|
||
|
"username" = "@user1"
|
||
|
"icon_emoji" = ":smiley:"
|
||
|
"text" = "sampletext"
|
||
|
}
|
||
|
|
||
|
$messageText = "sampletext"
|
||
|
|
||
|
Mock -ModuleName $moduleForMock Invoke-RestMethod { return $true }
|
||
|
Mock Write-Host -ModuleName $moduleForMock `
|
||
|
-ParameterFilter { $Object -like "*Both SlackParameters and MessageText parameters are not null. Choosing SlackParameters as message body." } `
|
||
|
-MockWith { }
|
||
|
|
||
|
Publish-MessageToSlack -channels $channels -slackHookUrl "www.dummyurl.com" -MessageBody $params -MessageText $messageText
|
||
|
|
||
|
Assert-MockCalled Write-Host -Exactly 1 -ModuleName $moduleForMock -Scope It
|
||
|
}
|
||
|
|
||
|
It "Should Call Invoke-RestMethod" {
|
||
|
$channels = @(
|
||
|
"#testChannel1",
|
||
|
"#testChannel2"
|
||
|
)
|
||
|
|
||
|
$params = @{
|
||
|
"username" = "@user1"
|
||
|
"icon_emoji" = ":smiley:"
|
||
|
"text" = "sampletext"
|
||
|
}
|
||
|
|
||
|
$messageText = "sampletext"
|
||
|
|
||
|
Mock -ModuleName $moduleForMock Invoke-RestMethod { return $true }
|
||
|
Mock Write-Host -ModuleName $moduleForMock `
|
||
|
-ParameterFilter { $Object -like "*Both SlackParameters and MessageText parameters are not null. Choosing SlackParameters as message body." } `
|
||
|
-MockWith { }
|
||
|
|
||
|
Publish-MessageToSlack -channels $channels -slackHookUrl "www.dummyurl.com" -MessageBody $params -MessageText $messageText
|
||
|
|
||
|
Assert-MockCalled Invoke-RestMethod -ModuleName $moduleForMock
|
||
|
}
|
||
|
}
|