4
4
using System . Net . Http ;
5
5
using System . Threading ;
6
6
using System . Threading . Tasks ;
7
+ using Atlassian . Bitbucket . Cloud ;
7
8
using GitCredentialManager ;
8
9
using GitCredentialManager . Authentication . OAuth ;
9
10
using Moq ;
10
11
using Xunit ;
11
12
12
- namespace Atlassian . Bitbucket . Tests
13
+ namespace Atlassian . Bitbucket . Tests . Cloud
13
14
{
14
15
public class BitbucketOAuth2ClientTest
15
16
{
16
17
private Mock < HttpClient > httpClient = new Mock < HttpClient > ( MockBehavior . Strict ) ;
17
18
private Mock < ISettings > settings = new Mock < ISettings > ( MockBehavior . Loose ) ;
19
+ private Mock < Trace > trace = new Mock < Trace > ( MockBehavior . Loose ) ;
18
20
private Mock < IOAuth2WebBrowser > browser = new Mock < IOAuth2WebBrowser > ( MockBehavior . Strict ) ;
19
21
private Mock < IOAuth2CodeGenerator > codeGenerator = new Mock < IOAuth2CodeGenerator > ( MockBehavior . Strict ) ;
20
22
private IEnumerable < string > scopes = new List < string > ( ) ;
@@ -32,13 +34,13 @@ public async Task BitbucketOAuth2Client_GetAuthorizationCodeAsync_ReturnsCode()
32
34
33
35
Uri finalCallbackUri = MockFinalCallbackUri ( ) ;
34
36
35
- MockGetAuthenticationCodeAsync ( finalCallbackUri , null ) ;
37
+ Bitbucket . Cloud . BitbucketOAuth2Client client = GetBitbucketOAuth2Client ( ) ;
36
38
37
- MockCodeGenerator ( ) ;
39
+ MockGetAuthenticationCodeAsync ( finalCallbackUri , null , client . Scopes ) ;
38
40
39
- BitbucketOAuth2Client client = GetBitbucketOAuth2Client ( ) ;
41
+ MockCodeGenerator ( ) ;
40
42
41
- var result = await client . GetAuthorizationCodeAsync ( scopes , browser . Object , ct ) ;
43
+ var result = await client . GetAuthorizationCodeAsync ( browser . Object , ct ) ;
42
44
43
45
VerifyAuthorizationCodeResult ( result ) ;
44
46
}
@@ -52,36 +54,42 @@ public async Task BitbucketOAuth2Client_GetAuthorizationCodeAsync_RespectsClient
52
54
53
55
Uri finalCallbackUri = MockFinalCallbackUri ( ) ;
54
56
55
- MockGetAuthenticationCodeAsync ( finalCallbackUri , clientId ) ;
57
+ Bitbucket . Cloud . BitbucketOAuth2Client client = GetBitbucketOAuth2Client ( ) ;
58
+
59
+ MockGetAuthenticationCodeAsync ( finalCallbackUri , clientId , client . Scopes ) ;
56
60
57
61
MockCodeGenerator ( ) ;
58
62
59
- BitbucketOAuth2Client client = GetBitbucketOAuth2Client ( ) ;
60
-
61
- var result = await client . GetAuthorizationCodeAsync ( scopes , browser . Object , ct ) ;
63
+ var result = await client . GetAuthorizationCodeAsync ( browser . Object , ct ) ;
62
64
63
65
VerifyAuthorizationCodeResult ( result ) ;
64
66
}
65
67
66
68
[ Fact ]
67
69
public async Task BitbucketOAuth2Client_GetDeviceCodeAsync ( )
68
70
{
69
- var client = new BitbucketOAuth2Client ( httpClient . Object , settings . Object ) ;
71
+ var client = new Bitbucket . Cloud . BitbucketOAuth2Client ( httpClient . Object , settings . Object , trace . Object ) ;
70
72
await Assert . ThrowsAsync < InvalidOperationException > ( async ( ) => await client . GetDeviceCodeAsync ( scopes , ct ) ) ;
71
73
}
72
74
73
- private void VerifyOAuth2TokenResult ( OAuth2TokenResult result )
75
+ [ Theory ]
76
+ [ InlineData ( "https" , "example.com" , "john" , "https://example.com/refresh_token" ) ]
77
+ [ InlineData ( "http" , "example.com" , "john" , "http://example.com/refresh_token" ) ]
78
+ [ InlineData ( "https" , "example.com" , "dave" , "https://example.com/refresh_token" ) ]
79
+ [ InlineData ( "https" , "example.com/" , "john" , "https://example.com/refresh_token" ) ]
80
+ public void BitbucketOAuth2Client_GetRefreshTokenServiceName ( string protocol , string host , string username , string expectedResult )
74
81
{
75
- Assert . NotNull ( result ) ;
76
- IEnumerable < char > access_token = null ;
77
- Assert . Equal ( access_token , result . AccessToken ) ;
78
- IEnumerable < char > refresh_token = null ;
79
- Assert . Equal ( refresh_token , result . RefreshToken ) ;
80
- IEnumerable < char > tokenType = null ;
81
- Assert . Equal ( tokenType , result . TokenType ) ;
82
- Assert . Null ( result . Scopes ) ;
82
+ var client = new Bitbucket . Cloud . BitbucketOAuth2Client ( httpClient . Object , settings . Object , trace . Object ) ;
83
+ var input = new InputArguments ( new Dictionary < string , string >
84
+ {
85
+ [ "protocol" ] = protocol ,
86
+ [ "host" ] = host ,
87
+ [ "username" ] = username
88
+ } ) ;
89
+ Assert . Equal ( expectedResult , client . GetRefreshTokenServiceName ( input ) ) ;
83
90
}
84
91
92
+
85
93
private void VerifyAuthorizationCodeResult ( OAuth2AuthorizationCodeResult result )
86
94
{
87
95
Assert . NotNull ( result ) ;
@@ -90,9 +98,9 @@ private void VerifyAuthorizationCodeResult(OAuth2AuthorizationCodeResult result)
90
98
Assert . Equal ( pkceCodeVerifier , result . CodeVerifier ) ;
91
99
}
92
100
93
- private BitbucketOAuth2Client GetBitbucketOAuth2Client ( )
101
+ private Bitbucket . Cloud . BitbucketOAuth2Client GetBitbucketOAuth2Client ( )
94
102
{
95
- var client = new BitbucketOAuth2Client ( httpClient . Object , settings . Object ) ;
103
+ var client = new Bitbucket . Cloud . BitbucketOAuth2Client ( httpClient . Object , settings . Object , trace . Object ) ;
96
104
client . CodeGenerator = codeGenerator . Object ;
97
105
return client ;
98
106
}
@@ -104,16 +112,17 @@ private void MockCodeGenerator()
104
112
codeGenerator . Setup ( c => c . CreatePkceCodeChallenge ( OAuth2PkceChallengeMethod . Sha256 , pkceCodeVerifier ) ) . Returns ( pkceCodeChallenge ) ;
105
113
}
106
114
107
- private void MockGetAuthenticationCodeAsync ( Uri finalCallbackUri , string overrideClientId )
115
+ private void MockGetAuthenticationCodeAsync ( Uri finalCallbackUri , string overrideClientId , IEnumerable < string > scopes )
108
116
{
109
- var authorizationUri = new UriBuilder ( BitbucketConstants . OAuth2AuthorizationEndpoint )
117
+ var authorizationUri = new UriBuilder ( CloudConstants . OAuth2AuthorizationEndpoint )
110
118
{
111
119
Query = "?response_type=code"
112
- + "&client_id=" + ( overrideClientId ?? BitbucketConstants . OAuth2ClientId )
120
+ + "&client_id=" + ( overrideClientId ?? CloudConstants . OAuth2ClientId )
113
121
+ "&state=12345"
114
122
+ "&code_challenge_method=" + OAuth2Constants . AuthorizationEndpoint . PkceChallengeMethodS256
115
123
+ "&code_challenge=" + WebUtility . UrlEncode ( pkceCodeChallenge ) . ToLower ( )
116
124
+ "&redirect_uri=" + WebUtility . UrlEncode ( rootCallbackUri . AbsoluteUri ) . ToLower ( )
125
+ + "&scope=" + WebUtility . UrlEncode ( string . Join ( " " , scopes ) ) . ToLower ( )
117
126
} . Uri ;
118
127
119
128
browser . Setup ( b => b . GetAuthenticationCodeAsync ( authorizationUri , rootCallbackUri , ct ) ) . Returns ( Task . FromResult ( finalCallbackUri ) ) ;
@@ -133,8 +142,8 @@ private string MockeClientIdOverride(bool set)
133
142
private string MockClientIdOverride ( bool set , string value )
134
143
{
135
144
settings . Setup ( s => s . TryGetSetting (
136
- BitbucketConstants . EnvironmentVariables . DevOAuthClientId ,
137
- Constants . GitConfiguration . Credential . SectionName , BitbucketConstants . GitConfiguration . Credential . DevOAuthClientId ,
145
+ CloudConstants . EnvironmentVariables . OAuthClientId ,
146
+ Constants . GitConfiguration . Credential . SectionName , CloudConstants . GitConfiguration . Credential . OAuthClientId ,
138
147
out value ) ) . Returns ( set ) ;
139
148
return value ;
140
149
}
0 commit comments