1+ using Moq ;
2+ using Moq . Protected ;
3+ using System ;
4+ using System . Collections . Generic ;
5+ using System . Net ;
6+ using System . Net . Http ;
7+ using System . Text ;
8+ using System . Threading ;
9+ using System . Threading . Tasks ;
10+ using Xunit ;
11+
12+ namespace Checkout
13+ {
14+ public class OAuthSdkCredentialsTests
15+ {
16+ private OAuthSdkCredentials CreateSdkCredentials ( HttpResponseMessage mockResponse )
17+ {
18+ var mockHttpMessageHandler = new Mock < HttpMessageHandler > ( MockBehavior . Strict ) ;
19+
20+ mockHttpMessageHandler
21+ . Protected ( )
22+ . Setup < Task < HttpResponseMessage > > (
23+ "SendAsync" ,
24+ ItExpr . IsAny < HttpRequestMessage > ( ) ,
25+ ItExpr . IsAny < CancellationToken > ( ) )
26+ . ReturnsAsync ( mockResponse )
27+ . Verifiable ( ) ;
28+
29+ var httpClient = new HttpClient ( mockHttpMessageHandler . Object )
30+ {
31+ BaseAddress = new Uri ( "https://fake-auth.com" )
32+ } ;
33+
34+ var httpClientFactoryMock = new Mock < IHttpClientFactory > ( ) ;
35+ httpClientFactoryMock
36+ . Setup ( _ => _ . CreateClient ( ) )
37+ . Returns ( httpClient ) ;
38+
39+ return new OAuthSdkCredentials (
40+ httpClientFactoryMock . Object ,
41+ new Uri ( "https://fake-auth.com" ) ,
42+ "test_client_id" ,
43+ "test_client_secret" ,
44+ new HashSet < OAuthScope > ( )
45+ ) ;
46+ }
47+
48+ [ Fact ]
49+ public void ShouldReturnAuthorizationHeaderWhenTokenIsValid ( )
50+ {
51+ using var mockResponse = new HttpResponseMessage ( ) ;
52+ mockResponse . StatusCode = HttpStatusCode . OK ;
53+ mockResponse . Content = new StringContent (
54+ "{\" access_token\" : \" valid_token\" , \" token_type\" : \" Bearer\" , \" expires_in\" : 3600}" ,
55+ Encoding . UTF8 ,
56+ "application/json" ) ;
57+ var sdk = CreateSdkCredentials ( mockResponse ) ;
58+ sdk . InitAccess ( ) ;
59+
60+ var authorization = sdk . GetSdkAuthorization ( SdkAuthorizationType . OAuth ) ;
61+ Assert . NotNull ( authorization ) ;
62+
63+ string expectedHeader = $ "Bearer valid_token";
64+ string actualHeader = authorization . GetAuthorizationHeader ( ) ;
65+
66+ Assert . Equal ( expectedHeader , actualHeader ) ;
67+ }
68+
69+ [ Fact ]
70+ public void ShouldThrowExceptionWhenApiReturnsError ( )
71+ {
72+ using var mockResponse = new HttpResponseMessage ( ) ;
73+ mockResponse . StatusCode = HttpStatusCode . BadRequest ;
74+ mockResponse . Content = new StringContent (
75+ "{\" error\" : \" invalid_client\" }" ,
76+ Encoding . UTF8 ,
77+ "application/json" ) ;
78+ var sdk = CreateSdkCredentials ( mockResponse ) ;
79+
80+ Assert . Throws < CheckoutAuthorizationException > ( ( ) => sdk . InitAccess ( ) ) ;
81+ }
82+
83+ [ Fact ]
84+ public void ShouldThrowExceptionWhenResponseHasInvalidToken ( )
85+ {
86+ var response = new OAuthServiceResponse { AccessToken = null , TokenType = "Bearer" , ExpiresIn = 3600 } ;
87+
88+ Assert . Throws < ArgumentException > ( ( ) => OAuthAccessToken . FromOAuthServiceResponse ( response ) ) ;
89+ }
90+ }
91+ }
0 commit comments