Exemple #1
0
        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);
        }
Exemple #2
0
        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
        }
Exemple #3
0
        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));
        }