Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Send-MailMessage.Tests.ps1373 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4# the send-mailmessage cmdlet is obsolete and will be removed in a future release.5# We will address no issues in this cmdlet, so these tests are marked as skipped.6$script:SkipTest = $true7 8if(-not ("netDumbster.smtp.SimpleSmtpServer" -as [type]) -and ! $script:SkipTest)9{10    Register-PackageSource -Name nuget.org -Location https://api.nuget.org/v3/index.json -ProviderName NuGet -ErrorAction SilentlyContinue11 12    $nugetPackage = "netDumbster"13    Install-Package -Name $nugetPackage -ProviderName NuGet -Scope CurrentUser -Force -Source 'nuget.org' -RequiredVersion 2.0.0.414 15    $dll = "$(Split-Path (Get-Package $nugetPackage).Source)\lib\netstandard2.0\netDumbster.dll"16    Add-Type -Path $dll17}18 19$DefaultInputObject = @{20    From = "user01@example.com"21    To = "user02@example.com"22    Subject = "Subject $(Get-Date)"23    Body = "Body $(Get-Date)"24    SmtpServer = "127.0.0.1"25}26 27Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix {28    BeforeAll {29        if ($script:SkipTest) {30            return31        }32        $server = [netDumbster.smtp.SimpleSmtpServer]::Start(25)33 34        function Read-Mail35        {36            param()37 38            if($server)39            {40                return $server.ReceivedEmail[0]41            }42            return $null43        }44    }45 46    BeforeEach {47        if ($script:SkipTest) {48            return49        }50 51        if($server)52        {53            $server.ClearReceivedEmail()54        }55    }56 57    AfterAll {58        if ($script:SkipTest) {59            return60        }61 62        if($server)63        {64            $server.Stop()65        }66    }67 68    $testCases = @(69        @{70            Name = "with mandatory parameters"71            InputObject = $DefaultInputObject72        }73        @{74            Name = "with ReplyTo"75            InputObject = @{76                From = "user01@example.com"77                To = "user02@example.com"78                ReplyTo = "noreply@example.com"79                Subject = "Subject $(Get-Date)"80                Body = "Body $(Get-Date)"81                SmtpServer = "127.0.0.1"82            }83        }84        @{85            Name = "with multiple To"86            InputObject = @{87                From = "user01@example.com"88                To = "user02@example.com","user03@example.com","user04@example.com"89                Subject = "Subject $(Get-Date)"90                Body = "Body $(Get-Date)"91                SmtpServer = "127.0.0.1"92            }93        }94        @{95            Name = "with multiple Cc"96            InputObject = @{97                From = "user01@example.com"98                To = "user02@example.com"99                Cc = "user03@example.com","user04@example.com"100                Subject = "Subject $(Get-Date)"101                Body = "Body $(Get-Date)"102                SmtpServer = "127.0.0.1"103            }104        }105        @{106            Name = "with multiple Bcc"107            InputObject = @{108                From = "user01@example.com"109                To = "user02@example.com"110                Bcc = "user03@example.com","user04@example.com"111                Subject = "Subject $(Get-Date)"112                Body = "Body $(Get-Date)"113                SmtpServer = "127.0.0.1"114            }115        }116        @{117            Name = "with No Subject"118            InputObject = @{119                From = "user01@example.com"120                To = "user02@example.com"121                Body = "Body $(Get-Date)"122                SmtpServer = "127.0.0.1"123            }124        }125    )126 127    It "Shows obsolete message for cmdlet" -skip:$script:SkipTest {128        $server | Should -Not -Be $null129 130        $powershell = [PowerShell]::Create()131 132        $null = $powershell.AddCommand("Send-MailMessage").AddParameters($testCases[0].InputObject).AddParameter("ErrorAction","SilentlyContinue")133 134        $powershell.Invoke()135 136        $warnings = $powershell.Streams.Warning137 138        $warnings.count | Should -BeGreaterThan 0139        $warnings[0].ToString() | Should -BeLike "The command 'Send-MailMessage' is obsolete. *"140    }141 142    It "Can send mail message using named parameters <Name>" -TestCases $testCases -skip:$script:SkipTest {143        param($InputObject)144 145        $server | Should -Not -Be $null146 147        Send-MailMessage @InputObject -ErrorAction SilentlyContinue148 149        $mail = Read-Mail150 151        $mail.FromAddress | Should -BeExactly $InputObject.From152        $mail.ToAddresses.Count | Should -BeExactly ($InputObject.To.Count + $InputObject.Cc.Count + $InputObject.Bcc.Count)153        $mail.ToAddresses | Should -BeIn ([array]$InputObject.To + $InputObject.Cc + $InputObject.Bcc)154 155        $mail.Headers["From"] | Should -BeExactly $InputObject.From156        $mail.Headers["To"] | Should -Not -BeNullOrEmpty157        $mail.Headers["To"].Split(", ") | Should -BeExactly $InputObject.To158        If($InputObject.Cc)159        {160            $mail.Headers["Cc"] | Should -Not -BeNullOrEmpty161            $mail.Headers["Cc"].Split(", ") | Should -BeExactly $InputObject.Cc162        }163        # BCC addresses can't be tested against mail message header, as this information is by design never included in the mail message itself.164        If($InputObject.ReplyTo)165        {166            $mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo167        }168        $mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject169 170        $mail.MessageParts.Count | Should -BeExactly 1171        $mail.MessageParts[0].BodyData | Should -BeExactly $InputObject.Body172    }173 174    It "Can send mail message using pipline named parameters <Name>" -TestCases $testCases -skip:$script:SkipTest {175        param($InputObject)176 177        Set-TestInconclusive "As of right now the Send-MailMessage cmdlet does not support piping named parameters (see issue 7591)"178 179        $server | Should -Not -Be $null180 181        [PsCustomObject]$InputObject | Send-MailMessage -ErrorAction SilentlyContinue182 183        $mail = Read-Mail184 185        $mail.FromAddress | Should -BeExactly $InputObject.From186        $mail.ToAddresses.Count | Should -BeExactly ($InputObject.To.Count + $InputObject.Cc.Count + $InputObject.Bcc.Count)187        $mail.ToAddresses | Should -BeIn ([array]$InputObject.To + $InputObject.Cc + $InputObject.Bcc)188 189        $mail.Headers["From"] | Should -BeExactly $InputObject.From190        $mail.Headers["To"] | Should -Not -BeNullOrEmpty191        $mail.Headers["To"].Split(", ") | Should -BeExactly $InputObject.To192        If($InputObject.Cc)193        {194            $mail.Headers["Cc"] | Should -Not -BeNullOrEmpty195            $mail.Headers["Cc"].Split(", ") | Should -BeExactly $InputObject.Cc196        }197        # BCC addresses can't be tested against mail message header, as this information is by design never included in the mail message itself.198        If($InputObject.ReplyTo)199        {200            $mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo201        }202        $mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject203 204        $mail.MessageParts.Count | Should -BeExactly 1205        $mail.MessageParts[0].BodyData | Should -BeExactly $InputObject.Body206    }207}208 209Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix {210    BeforeAll {211        if ($script:SkipTest) {212            return213        }214 215        function Read-Mail216        {217            param()218 219            if($server)220            {221                return $server.ReceivedEmail[0]222            }223            return $null224        }225    }226 227    BeforeEach {228        if ($script:SkipTest) {229            return230        }231 232        if($server)233        {234            $server.ClearReceivedEmail()235        }236    }237 238    Context "Default Port 25" {239        BeforeAll {240            if ($script:SkipTest) {241                return242            }243 244            $server = [netDumbster.smtp.SimpleSmtpServer]::Start(25)245        }246 247        AfterAll {248            if ($script:SkipTest) {249                return250            }251 252            if($server)253            {254                $server.Stop()255            }256        }257 258        It "Can throw on wrong mail addresses" -skip:$script:SkipTest {259            $server | Should -Not -Be $null260 261            $obj = $DefaultInputObject.Clone()262            $obj.To = "not_a_valid_mail.address"263 264            { Send-MailMessage @obj -ErrorAction Stop } | Should -Throw -ErrorId "FormatException,Microsoft.PowerShell.Commands.SendMailMessage"265        }266 267        It "Can send mail with free-form email address" -skip:$script:SkipTest {268            $server | Should -Not -Be $null269 270            $obj = $DefaultInputObject.Clone()271            $obj.From = "User01 <user01@example.com>"272            $obj.To = "User02 <user02@example.com>"273 274            Send-MailMessage @obj -ErrorAction SilentlyContinue275 276            $mail = Read-Mail277 278            $mail.FromAddress | Should -BeExactly "user01@example.com"279            $mail.ToAddresses | Should -BeExactly "user02@example.com"280        }281 282        It "Can send mail with high priority" -skip:$script:SkipTest {283            $server | Should -Not -Be $null284 285            Send-MailMessage @DefaultInputObject -Priority High -ErrorAction SilentlyContinue286 287            $mail = Read-Mail288            $mail.Priority | Should -BeExactly "urgent"289        }290 291        It "Can send mail with HTML content as body" -skip:$script:SkipTest {292            $server | Should -Not -Be $null293 294            $obj = $DefaultInputObject.Clone()295            $obj.Body = "<html><body><h1>PowerShell</h1></body></html>"296 297            Send-MailMessage @obj -BodyAsHtml -Encoding utf8 -ErrorAction SilentlyContinue298 299            $mail = Read-Mail300            $mail.Headers["content-type"] | Should -BeLike "text/html*"301            $mail.MessageParts.Count | Should -BeExactly 1302            $mail.MessageParts[0].BodyData | Should -Be $obj.Body303        }304 305        It "Can send mail with UTF8 encoding" -skip:$script:SkipTest {306            $server | Should -Not -Be $null307 308            $obj = $DefaultInputObject.Clone()309            $obj.Body = "We ❤ PowerShell"310 311            Send-MailMessage @obj -Encoding utf8Bom -ErrorAction SilentlyContinue312 313            $mail = Read-Mail314            $mail.MessageParts.Count | Should -BeExactly 1315            $mail.Headers["content-transfer-encoding"] | Should -BeExactly "base64"316            $utf8Text = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($mail.MessageParts[0].BodyData))317            $utf8Text | Should -Be $obj.Body318        }319 320        It "Can send mail with attachments" -skip:$script:SkipTest {321            $attachment1 = "TestDrive:\attachment1.txt"322            $attachment2 = "TestDrive:\attachment2.txt"323 324            $pngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAnElEQVR42u3RAQ0AAAgDoL9/aK3hHFSgyUw4o0KEIEQIQoQgRAhChAgRghAhCBGCECEIEYIQhAhBiBCECEGIEIQgRAhChCBECEKEIAQhQhAiBCFCECIEIQgRghAhCBGCECEIQYgQhAhBiBCECEEIQoQgRAhChCBECEIQIgQhQhAiBCFCEIIQIQgRghAhCBGCECFChCBECEKEIOS7BU5Hx50BmcQaAAAAAElFTkSuQmCC"325 326            Set-Content $attachment1 -Value "First attachment"327            Set-Content $attachment2 -AsByteStream -Value ([Convert]::FromBase64String($pngBase64))328 329            $server | Should -Not -Be $null330 331            Send-MailMessage @DefaultInputObject -Attachments $attachment1,$attachment2 -ErrorAction SilentlyContinue332 333            $mail = Read-Mail334            $mail.MessageParts.Count | Should -BeExactly 3335 336            $txt = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($mail.MessageParts[1].BodyData)) -replace "`n|`r"337            $txt | Should -BeExactly "First attachment"338 339            ($mail.MessageParts[2].BodyData -replace "`n|`r") | Should -BeExactly $pngBase64340        }341    }342 343    Context "Custom Port 2525" {344        BeforeAll {345            if ($script:SkipTest) {346                return347            }348 349            $server = [netDumbster.smtp.SimpleSmtpServer]::Start(2525)350        }351 352        AfterAll {353            if ($script:SkipTest) {354                return355            }356 357            if($server)358            {359                $server.Stop()360            }361        }362 363        It "Can send mail message using custom port 2525" -skip:$script:SkipTest {364            $server | Should -Not -Be $null365            $server.ReceivedEmailCount | Should -BeExactly 0366 367            Send-MailMessage @DefaultInputObject -Port 2525 -ErrorAction SilentlyContinue368 369            $server.ReceivedEmailCount | Should -BeExactly 1370        }371    }372}373