public void GetOAuthAccessToken_InvalidCode_ShouldReturnNullAccessTokenAndErrorMessage() { IConfiguration config = Mock.Of <IConfiguration>(m => m["Bungie:ApiKey"] == "dummy-api-key" && m["Bungie:ClientId"] == "dummy-client-id" && m["Bungie:ClientSecret"] == "dummy-client-secret"); // arrange Uri uri = new Uri("https://www.bungie.net/Platform/App/OAuth/Token/"); // just an old example auth code. let's pretend this code expired or was already used string authCode = "15bf7985a28208b83997b090302b36a7"; string content = $"grant_type=authorization_code&code={authCode}&client_id={"dummy-client-id"}&client_secret={"dummy-client-secret"}"; string responseString = TestUtils.ReadFile("OAuth-invalid-auth-code.json"); Mock <HttpMessageHandler> mock = new Mock <HttpMessageHandler>(MockBehavior.Strict); mock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.Is <HttpRequestMessage>( req => req.Method == HttpMethod.Post && req.RequestUri == uri && req.Content.Headers.Any(h => h.Key == "X-API-KEY" && !string.IsNullOrEmpty(h.Value.FirstOrDefault())) && req.Content.Headers.Any(h => h.Key == "Content-Type" && h.Value.FirstOrDefault() == "application/x-www-form-urlencoded") && req.Content.ReadAsStringAsync().Result == content), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent(responseString) }) .Verifiable(); HttpClient httpClient = new HttpClient(mock.Object); BungieApiService bungieApiService = new BungieApiService(config, httpClient); OAuthRequest request = new OAuthRequest(authCode); // act OAuthResponse actual = bungieApiService.GetOAuthAccessToken(request); // assert Assert.IsTrue(string.IsNullOrWhiteSpace(actual.AccessToken)); Assert.AreEqual("AuthorizationCodeInvalid", actual.ErrorDescription); }
public void GetOAuthAccessToken_ValidCode_ShouldReturnAccessTokenAndNullErrorMessage() { IConfiguration config = Mock.Of <IConfiguration>(m => m["Bungie:ApiKey"] == "dummy-api-key" && m["Bungie:ClientId"] == "dummy-client-id" && m["Bungie:ClientSecret"] == "dummy-client-secret"); // arrange Uri uri = new Uri("https://www.bungie.net/Platform/App/OAuth/Token/"); string authCode = "15bf7985a28208b83997b090302b36a7"; // just an old example auth code string content = $"grant_type=authorization_code&code={authCode}&client_id={"dummy-client-id"}&client_secret={"dummy-client-secret"}"; string responseString = TestUtils.ReadFile("OAuth-valid.json"); string expectedAccessCode = "CLDjARKGAgAgVJDu+K+f85W6S6eJJi+s7U9tXkxDzInlc8I78HfgcabgAAAATOBrgq37w0FGjQ6XoVCLI4Mntf9IjfT91ByO4T59755lmaJvWMdnNpm4YcKglZiJN9IT0lLuZNifSUZRtWl1Xi+m83Eoh6VBMxRaec9Feeu4Coa53XzEAVr/BadPeaugfqB8A5jgEcRdQrnSH092D1h1ntzLpm0cOUttRGqMFpw/nR9Sm0vF1i4kdrq8F9gx+PQ6fJvbBxOKYZRSnQUgr3WjSSgWGmOvAu778Ikf/0tN7dmgpX6JFHcb2U1fcvSprnbb0qcqsGB71KsSvRqgJ5T9/LswkBT9TIHbrtS/cPg="; Mock <HttpMessageHandler> mock = new Mock <HttpMessageHandler>(MockBehavior.Strict); mock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.Is <HttpRequestMessage>( req => req.Method == HttpMethod.Post && req.RequestUri == uri && req.Content.Headers.Any(h => h.Key == "X-API-KEY" && !string.IsNullOrEmpty(h.Value.FirstOrDefault())) && req.Content.Headers.Any(h => h.Key == "Content-Type" && h.Value.FirstOrDefault() == "application/x-www-form-urlencoded") && req.Content.ReadAsStringAsync().Result == content), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(responseString) }) .Verifiable(); HttpClient httpClient = new HttpClient(mock.Object); BungieApiService bungieApiService = new BungieApiService(config, httpClient); OAuthRequest request = new OAuthRequest(authCode); // act OAuthResponse actual = bungieApiService.GetOAuthAccessToken(request); // assert Assert.AreEqual(expectedAccessCode, actual.AccessToken); Assert.IsTrue(string.IsNullOrWhiteSpace(actual.ErrorType) && string.IsNullOrWhiteSpace(actual.ErrorDescription)); }