Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/Authentication/Authentication/Cmdlets/DisconnectMgGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}