diff --git a/src/Authentication/Authentication/Cmdlets/DisconnectMgGraph.cs b/src/Authentication/Authentication/Cmdlets/DisconnectMgGraph.cs index a7c88cfc1cd..f9629aebf68 100644 --- a/src/Authentication/Authentication/Cmdlets/DisconnectMgGraph.cs +++ b/src/Authentication/Authentication/Cmdlets/DisconnectMgGraph.cs @@ -4,11 +4,11 @@ namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets { using Microsoft.Graph.Authentication.Core; - using Microsoft.Graph.PowerShell.Authentication.Helpers; using System; using System.Management.Automation; [Cmdlet(VerbsCommunications.Disconnect, "MgGraph")] [Alias("Disconnect-Graph")] + [OutputType(typeof(IAuthContext))] public class DisconnectMgGraph : PSCmdlet { protected override void BeginProcessing() @@ -25,16 +25,17 @@ protected override void ProcessRecord() { base.ProcessRecord(); - IAuthContext authContext = GraphSession.Instance.AuthContext; + if (GraphSession.Instance.AuthContext == null) + { + WriteError(new ErrorRecord(new ArgumentException("No application to sign out from."), string.Empty, ErrorCategory.CloseError, null)); + } else + { + Authenticator.LogOut(GraphSession.Instance.AuthContext); + WriteObject(GraphSession.Instance.AuthContext); - if (authContext == null) - ThrowTerminatingError( - new ErrorRecord(new Exception("No application to sign out from."), Guid.NewGuid().ToString(), ErrorCategory.InvalidArgument, null)); - - Authenticator.LogOut(authContext); - - GraphSession.Instance.AuthContext = null; - GraphSession.Instance.GraphHttpClient = null; + GraphSession.Instance.AuthContext = null; + GraphSession.Instance.GraphHttpClient = null; + } } protected override void StopProcessing() diff --git a/src/Authentication/Authentication/test/Disconnect-MgGraph.Tests.ps1 b/src/Authentication/Authentication/test/Disconnect-MgGraph.Tests.ps1 new file mode 100644 index 00000000000..702bdfc3fff --- /dev/null +++ b/src/Authentication/Authentication/test/Disconnect-MgGraph.Tests.ps1 @@ -0,0 +1,18 @@ +# ------------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. +# ------------------------------------------------------------------------------ + +BeforeAll { + $ModuleName = "Microsoft.Graph.Authentication" + $ModulePath = Join-Path $PSScriptRoot "..\artifacts\$ModuleName.psd1" + Import-Module $ModulePath -Force +} +Describe 'Disconnect-MgGraph ErrorAction Handling' { + It 'Should throw error by default' { + { Disconnect-MgGraph } | Should -Throw -ExpectedMessage "No application to sign out from." + } + + It 'Should not throw error when ErrorAction is SilentlyContinue' { + { Disconnect-MgGraph -ErrorAction SilentlyContinue } | Should -Not -Throw + } +}