public async Task Authorize_WillObtainServerTimeAndPersist_ShouldShowValuesAreCalculated() { var client = GetRestClient(); // configure the AblyAuth test wrapper to return UTC+30m when ServerTime() is called // (By default the library uses DateTimeOffset.UtcNow whe Now() is called) var testAblyAuth = new TestAblyAuth(client.Options, client, () => Task.FromResult(DateTimeOffset.UtcNow.AddMinutes(30))); // RSA10k: If the AuthOption argument’s queryTime attribute is true // it will obtain the server time once and persist the offset from the local clock. testAblyAuth.Options.QueryTime = true; var tokenParams = new TokenParams(); testAblyAuth.Options.QueryTime = true; await testAblyAuth.AuthorizeAsync(tokenParams); // to show the values are calculated and not fixed // get the current server time offset, pause for a short time, // then get it again. // The new value should represent a time after the first var snapshot = testAblyAuth.GetServerNow(); await Task.Delay(500); testAblyAuth.GetServerNow()?.Should().BeAfter(snapshot.Value); }
public async Task Authorize_WillObtainServerTimeAndPersistTheOffsetFromTheLocalClock() { var client = GetRestClient(); bool serverTimeCalled = false; // configure the AblyAuth test wrapper to return UTC+30m when ServerTime() is called // (By default the library uses DateTimeOffset.UtcNow whe Now() is called) var testAblyAuth = new TestAblyAuth(client.Options, client, () => { serverTimeCalled = true; return(Task.FromResult(DateTimeOffset.UtcNow.AddMinutes(30))); }); // RSA10k: If the AuthOption argument’s queryTime attribute is true // it will obtain the server time once and persist the offset from the local clock. var tokenParams = new TokenParams(); testAblyAuth.Options.QueryTime = true; await testAblyAuth.AuthorizeAsync(tokenParams); serverTimeCalled.Should().BeTrue(); testAblyAuth.GetServerNow().Should().HaveValue(); const int precision = 1000; testAblyAuth.GetServerNow()?.Should().BeCloseTo(await testAblyAuth.GetServerTime(), TimeSpan.FromMilliseconds(precision)); // Allow 1s clock skew testAblyAuth.GetServerNow()?.Should().BeCloseTo(DateTimeOffset.UtcNow.AddMinutes(30), TimeSpan.FromMilliseconds(precision)); // Allow 1s clock skew }
public async Task ShouldKeepCurrentTokenParamsAndOptionsEvenIfCurrentTokenIsValidAndNoNewTokenIsRequested() { var client = GetRestClient( null, opts => opts.TokenDetails = new TokenDetails("boo") { Expires = Now.AddHours(10) }); var testAblyAuth = new TestAblyAuth(client.Options, client); var customTokenParams = TokenParams.WithDefaultsApplied(); customTokenParams.Ttl = TimeSpan.FromHours(2); customTokenParams.Timestamp = Now.AddHours(1); var customAuthOptions = AuthOptions.FromExisting(testAblyAuth.Options); customAuthOptions.UseTokenAuth = true; await testAblyAuth.AuthorizeAsync(customTokenParams, customAuthOptions); var expectedTokenParams = customTokenParams.Clone(); expectedTokenParams.Timestamp = null; testAblyAuth.CurrentTokenParams.Should().BeEquivalentTo(expectedTokenParams); testAblyAuth.CurrentAuthOptions.Should().BeSameAs(customAuthOptions); }
public async Task Authorize_UseRequestTokenToCreateTokensAndPassesTokenParamsAndAuthOptions() { var client = GetRestClient(); var testAblyAuth = new TestAblyAuth(client.Options, client); testAblyAuth.Options.UseTokenAuth = true; var customAuthOptions = testAblyAuth.Options; var customTokenParams = TokenParams.WithDefaultsApplied(); customTokenParams.Ttl = TimeSpan.FromHours(2); await testAblyAuth.AuthorizeAsync(customTokenParams, customAuthOptions); testAblyAuth.RequestTokenCalled.Should().BeTrue("Token creation was not delegated to RequestToken"); testAblyAuth.LastRequestTokenParams.Should().BeSameAs(customTokenParams); testAblyAuth.LastRequestAuthOptions.Should().BeSameAs(customAuthOptions); }
public async Task Authorize_ObtainServerTimeAndPersistOffset_ShouldShowServerTimeIsCalledOnlyOnce() { var client = GetRestClient(); bool serverTimeCalled = false; // configure the AblyAuth test wrapper to return UTC+30m when ServerTime() is called // (By default the library uses DateTimeOffset.UtcNow whe Now() is called) var testAblyAuth = new TestAblyAuth(client.Options, client, () => { serverTimeCalled = true; return(Task.FromResult(DateTimeOffset.UtcNow.AddMinutes(30))); }); // RSA10k: If the AuthOption argument’s queryTime attribute is true // it will obtain the server time once and persist the offset from the local clock. var tokenParams = new TokenParams(); testAblyAuth.Options.QueryTime = true; await testAblyAuth.AuthorizeAsync(tokenParams); // to show the values are calculated and not fixed // get the current server time offset, pause for a short time, // then get it again. // The new value should represent a time after the first testAblyAuth.GetServerNow(); // reset flag, used to show ServerTime() is not called again serverTimeCalled = false; // RSA10k: All future token requests generated directly or indirectly via a call to // authorize will not obtain the server time, but instead use the local clock // offset to calculate the server time. await testAblyAuth.AuthorizeAsync(); // ServerTime() should not have been called again serverTimeCalled.Should().BeFalse(); // and we should still be getting calculated offsets testAblyAuth.GetServerNow().Should().HaveValue(); const int precision = 1000; testAblyAuth.GetServerNow()?.Should().BeCloseTo(await testAblyAuth.GetServerTime(), TimeSpan.FromMilliseconds(precision)); testAblyAuth.GetServerNow()?.Should().BeCloseTo(DateTimeOffset.UtcNow.AddMinutes(30), TimeSpan.FromMilliseconds(precision)); }
public async Task Authorize_WillObtainServerTimeAndPersist_ShouldUserServerTimeEvenIfFurtherRequestsDoNotHaveQueryTimeSetToTrue() { var client = GetRestClient(); var serverTimeCalled = 0; // configure the AblyAuth test wrapper to return UTC+30m when ServerTime() is called // (By default the library uses DateTimeOffset.UtcNow whe Now() is called) var testAblyAuth = new TestAblyAuth(client.Options, client, () => { Interlocked.Increment(ref serverTimeCalled); return(Task.FromResult(DateTimeOffset.UtcNow.AddMinutes(30))); }); // RSA10k: If the AuthOption argument’s queryTime attribute is true // it will obtain the server time once and persist the offset from the local clock. testAblyAuth.Options.QueryTime = true; var tokenParams = new TokenParams(); testAblyAuth.Options.QueryTime = true; await testAblyAuth.AuthorizeAsync(tokenParams); // intercept the (mocked) HttpRequest so we can get a reference to the AblyRequest TokenRequest tokenRequest = null; var exFunc = client.ExecuteHttpRequest; client.ExecuteHttpRequest = request => { tokenRequest = request.PostData as TokenRequest; return(exFunc(request)); }; // demonstrate that we don't need QueryTime set to get a server time offset testAblyAuth.Options.QueryTime = false; await testAblyAuth.AuthorizeAsync(tokenParams); // offset should be cached serverTimeCalled.Should().Be(1); // the TokenRequest timestamp should have been set using the offset tokenRequest.Timestamp.Should().HaveValue(); tokenRequest.Timestamp.Should().BeCloseTo(await testAblyAuth.GetServerTime(), TimeSpan.FromMilliseconds(1000)); }
public async Task Authorize_RestClientAuthoriseMethodsShouldBeMarkedObsoleteAndLogADeprecationWarning() { /* Check for Obsolete Attribute */ MethodBase method = typeof(AblyAuth).GetMethod("Authorise"); method.Should().NotBeNull(); var attr = (ObsoleteAttribute)method?.GetCustomAttribute(typeof(ObsoleteAttribute)); attr.Should().NotBeNull(); method = typeof(AblyAuth).GetMethod("AuthoriseAsync"); method.Should().NotBeNull(); attr = (ObsoleteAttribute)method?.GetCustomAttribute(typeof(ObsoleteAttribute)); attr.Should().NotBeNull(); method = typeof(AblyAuth).GetMethod("Authorize"); method.Should().NotBeNull(); attr = (ObsoleteAttribute)method?.GetCustomAttribute(typeof(ObsoleteAttribute)); attr.Should().BeNull(); method = typeof(AblyAuth).GetMethod("AuthorizeAsync"); method.Should().NotBeNull(); attr = (ObsoleteAttribute)method?.GetCustomAttribute(typeof(ObsoleteAttribute)); attr.Should().BeNull(); #pragma warning disable CS0618 // Type or member is obsolete /* Check for logged warning */ var testLogger1 = new TestLogger("AuthoriseAsync is deprecated and will be removed in the future, please replace with a call to AuthorizeAsync"); var client = GetRestClient(setOptionsAction: options => { options.Logger = testLogger1; }); var testAblyAuth = new TestAblyAuth(client.Options, client); _ = await testAblyAuth.AuthoriseAsync(); testLogger1.MessageSeen.Should().BeTrue(); var testLogger2 = new TestLogger("Authorise is deprecated and will be removed in the future, please replace with a call to Authorize"); client = GetRestClient(setOptionsAction: options => { options.Logger = testLogger2; }); testAblyAuth = new TestAblyAuth(client.Options, client); _ = testAblyAuth.Authorise(); testLogger2.MessageSeen.Should().BeTrue(); #pragma warning restore CS0618 // Type or member is obsolete }
public async Task ShouldKeepTokenParamsAndAuthOptionsExceptForceAndCurrentTimestamp() { var client = GetRestClient(); var testAblyAuth = new TestAblyAuth(client.Options, client); var customTokenParams = TokenParams.WithDefaultsApplied(); _ = customTokenParams.Merge(new TokenParams { Ttl = TimeSpan.FromHours(2), Timestamp = Now.AddHours(1) }); var customAuthOptions = AuthOptions.FromExisting(testAblyAuth.Options); customAuthOptions.UseTokenAuth = true; await testAblyAuth.AuthorizeAsync(customTokenParams, customAuthOptions); var expectedTokenParams = customTokenParams.Clone(); expectedTokenParams.Timestamp = null; testAblyAuth.CurrentTokenParams.Should().BeEquivalentTo(expectedTokenParams); testAblyAuth.CurrentAuthOptions.Should().BeSameAs(customAuthOptions); testAblyAuth.CurrentTokenParams.Timestamp.Should().Be(null); }
public async Task Issue336_SetServerTimeExceptionsShouldBeHandled() { /* * This is a test to demonstrate the fix for issue * https://github.com/ably/ably-dotnet/issues/346 * against the 1.1.6 release this test would fail */ var client = GetRestClient(); bool serverTimeCalled = false; // configure serverTime delegate to throw an exception var testAblyAuth = new TestAblyAuth(client.Options, client, () => { serverTimeCalled = true; throw new Exception("baz"); }); var tokenParams = new TokenParams(); testAblyAuth.Options.QueryTime = true; Exception exception = null; try { await testAblyAuth.RequestTokenAsync(tokenParams, client.Options); } catch (Exception e) { exception = e; } serverTimeCalled.Should().BeTrue(); exception.Should().NotBeNull(); }
public async Task ShouldKeepTokenParamsAndAuthOptionsExcetpForceAndCurrentTimestamp() { var client = GetRestClient(); var testAblyAuth = new TestAblyAuth(client.Options, client); var customTokenParams = new TokenParams() { Ttl = TimeSpan.FromHours(2), Timestamp = Now.AddHours(1) }; var customAuthOptions = new AuthOptions() { UseTokenAuth = true, Force = true }; await testAblyAuth.AuthoriseAsync(customTokenParams, customAuthOptions); var expectedTokenParams = customTokenParams.Clone(); expectedTokenParams.Timestamp = null; testAblyAuth.CurrentTokenParams.ShouldBeEquivalentTo(expectedTokenParams); testAblyAuth.CurrentAuthOptions.Should().BeSameAs(customAuthOptions); testAblyAuth.CurrentTokenParams.Timestamp.Should().Be(null); testAblyAuth.CurrentAuthOptions.Force.Should().BeFalse(); }