public void GetUsersFromDepartment_ContainsResult() { // Arrange var userService = new UserService(context); var user = userService.FindUserByEmail("*****@*****.**"); user.Department = new DepartmentService(context).FindDepartmentByDepartmentCode("ENGL"); userService.Save(user); var expected = "Admin "; var controller = new UserApiController { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; // Act IHttpActionResult actionResult = controller.GetUsersFromDepartment("ENGL"); var contentResult = actionResult as OkNegotiatedContentResult <IEnumerable <EmailNameViewModel> >; // Assert Assert.IsNotNull(contentResult); Assert.IsNotNull(contentResult.Content); Assert.IsTrue(contentResult.Content.Select(x => x.Name).Contains(expected)); }
public void GetRoles_ContainsResult() { // Arrange var userService = new UserService(context); var email = "*****@*****.**"; var expected = "Admin"; var controller = new UserApiController { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; // Act IHttpActionResult actionResult = controller.GetRoles(new EmailNameViewModel() { Email = email, }); var contentResult = actionResult as OkNegotiatedContentResult <RoleViewModel>; // Assert Assert.IsNotNull(contentResult); Assert.IsNotNull(contentResult.Content); Assert.AreEqual(expected, contentResult.Content.Roles.FirstOrDefault()); }
public UserApiControllerTests() { _credentialService = Substitute.For <ICredentialService>(); _userRepository = Substitute.For <IUserRepository>(); _logger = Substitute.For <ILogger>(); _notifier = Substitute.For <INotifier>(); _sut = new UserApiController(_credentialService, _userRepository, _logger, _notifier); }
public void Init() { unitOfWork = new UnitOfWorkMock(); mailHelper = new MailHelperMock(); jwtDecoder = new JwtDecoderMock(); encrypter = new EncrypterMock(); cache = new CacheMock(); controller = new UserApiController(unitOfWork, mailHelper, new ApiClientMock(), jwtDecoder, encrypter, cache); controller.Request = new HttpRequestMessage(); }
public async Task UserApiControllerTest_Get_returnsNotFound() { // Arrange // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Get(); // Assert Assert.IsInstanceOfType(res, typeof(NotFoundResult)); }
public async Task UserApiControllerTest_Get_returnsInteralServerError() { // Arrange _userStore.Setup(x => x.GetAll()).Throws(new Exception("asd")); // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Get(); // Assert Assert.IsInstanceOfType(res, typeof(ExceptionResult)); }
public async Task UserControllerTest_Find_returnInternalServerError() { // Arrange _userStore.Setup(x => x.SearchUser(It.IsAny <string>())).Throws(new Exception("asd")); // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Find("jonathan"); // Assert _userStore.Verify(x => x.SearchUser(It.IsAny <string>())); Assert.IsInstanceOfType(res, typeof(ExceptionResult)); }
public async Task UserControllerTest_Delete_returnsBadRequest() { // Arrange var user = new UserViewModel { FirstName = "Jonathan" }; // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Delete(""); // Assert Assert.IsInstanceOfType(res, typeof(BadRequestErrorMessageResult)); }
public async Task UserControllerTest_Delete_returnsNotFound() { // Arrange var user = new UserViewModel { FirstName = "Jonathan" }; // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Delete("*****@*****.**"); // Assert Assert.IsInstanceOfType(res, typeof(NotFoundResult)); }
public async Task UserControllerTest_GetSingle_returnsOk() { // Arrange _userStore.Setup(x => x.Get(It.IsAny <string>())).Returns(Task.FromResult <IAzureUser>(new UserViewModel { FirstName = "Jonathan" })); // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Get("Jonathan") as OkNegotiatedContentResult <UserViewModel>; // Assert Assert.IsInstanceOfType(res.Content, typeof(IAzureUser)); }
public void GetUserListFailureTest() { // Arrange UserApiController controller = new UserApiController(); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); // Act var response = controller.GetUserList(); // Assert Assert.IsNotNull(response); Assert.AreEqual(System.Net.HttpStatusCode.InternalServerError, response.StatusCode); }
// ------------------------ Utility --------------------------- private (WebmailContext, UserApiController) CreateTestTools(string name) { var context = CreateTestContext(name); var controller = new UserApiController(new SqlUserService(context, new SqlMessageService(context))) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal() } } }; return(context, controller); }
public async Task TestGetCurrentUser() { IUserProfileService _UserProfileProvider = new MockUserProfileService(graphUri, clientId2, appKey, AADInstance); ILogger <UserApiController> _logger1 = new LoggerFactory().CreateLogger <UserApiController>(); UserApiController userApiController = new UserApiController(_UserProfileProvider, _logger1); var actionResult = await userApiController.GetCurrentUser(); actionResult.AssertOkValueType <User>(); var result = actionResult.OkayContent <User>();; Assert.Equal("*****@*****.**", result.Id); }
public void GetUsersFromDepartment_ReturnsNotFound() { // Arrange var controller = new UserApiController() { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; // Act IHttpActionResult actionResult = controller.GetUsersFromDepartment("XXXX"); // Assert Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult)); }
public async Task UserControllerTest_Post_returnsInternalServerError() { // Arrange _userStore.Setup(x => x.AddUser(It.IsAny <IAzureUser>())).Throws(new Exception("asd")); var user = new UserViewModel { FirstName = "Jonathan" }; // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Post(user); // Assert Assert.IsInstanceOfType(res, typeof(ExceptionResult)); }
public async Task UserControllerTest_Post_returnsOk() { // Arrange var user = new UserViewModel { FirstName = "Jonathan" }; // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Post(user); // Assert _userStore.Verify(x => x.AddUser(It.IsAny <IAzureUser>())); Assert.IsInstanceOfType(res, typeof(OkResult)); }
public async Task UserControllerTest_Delete_returnsInternalServerError() { // Arrange _userStore.Setup(x => x.Get(It.IsAny <string>())).Throws(new Exception("asd")); var user = new UserViewModel { FirstName = "Jonathan" }; // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Delete("*****@*****.**"); // Assert Assert.IsInstanceOfType(res, typeof(ExceptionResult)); }
public async Task UnlockAccount_GivenRequestIsNotValid_ExpectResponseWithFailedStatus() { var mediator = new Mock <IMediator>(); var currentAuthenticatedUserProvider = new Mock <ICurrentAuthenticatedUserProvider>(); currentAuthenticatedUserProvider.Setup(x => x.CurrentAuthenticatedUser) .Returns(Maybe.From(new Mock <ISystemUser>().Object)); var controller = new UserApiController(mediator.Object, currentAuthenticatedUserProvider.Object); controller.ModelState.AddModelError("key", "error-message"); var result = Assert.IsType <JsonResult>(await controller.UnlockAccount(new UnlockAccountRequest())); var responseModel = Assert.IsType <UnlockAccountResponse>(result.Value); Assert.False(responseModel.IsSuccess); }
public void GetUserByIdNoContentTest() { // Arrange int UserId = 4; UserApiController controller = new UserApiController(); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); // Act var response = controller.GetUserById(UserId); // Assert Assert.IsNotNull(response); Assert.AreEqual(System.Net.HttpStatusCode.NoContent, response.StatusCode); }
public async Task UserControllerTest_Put_returnBadRequest() { // Arrange var user = new UserViewModel { FirstName = "Jonathan" }; // Act var controller = new UserApiController(_userStore.Object); controller.ModelState.AddModelError("error", "error"); var res = await controller.Put(user); // Assert Assert.IsInstanceOfType(res, typeof(BadRequestErrorMessageResult)); }
public async Task UserApiControllerTest_Get_returnsOk() { // Arrange _userStore.Setup(x => x.GetAll()).Returns(Task.FromResult <IEnumerable <IAzureUser> >(new List <UserViewModel> { new UserViewModel(), new UserViewModel(), })); // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Get() as OkNegotiatedContentResult <List <UserViewModel> >; // Assert Assert.IsNotNull(res); Assert.AreEqual(2, res.Content.Count); }
public async Task UnlockAccount_GivenFailedExecution_ExpectResponseWithFailedStatus() { var mediator = new Mock <IMediator>(); mediator.Setup(x => x.Send(It.IsAny <UnlockAccountCommand>(), It.IsAny <CancellationToken>())) .ReturnsAsync(ResultWithError.Fail(new ErrorData(ErrorCodes.AuthenticationFailed))); var currentAuthenticatedUserProvider = new Mock <ICurrentAuthenticatedUserProvider>(); currentAuthenticatedUserProvider.Setup(x => x.CurrentAuthenticatedUser) .Returns(Maybe.From(new Mock <ISystemUser>().Object)); var controller = new UserApiController(mediator.Object, currentAuthenticatedUserProvider.Object); var result = Assert.IsType <JsonResult>(await controller.UnlockAccount(new UnlockAccountRequest())); var responseModel = Assert.IsType <UnlockAccountResponse>(result.Value); Assert.False(responseModel.IsSuccess); }
public async Task UserControllerTest_Put_returnsOk() { // Arrange var user = new UserViewModel { FirstName = "Jonathan", LastName = "Mourtada", Id = "jonte" }; _userStore.Setup(x => x.Get(It.IsAny <string>())).Returns(Task.FromResult <IAzureUser>(user)); // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Put(user); // Assert _userStore.Verify(x => x.UpdateUser(It.IsAny <IAzureUser>())); Assert.IsInstanceOfType(res, typeof(OkResult)); }
public async Task UserControllerTest_Put_returnsNotFound() { // Arrange UserViewModel fakeUser = null; _userStore.Setup(x => x.Get(It.IsAny <string>())).Returns(Task.FromResult <IAzureUser>(fakeUser)); var user = new UserViewModel { FirstName = "Jonathan" }; // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Put(user); // Assert Assert.IsInstanceOfType(res, typeof(NotFoundResult)); }
public async Task UserControllerTest_Find_returnsOk() { // Arrange _userStore.Setup(x => x.SearchUser(It.IsAny <string>())).Returns(Task.FromResult <IEnumerable <IAzureUser> >(new List <IAzureUser> { new UserViewModel(), new UserViewModel(), })); // Act var controller = new UserApiController(_userStore.Object); var res = await controller.Find("jonathan") as OkNegotiatedContentResult <List <UserViewModel> >; // Assert _userStore.Verify(x => x.SearchUser(It.IsAny <string>())); Assert.IsNotNull(res); Assert.AreEqual(res.Content.Count(), 2); }
public void GetCustomerUIObject() { var customer = new Customer { code = "c1", name = "n1" }; var result = UserApiController.GetCustomerUIObject(customer); var combinedProp = result.GetType().GetProperty("combined_name"); Assert.IsNotNull(combinedProp); var data = combinedProp.GetValue(result).ToString(); Assert.AreEqual($"{customer.code} {customer.name}", data); customer.address6 = "XL3"; result = UserApiController.GetCustomerUIObject(customer); data = combinedProp.GetValue(result).ToString(); Assert.AreEqual($"{customer.code} {customer.name} ({customer.address6})", data); }
public void GetRoles_ReturnsNotFound() { // Arrange var email = "*****@*****.**"; var controller = new UserApiController() { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; // Act IHttpActionResult actionResult = controller.GetRoles(new EmailNameViewModel() { Email = email, }); // Assert Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult)); }
/// <summary> /// Setup the test /// </summary> public UserApiUnitTest() { DbContextOptions <DbAppContext> options = new DbContextOptions <DbAppContext>(); Mock <DbAppContext> dbAppContext = new Mock <DbAppContext>(options); /* * * Here you will need to mock up the context. * * ItemType fakeItem = new ItemType(...); * * Mock<DbSet<ItemType>> mockList = MockDbSet.Create(fakeItem); * * dbAppContext.Setup(x => x.ModelEndpoint).Returns(mockItem.Object); * */ UserApiService _service = new UserApiService(dbAppContext.Object); _UserApi = new UserApiController(_service); }
public async Task UnlockAccount_GivenNoAuthenticatedUser_ExpectFailedResponse() { var mediator = new Mock <IMediator>(); mediator.Setup(x => x.Send(It.IsAny <UnlockAccountCommand>(), It.IsAny <CancellationToken>())) .ReturnsAsync(ResultWithError.Ok <ErrorData>()); var currentAuthenticatedUserProvider = new Mock <ICurrentAuthenticatedUserProvider>(); currentAuthenticatedUserProvider.Setup(x => x.CurrentAuthenticatedUser) .Returns(Maybe <ISystemUser> .Nothing); var controller = new UserApiController(mediator.Object, currentAuthenticatedUserProvider.Object); var result = Assert.IsType <JsonResult>(await controller.UnlockAccount(new UnlockAccountRequest { UserId = TestVariables.AuthenticatedUserId, })); var responseModel = Assert.IsType <UnlockAccountResponse>(result.Value); Assert.False(responseModel.IsSuccess); }
public async Task EnableAccount_GivenAttemptToEnableSelf_ExpectResponseWithFailedStatus() { var mediator = new Mock <IMediator>(); mediator.Setup(x => x.Send(It.IsAny <EnableAccountCommand>(), It.IsAny <CancellationToken>())) .ReturnsAsync(ResultWithError.Fail(new ErrorData(ErrorCodes.AuthenticationFailed))); var currentAuthenticatedUserProvider = new Mock <ICurrentAuthenticatedUserProvider>(); var authenticatedUser = new Mock <ISystemUser>(); authenticatedUser.Setup(x => x.UserId).Returns(TestVariables.AuthenticatedUserId); currentAuthenticatedUserProvider.Setup(x => x.CurrentAuthenticatedUser) .Returns(Maybe.From(authenticatedUser.Object)); var controller = new UserApiController(mediator.Object, currentAuthenticatedUserProvider.Object); var result = Assert.IsType <JsonResult>(await controller.EnableAccount(new EnableAccountRequest { UserId = TestVariables.AuthenticatedUserId, })); var responseModel = Assert.IsType <EnableAccountResponse>(result.Value); Assert.False(responseModel.IsSuccess); }