11
22namespace BatchClientIntegrationTests
33{
4+ using System ;
5+ using System . Net . Http ;
6+ using System . Text ;
47 using System . Threading . Tasks ;
8+ using BatchClientIntegrationTests . IntegrationTestUtilities ;
59 using BatchTestCommon ;
610 using IntegrationTestCommon ;
711 using Microsoft . Azure . Batch ;
812 using Microsoft . Azure . Batch . Auth ;
13+ using Microsoft . Azure . Batch . Common ;
914 using Microsoft . Azure . Batch . Integration . Tests . Infrastructure ;
1015 using Xunit ;
11- using Xunit . Abstractions ;
1216
1317 public class AuthenticationTest
1418 {
19+ private string invalidUrl = "https://fakeurl.batchintegrationtesting.com" ;
20+
1521 [ LiveTest ]
1622 [ Fact ]
1723 [ Trait ( TestTraits . Duration . TraitName , TestTraits . Duration . Values . ShortDuration ) ]
@@ -22,5 +28,106 @@ public async Task CanAuthenticateToServiceWithAADToken()
2228 using var client = BatchClient . Open ( new BatchTokenCredentials ( TestCommon . Configuration . BatchAccountUrl , tokenProvider ) ) ;
2329 await client . JobOperations . ListJobs ( ) . ToListAsync ( ) ;
2430 }
31+
32+ [ LiveTest ]
33+ [ Fact ]
34+ [ Trait ( TestTraits . Duration . TraitName , TestTraits . Duration . Values . VeryShortDuration ) ]
35+ public async Task CanAuthenticateToServiceWithSharedKeyCredentials ( )
36+ {
37+ BatchSharedKeyCredentials credentials = TestUtilities . GetCredentialsFromEnvironment ( ) ;
38+
39+ using var client = BatchClient . Open ( credentials ) ;
40+ await client . JobOperations . ListJobs ( ) . ToListAsync ( ) ;
41+ }
42+
43+ [ LiveTest ]
44+ [ Fact ]
45+ [ Trait ( TestTraits . Duration . TraitName , TestTraits . Duration . Values . VeryShortDuration ) ]
46+ public async Task BadAccountUrlThrows ( )
47+ {
48+ BatchSharedKeyCredentials credentials = new BatchSharedKeyCredentials (
49+ $ "{ TestCommon . Configuration . BatchAccountUrl } /badendpoint",
50+ TestCommon . Configuration . BatchAccountName ,
51+ TestCommon . Configuration . BatchAccountKey ) ;
52+
53+ using var client = BatchClient . Open ( credentials ) ;
54+ await TestUtilities . AssertThrowsAsync < BatchException > ( async ( ) => await client . JobOperations . ListJobs ( ) . ToListAsync ( ) ) ;
55+ }
56+
57+ [ LiveTest ]
58+ [ Fact ]
59+ [ Trait ( TestTraits . Duration . TraitName , TestTraits . Duration . Values . LongDuration ) ]
60+ public async Task InvalidAccountUrlThrows ( )
61+ {
62+ BatchSharedKeyCredentials credentials = new BatchSharedKeyCredentials (
63+ invalidUrl ,
64+ TestCommon . Configuration . BatchAccountName ,
65+ TestCommon . Configuration . BatchAccountKey ) ;
66+
67+ using var client = BatchClient . Open ( credentials ) ;
68+ await TestUtilities . AssertThrowsAsync < HttpRequestException > ( async ( ) => await client . JobOperations . ListJobs ( ) . ToListAsync ( ) ) ;
69+ }
70+
71+ [ LiveTest ]
72+ [ Fact ]
73+ [ Trait ( TestTraits . Duration . TraitName , TestTraits . Duration . Values . VeryShortDuration ) ]
74+ public async Task BadAccountNameThrows ( )
75+ {
76+ BatchSharedKeyCredentials credentials = new BatchSharedKeyCredentials (
77+ TestCommon . Configuration . BatchAccountUrl ,
78+ "BadAccountName" ,
79+ TestCommon . Configuration . BatchAccountKey ) ;
80+
81+ using var client = BatchClient . Open ( credentials ) ;
82+ await TestUtilities . AssertThrowsAsync < BatchException > ( async ( ) => await client . JobOperations . ListJobs ( ) . ToListAsync ( ) ) ;
83+ }
84+
85+ [ LiveTest ]
86+ [ Fact ]
87+ [ Trait ( TestTraits . Duration . TraitName , TestTraits . Duration . Values . ShortDuration ) ]
88+ public async Task InvalidAccountKeyThrows ( )
89+ {
90+ BatchSharedKeyCredentials credentials = new BatchSharedKeyCredentials (
91+ TestCommon . Configuration . BatchAccountUrl ,
92+ TestCommon . Configuration . BatchAccountName ,
93+ Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( "InvalidAccountKey" ) ) ) ;
94+
95+ using var client = BatchClient . Open ( credentials ) ;
96+ await TestUtilities . AssertThrowsAsync < BatchException > ( async ( ) => await client . JobOperations . ListJobs ( ) . ToListAsync ( ) ) ;
97+ }
98+
99+ [ LiveTest ]
100+ [ Fact ]
101+ [ Trait ( TestTraits . Duration . TraitName , TestTraits . Duration . Values . LongDuration ) ]
102+ public async Task BadAccountKeyThrows ( )
103+ {
104+ BatchSharedKeyCredentials credentials = new BatchSharedKeyCredentials (
105+ TestCommon . Configuration . BatchAccountUrl ,
106+ TestCommon . Configuration . BatchAccountName ,
107+ "BadAccountKey" ) ;
108+
109+ using var client = BatchClient . Open ( credentials ) ;
110+ await TestUtilities . AssertThrowsAsync < FormatException > ( async ( ) => await client . JobOperations . ListJobs ( ) . ToListAsync ( ) ) ;
111+ }
112+
113+ [ LiveTest ]
114+ [ Fact ]
115+ [ Trait ( TestTraits . Duration . TraitName , TestTraits . Duration . Values . LongDuration ) ]
116+ public async Task BadTokenHostThrows ( )
117+ {
118+ static Task < string > tokenProvider ( ) => IntegrationTestCommon . GetAuthenticationTokenAsync ( "https://batch.core.windows.net/" ) ;
119+
120+ using var client = BatchClient . Open ( new BatchTokenCredentials ( invalidUrl , tokenProvider ) ) ;
121+ await TestUtilities . AssertThrowsAsync < HttpRequestException > ( async ( ) => await client . JobOperations . ListJobs ( ) . ToListAsync ( ) ) ;
122+ }
123+
124+ [ LiveTest ]
125+ [ Fact ]
126+ [ Trait ( TestTraits . Duration . TraitName , TestTraits . Duration . Values . VeryShortDuration ) ]
127+ public async Task BadTokenThrows ( )
128+ {
129+ using var client = BatchClient . Open ( new BatchTokenCredentials ( TestCommon . Configuration . BatchAccountUrl , "BadToken" ) ) ;
130+ await TestUtilities . AssertThrowsAsync < BatchException > ( async ( ) => await client . JobOperations . ListJobs ( ) . ToListAsync ( ) ) ;
131+ }
25132 }
26133}
0 commit comments