Beispiel #1
0
        public async Task DeleteTest()
        {
            DocumentsController.DocFolder = "__DocContTest/";
            Directory.CreateDirectory(DocumentsController.DocFolder + "1/DEL/");

            UserContext _context = GetContext();

            _context.Document.Add(new Document()
            {
                Id = "DEL", Title = "DEL", File = "1/DEL/.pdf", UserId = users[2].Id, GroupId = 1
            });
            await _context.SaveChangesAsync();

            LogOut();
            JsonResult res = await controller.apiDelete("DELDFHdjghfdgdfgjk");

            Assert.AreEqual(JsonError.ERROR_NOT_FOUND, res.Value);
            res = await controller.apiDelete("DEL");

            Assert.AreEqual(JsonError.ERROR_ACCESS_DENIED, res.Value);
            LogIn(users[3]);
            res = await controller.apiDelete("DEL");

            Assert.AreEqual(JsonError.ERROR_ACCESS_DENIED, res.Value);
            LogIn(users[2]);
            res = await controller.apiDelete("DEL");

            Assert.AreEqual(JsonError.OK, res.Value);
            // Check folder
            Assert.False(Directory.Exists(DocumentsController.DocFolder + "1/DEL/"));
            // after that not found
            Assert.Null(_context.Document.Find("DEL"));
            // Now add bookmark to document and try to delete
            Directory.CreateDirectory(DocumentsController.DocFolder + "1/DEL/");
            _context.Document.Add(new Document()
            {
                Id = "DEL", Title = "DEL", File = "1/DEL/.pdf", UserId = users[2].Id, GroupId = 1
            });
            Bookmark bm = new Bookmark()
            {
                DocumentId = "DEL", Name = "TEST", UserId = users[3].Id
            };

            _context.Bookmark.Add(bm);
            await _context.SaveChangesAsync();

            res = await controller.apiDelete("DEL");

            Assert.AreEqual(JsonError.OK, res.Value);
            Assert.True(Directory.Exists(DocumentsController.DocFolder + "1/DEL/"));
            Assert.NotNull(_context.Document.Find("DEL"));
            // now delete bookmark via BookmarksController
            LogIn(users[3]);
            BookmarksController bmc = new BookmarksController(_context, smngrMoq.Object);

            SetupController(bmc);
            await bmc.apiDelete(bm.Id);

            Assert.Null(_context.Bookmark.Find(bm.Id));
            Assert.False(Directory.Exists(DocumentsController.DocFolder + "1/DEL/"));
            Assert.Null(_context.Document.Find("DEL"));
            // Check delete from group
            Directory.CreateDirectory(DocumentsController.DocFolder + "1/DEL/");
            _context.Document.Add(new Document()
            {
                Id = "DEL", Title = "DEL", File = "1/DEL/.pdf", UserId = users[2].Id, GroupId = 1
            });
            await _context.SaveChangesAsync();

            // user[1] is groups admin, he can only "hide" document from group
            LogIn(users[1]);
            res = await controller.apiDelete("DEL");

            Assert.AreEqual(JsonError.OK, res.Value);
            var doc = _context.Document.Find("DEL");

            Assert.NotNull(doc);
            Assert.AreEqual(Document.DocStatusEnum.Invisible, doc.DocStatus);
            // check messages
            LogIn(users[2]);
            res = await(new MessagesController(_context, umngr)).apiIndex();
            Assert.AreEqual(Message.MessageType.DocRemoved, ((List <MessageModel>)res.Value)[0].Type);

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