public void CtorUriAuth_Valid_SetsEndpoint(EndpointTestCase testcase) { var authProvider = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken), "X-ZUMO-AUTH"); var client = new DatasyncClient(new Uri(testcase.BaseEndpoint), authProvider); Assert.Equal(testcase.NormalizedEndpoint, client.Endpoint.ToString()); Assert.NotNull(client.ClientOptions); Assert.NotNull(client.HttpClient); }
public void Ctor_CanDoXZumoAuth() { var sut = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken), "X-ZUMO-AUTH", null); Assert.Equal("X-ZUMO-AUTH", sut.HeaderName); Assert.Null(sut.AuthenticationType); Assert.Null(sut.Current); Assert.True(sut.RefreshBufferTimeSpan.TotalMilliseconds > 0); }
public void Ctor_CanDoAuthBearer() { var sut = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken), "Authorization"); Assert.Equal("Authorization", sut.HeaderName); Assert.Equal("Bearer", sut.AuthenticationType); Assert.Null(sut.Current); Assert.True(sut.RefreshBufferTimeSpan.TotalMilliseconds > 0); }
public void IsExpired_BasicToken_ReturnsFalse() { var sut = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken)) { RefreshBufferTimeSpan = TimeSpan.FromMinutes(2) }; Assert.False(sut.IsExpired(ValidAuthenticationToken)); }
public async Task LoginAsync_CallsTokenRequestor() { var count = 0; var requestor = () => { count++; return(Task.FromResult(ValidAuthenticationToken)); }; var sut = new GenericAuthenticationProvider(requestor); await sut.LoginAsync().ConfigureAwait(false); Assert.Equal(1, count); }
public async Task GetTokenAsync_CallsOnFirstRun() { var count = 0; var requestor = () => { count++; return(Task.FromResult(ValidAuthenticationToken)); }; var sut = new GenericAuthenticationProvider(requestor); var actual = await sut.GetTokenAsync().ConfigureAwait(false); Assert.Equal(ValidAuthenticationToken.Token, actual); Assert.Equal(1, count); }
public void RefreshBufferTimeSpan_Roundtrips() { var ts = TimeSpan.FromMinutes(1); var sut = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken)) { RefreshBufferTimeSpan = ts }; Assert.Equal(ts, sut.RefreshBufferTimeSpan); }
public void IsExpired_Expired_ReturnsTrue() { var sut = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken)) { RefreshBufferTimeSpan = TimeSpan.FromMinutes(2) }; sut.Current = new AuthenticationToken { ExpiresOn = DateTimeOffset.Now.AddMinutes(-3) }; Assert.True(sut.IsExpired(sut.Current)); }
public async Task GetTokenAsync_CallsOnForce() { var count = 0; var requestor = () => { count++; return(Task.FromResult(ValidAuthenticationToken)); }; var sut = new GenericAuthenticationProvider(requestor); var firstCall = await sut.GetTokenAsync().ConfigureAwait(false); Assert.Equal(ValidAuthenticationToken.Token, firstCall); var secondCall = await sut.GetTokenAsync(true).ConfigureAwait(false); Assert.Equal(ValidAuthenticationToken.Token, secondCall); Assert.Equal(2, count); }
public void CreatePipeline_CBWithAuth_ThrowsArgumentException() { var b = new MockDelegatingHandler(); var c = new HttpClientHandler(); var options = new DatasyncClientOptions { HttpPipeline = new HttpMessageHandler[] { c, b } }; // Act var authProvider = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken), "X-ZUMO-AUTH"); Assert.Throws <ArgumentException>(() => _ = new WrappedHttpClient(Endpoint, authProvider, options)); }
public void CreatePipeline_NoHandlersWithAuth_CreatesPipeline() { var options = new DatasyncClientOptions { HttpPipeline = Array.Empty <HttpMessageHandler>() }; // Act var authProvider = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken), "X-ZUMO-AUTH"); var client = new WrappedHttpClient(Endpoint, authProvider, options); // Assert Assert.Same(authProvider, client.HttpHandler); Assert.IsAssignableFrom <HttpClientHandler>(authProvider.InnerHandler); }
public async Task GetTokenAsync_LogsOutWhenExpired() { var sut = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken)); var firstCall = await sut.GetTokenAsync().ConfigureAwait(false); Assert.Equal(ValidAuthenticationToken.Token, firstCall); Assert.Equal(ValidAuthenticationToken.DisplayName, sut.DisplayName); Assert.Equal(ValidAuthenticationToken.UserId, sut.UserId); Assert.True(sut.IsLoggedIn); sut.TokenRequestorAsync = () => Task.FromResult(ExpiredAuthenticationToken); var secondCall = await sut.GetTokenAsync(true).ConfigureAwait(false); Assert.Null(secondCall); Assert.Null(sut.DisplayName); Assert.Null(sut.UserId); Assert.False(sut.IsLoggedIn); }
public void CreatePipeline_BCWithAuth_CreatesPipeline() { var b = new MockDelegatingHandler(); var c = new HttpClientHandler(); var options = new DatasyncClientOptions { HttpPipeline = new HttpMessageHandler[] { b, c } }; // Act var authProvider = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken), "X-ZUMO-AUTH"); var client = new WrappedHttpClient(Endpoint, authProvider, options); // Assert Assert.Same(authProvider, client.HttpHandler); Assert.Same(b, authProvider.InnerHandler); Assert.Same(c, b.InnerHandler); }
public void RefreshBufferTimeSpan_CannotBeSmall(long ms) { var sut = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken)); Assert.Throws <ArgumentException>(() => sut.RefreshBufferTimeSpan = TimeSpan.FromMilliseconds(ms)); }