Ejemplo n.º 1
0
        public void Post_Edit_WithInvalidModelStateShouldReturnView()
        {
            //Arrange
            var controller = new AdminStationsController(this.townService.Object, this.stationService.Object);

            controller.ModelState.AddModelError(string.Empty, "Error");

            this.stationService.Setup(s => s.StationExists(It.IsAny <int>()))
            .Returns(true);

            this.stationService.Setup(s => s.EditedStationIsSame(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>()))
            .Returns(false);

            //Act
            var result = controller.Edit(this.GetEditStationFormModel());

            //Assert
            result.Should().BeOfType <ViewResult>();
            var model = result.As <ViewResult>().Model;

            model.Should().BeOfType <StationFormModel>();
            var form = model.As <StationFormModel>();

            form.IsEdit.Should().BeTrue();
            form.Name.Should().Be(StationName);
            form.Phone.Should().Be(StationPhone);
            form.TownId.Should().Be(StationTownId);
        }
Ejemplo n.º 2
0
        public void Post_Edit_WithCorrectDataShouldRedirectToAllStations()
        {
            //Arrange
            var controller = new AdminStationsController(this.townService.Object, this.stationService.Object);

            this.stationService.Setup(s => s.StationExists(It.IsAny <int>()))
            .Returns(true);

            this.stationService.Setup(s => s.EditedStationIsSame(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>()))
            .Returns(false);

            this.stationService.Setup(s => s.Edit(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>()))
            .Returns(true);

            this.townService.Setup(t => t.GetTownNameById(It.IsAny <int>()))
            .Returns(TownName);

            this.PrepareTempData();

            controller.TempData = this.tempData.Object;

            //Act
            var result = controller.Edit(this.GetEditStationFormModel());

            //Assert
            result.Should().BeOfType <RedirectToActionResult>();
            this.customMessage.Should().Be(string.Format(WebConstants.Message.EntityEdited, nameof(WebConstants.Entity.Station)));
        }
Ejemplo n.º 3
0
        public void Post_Edit_WhenNoChangesWereFoundShouldReturnView()
        {
            //Arrange
            var controller = new AdminStationsController(this.townService.Object, this.stationService.Object);

            this.stationService.Setup(s => s.StationExists(It.IsAny <int>()))
            .Returns(true);

            this.stationService.Setup(s => s.EditedStationIsSame(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>()))
            .Returns(true);

            this.PrepareTempData();

            controller.TempData = this.tempData.Object;

            //Act
            var result = controller.Edit(this.GetEditStationFormModel());

            //Assert
            result.Should().BeOfType <ViewResult>();
            var model = result.As <ViewResult>().Model;

            model.Should().BeOfType <StationFormModel>();
            var form = model.As <StationFormModel>();

            form.Id.Should().Be(EditStationId);
            form.Name.Should().Be(StationName);
            form.Phone.Should().Be(StationPhone);
            form.TownId.Should().Be(StationTownId);
            form.IsEdit.Should().BeTrue();
            this.customMessage.Should().Be(WebConstants.Message.NoChangesFound);
        }
Ejemplo n.º 4
0
        public void Post_Add_WithAlreadyExistingStationShouldReturnView()
        {
            //Arrange
            var controller = new AdminStationsController(this.townService.Object, this.stationService.Object);

            this.stationService.Setup(s => s.Add(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <string>()))
            .Returns(false);

            this.townService.Setup(t => t.GetTownNameById(It.IsAny <int>()))
            .Returns(Town);

            this.PrepareTempData();

            controller.TempData = this.tempData.Object;

            //Act
            var result = controller.Add(this.GetAddStationFormModel());

            //Assert
            result.Should().BeOfType <ViewResult>();
            var model = result.As <ViewResult>().Model;

            model.Should().BeOfType <StationFormModel>();
            var form = model.As <StationFormModel>();

            form.IsEdit.Should().Be(false);
            form.Name.Should().Be(StationName);
            form.Phone.Should().Be(StationPhone);
            form.TownId.Should().Be(StationTownId);
            this.customMessage.Should().Be(string.Format(WebConstants.Message.StationAlreadyExists, form.Name, Town));
        }
Ejemplo n.º 5
0
        public void All_PageGreaterThanTotalPagesShouldRedirectToAllStations()
        {
            const int InvalidPageSize = 10;

            //Arrange
            var controller = new AdminStationsController(null, this.stationService.Object);

            this.stationService.Setup(s => s.All(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()))
            .Returns(this.GetStations());

            this.stationService.Setup(s => s.TotalStations(It.IsAny <string>()))
            .Returns(TotalStations);

            //Act
            var result = controller.All(string.Empty, InvalidPageSize);

            //Assert
            result.Should().BeOfType <RedirectToActionResult>();
            var model = result.As <RedirectToActionResult>();

            model.ActionName.Should().Be(WebConstants.Action.AdminAllStations);
            model.RouteValues.Keys.Should().Contain(RouteValueKeyPage);
            model.RouteValues.Values.Should().Contain(MaxPageSize);
            model.RouteValues.Keys.Should().Contain(RouteValueKeySearchTerm);
            model.RouteValues.Values.Should().Contain(string.Empty);
        }
Ejemplo n.º 6
0
        public void Post_Add_WithValidDataShouldRedirectToAllStations()
        {
            //Arrange
            var controller = new AdminStationsController(this.townService.Object, this.stationService.Object);

            this.stationService.Setup(s => s.Add(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <string>()))
            .Returns(true);

            this.townService.Setup(t => t.GetTownNameById(It.IsAny <int>()))
            .Returns(Town);

            this.PrepareTempData();

            controller.TempData = this.tempData.Object;

            //Act
            var result = controller.Add(this.GetAddStationFormModel());

            //Assert
            result.Should().BeOfType <RedirectToActionResult>();
            var model = result.As <RedirectToActionResult>();

            model.ActionName.Should().Be(WebConstants.Action.AdminAllStations);
            this.customMessage.Should().Be(string.Format(WebConstants.Message.StationCreated, StationName, Town));
        }
Ejemplo n.º 7
0
        public void All_ShouldReturnCorrectDataWithValidInputAndParticularSearchTerm()
        {
            const string SearchTerm = "Test";

            //Arrange
            var controller = new AdminStationsController(null, this.stationService.Object);

            this.stationService.Setup(s => s.All(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()))
            .Returns(this.GetStations());

            this.stationService.Setup(s => s.TotalStations(It.IsAny <string>()))
            .Returns(TotalStations);

            //Act
            var result = controller.All(SearchTerm, MinPageSize);

            //Assert
            result.Should().BeOfType <ViewResult>();
            var model = result.As <ViewResult>().Model.As <AllStations>();

            model.Stations.Should().HaveCount(TotalStations);
            model.Pagination.SearchTerm.Should().Be(SearchTerm);
            model.Pagination.CurrentPage.Should().Be(MinPageSize);
            model.Pagination.NextPage.Should().Be(MaxPageSize);
            model.Pagination.PreviousPage.Should().Be(MinPageSize);
            model.Pagination.TotalElements.Should().Be(TotalStations);
        }
Ejemplo n.º 8
0
        public void All_ValidPageAndNoStationsShouldReturnCountZero()
        {
            //Arrange
            var controller = new AdminStationsController(null, this.stationService.Object);

            this.stationService.Setup(s => s.All(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()))
            .Returns(new List <AdminStationListingServiceModel>());

            this.stationService.Setup(s => s.TotalStations(It.IsAny <string>()))
            .Returns(0);

            //Act
            var result = controller.All(string.Empty, MinPageSize);

            //Assert
            result.Should().BeOfType <ViewResult>();
            var model = result.As <ViewResult>().Model.As <AllStations>();

            model.Stations.Should().HaveCount(0);
            model.Pagination.SearchTerm.Should().Be(string.Empty);
            model.Pagination.CurrentPage.Should().Be(MinPageSize);
            model.Pagination.NextPage.Should().Be(0);
            model.Pagination.PreviousPage.Should().Be(MinPageSize);
            model.Pagination.TotalElements.Should().Be(0);
        }
Ejemplo n.º 9
0
        public void ControllerShouldBeForAuthorizedUsersOnly()
        {
            //Arrange
            var controller = new AdminStationsController(null, null);

            //Act
            var attributes = controller.GetType().GetCustomAttributes(true);

            //Assert
            attributes.Any(a => a.GetType() == typeof(AuthorizeAttribute));
        }
Ejemplo n.º 10
0
        public void ControllerShouldBeForAdministratorsOnly()
        {
            //Arrange
            var controller = new AdminStationsController(null, null);

            //Act
            var attributes = controller.GetType().GetCustomAttributes(true);

            //Assert
            attributes.Any(a => a.GetType() == typeof(AuthorizeAttribute));
            var authorizeAttribute = attributes.FirstOrDefault(a => a.GetType() == typeof(AuthorizeAttribute)).As <AuthorizeAttribute>();

            authorizeAttribute.Roles.Should().Be(Role.Administrator.ToString());
        }
Ejemplo n.º 11
0
        public void ControllerShouldBeInAdminArea()
        {
            //Arrange
            var controller = new AdminStationsController(null, null);

            //Act
            var attributes = controller.GetType().GetCustomAttributes(true);

            //Assert
            attributes.Any(a => a.GetType() == typeof(AreaAttribute));
            var areaAttribute = attributes.FirstOrDefault(a => a.GetType() == typeof(AreaAttribute)).As <AreaAttribute>();

            areaAttribute.RouteValue.Should().Be(WebConstants.Area.Admin);
        }
Ejemplo n.º 12
0
        public void Get_Add_ShouldReturnView()
        {
            //Arrange
            var controller = new AdminStationsController(this.townService.Object, this.stationService.Object);

            //Act
            var result = controller.Add();

            //Assert
            result.Should().BeOfType <ViewResult>();
            var model = result.As <ViewResult>().Model;

            model.Should().BeOfType <StationFormModel>();
        }
Ejemplo n.º 13
0
        public void All_WithPageLessOrEqualToZeroShouldRedirectToAllStations(int page)
        {
            //Arrange
            var controller = new AdminStationsController(null, null);

            //Act
            var result = controller.All(string.Empty, page);

            //Assert
            result.Should().BeOfType <RedirectToActionResult>();
            var model = result.As <RedirectToActionResult>();

            model.ActionName.Should().Be(WebConstants.Action.AdminAllStations);
            model.RouteValues.Keys.Should().Contain(RouteValueKeyPage);
            model.RouteValues.Values.Should().Contain(MinPageSize);
            model.RouteValues.Keys.Should().Contain(RouteValueKeySearchTerm);
            model.RouteValues.Values.Should().Contain(string.Empty);
        }
Ejemplo n.º 14
0
        public void Post_Add_WithInvalidModelStateShouldReturnView()
        {
            //Arrange
            var controller = new AdminStationsController(this.townService.Object, this.stationService.Object);

            controller.ModelState.AddModelError(string.Empty, "Error");

            //Act
            var result = controller.Add(this.GetAddStationFormModel());

            //Assert
            result.Should().BeOfType <ViewResult>();
            var model = result.As <ViewResult>().Model;

            model.Should().BeOfType <StationFormModel>();
            var form = model.As <StationFormModel>();

            form.IsEdit.Should().Be(false);
            form.Name.Should().Be(StationName);
            form.Phone.Should().Be(StationPhone);
            form.TownId.Should().Be(StationTownId);
        }
Ejemplo n.º 15
0
        public void Post_Edit_WithNonExistingStationShouldRedirectToAllTowns()
        {
            //Arrange
            var controller = new AdminStationsController(null, this.stationService.Object);

            this.stationService.Setup(s => s.StationExists(It.IsAny <int>()))
            .Returns(false);

            this.PrepareTempData();

            controller.TempData = this.tempData.Object;

            //Act
            var result = controller.Edit(this.GetEditStationFormModel());

            //Assert
            result.Should().BeOfType <RedirectResult>();
            var model = result.As <RedirectResult>();

            model.Url.Should().Be(WebConstants.Routing.AdminAllTownsUrl);
            this.customMessage.Should().Be(string.Format(WebConstants.Message.NonExistingEntity, nameof(WebConstants.Entity.Station), EditStationId));
        }
Ejemplo n.º 16
0
        public void Get_Edit_WithInvalidStationIdShouldRedirectToAllStations()
        {
            //Arrange
            AdminStationEditServiceModel station = null;
            var controller = new AdminStationsController(null, this.stationService.Object);

            this.stationService.Setup(s => s.GetStationToEdit(It.IsAny <int>()))
            .Returns(station);

            this.PrepareTempData();

            controller.TempData = this.tempData.Object;

            //Act
            var result = controller.Edit(EditStationId);

            //Assert
            result.Should().BeOfType <RedirectToActionResult>();
            var model = result.As <RedirectToActionResult>();

            model.ActionName.Should().Be(WebConstants.Action.AdminAllStations);
            this.customMessage.Should().Be(string.Format(WebConstants.Message.NonExistingEntity, nameof(WebConstants.Entity.Station), EditStationId));
        }
Ejemplo n.º 17
0
        public void Get_Edit_WithValidStationIdShouldReturnView()
        {
            //Arrange
            var controller = new AdminStationsController(this.townService.Object, this.stationService.Object);

            this.stationService.Setup(s => s.GetStationToEdit(It.IsAny <int>()))
            .Returns(this.GetStationToEdit());

            //Act
            var result = controller.Edit(EditStationId);

            //Assert
            result.Should().BeOfType <ViewResult>();
            var model = result.As <ViewResult>().Model;

            model.Should().BeOfType <StationFormModel>();
            var form = model.As <StationFormModel>();

            form.Id.Should().Be(EditStationId);
            form.Name.Should().Be(StationName);
            form.Phone.Should().Be(StationPhone);
            form.TownId.Should().Be(StationTownId);
            form.IsEdit.Should().BeTrue();
        }