-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Improve Send-MailMessage tests #7793
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
257 changes: 128 additions & 129 deletions
257
test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?