forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSend-MailMessage.Tests.ps1
More file actions
171 lines (153 loc) · 5.25 KB
/
Copy pathSend-MailMessage.Tests.ps1
File metadata and controls
171 lines (153 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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 read-mail
{
Param(
[parameter(Mandatory=$true)]
[String]
$mailBox
)
$state = "init"
$mail = Get-Content $mailBox
$rv = @{}
foreach ($line in $mail)
{
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("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 $rv
}
$PesterArgs = @{Name = ""}
$alreadyHasMail = $true
if (-not $IsLinux)
{
$PesterArgs["Skip"] = $true
$PesterArgs["Name"] += " (skipped: not Linux)"
return
}
$domain = [Environment]::MachineName
if (-not (test-smtpserver))
{
$PesterArgs["Pending"] = $true
$PesterArgs["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)"
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
}
AfterEach {
if (-not $alreadyHasMail)
{
Set-Content -Value "" -Path $mailBox -Force -ErrorAction SilentlyContinue
}
}
$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 -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.Subject | Should -BeExactly $subject
$mail.Body.Count | Should -BeExactly 1
$mail.Body[0] | Should -BeExactly $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; 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.Subject | Should -BeExactly $subject
$mail.Body.Count | Should -BeExactly 1
$mail.Body[0] | Should -BeExactly $body
}
}