Skip to content

Commit bd11c7b

Browse files
committed
Add Pester test.
1 parent a652233 commit bd11c7b

File tree

4 files changed

+81
-20
lines changed

4 files changed

+81
-20
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"New-MgGroup+[NoContext]+ShouldCreateNewGroup+$POST+https://graph.microsoft.com/v1.0/groups+1": {
3+
"Request": {
4+
"Method": "POST",
5+
"RequestUri": "https://graph.microsoft.com/v1.0/groups",
6+
"Content": "{\r\n \"displayName\": \"new-mggroup-test\",\r\n \"mailEnabled\": false,\r\n \"mailNickname\": \"unused\",\r\n \"securityEnabled\": true\r\n}",
7+
"isContentBase64": false,
8+
"Headers": {
9+
},
10+
"ContentHeaders": {
11+
"Content-Type": [ "application/json" ],
12+
"Content-Length": [ "123" ]
13+
}
14+
},
15+
"Response": {
16+
"StatusCode": 200,
17+
"Headers": {
18+
"Transfer-Encoding": [ "chunked" ],
19+
"Vary": [ "Accept-Encoding" ],
20+
"Strict-Transport-Security": [ "max-age=31536000" ],
21+
"request-id": [ "86476695-f973-44df-8964-2a53227404ab" ],
22+
"client-request-id": [ "aeceaf2b-98af-4fc0-a276-577a2e182727" ],
23+
"x-ms-ags-diagnostic": [ "{\"ServerInfo\":{\"DataCenter\":\"West US 2\",\"Slice\":\"E\",\"Ring\":\"1\",\"ScaleUnit\":\"001\",\"RoleInstance\":\"MW2PEPF0000749D\"}}" ],
24+
"WWW-Authenticate": [ "Bearer realm=\"\", authorization_uri=\"https://login.microsoftonline.com/common/oauth2/authorize\", client_id=\"00000003-0000-0000-c000-000000000000\"" ],
25+
"Date": [ "Mon, 19 Jun 2023 23:03:34 GMT" ]
26+
},
27+
"ContentHeaders": {
28+
"Content-Type": [ "application/json" ],
29+
"Content-Encoding": [ "gzip" ]
30+
},
31+
"Content": "{\r\n \"@odata.context\": \"https://graph.microsoft.com/v1.0/$metadata#groups/$entity\",\r\n \"id\": \"02bd9fd6-8f93-4758-87c3-1fb73740a315\",\r\n \"displayName\": \"new-mggroup-test\",\r\n \"groupTypes\": [\r\n \"Unified\"\r\n ],\r\n \"mailEnabled\": false,\r\n \"mailNickname\": \"unused\",\r\n \"securityEnabled\": true\r\n}",
32+
"isContentBase64": false
33+
}
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
BeforeAll {
2+
if (($null -eq $TestName) -or ($TestName -contains 'New-MgGroup')) {
3+
# Set test mode to playback.
4+
$TestMode = 'playback'
5+
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
6+
if (-Not (Test-Path -Path $loadEnvPath)) {
7+
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
8+
}
9+
. ($loadEnvPath)
10+
$TestRecordingFile = Join-Path $PSScriptRoot 'New-MgGroup.Recording.json'
11+
$currentPath = $PSScriptRoot
12+
while (-not $mockingPath) {
13+
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
14+
$currentPath = Split-Path -Path $currentPath -Parent
15+
}
16+
. ($mockingPath | Select-Object -First 1).FullName
17+
}
18+
}
19+
20+
Describe 'New-MgGroup' {
21+
BeforeAll {
22+
$Mock.PushDescription('New-MgGroup')
23+
}
24+
25+
Context 'Create' {
26+
It 'ShouldCreateNewGroup' {
27+
$CreateGroups = @()
28+
1..100 | ForEach-Object {
29+
$Mock.PushScenario('ShouldCreateNewGroup')
30+
$CreateGroups += New-MgGroup -DisplayName "new-mggroup-test" -MailEnabled:$false -MailNickname 'unused' -SecurityEnabled
31+
}
32+
33+
$CreateGroups | Should -HaveCount 100
34+
$CreateGroups[0].DisplayName | Should -Be "new-mggroup-test"
35+
$CreateGroups[0].MailEnabled | Should -BeFalse
36+
}
37+
}
38+
}

tools/Custom/HttpMessageLogFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal static async Task<HttpRequestMessage> CloneAsync(this HttpRequestMessag
3939
ms.Position = 0;
4040
newRequest.Content = new StreamContent(ms);
4141
// Attempt to copy request content headers with a single retry.
42-
// In .NET Framework, HttpHeaders dictionary is not thread safe. See https://github.com/dotnet/runtime/issues/61798.
42+
// HttpHeaders dictionary is not thread-safe when targeting anything below .NET 7. For more information, see https://github.com/dotnet/runtime/issues/61798.
4343
int retryCount = 0;
4444
int maxRetryCount = 2;
4545
while (retryCount < maxRetryCount)

tools/Tests/loadEnv.ps1

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,12 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313
# ----------------------------------------------------------------------------------
14-
$envFile = 'env.json'
15-
if ($TestMode -eq 'live') {
16-
$envFile = 'localEnv.json'
17-
}
1814

19-
if (Test-Path -Path (Join-Path $PSScriptRoot $envFile)) {
20-
$envFilePath = Join-Path $PSScriptRoot $envFile
21-
} else {
22-
$envFilePath = Join-Path $PSScriptRoot '..\$envFile'
15+
if ($TestMode -eq 'live' -or $TestMode -eq 'record') {
16+
Connect-MgGraph -ClientId $env:testApp_clientId -TenantId $env:testApp_tenantId -CertificateThumbprint $env:testApp_certThumbprint
17+
}
18+
else {
19+
# Use dummy access token to run Pester tests.
20+
# Provide the dummy access token to $env:testApp_dummyAccessToken in your environment variable.
21+
Connect-MgGraph -AccessToken (ConvertTo-SecureString -String $env:testApp_dummyAccessToken -AsPlainText)
2322
}
24-
$env = @{}
25-
if (Test-Path -Path $envFilePath) {
26-
# Load dummy auth configuration. This is used to run Pester tests.
27-
$env = Get-Content (Join-Path $PSScriptRoot $envFile) | ConvertFrom-Json -AsHashTable
28-
[Microsoft.Graph.PowerShell.Authentication.GraphSession]::Instance.AuthContext = New-Object Microsoft.Graph.PowerShell.Authentication.AuthContext -Property @{
29-
ClientId = $env.ClientId
30-
TenantId = $env.TenantId
31-
AuthType = [Microsoft.Graph.PowerShell.Authentication.AuthenticationType]::UserProvidedAccessToken
32-
TokenCredentialType = [Microsoft.Graph.PowerShell.Authentication.TokenCredentialType]::UserProvidedAccessToken
33-
}
34-
}

0 commit comments

Comments
 (0)