Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable cross-platform Send-MailMessage tests for CI #8859

Merged
merged 2 commits into from
Mar 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,184 +1,109 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "Basic Send-MailMessage tests" -Tags CI {

Describe "Send-MailMessage" -Tags CI, RequireSudoOnUnix {
BeforeAll {
function test-smtpserver
{
$rv = $false
Register-PackageSource -Name nuget.org -Location https://api.nuget.org/v3/index.json -ProviderName NuGet -ErrorAction SilentlyContinue

try
{
$tc = New-Object -TypeName System.Net.Sockets.TcpClient -ArgumentList "localhost", 25
$rv = $tc.Connected
$tc.Close()
}
catch
{
$rv = false
}
$nugetPackage = "netDumbster"
Install-Package -Name $nugetPackage -ProviderName NuGet -Scope CurrentUser -Force

return $rv
}
$dll = "$(Split-Path (Get-Package $nugetPackage).Source)\lib\netstandard2.0\netDumbster.dll"
Add-Type -Path $dll

function read-mail
$server = [netDumbster.smtp.SimpleSmtpServer]::Start(25)

function Read-Mail
{
Param(
[parameter(Mandatory=$true)]
[String]
$mailBox
)

$state = "init"
$mail = Get-Content $mailBox
$rv = @{}
foreach ($line in $mail)
param()

if($server)
{
switch ($state)
{
"init"
{
if ($line.Length -gt 0)
{
$state = "headers"
}
}
"headers"
{
if ($line.StartsWith("From: "))
{
$rv.From = $line.Substring(6)
}
elseif ($line.StartsWith("To: "))
{
if ($null -eq $rv.To)
{
$rv.To = @()
}

$rv.To += $line.Substring(4)
}
elseif ($line.StartsWith("Reply-To: "))
{
if ($null -eq $rv.ReplyTo)
{
$rv.ReplyTo = @()
}

$rv.ReplyTo += $line.Substring(10)
}
elseif ($line.StartsWith("Subject: "))
{
$rv.Subject = $line.Substring(9);
}
elseif ($line.Length -eq 0)
{
$state = "body"
}
}
"body"
{
if ($line.Length -eq 0)
{
$state = "done"
continue
}

if ($null -eq $rv.Body)
{
$rv.Body = @()
}

$rv.Body += $line
}
}
return $server.ReceivedEmail[0]
}

return $rv
return $null
}
}

$PesterArgs = @{Name = ""}
$alreadyHasMail = $true

if (-not $IsLinux)
AfterEach {
if($server)
{
$PesterArgs["Skip"] = $true
$PesterArgs["Name"] += " (skipped: not Linux)"
return
$server.ClearReceivedEmail()
}
}

$domain = [Environment]::MachineName
if (-not (test-smtpserver))
AfterAll {
if($server)
{
$PesterArgs["Pending"] = $true
$PesterArgs["Name"] += " (pending: no mail server detected)"
return
$server.Stop()
}
}

$user = [Environment]::UserName
$inPassword = Select-String "^${user}:" /etc/passwd -ErrorAction SilentlyContinue
if (-not $inPassword)
{
$PesterArgs["Pending"] = $true
$PesterArgs["Name"] += " (pending: user not in /etc/passwd)"
return
$testCases = @(
@{
Name = "with mandatory parameters"
InputObject = @{
From = "user01@example.com"
To = "user02@example.com"
Subject = "Subject $(Get-Date)"
Body = "Body $(Get-Date)"
SmtpServer = "127.0.0.1"
}
}

$address = "$user@$domain"
$mailStore = "/var/mail"
$mailBox = Join-Path $mailStore $user
$mailBoxFile = Get-Item $mailBox -ErrorAction SilentlyContinue
if ($null -ne $mailBoxFile -and $mailBoxFile.Length -gt 2)
{
$PesterArgs["Pending"] = $true
$PesterArgs["Name"] += " (pending: mailbox not empty)"
return
@{
Name = "with ReplyTo"
InputObject = @{
From = "user01@example.com"
To = "user02@example.com"
ReplyTo = "noreply@example.com"
Subject = "Subject $(Get-Date)"
Body = "Body $(Get-Date)"
SmtpServer = "127.0.0.1"
}
}
$alreadyHasMail = $false
}
)

AfterEach {
if (-not $alreadyHasMail)
{
Set-Content -Value "" -Path $mailBox -Force -ErrorAction SilentlyContinue
}
}
It "Can send mail message using named parameters <Name>" -TestCases $testCases {
param($InputObject)

$server | Should -Not -Be $null

Send-MailMessage @InputObject -ErrorAction SilentlyContinue

$mail = Read-Mail

$mail.FromAddress | Should -BeExactly $InputObject.From
$mail.ToAddresses | Should -BeExactly $InputObject.To

$ItArgs = $PesterArgs.Clone()
$ItArgs['Name'] = "Can send mail message from user to self " + $ItArgs['Name']

It @ItArgs {
$body = "Greetings from me."
$subject = "Test message"
Send-MailMessage -To $address -From $address -ReplyTo $address -Subject $subject -Body $body -SmtpServer 127.0.0.1
Test-Path -Path $mailBox | Should -BeTrue
$mail = read-mail $mailBox
$mail.From | Should -BeExactly $address
$mail.To.Count | Should -BeExactly 1
$mail.To[0] | Should -BeExactly $address
$mail.ReplyTo.Count | Should -BeExactly 1
$mail.ReplyTo[0] | Should -BeExactly $address
$mail.Subject | Should -BeExactly $subject
$mail.Body.Count | Should -BeExactly 1
$mail.Body[0] | Should -BeExactly $body
$mail.Headers["From"] | Should -BeExactly $InputObject.From
$mail.Headers["To"] | Should -BeExactly $InputObject.To
$mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo
$mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject

$mail.MessageParts.Count | Should -BeExactly 1
$mail.MessageParts[0].BodyData | Should -BeExactly $InputObject.Body
}

$ItArgs = $PesterArgs.Clone()
$ItArgs['Name'] = "Can send mail message from user to self using pipeline " + $ItArgs['Name']

It @ItArgs {
$body = "Greetings from me again."
$subject = "Second test message"
$object = [PSCustomObject]@{To = $address; From = $address; ReplyTo = $address; Subject = $subject; Body = $body; SmtpServer = '127.0.0.1'}
$object | Send-MailMessage
Test-Path -Path $mailBox | Should -BeTrue
$mail = read-mail $mailBox
$mail.From | Should -BeExactly $address
$mail.To.Count | Should -BeExactly 1
$mail.To[0] | Should -BeExactly $address
$mail.ReplyTo.Count | Should -BeExactly 1
$mail.ReplyTo[0] | Should -BeExactly $address
$mail.Subject | Should -BeExactly $subject
$mail.Body.Count | Should -BeExactly 1
$mail.Body[0] | Should -BeExactly $body
It "Can send mail message using pipline named parameters <Name>" -TestCases $testCases -Pending {
param($InputObject)

Set-TestInconclusive "As of right now the Send-MailMessage cmdlet does not support piping named parameters (see issue 7591)"

$server | Should -Not -Be $null

[PsCustomObject]$InputObject | Send-MailMessage -ErrorAction SilentlyContinue

$mail = Read-Mail

$mail.FromAddress | Should -BeExactly $InputObject.From
$mail.ToAddresses | Should -BeExactly $InputObject.To

$mail.Headers["From"] | Should -BeExactly $InputObject.From
$mail.Headers["To"] | Should -BeExactly $InputObject.To
$mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo
$mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject

$mail.MessageParts.Count | Should -BeExactly 1
$mail.MessageParts[0].BodyData | Should -BeExactly $InputObject.Body
}
}