File tree Expand file tree Collapse file tree 2 files changed +59
-1
lines changed
src/ResourceManager/Profile Expand file tree Collapse file tree 2 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -71,6 +71,38 @@ public void GetAzureContext()
71
71
Assert . Equal ( "test" , context . Subscription . SubscriptionName ) ;
72
72
}
73
73
74
+ [ Fact ]
75
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
76
+ public void GetAzureContextNoLogin ( )
77
+ {
78
+ var cmdlt = new GetAzureRMContextCommand ( ) ;
79
+
80
+ // Setup
81
+ cmdlt . CommandRuntime = commandRuntimeMock ;
82
+ var profile = AzureRmProfileProvider . Instance . Profile ;
83
+ AzureRmProfileProvider . Instance . Profile = new AzureRMProfile ( ) ;
84
+
85
+ try
86
+ {
87
+ // Act
88
+ cmdlt . InvokeBeginProcessing ( ) ;
89
+ cmdlt . ExecuteCmdlet ( ) ;
90
+ cmdlt . InvokeEndProcessing ( ) ;
91
+ }
92
+ finally
93
+ {
94
+ AzureRmProfileProvider . Instance . Profile = profile ;
95
+ }
96
+
97
+ // Verify
98
+ Assert . True ( commandRuntimeMock . OutputPipeline . Count == 1 ) ;
99
+ var context = ( PSAzureContext ) commandRuntimeMock . OutputPipeline [ 0 ] ;
100
+ Assert . Null ( context ) ;
101
+ Assert . True ( commandRuntimeMock . ErrorStream . Count == 1 ) ;
102
+ var error = commandRuntimeMock . ErrorStream [ 0 ] ;
103
+ Assert . Equal ( "Run Login-AzureRmAccount to login." , error . Exception . Message ) ;
104
+ }
105
+
74
106
[ Fact ]
75
107
[ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
76
108
public void SelectAzureContextWithNoSubscriptionAndTenant ( )
Original file line number Diff line number Diff line change 12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
+ using Microsoft . Azure . Commands . Common . Authentication . Models ;
15
16
using Microsoft . Azure . Commands . Profile . Models ;
16
17
using Microsoft . Azure . Commands . ResourceManager . Common ;
17
18
using Microsoft . WindowsAzure . Commands . Common ;
@@ -26,9 +27,34 @@ namespace Microsoft.Azure.Commands.Profile
26
27
[ OutputType ( typeof ( PSAzureContext ) ) ]
27
28
public class GetAzureRMContextCommand : AzureRMCmdlet
28
29
{
30
+ /// <summary>
31
+ /// Gets the current default context.
32
+ /// </summary>
33
+ protected override AzureContext DefaultContext
34
+ {
35
+ get
36
+ {
37
+ if ( DefaultProfile == null || DefaultProfile . Context == null )
38
+ {
39
+ return null ;
40
+ }
41
+
42
+ return DefaultProfile . Context ;
43
+ }
44
+ }
45
+
29
46
public override void ExecuteCmdlet ( )
30
47
{
31
- WriteObject ( ( PSAzureContext ) AzureRmProfileProvider . Instance . Profile . Context ) ;
48
+ var context = ( PSAzureContext ) DefaultContext ;
49
+ if ( context == null )
50
+ {
51
+ WriteError ( new ErrorRecord (
52
+ new PSInvalidOperationException ( "Run Login-AzureRmAccount to login." ) ,
53
+ string . Empty ,
54
+ ErrorCategory . AuthenticationError ,
55
+ null ) ) ;
56
+ }
57
+ WriteObject ( context ) ;
32
58
}
33
59
}
34
60
}
You can’t perform that action at this time.
0 commit comments