Skip to content

Commit 64db17e

Browse files
authored
Throw Connect-MgGraph required when auth session in null. (#1740)
1 parent d9a8d7f commit 64db17e

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/Authentication/Authentication/Helpers/HttpHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static HttpClient GetGraphHttpClient()
2525
if (GraphSession.Instance?.GraphHttpClient != null)
2626
return GraphSession.Instance.GraphHttpClient;
2727

28-
var requestUserAgent = new RequestUserAgent(GraphSession.Instance.AuthContext.PSHostVersion, null);
28+
var requestUserAgent = new RequestUserAgent(GraphSession.Instance.AuthContext?.PSHostVersion, null);
2929

3030
IAuthenticationProvider authProvider = AuthenticationHelpers.GetAuthenticationProviderAsync(GraphSession.Instance.AuthContext).ConfigureAwait(false).GetAwaiter().GetResult();
3131
var newHttpClient = GetGraphHttpClient(authProvider, GraphSession.Instance.RequestContext);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# ------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
# ------------------------------------------------------------------------------
4+
Describe "Microsoft.Graph.Users Module" {
5+
Context "On module import" {
6+
BeforeAll {
7+
$ModuleName = "Microsoft.Graph.Users"
8+
$ModulePath = Join-Path $PSScriptRoot "..\$ModuleName.psd1"
9+
$PSModuleInfo = Get-Module $ModuleName
10+
}
11+
12+
It "Should have exported commands" {
13+
$PSModuleInfo | Should -Not -Be $null
14+
$PSModuleInfo.ExportedCommands.Count | Should -Not -Be 0
15+
}
16+
17+
It 'Should be compatible with PS core and desktop' {
18+
$PSModuleInfo.CompatiblePSEditions | Should -BeIn @("Core", "Desktop")
19+
}
20+
21+
It 'Should point to script module' {
22+
$PSModuleInfo.Path | Should -BeLikeExactly "*$ModuleName.psm1"
23+
}
24+
25+
It 'Should have a definition' {
26+
$PSModuleInfo.Definition | Should -Not -BeNullOrEmpty
27+
}
28+
29+
It 'Should lock GUID' {
30+
$PSModuleInfo.Guid.Guid | Should -Be "71150504-37a3-48c6-82c7-7a00a12168db"
31+
}
32+
33+
It "Module import should not write to error and information streams" {
34+
$ps = [powershell]::Create()
35+
$ps.AddScript("Import-Module $ModulePath -ErrorAction SilentlyContinue").Invoke()
36+
37+
$ps.Streams.Information.Count | Should -Be 0
38+
$ps.Streams.Error.Count | Should -Be 0
39+
$ps.Streams.Verbose.Count | Should -Be 0
40+
$ps.Streams.Warning.Count | Should -Be 0
41+
$ps.Streams.Progress.Count | Should -Be 0
42+
43+
$ps.Dispose()
44+
}
45+
46+
It "Get-MgUser should fail when auth session hasn't been initialized" {
47+
$ps = [powershell]::Create()
48+
$ps.AddScript("Import-Module $ModulePath -ErrorAction SilentlyContinue").Invoke()
49+
$ps.AddScript("Get-MgUser").Invoke()
50+
51+
$ps.Streams.Information.Count | Should -Be 0
52+
$ps.Streams.Verbose.Count | Should -Be 0
53+
$ps.Streams.Warning.Count | Should -Be 0
54+
$ps.Streams.Progress.Count | Should -Be 0
55+
56+
$ps.Streams.Error | Should -Be "$([Microsoft.Graph.PowerShell.Authentication.Core.ErrorConstants+Message]::MissingAuthContext)"
57+
58+
$ps.Dispose()
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)