Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that the order of arguments is expected, actual, take two! #1386

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions projects/Unit/TestOAuth2ClientCredentialsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//---------------------------------------------------------------------------

using System;

using System.Threading;
using Moq;
using RabbitMQ.Client.OAuth2;
using Xunit;
Expand Down Expand Up @@ -67,8 +67,8 @@ public void ShouldRequestTokenWhenGettingPasswordOrValidUntilForFirstTimeAccess(
{
IToken firstToken = newToken("the_access_token", "the_refresh_token", TimeSpan.FromSeconds(1));
_oAuth2Client.Setup(p => p.RequestToken()).Returns(firstToken);
Assert.Equal(_provider.Password, firstToken.AccessToken);
Assert.Equal(_provider.ValidUntil.Value, firstToken.ExpiresIn);
Assert.Equal(firstToken.AccessToken, _provider.Password);
Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value);
}

[Fact]
Expand All @@ -78,16 +78,16 @@ public void ShouldRefreshTokenUsingRefreshTokenWhenAvailable()
IToken refreshedToken = newToken("the_access_token2", "the_refresh_token_2", TimeSpan.FromSeconds(60));
_oAuth2Client.Setup(p => p.RequestToken()).Returns(firstToken);
_provider.Refresh();
Assert.Equal(_provider.Password, firstToken.AccessToken);
Assert.Equal(_provider.ValidUntil.Value, firstToken.ExpiresIn);
_oAuth2Client.Reset();
System.Threading.Thread.Sleep(1000);
Assert.Equal(firstToken.AccessToken, _provider.Password);
Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value);

_oAuth2Client.Reset();
Thread.Sleep(1000);
_oAuth2Client.Setup(p => p.RefreshToken(firstToken)).Returns(refreshedToken);
_provider.Refresh();
Assert.Equal(_provider.Password, refreshedToken.AccessToken);
Assert.Equal(_provider.ValidUntil.Value, refreshedToken.ExpiresIn);

Assert.Equal(refreshedToken.AccessToken, _provider.Password);
Assert.Equal(refreshedToken.ExpiresIn, _provider.ValidUntil.Value);
}

[Fact]
Expand All @@ -99,12 +99,14 @@ public void ShouldRequestTokenWhenRefreshTokenNotAvailable()
.Returns(firstToken)
.Returns(refreshedToken);
_provider.Refresh();
Assert.Equal(_provider.Password, firstToken.AccessToken);
Assert.Equal(_provider.ValidUntil.Value, firstToken.ExpiresIn);

Assert.Equal(firstToken.AccessToken, _provider.Password);
Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value);

_provider.Refresh();
Assert.Equal(_provider.Password, refreshedToken.AccessToken);
Assert.Equal(_provider.ValidUntil.Value, refreshedToken.ExpiresIn);

Assert.Equal(refreshedToken.AccessToken, _provider.Password);
Assert.Equal(refreshedToken.ExpiresIn, _provider.ValidUntil.Value);
Mock.Verify(_oAuth2Client);
}

Expand Down
12 changes: 7 additions & 5 deletions projects/Unit/TestTimerBasedCredentialRefresher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//---------------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using System.Threading;
using Moq;
using Xunit;

Expand Down Expand Up @@ -67,28 +67,30 @@ public void TestDoNotRegisterWhenHasNoExpiry()
}

[Fact]
public async void TestRefreshToken()
public void TestRefreshToken()
{
_credentialsProvider.Setup(p => p.ValidUntil).Returns(TimeSpan.FromSeconds(1));
_credentialsProvider.Setup(p => p.Password).Returns("the-token").Verifiable();
_callback.Setup(p => p.Invoke(true));
_refresher.Register(_credentialsProvider.Object, _callback.Object);
await Task.Delay(1 * 1000);

Thread.Sleep(TimeSpan.FromSeconds(1));

_credentialsProvider.Verify();
_callback.Verify();
}

[Fact]
public async void TestRefreshTokenFailed()
public void TestRefreshTokenFailed()
{
_credentialsProvider.Setup(p => p.ValidUntil).Returns(TimeSpan.FromSeconds(1));
_credentialsProvider.SetupSequence(p => p.Password)
.Returns("the-token")
.Throws(new Exception());
_callback.Setup(p => p.Invoke(false));
_refresher.Register(_credentialsProvider.Object, _callback.Object);
await Task.Delay(1 * 1000);

Thread.Sleep(TimeSpan.FromSeconds(1));

_credentialsProvider.Verify();
_callback.Verify();
Expand Down
Loading