Skip to content

Commit a91325c

Browse files
authored
Fixes Missing -Debug Request Body in PS 5.1 (#1179)
* Fall back to OnBeforeCall in PS 5.1. * Add Pester tests. * Use PSHostVersion to detect PS edition.
1 parent d7bdcab commit a91325c

16 files changed

+531
-56
lines changed

src/Search/Search/test/env.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"ClientId": "56db239f-b87c-4dc0-9d9b-2ec0318e4ad9",
3+
"TenantId": "61fcf17f-000c-4ab8-a4dc-9e92405fc737"
4+
}

src/Search/Search/test/loadEnv.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ if (Test-Path -Path (Join-Path $PSScriptRoot $envFile)) {
2323
}
2424
$env = @{}
2525
if (Test-Path -Path $envFilePath) {
26-
$env = Get-Content (Join-Path $PSScriptRoot $envFile) | ConvertFrom-Json
27-
$PSDefaultParameterValues=@{"*:SubscriptionId"=$env.SubscriptionId; "*:Tenant"=$env.Tenant}
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+
AuthProviderType = [Microsoft.Graph.PowerShell.Authentication.AuthProviderType]::UserProvidedToken
33+
}
2834
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"New-MgSearchAcronym+[NoContext]+[NoScenario]+$POST+https://graph.microsoft.com/beta/search/acronyms+1": {
3+
"Request": {
4+
"Method": "POST",
5+
"RequestUri": "https://graph.microsoft.com/beta/search/acronyms",
6+
"Content": "{\r\n \"description\": \"A deep neural network is a neural network with a certain level of complexity, a neural network with more than two layers.\",\r\n \"displayName\": \"DNN\",\r\n \"webUrl\": \"http://microsoft.com/deep-neural-network\",\r\n \"standsFor\": \"Deep Neural Network\",\r\n \"state\": \"draft\"\r\n}",
7+
"Headers": {},
8+
"ContentHeaders": {
9+
"Content-Type": ["application/json"],
10+
"Content-Length": ["288"]
11+
}
12+
},
13+
"Response": {
14+
"StatusCode": 201,
15+
"Headers": {
16+
"Transfer-Encoding": ["chunked"],
17+
"Vary": ["Accept-Encoding"],
18+
"Strict-Transport-Security": ["max-age=31536000"],
19+
"request-id": ["1c3f51aa-e666-4405-b643-53750fd46110"],
20+
"client-request-id": ["1c3f51aa-e666-4405-b643-53750fd46110"],
21+
"x-ms-ags-diagnostic": [
22+
"{\"ServerInfo\":{\"DataCenter\":\"West Central US\",\"Slice\":\"E\",\"Ring\":\"1\",\"ScaleUnit\":\"001\",\"RoleInstance\":\"CY4PEPF00005443\"}}"
23+
],
24+
"WWW-Authenticate": [
25+
"Bearer realm=\"\", authorization_uri=\"https://login.microsoftonline.com/common/oauth2/authorize\", client_id=\"00000003-0000-0000-c000-000000000000\""
26+
],
27+
"Date": ["Tue, 29 Mar 2022 23:07:09 GMT"]
28+
},
29+
"ContentHeaders": {
30+
"Content-Type": ["application/json"],
31+
"Content-Encoding": ["gzip"]
32+
},
33+
"Content": "{\r\"id\":\"733b26d5-af76-4eea-ac69-1a0ce8716897\"\r}"
34+
}
35+
},
36+
"New-MgSearchAcronym+[NoContext]+[NoScenario]+$POST+https://graph.microsoft.com/beta/search/acronyms+2": {
37+
"Request": {
38+
"Method": "POST",
39+
"RequestUri": "https://graph.microsoft.com/beta/search/acronyms",
40+
"Content": "{\r\n \"description\": \"A deep neural network is a neural network with a certain level of complexity, a neural network with more than two layers.\",\r\n \"displayName\": \"DNN\",\r\n \"webUrl\": \"http://microsoft.com/deep-neural-network\",\r\n \"standsFor\": \"Deep Neural Network\",\r\n \"state\": \"draft\"\r\n}",
41+
"Headers": {},
42+
"ContentHeaders": {
43+
"Content-Type": ["application/json"],
44+
"Content-Length": ["288"]
45+
}
46+
},
47+
"Response": {
48+
"StatusCode": 201,
49+
"Headers": {
50+
"Transfer-Encoding": ["chunked"],
51+
"Vary": ["Accept-Encoding"],
52+
"Strict-Transport-Security": ["max-age=31536000"],
53+
"request-id": ["80cd953c-b369-49f8-a3dc-d81a91a19537"],
54+
"client-request-id": ["80cd953c-b369-49f8-a3dc-d81a91a19537"],
55+
"x-ms-ags-diagnostic": [
56+
"{\"ServerInfo\":{\"DataCenter\":\"West Central US\",\"Slice\":\"E\",\"Ring\":\"1\",\"ScaleUnit\":\"001\",\"RoleInstance\":\"CY4PEPF00005445\"}}"
57+
],
58+
"WWW-Authenticate": [
59+
"Bearer realm=\"\", authorization_uri=\"https://login.microsoftonline.com/common/oauth2/authorize\", client_id=\"00000003-0000-0000-c000-000000000000\""
60+
],
61+
"Date": ["Tue, 29 Mar 2022 23:07:09 GMT"]
62+
},
63+
"ContentHeaders": {
64+
"Content-Type": ["application/json"],
65+
"Content-Encoding": ["gzip"]
66+
},
67+
"Content": "{\r\"id\":\"733b26d5-af76-4eea-ac69-1a0ce8716897\"\r}"
68+
}
69+
}
70+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Describe 'New-MgSearchAcronym' {
2+
BeforeAll {
3+
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
4+
if (-Not (Test-Path -Path $loadEnvPath)) {
5+
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
6+
}
7+
. ($loadEnvPath)
8+
# Set test mode to playback.
9+
$TestMode = 'playback'
10+
$TestRecordingFile = Join-Path $PSScriptRoot 'New-MgSearchAcronym.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+
Select-MgProfile 'beta'
19+
$debugLogPath = Join-Path $PSScriptRoot 'New-MgSearchAcronym.Debug.log'
20+
$Mock.PushDescription('New-MgSearchAcronym')
21+
$params = @{
22+
DisplayName = "DNN"
23+
StandsFor = "Deep Neural Network"
24+
Description = "A deep neural network is a neural network with a certain level of complexity, a neural network with more than two layers."
25+
WebUrl = "http://microsoft.com/deep-neural-network"
26+
State = "draft"
27+
}
28+
$recordedAcronymId = '733b26d5-af76-4eea-ac69-1a0ce8716897'
29+
}
30+
31+
Context 'CreateExpanded' {
32+
It 'ShouldCreateSearchAcronymViaExpandedParameters' {
33+
$expandedAcronymResponse = New-MgSearchAcronym -DisplayName $params.DisplayName -StandsFor $params.StandsFor -Description $params.Description -WebUrl $params.WebUrl -State $params.State -Confirm:$false
34+
$expandedAcronymResponse | Should -Not -BeNullOrEmpty
35+
$expandedAcronymResponse.Id | Should -Be $recordedAcronymId
36+
}
37+
}
38+
39+
Context 'Create'{
40+
It 'ShouldCreateSearchAcronymViaBodyParameterWithDebugLog' {
41+
$DebugPreference = 'Continue'
42+
$expandedAcronymResponse = New-MgSearchAcronym -BodyParameter $params -Confirm:$false -Debug 5>$debugLogPath
43+
$expandedAcronymResponse | Should -Not -BeNullOrEmpty
44+
$expandedAcronymResponse.Id | Should -Be $recordedAcronymId
45+
46+
$debugContent = Get-Content $debugLogPath
47+
$debugContent | Should -Not -BeNullOrEmpty
48+
$debugContent | Should -Contain 'Body:'
49+
for ($i = 0; $i -lt $debugContent.Count; $i++) {
50+
if ($debugContent[$i] -eq 'Body:') {
51+
$debugContent[$i+1] | Should -Be '{'
52+
}
53+
}
54+
}
55+
}
56+
57+
AfterAll {
58+
if (Test-Path $debugLogPath) {
59+
Remove-Item $debugLogPath -Force
60+
}
61+
}
62+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"Invoke-MgQuerySearch+[NoContext]+[NoScenario]+$POST+https://graph.microsoft.com/v1.0/search/microsoft.graph.query+1": {
3+
"Request": {
4+
"Method": "POST",
5+
"RequestUri": "https://graph.microsoft.com/v1.0/search/microsoft.graph.query",
6+
"Content": "{\r\n \"requests\": [\r\n {\r\n \"contentSources\": [ \"/external/connections/connectionfriendlyname\" ],\r\n \"entityTypes\": [ \"externalItem\" ],\r\n \"fields\": [ \"title\", \"description\" ],\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"query\": {\r\n \"queryString\": \"contoso product\"\r\n }\r\n }\r\n ]\r\n}",
7+
"Headers": {},
8+
"ContentHeaders": {
9+
"Content-Type": ["application/json"],
10+
"Content-Length": ["310"]
11+
}
12+
},
13+
"Response": {
14+
"StatusCode": 200,
15+
"Headers": {
16+
"Transfer-Encoding": ["chunked"],
17+
"Vary": ["Accept-Encoding"],
18+
"Strict-Transport-Security": ["max-age=31536000"],
19+
"request-id": ["08340636-e3fd-401f-b79f-8fd69bf15378"],
20+
"client-request-id": ["08340636-e3fd-401f-b79f-8fd69bf15378"],
21+
"x-ms-ags-diagnostic": [
22+
"{\"ServerInfo\":{\"DataCenter\":\"West US 2\",\"Slice\":\"E\",\"Ring\":\"1\",\"ScaleUnit\":\"000\",\"RoleInstance\":\"CO1PEPF00000107\"}}"
23+
],
24+
"WWW-Authenticate": [
25+
"Bearer realm=\"\", authorization_uri=\"https://login.microsoftonline.com/common/oauth2/authorize\", client_id=\"00000003-0000-0000-c000-000000000000\""
26+
],
27+
"Date": ["Tue, 29 Mar 2022 22:50:20 GMT"]
28+
},
29+
"ContentHeaders": {
30+
"Content-Type": ["application/json"],
31+
"Content-Encoding": ["gzip"]
32+
},
33+
"Content": "{\r\"value\":[\r{\r\"searchTerms\":[\r\"searchTerms-value\"\r],\r\"hitsContainers\":[\r{\r\"hits\":[\r{\r\"hitId\":\"1\",\r\"rank\":1,\r\"summary\":\"_summary-value\",\r\"resource\":\"Thesourcefieldwillcontaintheunderlyinggraphentitypartoftheresponse\"\r}\r],\r\"total\":47,\r\"moreResultsAvailable\":true\r}\r]\r}\r]\r}"
34+
}
35+
},
36+
"Invoke-MgQuerySearch+[NoContext]+[NoScenario]+$POST+https://graph.microsoft.com/v1.0/search/microsoft.graph.query+2": {
37+
"Request": {
38+
"Method": "POST",
39+
"RequestUri": "https://graph.microsoft.com/v1.0/search/microsoft.graph.query",
40+
"Content": "{\r\n \"requests\": [\r\n {\r\n \"contentSources\": [ \"/external/connections/connectionfriendlyname\" ],\r\n \"entityTypes\": [ \"externalItem\" ],\r\n \"fields\": [ \"title\", \"description\" ],\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"query\": {\r\n \"queryString\": \"contoso product\"\r\n }\r\n }\r\n ]\r\n}",
41+
"Headers": {},
42+
"ContentHeaders": {
43+
"Content-Type": ["application/json"],
44+
"Content-Length": ["310"]
45+
}
46+
},
47+
"Response": {
48+
"StatusCode": 200,
49+
"Headers": {
50+
"Transfer-Encoding": ["chunked"],
51+
"Vary": ["Accept-Encoding"],
52+
"Strict-Transport-Security": ["max-age=31536000"],
53+
"request-id": ["4db8f8df-c8d5-4117-bc7f-10bc3cb66c8b"],
54+
"client-request-id": ["4db8f8df-c8d5-4117-bc7f-10bc3cb66c8b"],
55+
"x-ms-ags-diagnostic": [
56+
"{\"ServerInfo\":{\"DataCenter\":\"West US 2\",\"Slice\":\"E\",\"Ring\":\"1\",\"ScaleUnit\":\"000\",\"RoleInstance\":\"CO1PEPF00001821\"}}"
57+
],
58+
"WWW-Authenticate": [
59+
"Bearer realm=\"\", authorization_uri=\"https://login.microsoftonline.com/common/oauth2/authorize\", client_id=\"00000003-0000-0000-c000-000000000000\""
60+
],
61+
"Date": ["Tue, 29 Mar 2022 22:50:20 GMT"]
62+
},
63+
"ContentHeaders": {
64+
"Content-Type": ["application/json"],
65+
"Content-Encoding": ["gzip"]
66+
},
67+
"Content": "{\r\"value\":[\r{\r\"searchTerms\":[\r\"searchTerms-value\"\r],\r\"hitsContainers\":[\r{\r\"hits\":[\r{\r\"hitId\":\"1\",\r\"rank\":1,\r\"summary\":\"_summary-value\",\r\"resource\":\"Thesourcefieldwillcontaintheunderlyinggraphentitypartoftheresponse\"\r}\r],\r\"total\":47,\r\"moreResultsAvailable\":true\r}\r]\r}\r]\r}"
68+
}
69+
}
70+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Fails due to response object mismatch between OpenAPI & OData.
2+
Describe 'Invoke-MgQuerySearch' -skip{
3+
BeforeAll {
4+
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
5+
if (-Not (Test-Path -Path $loadEnvPath)) {
6+
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
7+
}
8+
. ($loadEnvPath)
9+
# Set test mode to playback.
10+
$TestMode = 'playback'
11+
$TestRecordingFile = Join-Path $PSScriptRoot 'Invoke-MgQuerySearch.Recording.json'
12+
$currentPath = $PSScriptRoot
13+
while(-not $mockingPath) {
14+
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
15+
$currentPath = Split-Path -Path $currentPath -Parent
16+
}
17+
. ($mockingPath | Select-Object -First 1).FullName
18+
19+
Select-MgProfile 'v1.0'
20+
$debugLogPath = Join-Path $PSScriptRoot 'Invoke-MgQuerySearch.Debug.log'
21+
$Mock.PushDescription('Invoke-MgQuerySearch')
22+
$params = @{
23+
Requests = @(
24+
@{
25+
EntityTypes = @(
26+
"externalItem"
27+
)
28+
ContentSources = @(
29+
"/external/connections/connectionfriendlyname"
30+
)
31+
Query = @{
32+
QueryString = "contoso product"
33+
}
34+
From = 0
35+
Size = 25
36+
Fields = @(
37+
"title"
38+
"description"
39+
)
40+
}
41+
)
42+
}
43+
$debugLogPath = Join-Path $PSScriptRoot 'Invoke-MgQuerySearch.Debug.log'
44+
}
45+
46+
Context 'QueryExpanded1' {
47+
It "ShouldQueryDataViaExpandedParameters" {
48+
$expandedSearchResponse = Invoke-MgQuerySearch -Requests $params.Requests
49+
$expandedSearchResponse | Should -Not -BeNullOrEmpty
50+
$expandedSearchResponse.Count | Should -Be 1
51+
}
52+
}
53+
54+
Context 'Query1' {
55+
It "ShouldQueryDataViaBodyParameterWithDebugLog" {
56+
$DebugPreference = 'Continue'
57+
$expandedSearchResponse = Invoke-MgQuerySearch -BodyParameter $params -Debug 5>$debugLogPath
58+
$expandedSearchResponse | Should -Not -BeNullOrEmpty
59+
$expandedSearchResponse.Count | Should -Be 1
60+
61+
$debugContent = Get-Content $debugLogPath
62+
$debugContent | Should -Not -BeNullOrEmpty
63+
$debugContent | Should -Contain 'Body:'
64+
for ($i = 0; $i -lt $debugContent.Count; $i++) {
65+
if ($debugContent[$i] -eq 'Body:') {
66+
$debugContent[$i+1] | Should -Be '{'
67+
}
68+
}
69+
}
70+
}
71+
72+
AfterAll {
73+
if (Test-Path $debugLogPath) {
74+
Remove-Item $debugLogPath -Force
75+
}
76+
}
77+
}

src/Security/Security/test/env.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"ClientId": "56db239f-b87c-4dc0-9d9b-2ec0318e4ad9",
3+
"TenantId": "61fcf17f-000c-4ab8-a4dc-9e92405fc737"
4+
}

src/Security/Security/test/loadEnv.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ if (Test-Path -Path (Join-Path $PSScriptRoot $envFile)) {
2323
}
2424
$env = @{}
2525
if (Test-Path -Path $envFilePath) {
26-
$env = Get-Content (Join-Path $PSScriptRoot $envFile) | ConvertFrom-Json
27-
$PSDefaultParameterValues=@{"*:SubscriptionId"=$env.SubscriptionId; "*:Tenant"=$env.Tenant}
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+
AuthProviderType = [Microsoft.Graph.PowerShell.Authentication.AuthProviderType]::UserProvidedToken
33+
}
2834
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"Get-MgSecurityAlert+[NoContext]+ShouldGetACollectionOfSecurityAlerts+$GET+https://graph.microsoft.com/v1.0/security/alerts+1": {
3+
"Request": {
4+
"Method": "GET",
5+
"RequestUri": "https://graph.microsoft.com/v1.0/security/alerts",
6+
"Content": null,
7+
"Headers": {
8+
"SdkVersion": ["graph-powershell/1.9.4", "Graph-dotnet-1.25.1"],
9+
"FeatureFlag": ["00000047"],
10+
"Cache-Control": ["no-store, no-cache"],
11+
"User-Agent": [
12+
"Mozilla/5.0",
13+
"(Windows NT 10.0; Microsoft Windows 10.0.22000; en-US)",
14+
"PowerShell/Get-MgSecurityAlert_List"
15+
],
16+
"Authorization": ["[Filtered]"],
17+
"Accept-Encoding": ["gzip"]
18+
},
19+
"ContentHeaders": {}
20+
},
21+
"Response": {
22+
"StatusCode": 200,
23+
"Headers": {
24+
"Transfer-Encoding": ["chunked"],
25+
"Vary": ["Accept-Encoding"],
26+
"Strict-Transport-Security": ["max-age=31536000"],
27+
"request-id": ["2f51d1cc-1e67-4adf-88bb-779699673f21"],
28+
"client-request-id": ["2f51d1cc-1e67-4adf-88bb-779699673f21"],
29+
"Date": ["Tue, 29 Mar 2022 20:49:50 GMT"]
30+
},
31+
"ContentHeaders": {
32+
"Content-Type": ["application/json"],
33+
"Content-Encoding": ["gzip"]
34+
},
35+
"Content": "{\n\"value\":[\n{\n\"activityGroupName\":\"activityGroupName-value\",\n\"assignedTo\":\"assignedTo-value\",\n\"azureSubscriptionId\":\"azureSubscriptionId-value\",\n\"azureTenantId\":\"azureTenantId-value\",\n\"category\":\"category-value\",\n\"closedDateTime\":\"datetime-value\"\n}\n]\n}"
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)