public async Task TestLockFileAndExpiration()
        {
            var fileRepository = new WopiFileRepository();

            using (var fileStream = new FileStream(fileName, FileMode.Open))
            {
                var fileName = Path.GetFileName(this.fileName);
                var wopiFile = await fileRepository.AddFile(tenant1User1, tenant1, fileStream, fileName);

                // lock for 3 seconds
                var lockResponse = await fileRepository.LockFile(wopiFile.FileId, tenant1User1, lockId, null, 0.05);

                Assert.AreEqual(HttpStatusCode.OK, lockResponse.Item1);
                using (var wopiContext = new WopiContext())
                {
                    var file = await wopiContext.Files.Where(f => f.FileId == wopiFile.FileId).FirstOrDefaultAsync();

                    Assert.AreEqual(lockId, file.LockValue);
                    Assert.IsTrue(file.LockExpires.Value > DateTime.UtcNow);
                }

                // wait 5 seconds for lock to expire
                await Task.Delay(5000);

                var response2 = await fileRepository.GetLockStatus(wopiFile.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, response2.Item1);

                var deleteResponse = await fileRepository.DeleteFile(wopiFile.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, deleteResponse);
            }
        }
        public async Task TestRename()
        {
            var fileRepository = new WopiFileRepository();
            var fileName       = Path.GetFileName(this.fileName);

            using (var fileStream = new FileStream(this.fileName, FileMode.Open))
            {
                var wopiFile = await fileRepository.AddFile(tenant1User1, tenant1, fileStream, fileName);

                var result = await fileRepository.RenameFile(wopiFile.FileId, tenant1User1, lockId, renamedFileName);

                Assert.AreEqual(HttpStatusCode.OK, result);
                using (var wopiContext = new WopiContext())
                {
                    var file = await wopiContext.Files.Where(f => f.FileId == wopiFile.FileId).FirstOrDefaultAsync();

                    Assert.AreEqual(renamedFileName, Path.GetFileNameWithoutExtension(file.FileName));
                }
                var deleteResponse = await fileRepository.DeleteFile(wopiFile.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, deleteResponse);
            }

            //await userRepository.DeleteUser(tenant1User1);
        }
        public async Task TestAddFileGetFileDeleteFile()
        {
            var fileRepository = new WopiFileRepository();

            using (var fileStream = new FileStream(fileName, FileMode.Open))
            {
                var fileName  = Path.GetFileName(this.fileName);
                var wopiFile1 = await fileRepository.AddFile(tenant1User1, tenant1, fileStream, fileName);

                var wopiFile2 = await fileRepository.GetFileInfo(wopiFile1.FileId, tenant1User1);

                Assert.IsNotNull(wopiFile2);
                Assert.AreEqual(wopiFile1.FileId, wopiFile2.FileId);
                Assert.IsTrue(wopiFile2.FilePermissions.Count == 1);

                var contentResponse = await fileRepository.GetFileContent(wopiFile1.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, contentResponse.Item1);
                using (var stream = contentResponse.Item2)
                {
                    Assert.IsNotNull(stream);
                    Assert.IsTrue(stream.Length > 0);
                }

                var deleteResponse = await fileRepository.DeleteFile(wopiFile1.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, deleteResponse);
                using (var wopiContext = new WopiContext())
                {
                    var file = await wopiContext.Files.Where(f => f.FileId == wopiFile1.FileId).FirstOrDefaultAsync();

                    Assert.IsNull(file);
                    var filePermission = await wopiContext.FilePermissions.Where(f => f.FileId == wopiFile1.FileId).FirstOrDefaultAsync();

                    Assert.IsNull(filePermission);

                    contentResponse = await fileRepository.GetFileContent(wopiFile1.FileId, wopiFile1.Container);

                    Assert.AreEqual(HttpStatusCode.NotFound, contentResponse.Item1);
                    Assert.IsNull(contentResponse.Item2);
                }
            }
        }
        public async Task TestAddGetAndDeleteUser()
        {
            var      userRepository = new WopiUserRepository();
            WopiUser user;

            // Test AddUser
            await userRepository.AddUser(userId1, tenant1);

            using (var wopiContext = new WopiContext())
            {
                user = await wopiContext.Users.Where(u => u.UserId == userId1).FirstOrDefaultAsync();

                Assert.IsNotNull(user);
            }

            // Test GetUser
            user = await userRepository.GetUser(userId1);

            Assert.IsNotNull(user);

            // Test Add DuplicateUser
            try
            {
                await userRepository.AddUser(userId1, tenant1);

                Assert.Fail();
            }
            catch
            {
            }

            // Test Deleteuser
            await userRepository.DeleteUser(userId1);

            using (var wopiContext = new WopiContext())
            {
                user = await wopiContext.Users.Where(u => u.UserId == userId1).FirstOrDefaultAsync();

                Assert.IsNull(user);
            }
        }
        public async Task TestSaveWopiUserInfo()
        {
            var fileName = Path.GetFileName(this.fileName);

            using (var fileStream = new FileStream(this.fileName, FileMode.Open))
            {
                var fileRepository = new WopiFileRepository();

                var wopiFile = await fileRepository.AddFile(tenant1User1, tenant1, fileStream, fileName);

                await fileRepository.SaveWopiUserInfo(wopiFile.FileId, tenant1User1, wopiUserInfo);

                using (var wopiContext = new WopiContext())
                {
                    var filePermission = await wopiContext.FilePermissions.Where(fp => fp.UserId == tenant1User1 && fp.FileId == wopiFile.FileId).FirstOrDefaultAsync();

                    Assert.AreEqual(wopiUserInfo, filePermission.UserInfo);
                }

                var deleteResponse = await fileRepository.DeleteFile(wopiFile.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, deleteResponse);
            }
        }
        public async Task TestLockAndUnlockFile()
        {
            var fileRepository = new WopiFileRepository();

            using (var fileStream = new FileStream(fileName, FileMode.Open))
            {
                var fileName = Path.GetFileName(this.fileName);
                var wopiFile = await fileRepository.AddFile(tenant1User1, tenant1, fileStream, fileName);

                var lockResponse = await fileRepository.LockFile(wopiFile.FileId, tenant1User1, lockId, null, 0.10);

                Assert.AreEqual(HttpStatusCode.OK, lockResponse.Item1);

                using (var wopiContext = new WopiContext())
                {
                    var file = await wopiContext.Files.Where(f => f.FileId == wopiFile.FileId).FirstOrDefaultAsync();

                    Assert.AreEqual(lockId, file.LockValue);
                    Assert.IsTrue(file.LockExpires.Value > DateTime.UtcNow);
                }

                var unlockResponse = await fileRepository.UnlockFile(wopiFile.FileId, tenant1User1, lockId);

                Assert.AreEqual(HttpStatusCode.OK, unlockResponse.Item1);

                using (var wopiContext = new WopiContext())
                {
                    var file = await wopiContext.Files.Where(f => f.FileId == wopiFile.FileId).FirstOrDefaultAsync();

                    Assert.IsNull(file.LockValue);
                }
                var deleteResponse = await fileRepository.DeleteFile(wopiFile.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, deleteResponse);
            }
        }