|
30 | 30 | //---------------------------------------------------------------------------
|
31 | 31 |
|
32 | 32 | using System;
|
| 33 | +using System.Diagnostics; |
33 | 34 | using System.Threading;
|
34 |
| -using Moq; |
35 | 35 | using RabbitMQ.Client.OAuth2;
|
36 | 36 | using Xunit;
|
| 37 | +using Xunit.Abstractions; |
37 | 38 |
|
38 | 39 | namespace RabbitMQ.Client.Unit
|
39 | 40 | {
|
| 41 | + public class MockIOAuth2Client : IOAuth2Client |
| 42 | + { |
| 43 | + private readonly ITestOutputHelper _testOutputHelper; |
| 44 | + private IToken _refreshToken; |
| 45 | + private IToken _requestToken; |
| 46 | + |
| 47 | + public MockIOAuth2Client(ITestOutputHelper testOutputHelper) |
| 48 | + { |
| 49 | + _testOutputHelper = testOutputHelper; |
| 50 | + } |
| 51 | + |
| 52 | + public IToken RefreshTokenValue |
| 53 | + { |
| 54 | + get { return _refreshToken; } |
| 55 | + set |
| 56 | + { |
| 57 | + if (value == null) |
| 58 | + { |
| 59 | + throw new ArgumentNullException(nameof(value)); |
| 60 | + } |
| 61 | + |
| 62 | + _refreshToken = value; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public IToken RequestTokenValue |
| 67 | + { |
| 68 | + get { return _requestToken; } |
| 69 | + set |
| 70 | + { |
| 71 | + if (value == null) |
| 72 | + { |
| 73 | + throw new ArgumentNullException(nameof(value)); |
| 74 | + } |
| 75 | + |
| 76 | + _requestToken = value; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public IToken RefreshToken(IToken initialToken) |
| 81 | + { |
| 82 | + Debug.Assert(Object.ReferenceEquals(_requestToken, initialToken)); |
| 83 | + return _refreshToken; |
| 84 | + } |
| 85 | + |
| 86 | + public IToken RequestToken() |
| 87 | + { |
| 88 | + return _requestToken; |
| 89 | + } |
| 90 | + } |
| 91 | + |
40 | 92 | public class TestOAuth2CredentialsProvider
|
41 | 93 | {
|
42 |
| - protected OAuth2ClientCredentialsProvider _provider; |
43 |
| - protected Mock<IOAuth2Client> _oAuth2Client; |
| 94 | + private readonly ITestOutputHelper _testOutputHelper; |
44 | 95 |
|
45 |
| - public TestOAuth2CredentialsProvider() |
| 96 | + public TestOAuth2CredentialsProvider(ITestOutputHelper testOutputHelper) |
46 | 97 | {
|
47 |
| - _oAuth2Client = new Mock<IOAuth2Client>(); |
48 |
| - _provider = new OAuth2ClientCredentialsProvider("aName", _oAuth2Client.Object); |
| 98 | + _testOutputHelper = testOutputHelper; |
49 | 99 | }
|
50 | 100 |
|
51 | 101 | [Fact]
|
52 |
| - public void shouldHaveAName() |
| 102 | + public void ShouldHaveAName() |
53 | 103 | {
|
54 |
| - Assert.Equal("aName", _provider.Name); |
| 104 | + const string name = "aName"; |
| 105 | + IOAuth2Client oAuth2Client = new MockIOAuth2Client(_testOutputHelper); |
| 106 | + var provider = new OAuth2ClientCredentialsProvider(name, oAuth2Client); |
| 107 | + |
| 108 | + Assert.Equal(name, provider.Name); |
55 | 109 | }
|
56 | 110 |
|
57 | 111 | [Fact]
|
58 | 112 | public void ShouldRequestTokenWhenAskToRefresh()
|
59 | 113 | {
|
60 |
| - _oAuth2Client.Setup(p => p.RequestToken()).Returns(newToken("the_access_token", TimeSpan.FromSeconds(60))); |
61 |
| - _provider.Refresh(); |
62 |
| - Assert.Equal("the_access_token", _provider.Password); |
| 114 | + const string newTokenValue = "the_access_token"; |
| 115 | + IToken newToken = NewToken(newTokenValue, TimeSpan.FromSeconds(60)); |
| 116 | + var oAuth2Client = new MockIOAuth2Client(_testOutputHelper); |
| 117 | + oAuth2Client.RequestTokenValue = newToken; |
| 118 | + var provider = new OAuth2ClientCredentialsProvider(nameof(ShouldRequestTokenWhenAskToRefresh), oAuth2Client); |
| 119 | + |
| 120 | + provider.Refresh(); |
| 121 | + |
| 122 | + Assert.Equal(newTokenValue, provider.Password); |
63 | 123 | }
|
64 | 124 |
|
65 | 125 | [Fact]
|
66 | 126 | public void ShouldRequestTokenWhenGettingPasswordOrValidUntilForFirstTimeAccess()
|
67 | 127 | {
|
68 |
| - IToken firstToken = newToken("the_access_token", "the_refresh_token", TimeSpan.FromSeconds(1)); |
69 |
| - _oAuth2Client.Setup(p => p.RequestToken()).Returns(firstToken); |
70 |
| - Assert.Equal(firstToken.AccessToken, _provider.Password); |
71 |
| - Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value); |
| 128 | + const string accessToken = "the_access_token"; |
| 129 | + const string refreshToken = "the_refresh_token"; |
| 130 | + IToken firstToken = NewToken(accessToken, refreshToken, TimeSpan.FromSeconds(1)); |
| 131 | + var oAuth2Client = new MockIOAuth2Client(_testOutputHelper); |
| 132 | + oAuth2Client.RequestTokenValue = firstToken; |
| 133 | + var provider = new OAuth2ClientCredentialsProvider(nameof(ShouldRequestTokenWhenGettingPasswordOrValidUntilForFirstTimeAccess), oAuth2Client); |
| 134 | + |
| 135 | + Assert.Equal(firstToken.AccessToken, provider.Password); |
| 136 | + Assert.Equal(firstToken.ExpiresIn, provider.ValidUntil.Value); |
72 | 137 | }
|
73 | 138 |
|
74 | 139 | [Fact]
|
75 | 140 | public void ShouldRefreshTokenUsingRefreshTokenWhenAvailable()
|
76 | 141 | {
|
77 |
| - IToken firstToken = newToken("the_access_token", "the_refresh_token", TimeSpan.FromSeconds(1)); |
78 |
| - IToken refreshedToken = newToken("the_access_token2", "the_refresh_token_2", TimeSpan.FromSeconds(60)); |
79 |
| - _oAuth2Client.Setup(p => p.RequestToken()).Returns(firstToken); |
80 |
| - _provider.Refresh(); |
81 |
| - Assert.Equal(firstToken.AccessToken, _provider.Password); |
82 |
| - Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value); |
83 |
| - |
84 |
| - _oAuth2Client.Reset(); |
85 |
| - Thread.Sleep(1000); |
86 |
| - _oAuth2Client.Setup(p => p.RefreshToken(firstToken)).Returns(refreshedToken); |
87 |
| - _provider.Refresh(); |
88 |
| - |
89 |
| - Assert.Equal(refreshedToken.AccessToken, _provider.Password); |
90 |
| - Assert.Equal(refreshedToken.ExpiresIn, _provider.ValidUntil.Value); |
| 142 | + const string accessToken = "the_access_token"; |
| 143 | + const string refreshToken = "the_refresh_token"; |
| 144 | + const string accessToken2 = "the_access_token_2"; |
| 145 | + const string refreshToken2 = "the_refresh_token_2"; |
| 146 | + |
| 147 | + IToken firstToken = NewToken(accessToken, refreshToken, TimeSpan.FromSeconds(1)); |
| 148 | + IToken refreshedToken = NewToken(accessToken2, refreshToken2, TimeSpan.FromSeconds(60)); |
| 149 | + var oAuth2Client = new MockIOAuth2Client(_testOutputHelper); |
| 150 | + oAuth2Client.RequestTokenValue = firstToken; |
| 151 | + var provider = new OAuth2ClientCredentialsProvider(nameof(ShouldRequestTokenWhenGettingPasswordOrValidUntilForFirstTimeAccess), oAuth2Client); |
| 152 | + |
| 153 | + provider.Refresh(); |
| 154 | + |
| 155 | + Assert.Equal(firstToken.AccessToken, provider.Password); |
| 156 | + Assert.Equal(firstToken.ExpiresIn, provider.ValidUntil.Value); |
| 157 | + |
| 158 | + oAuth2Client.RefreshTokenValue = refreshedToken; |
| 159 | + provider.Refresh(); |
| 160 | + |
| 161 | + Assert.Equal(refreshedToken.AccessToken, provider.Password); |
| 162 | + Assert.Equal(refreshedToken.ExpiresIn, provider.ValidUntil.Value); |
91 | 163 | }
|
92 | 164 |
|
93 | 165 | [Fact]
|
94 | 166 | public void ShouldRequestTokenWhenRefreshTokenNotAvailable()
|
95 | 167 | {
|
96 |
| - IToken firstToken = newToken("the_access_token", null, TimeSpan.FromSeconds(1)); |
97 |
| - IToken refreshedToken = newToken("the_access_token2", null, TimeSpan.FromSeconds(1)); |
98 |
| - _oAuth2Client.SetupSequence(p => p.RequestToken()) |
99 |
| - .Returns(firstToken) |
100 |
| - .Returns(refreshedToken); |
101 |
| - _provider.Refresh(); |
| 168 | + const string accessToken = "the_access_token"; |
| 169 | + const string accessToken2 = "the_access_token_2"; |
| 170 | + IToken firstToken = NewToken(accessToken, null, TimeSpan.FromSeconds(1)); |
| 171 | + IToken secondToken = NewToken(accessToken2, null, TimeSpan.FromSeconds(60)); |
| 172 | + |
| 173 | + var oAuth2Client = new MockIOAuth2Client(_testOutputHelper); |
| 174 | + oAuth2Client.RequestTokenValue = firstToken; |
| 175 | + var provider = new OAuth2ClientCredentialsProvider(nameof(ShouldRequestTokenWhenRefreshTokenNotAvailable), oAuth2Client); |
| 176 | + |
| 177 | + provider.Refresh(); |
102 | 178 |
|
103 |
| - Assert.Equal(firstToken.AccessToken, _provider.Password); |
104 |
| - Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value); |
| 179 | + Assert.Equal(firstToken.AccessToken, provider.Password); |
| 180 | + Assert.Equal(firstToken.ExpiresIn, provider.ValidUntil.Value); |
105 | 181 |
|
106 |
| - _provider.Refresh(); |
| 182 | + oAuth2Client.RequestTokenValue = secondToken; |
| 183 | + provider.Refresh(); |
107 | 184 |
|
108 |
| - Assert.Equal(refreshedToken.AccessToken, _provider.Password); |
109 |
| - Assert.Equal(refreshedToken.ExpiresIn, _provider.ValidUntil.Value); |
110 |
| - Mock.Verify(_oAuth2Client); |
| 185 | + Assert.Equal(secondToken.AccessToken, provider.Password); |
| 186 | + Assert.Equal(secondToken.ExpiresIn, provider.ValidUntil.Value); |
111 | 187 | }
|
112 | 188 |
|
113 |
| - private static Token newToken(string access_token, TimeSpan expiresIn) |
| 189 | + private static Token NewToken(string access_token, TimeSpan expiresIn) |
114 | 190 | {
|
115 | 191 | var token = new JsonToken(access_token, string.Empty, expiresIn);
|
116 | 192 | return new Token(token);
|
117 | 193 | }
|
118 | 194 |
|
119 |
| - private static Token newToken(string access_token, string refresh_token, TimeSpan expiresIn) |
| 195 | + private static Token NewToken(string access_token, string refresh_token, TimeSpan expiresIn) |
120 | 196 | {
|
121 | 197 | JsonToken token = new JsonToken(access_token, refresh_token, expiresIn);
|
122 | 198 | return new Token(token);
|
|
0 commit comments