public async Task Create_ReturnsReDirectResult_When_ValidModelIsGiven()
        {
            // Arrange
            DocumentViewModel tempDocumentVM = new DocumentViewModel
            {
                DocumentName = "new DocumentViewModel"
            };


            ApplicationUser user = new ApplicationUser
            {
                Id = "ids"
            };
            Mock <IUserRepository> mockUserRepo = new Mock <IUserRepository>();
            HttpContext            temp         = null;

            mockUserRepo.Setup(repo => repo.getUserFromHttpContextAsync(temp))
            .ReturnsAsync(user);

            Mock <IDocumentRepository> mockDocumentRepo = new Mock <IDocumentRepository>();

            mockDocumentRepo.Setup(repo => repo.SaveDocumentToUser(tempDocumentVM, user))
            .ReturnsAsync(true);

            DocumentsController controller = new DocumentsController(mockUserRepo.Object,
                                                                     mockDocumentRepo.Object);

            // Act
            IActionResult result = await controller.Create(tempDocumentVM);

            // Assert
            RedirectToActionResult viewResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Equal("Index", viewResult.ActionName);
        }
Пример #2
0
        public void ShouldCreateNewDocument()
        {
            var testDocument = MakeNew(0);
            var response     = _docsController.Create(testDocument);
            var result       = Assert.IsType <OkObjectResult>(response.Result);

            Assert.NotNull(result.Value);
            var newDocument = Assert.IsType <Document>(result.Value);

            Assert.True(newDocument.Id > 0);
            Assert.Null(newDocument.FirstReadTime);
            Assert.Null(newDocument.UpdateTime);
            Assert.Null(newDocument.ArchiveTime);
            Assert.Equal(testDocument.Title, newDocument.Title);
            Assert.Equal(testDocument.Author, newDocument.Author);
        }
Пример #3
0
        public void CreatePostAction_SaveModel()
        {
            // arrange
            var                 mock       = new Mock <IDocRepository>();
            Document            doc        = new Document();
            DocumentsController controller = new DocumentsController(mock.Object);
            // act
            RedirectToRouteResult result = controller.Create(doc, null) as RedirectToRouteResult;

            // assert
            mock.Verify(a => a.Create(doc));
        }
Пример #4
0
        public void CreatePostAction_RedirectToIndexView()
        {
            // arrange
            string              expected   = "Index";
            var                 mock       = new Mock <IDocRepository>();
            Document            doc        = new Document();
            DocumentsController controller = new DocumentsController(mock.Object);
            // act
            RedirectToRouteResult result = controller.Create(doc, null) as RedirectToRouteResult;

            // assert
            Assert.IsNotNull(result);
            Assert.AreEqual(expected, result.RouteValues["action"]);
        }
Пример #5
0
        public void CreatePostAction_ModelError()
        {
            // arrange
            var                 mock       = new Mock <IDocRepository>();
            Document            doc        = new Document();
            DocumentsController controller = new DocumentsController(mock.Object);

            controller.ModelState.AddModelError("Name", "Название модели не установлено");
            // act
            ViewResult result = controller.Create(doc, null) as ViewResult;

            // assert
            Assert.IsNotNull(result);
        }
        public async Task Create_ReturnsViewResult_When_InValidModelIsGiven()
        {
            // Arrange
            Mock <IFormFile> mockResumeFormFile = new Mock <IFormFile>();

            mockResumeFormFile.Setup(file => file.Length)
            .Returns(0);


            DocumentViewModel tempDocumentVM = new DocumentViewModel
            {
                DocumentName = "new DocumentViewModel",
                Resume       = mockResumeFormFile.Object
            };


            ApplicationUser user = new ApplicationUser
            {
                Id = "ids"
            };
            Mock <IUserRepository> mockUserRepo = new Mock <IUserRepository>();
            HttpContext            temp         = null;

            mockUserRepo.Setup(repo => repo.getUserFromHttpContextAsync(temp))
            .ReturnsAsync(user);

            Mock <IDocumentRepository> mockDocumentRepo = new Mock <IDocumentRepository>();

            mockDocumentRepo.Setup(repo => repo.SaveDocumentToUser(tempDocumentVM, user))
            .ReturnsAsync(false);

            DocumentsController controller = new DocumentsController(mockUserRepo.Object,
                                                                     mockDocumentRepo.Object);

            // Act
            IActionResult result = await controller.Create(tempDocumentVM);

            // Assert
            ViewResult viewResult = Assert.IsType <ViewResult>(result);
        }
Пример #7
0
        public async Task CreateTest()
        {
            DocumentsController.DocFolder = "__DocContTest/";
            Directory.CreateDirectory(DocumentsController.DocFolder + "1/DEL/");
            UserContext _context = GetContext();

            // create GET
            LogIn(users[1]);
            Assert.IsInstanceOf <ViewResult>(await controller.Create((int?)null, ""));
            Assert.IsInstanceOf <ViewResult>(await controller.Create(1, ""));
            Assert.IsInstanceOf <ViewResult>(await controller.Create(2, ""));
            Assert.IsInstanceOf <ViewResult>(await controller.Create(3, ""));
            LogIn(users[2]);
            Assert.IsInstanceOf <ViewResult>(await controller.Create((int?)null, ""));
            Assert.IsInstanceOf <ForbidResult>(await controller.Create(1, ""));
            Assert.IsInstanceOf <ViewResult>(await controller.Create(2, ""));
            Assert.IsInstanceOf <ForbidResult>(await controller.Create(3, ""));

            // create POST
            LogIn(users[1]);
            DocModel newDoc = new DocModel()
            {
                TempFileName = "ABC.pdf", Title = "BAT", GroupId = null
            };

            controller.ModelState.AddModelError("Name", "Error");
            Assert.IsInstanceOf <ViewResult>(await controller.Create(newDoc, ""));
            MakeVerified(controller.ModelState);

            newDoc = new DocModel()
            {
                TempFileName = "ABC.xdf", Title = "BAT", GroupId = null
            };
            Assert.IsInstanceOf <ViewResult>(await controller.Create(newDoc, ""));
            Assert.False(controller.ModelState.IsValid);
            MakeVerified(controller.ModelState);

            newDoc = new DocModel()
            {
                TempFileName = "ABC.pdf", Title = "BAT", GroupId = null
            };
            this.Folder = "DOCCRTEST_TEMP/";
            DocumentsController.DocFolder = "DOCCRTEST_DOC/";
            Directory.Delete(DocumentsController.DocFolder, true);
            Directory.CreateDirectory(this.Folder);
            Directory.CreateDirectory(DocumentsController.DocFolder);
            File.WriteAllText(this.Folder + "ABC.pdf", "ABC");

            Assert.IsInstanceOf <RedirectToActionResult>(await controller.Create(newDoc, ""));
            Document doc = await _context.Document.FirstOrDefaultAsync(d => d.Title.Equals("BAT"));

            Assert.NotNull(doc);
            Assert.AreEqual(doc.UserId, users[1].Id);
            Assert.IsNull(doc.GroupId);
            _context.Document.Remove(doc);
            _context.SaveChanges();
            Directory.Delete(DocumentsController.DocFolder, true);
            Directory.CreateDirectory(DocumentsController.DocFolder);

            File.WriteAllText(this.Folder + "ABC.pdf", "ABC");
            newDoc = new DocModel()
            {
                TempFileName = "ABC.pdf", Title = "BATG", GroupId = 1
            };
            Assert.IsInstanceOf <RedirectToActionResult>(await controller.Create(newDoc, ""));
            doc = await _context.Document.FirstOrDefaultAsync(d => d.Title.Equals("BATG"));

            Assert.NotNull(doc);
            Assert.AreEqual(users[1].Id, doc.UserId);
            Assert.AreEqual(1, doc.GroupId);
            _context.Document.Remove(doc);
            _context.SaveChanges();
            // not allowed to post to group
            LogIn(users[2]);
            File.WriteAllText(this.Folder + "ABC.pdf", "ABC");
            Assert.IsInstanceOf <ForbidResult>(await controller.Create(newDoc, ""));

            Directory.Delete(this.Folder, true);
            Directory.Delete(DocumentsController.DocFolder, true);
        }