Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,171 +1,170 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "Basic Send-MailMessage tests" -Tags CI {
BeforeAll {
function test-smtpserver
{
$rv = $false

try
{
$tc = New-Object -TypeName System.Net.Sockets.TcpClient -ArgumentList "localhost", 25
$rv = $tc.Connected
$tc.Close()
}
catch
{
$rv = false
}

return $rv
}
function Test-SmtpServer
{
try
{
$tc = New-Object -TypeName System.Net.Sockets.TcpClient -ArgumentList "localhost", 25
$rv = $tc.Connected
$tc.Close()
return $rv
}
catch
{
return $false
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a newline between these lines.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adityapatwardhan , Are we having CodeFactor only for .cs. Isn't this should be caught by CodeFactor ?


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

$state = "init"
$mail = Get-Content $mailBox
$rv = @{}
foreach ($line in $mail)
{
switch ($state)
{
Param(
[parameter(Mandatory=$true)]
[String]
$mailBox
)

$state = "init"
$mail = Get-Content $mailBox
$rv = @{}
foreach ($line in $mail)
"init"
{
switch ($state)
if ($line.Length -gt 0)
{
"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("Subject: "))
{
$rv.Subject = $line.Substring(9);
}
elseif ($line.Length -eq 0)
{
$state = "body"
}
}
"body"
$state = "headers"
}
}
"headers"
{
if ($line.StartsWith("From: "))
{
$rv.From = $line.Substring(6)
}
elseif ($line.StartsWith("To: "))
{
if ($null -eq $rv.To)
{
if ($line.Length -eq 0)
{
$state = "done"
continue
}

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

$rv.Body += $line
$rv.To = @()
}

$rv.To += $line.Substring(4)
}
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 = @()
}

return $rv
$rv.Body += $line
}
}
}

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

Describe "Send-MailMessage" -Tags CI {
BeforeAll {
$user = [Environment]::UserName
$domain = [Environment]::MachineName
$address = "$user@$domain"
$mailBox = "/var/mail/$user"

if (-not $IsLinux)
{
$PesterArgs["Skip"] = $true
$PesterArgs["Name"] += " (skipped: not Linux)"
$ItArgs = @{ Skip = $true }
$testCases = @{ Name = "(skipped: not Linux)" }
return
}

$domain = [Environment]::MachineName
if (-not (test-smtpserver))
if (-not (Test-SmtpServer))
{
$PesterArgs["Pending"] = $true
$PesterArgs["Name"] += " (pending: no mail server detected)"
$ItArgs = @{ Pending = $true }
$testCases = @{ Name = "(pending: no mail server detected)" }
return
}

$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)"
$ItArgs = @{ Pending = $true }
$testCases = @{ Name = "(pending: user not in /etc/passwd)" }
return
}

$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
}
$alreadyHasMail = $false
# Save content of mail box before running tests
$mailBoxContent = Get-Content -Path $mailBox -ErrorAction SilentlyContinue

$ItArgs = @{}
$testCases = @(
@{
Name = "with minimal set"
InputObject = @{
To = $address
From = $address
Subject = "Subject $(Get-Date)"
Body = "Body $(Get-Date)"
SmtpServer = "127.0.0.1"
}
}
)
}

BeforeEach {
# Clear mail box before each test
"" | Set-Content -Path $mailBox -Force -ErrorAction SilentlyContinue
}

AfterEach {
if (-not $alreadyHasMail)
{
Set-Content -Value "" -Path $mailBox -Force -ErrorAction SilentlyContinue
}
AfterAll {
# Restore content of mail box after running tests
$mailBoxContent | Set-Content -Path $mailBox -Force -ErrorAction SilentlyContinue
}

$ItArgs = $PesterArgs.Clone()
$ItArgs['Name'] = "Can send mail message from user to self " + $ItArgs['Name']
It "Can send mail message using named parameters <Name>" -TestCases $testCases @ItArgs {
param($InputObject)

It @ItArgs {
$body = "Greetings from me."
$subject = "Test message"
Send-MailMessage -To $address -From $address -Subject $subject -Body $body -SmtpServer 127.0.0.1
Send-MailMessage @InputObject -ErrorAction SilentlyContinue
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.Subject | Should -BeExactly $subject
$mail.Body.Count | Should -BeExactly 1
$mail.Body[0] | Should -BeExactly $body
$mail = Get-Mail $mailBox
$mail.From | Should -BeExactly $InputObject.From
$mail.To.Count | Should -BeExactly $InputObject.To.Count
$mail.To | Should -BeExactly $InputObject.To
$mail.Subject | Should -BeExactly $InputObject.Subject
$mail.Body.Count | Should -BeExactly $InputObject.Body.Count
$mail.Body | Should -BeExactly $InputObject.Body
}

$ItArgs = $PesterArgs.Clone()
$ItArgs['Name'] = "Can send mail message from user to self using pipeline " + $ItArgs['Name']
It "Can send mail message using pipline named parameters <Name>" -TestCases $testCases @ItArgs {
param($InputObject)

It @ItArgs {
$body = "Greetings from me again."
$subject = "Second test message"
$object = [PSCustomObject]@{To = $address; From = $address; Subject = $subject; Body = $body; SmtpServer = '127.0.0.1'}
$object | Send-MailMessage
[PsCustomObject]$InputObject | Send-MailMessage -ErrorAction SilentlyContinue
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.Subject | Should -BeExactly $subject
$mail.Body.Count | Should -BeExactly 1
$mail.Body[0] | Should -BeExactly $body
$mail = Get-Mail $mailBox
$mail.From | Should -BeExactly $InputObject.From
$mail.To.Count | Should -BeExactly $InputObject.To.Count
$mail.To | Should -BeExactly $InputObject.To
$mail.Subject | Should -BeExactly $InputObject.Subject
$mail.Body.Count | Should -BeExactly $InputObject.Body.Count
$mail.Body | Should -BeExactly $InputObject.Body
}
}