public async void AccessTokenReceivedSuccessfully() { var httpClientMock = new Mock <IHttpClient>(); httpClientMock.Setup(x => x.SendAsync(It.IsAny <HttpRequestMessage>())).ReturnsAsync( new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{ \"access_token\" : \"token\", \"expires_in\" : 5000 }", Encoding.UTF8, "application/json") }); var httpClient = httpClientMock.Object; var reader = new FileReader(); var p = new OAuth2Params { ClientId = "MyTempApp", Audience = "https://auth.supplier.redbearapp.io", CertificateFilePath = "ServiceApp.cer", Subject = "*****@*****.**", Scopes = new[] { "https://auth.supplier.redbearapp.io/UI" }, AuthServerUri = new Uri("https://auth.supplier.redbearapp.io/connect/token") }; var client = new OAuth2Client(httpClient, reader, p); var token = await client.GetAccessTokenAsync(); Assert.Equal("token", token.Token); Assert.True(token.Expires > DateTime.UtcNow); }
public async void BadRequestFails() { var httpClientMock = new Mock <IHttpClient>(); httpClientMock.Setup(x => x.SendAsync(It.IsAny <HttpRequestMessage>())).ReturnsAsync( new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Empty, Encoding.UTF8, "application/json") }); var httpClient = httpClientMock.Object; var reader = new FileReader(); var p = new OAuth2Params { ClientId = "MyTempApp", Audience = "https://auth.supplier.redbearapp.io", CertificateFilePath = "ServiceApp.cer", Subject = "*****@*****.**", Scopes = new[] { "https://auth.supplier.redbearapp.io/UI" }, AuthServerUri = new Uri("https://auth.supplier.redbearapp.io/connect/token") }; var client = new OAuth2Client(httpClient, reader, p); await Assert.ThrowsAsync <ServiceClientException>(async() => { await client.GetAccessTokenAsync(); }); }
/// <summary> /// Uncomment [Fact] to use as an integration test with a real certificate. /// The certificate will need to be copied to the test's bin folder. /// The easiest way of doing this is to include the certificate within /// this test project and set its build action to "Copy Always". /// /// Remember to take care of your certificates. Anybody who has access to them /// can access Red Bear's APIs and your data. /// /// The certificate included in this project is a placeholder and isn't used /// by any Red Bear energy suppliers. /// </summary> /// //[Fact] public async void IntegrationTest() { var httpClient = new HttpClient(); var reader = new FileReader(); var p = new OAuth2Params { ClientId = "MyTempApp", Audience = "https://auth.supplier.redbearapp.io", CertificateFilePath = "ServiceApp.cer", Subject = "*****@*****.**", Scopes = new[] { "https://auth.supplier.redbearapp.io/UI" }, AuthServerUri = new Uri("https://auth.supplier.redbearapp.io/connect/token") }; var client = new OAuth2Client(httpClient, reader, p); var token = await client.GetAccessTokenAsync(); Assert.NotEmpty(token.Token); }