public void GivenNotExistedApiKey_ItReturnsResultWithNullApiKeyViewModel() { // Arrange var verifyQuery = "{\"ApiKey\":\"apiKey1\",\"LeakedUrl\":\"https://leakedUrl1\"}"; var _authenticationService = new Mock <IAuthenticationService>(); _authenticationService.Setup(x => x.GetApiKeyCredential(It.IsAny <string>())) .Returns(() => null); var apiKeysController = new ApiKeysController(_authenticationService.Object); TestUtility.SetupHttpContextMockForUrlGeneration(_httpContextBase, apiKeysController); // Act var result = apiKeysController.Verify(verifyQuery); // Assert var jsonResult = Assert.IsType <JsonResult>(result); Assert.Equal((int)HttpStatusCode.OK, apiKeysController.Response.StatusCode); var apiKeyRevokeViewModels = Assert.IsType <List <ApiKeyRevokeViewModel> >(jsonResult.Data); Assert.Equal(1, apiKeyRevokeViewModels.Count); var apiKeyRevokeViewModel = Assert.IsType <ApiKeyRevokeViewModel>(apiKeyRevokeViewModels[0]); Assert.Null(apiKeyRevokeViewModel.ApiKeyViewModel); Assert.Equal("apiKey1", apiKeyRevokeViewModel.ApiKey); Assert.Equal("https://leakedUrl1", apiKeyRevokeViewModel.LeakedUrl); Assert.False(apiKeyRevokeViewModel.IsRevocable); _authenticationService.Verify(x => x.GetApiKeyCredential(It.IsAny <string>()), Times.Once); _authenticationService.Verify(x => x.DescribeCredential(It.IsAny <Credential>()), Times.Never); }
public void GivenMultipleApiKeys_ItReturnsNotRepeatedResults(string verifyQuery, List <string> expectedApiKeys, List <string> expectedLeakedUrls) { // Arrange var _authenticationService = new Mock <IAuthenticationService>(); _authenticationService.Setup(x => x.GetApiKeyCredential(It.IsAny <string>())) .Returns(() => new Credential()); _authenticationService.Setup(x => x.DescribeCredential(It.IsAny <Credential>())) .Returns(() => GetCredentialViewModel(CredentialTypes.ApiKey.V4, false)); var apiKeysController = new ApiKeysController(_authenticationService.Object); TestUtility.SetupHttpContextMockForUrlGeneration(_httpContextBase, apiKeysController); // Act var result = apiKeysController.Verify(verifyQuery); // Assert var jsonResult = Assert.IsType <JsonResult>(result); Assert.Equal((int)HttpStatusCode.OK, apiKeysController.Response.StatusCode); var apiKeyRevokeViewModels = Assert.IsType <List <ApiKeyRevokeViewModel> >(jsonResult.Data); Assert.Equal(expectedApiKeys.Count, apiKeyRevokeViewModels.Count); for (var i = 0; i < apiKeyRevokeViewModels.Count; i++) { Assert.Equal(expectedApiKeys[i], apiKeyRevokeViewModels[i].ApiKey); Assert.Equal(expectedLeakedUrls[i], apiKeyRevokeViewModels[i].LeakedUrl); Assert.Equal(true, apiKeyRevokeViewModels[i].IsRevocable); } _authenticationService.Verify(x => x.GetApiKeyCredential(It.IsAny <string>()), Times.Exactly(expectedApiKeys.Count)); _authenticationService.Verify(x => x.DescribeCredential(It.IsAny <Credential>()), Times.Exactly(expectedApiKeys.Count)); }
public void GivenEmptyVerifyQuery_ItReturnsWarning(string verifyQuery) { // Arrange var apiKeysController = new ApiKeysController(_authenticationService.Object); TestUtility.SetupHttpContextMockForUrlGeneration(_httpContextBase, apiKeysController); // Act var result = apiKeysController.Verify(verifyQuery); // Assert var jsonResult = Assert.IsType <JsonResult>(result); Assert.Equal((int)HttpStatusCode.BadRequest, apiKeysController.Response.StatusCode); Assert.Equal("Invalid empty input!", jsonResult.Data); }
public void GivenInvalidVerifyQuery_ItReturnsWarning(string verifyQuery, string expectedMessageQuery) { // Arrange var _authenticationService = new Mock <IAuthenticationService>(); var apiKeysController = new ApiKeysController(_authenticationService.Object); TestUtility.SetupHttpContextMockForUrlGeneration(_httpContextBase, apiKeysController); // Act var result = apiKeysController.Verify(verifyQuery); // Assert var jsonResult = Assert.IsType <JsonResult>(result); Assert.Equal((int)HttpStatusCode.BadRequest, apiKeysController.Response.StatusCode); Assert.Equal($"Invalid input! {expectedMessageQuery} is not using the valid JSON format.", jsonResult.Data); }
public void GivenVerifyQuery_ItThrowsExceptionFromDependencies() { // Arrange var verifyQuery = "{\"ApiKey\":\"apiKey1\",\"LeakedUrl\":\"https://leakedUrl1\"}"; var exceptionMessage = "Some exceptions!"; var _authenticationService = new Mock <IAuthenticationService>(); _authenticationService.Setup(x => x.GetApiKeyCredential(It.IsAny <string>())) .Throws(new Exception(exceptionMessage)); var apiKeysController = new ApiKeysController(_authenticationService.Object); TestUtility.SetupHttpContextMockForUrlGeneration(_httpContextBase, apiKeysController); // Act and Assert var exception = Assert.Throws <Exception>(() => apiKeysController.Verify(verifyQuery)); Assert.Equal(exceptionMessage, exception.Message); }