public async Task GetFile_ImproperGuid_FormatException()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files")
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            var actionResult = await controller.GetFileContent("333333333!@#@!@!@!33333333333333333333333");

            System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
            HttpResponseMessage response = await actionResult.ExecuteAsync(cancellationToken);

            // Assert
            Assert.IsTrue(response.StatusCode == HttpStatusCode.BadRequest);
        }
Ejemplo n.º 2
0
        public void DeleteFile_FileNotFound()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            moq.Setup(t => t.DeleteFile(It.IsAny <Guid>(), It.IsAny <DateTime?>())).Returns(Task.FromResult((Guid?)null));

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files")
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Assert
            var result = controller.DeleteFile(Guid.NewGuid().ToString("N"));

            // Act
            Assert.IsInstanceOfType(result.Result, typeof(NotFoundResult), "Result should be NotFound");
        }
Ejemplo n.º 3
0
        public async Task HeadFile_UnknownException_InternalServerErrorFailure()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();

            moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).Throws(new Exception());
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files"),
                    Method     = HttpMethod.Head
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            var actionResult = await controller.GetFileHead("33333333333333333333333333333333");

            System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
            HttpResponseMessage response = await actionResult.ExecuteAsync(cancellationToken);

            // Assert
            Assert.IsTrue(response.StatusCode == HttpStatusCode.InternalServerError);
        }
Ejemplo n.º 4
0
        public async Task MakePermanent_Returns_NotFound()
        {
            var guid = Guid.NewGuid();
            var filesRepositoryMock  = new Mock <IFilesRepository>();
            var streamRepositoryMock = new Mock <IFileStreamRepository>();
            var configRepositoryMock = new Mock <IConfigRepository>();
            var logRepository        = new Mock <sl.IServiceLogRepository>();

            filesRepositoryMock.Setup(m => m.MakeFilePermanent(It.IsAny <Guid>())).ReturnsAsync(0);

            var controller = new FilesController(filesRepositoryMock.Object,
                                                 streamRepositoryMock.Object,
                                                 configRepositoryMock.Object,
                                                 logRepository.Object);

            controller.Request = new HttpRequestMessage
            {
                RequestUri = new Uri("http://localhost/files")
            };

            // Act
            var actionResult = await controller.MakePermanent(guid.ToString());

            var cancellationToken = new CancellationToken();
            var response          = actionResult.ExecuteAsync(cancellationToken).Result;

            // Assert
            Assert.IsTrue(response.StatusCode == HttpStatusCode.NotFound);
        }
Ejemplo n.º 5
0
        public async Task MakePermanent_ExceptionThrows_BadRequest()
        {
            var guid = "broken-guid";
            var filesRepositoryMock  = new Mock <IFilesRepository>();
            var streamRepositoryMock = new Mock <IFileStreamRepository>();
            var configRepositoryMock = new Mock <IConfigRepository>();
            var logRepository        = new Mock <sl.IServiceLogRepository>();

            var controller = new FilesController(filesRepositoryMock.Object,
                                                 streamRepositoryMock.Object,
                                                 configRepositoryMock.Object,
                                                 logRepository.Object);

            controller.Request = new HttpRequestMessage
            {
                RequestUri = new Uri("http://localhost/files")
            };

            // Act
            var actionResult = await controller.MakePermanent(guid.ToString());

            var cancellationToken = new CancellationToken();
            var response          = actionResult.ExecuteAsync(cancellationToken).Result;

            // Assert
            Assert.IsTrue(response.StatusCode == HttpStatusCode.BadRequest);
        }
Ejemplo n.º 6
0
        public void DeleteFile_FileFoundHFutureDateSupplied_Successfull()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();
            var guid = Guid.NewGuid();

            moq.Setup(t => t.DeleteFile(It.IsAny <Guid>(), It.IsAny <DateTime?>())).Returns(Task.FromResult((Guid?)guid));

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files")
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Assert
            var result = controller.DeleteFile(guid.ToString("N"), DateTime.Now.AddDays(10));

            // Act
            var okNegotiatedContentResult = result.Result as OkNegotiatedContentResult <string>;

            Assert.IsNotNull(okNegotiatedContentResult);
            Assert.AreEqual(guid.ToString("N"), okNegotiatedContentResult.Content, "Guid returned is not same as supplied");
        }
Ejemplo n.º 7
0
        public void DeleteFile_FormatException_BadRequest()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            moq.Setup(t => t.DeleteFile(It.IsAny <Guid>(), DateTime.UtcNow)).Throws(new FormatException());

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files")
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });


            // ct
            var result = controller.DeleteFile("");

            // Assert
            Assert.IsInstanceOfType(result.Result, typeof(BadRequestResult), "Result should be BadRequestResult");
        }
        public async Task GetFile_FileAlreadyExpiredBeforeNow_NotFound()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();
            var contentString     = "Test2 content";
            var fileChunk         = new FileChunk()
            {
                ChunkNum     = 1,
                ChunkContent = Encoding.UTF8.GetBytes(contentString),
                ChunkSize    = Encoding.UTF8.GetBytes(contentString).Length
            };
            var file = new File
            {
                FileId      = new Guid("22222222-2222-2222-2222-222222222222"),
                FileName    = "Test2.txt",
                StoredTime  = I18NHelper.DateTimeParseExactInvariant("2015-09-05T22:57:31.7824054-04:00", "o"),
                FileType    = "application/octet-stream",
                FileSize    = fileChunk.ChunkSize,
                ChunkCount  = 1,
                ExpiredTime = DateTime.UtcNow
            };

            var moqDbConnection = new Mock <DbConnection>();

            moq.Setup(t => t.CreateConnection()).Returns(moqDbConnection.Object);

            moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync(file);
            moq.Setup(t => t.GetFileInfo(It.IsAny <Guid>())).Returns(file);

            moq.Setup(t => t.ReadChunkContent(moqDbConnection.Object, file.FileId, 1)).Returns(fileChunk.ChunkContent);

            moqConfigRepo.Setup(t => t.FileChunkSize).Returns(1 * 1024 * 1024);

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files")
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            var actionResult = await controller.GetFileContent("22222222222222222222222222222222");

            System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
            HttpResponseMessage response = await actionResult.ExecuteAsync(cancellationToken);

            // Assert
            Assert.IsTrue(response.StatusCode == HttpStatusCode.NotFound, "Expired file should return status code as NotFound");
        }
Ejemplo n.º 9
0
        public async Task PutFile_FileChunkCount_Correct()
        {
            // Arrange
            HttpContext.Current = new HttpContext(
                new HttpRequest("", "http://tempuri.org", ""),
                new HttpResponse(new StringWriter()));

            var guid = Guid.NewGuid();
            var moq  = new Mock <IFilesRepository>();
            var moqFileStreamRepo     = new Mock <IFileStreamRepository>();
            var moqConfigRepo         = new Mock <IConfigRepository>();
            var moqHttpContextWrapper = new Mock <HttpContextWrapper>(HttpContext.Current);
            var moqLog = new Mock <sl.IServiceLogRepository>();
            var file   = new File {
                ChunkCount = 1, FileId = guid
            };
            var         paramFileChunk = new FileChunk();
            var         httpContent    = "my file";
            HttpContent content        = new ByteArrayContent(Encoding.UTF8.GetBytes(httpContent));
            var         stream         = await content.ReadAsStreamAsync();

            moq.Setup(t => t.PostFileHead(It.IsAny <File>())).ReturnsAsync(guid);
            moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync(file);
            moq.Setup(t => t.PostFileChunk(It.IsAny <FileChunk>()))
            .Callback <FileChunk>(chunk => paramFileChunk = chunk).
            ReturnsAsync(3);
            moq.Setup(t => t.UpdateFileHead(It.IsAny <Guid>(), It.IsAny <long>(), It.IsAny <int>())).Returns(Task.FromResult(0));

            moqHttpContextWrapper.Setup(c => c.Request.GetBufferlessInputStream()).Returns(stream);

            moqConfigRepo.Setup(t => t.FileChunkSize).Returns(DefaultChunkSize);

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files"),
                    Content    = content
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            // 1. Upload file
            var response = await controller.PutFileHttpContext(guid.ToString(), moqHttpContextWrapper.Object);


            // Assert
            Assert.IsTrue(response.Status == HttpStatusCode.OK);
            Assert.IsTrue(paramFileChunk.ChunkNum == file.ChunkCount + 2);
        }
Ejemplo n.º 10
0
        public async Task HeadFile_GetHeadForExistentFileStreamFile_Success()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            var file = new File
            {
                FileId       = new Guid("33333333-3333-3333-3333-333333333333"),
                FileName     = "Test3.txt",
                StoredTime   = I18NHelper.DateTimeParseExactInvariant("2015-09-05T22:57:31.7824054-04:00", "o"),
                FileType     = "text/html",
                ChunkCount   = 1,
                IsLegacyFile = true
            };

            moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync((File)null);

            moqFileStreamRepo.Setup(t => t.FileExists(file.FileId)).Returns(true);
            moqFileStreamRepo.Setup(t => t.GetFileHead(file.FileId)).Returns(file);

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files"),
                    Method     = HttpMethod.Head
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            var actionResult = await controller.GetFileHead("33333333333333333333333333333333");

            System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
            HttpResponseMessage response = await actionResult.ExecuteAsync(cancellationToken);

            var content     = response.Content;
            var contentType = content.Headers.ContentType;
            var fileName    = content.Headers.ContentDisposition.FileName;
            var storedTime  = response.Headers.GetValues("Stored-Date");

            // Assert
            Assert.IsTrue(response.IsSuccessStatusCode);
            Assert.AreEqual("Test3.txt", fileName);
            Assert.AreEqual("application/octet-stream", contentType.MediaType, string.Format("Returned content type {0} does not match expected {1}", contentType, "text/html"));
            Assert.AreEqual(DateTime.Parse("2015-09-05T22:57:31.7824054-04:00"), DateTime.Parse(storedTime.First()));
        }
Ejemplo n.º 11
0
        public async Task PostFile_MultipartMultipleFiles_BadRequestFailure()
        {
            // Arrange
            var guid = Guid.NewGuid();
            var moq  = new Mock <IFilesRepository>();

            moq.Setup(t => t.PostFileHead(It.IsAny <FsFile>())).ReturnsAsync(guid);
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            string fileName4Upload    = "\"UploadTest.txt\"";
            string fileContent4Upload = "This is the content of the uploaded test file";

            var multiPartContent1 = new MultipartFormDataContent("----MyGreatBoundary");
            ByteArrayContent byteArrayContent1 = new ByteArrayContent(Encoding.UTF8.GetBytes(fileContent4Upload));
            ByteArrayContent byteArrayContent2 = new ByteArrayContent(Encoding.UTF8.GetBytes(fileContent4Upload));

            byteArrayContent1.Headers.Add("Content-Type", "multipart/form-data");
            multiPartContent1.Add(byteArrayContent1, "this is the name of the content", fileName4Upload);
            multiPartContent1.Add(byteArrayContent2, "this is the name of the content", fileName4Upload);

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files"),
                    Content    = multiPartContent1
                },
                Configuration = new HttpConfiguration()
            };

            var context = await SetupMultipartPost(multiPartContent1);


            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            // 1. Upload file
            var response = await controller.PostFileHttpContext(context.Object, null);

            // Assert
            Assert.IsTrue(response.Status == HttpStatusCode.BadRequest);
        }
Ejemplo n.º 12
0
        public async Task PutFile_ExceptionThrows_InternalServerError()
        {
            var guid = Guid.NewGuid();
            var moq  = new Mock <IFilesRepository>();
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();
            var httpContent       = "my file";

            moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).Throws(new Exception());
            HttpContent content = new ByteArrayContent(Encoding.UTF8.GetBytes(httpContent));

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files"),
                    Content    = content
                },
                Configuration = new HttpConfiguration()
            };

            HttpContext.Current = new HttpContext(
                new HttpRequest("", "http://tempuri.org", ""),
                new HttpResponse(new StringWriter()));


            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            // 1. Upload file
            var actionResult = await controller.PutFile(guid.ToString());

            // Assert
            System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
            HttpResponseMessage response = actionResult.ExecuteAsync(cancellationToken).Result;

            // Assert
            Assert.IsTrue(response.StatusCode == HttpStatusCode.InternalServerError);
        }
Ejemplo n.º 13
0
        public async Task PostFile_NonMultipartDateTimeExpired_BadRequestFailure()
        {
            // Arrange
            var guid = Guid.NewGuid();
            var moq  = new Mock <IFilesRepository>();

            moq.Setup(t => t.PostFileHead(It.IsAny <FsFile>())).ReturnsAsync(guid);
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            var httpContent = new StringContent("my file");
            var controller  = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files"),
                    Content    = httpContent
                },
                Configuration = new HttpConfiguration()
            };

            HttpContext.Current = new HttpContext(
                new HttpRequest("", "http://tempuri.org", ""),
                new HttpResponse(new StringWriter()));

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            // 1. Upload file
            var actionResult = await controller.PostFile(DateTime.Now);

            // Assert
            System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
            HttpResponseMessage response = await actionResult.ExecuteAsync(cancellationToken);

            // Assert
            Assert.IsTrue(response.StatusCode == HttpStatusCode.BadRequest);
        }
Ejemplo n.º 14
0
        public async Task PostFile_MultipartRepoThrowsException()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();

            moq.Setup(t => t.PostFileHead(It.IsAny <FsFile>())).Throws(new Exception());
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            string fileName4Upload    = "\"UploadTest.txt\"";
            string fileContent4Upload = "This is the content of the uploaded test file";

            MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary");
            ByteArrayContent         byteArrayContent = new ByteArrayContent(Encoding.UTF8.GetBytes(fileContent4Upload));

            byteArrayContent.Headers.Add("Content-Type", "multipart/form-data");
            multiPartContent.Add(byteArrayContent, "this is the name of the content", fileName4Upload);

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files"),
                    Content    = multiPartContent
                },
                Configuration = new HttpConfiguration()
            };

            var context = await SetupMultipartPost(multiPartContent);


            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            // 1. Upload file
            await controller.PostFileHttpContext(context.Object, null);
        }
Ejemplo n.º 15
0
        public async Task GetFile_NoFileRetrievedEmptyName_Failure()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            File file = new File();

            moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync((File)null);

            // #DEBUG
            // moqFileStreamRepo.Setup(m => m.GetFileContent(It.IsAny<Guid>())).Returns((System.IO.Stream)null);


            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files")
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            var actionResult = controller.GetFileContent("22222222222222222222222222222222").Result;

            System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
            HttpResponseMessage response = await actionResult.ExecuteAsync(cancellationToken);

            // Assert
            Assert.IsTrue(response.StatusCode == HttpStatusCode.NotFound);
        }
Ejemplo n.º 16
0
        public async Task PutFile_FileNotFound()
        {
            // Arrange
            var guid = Guid.NewGuid();
            var moq  = new Mock <IFilesRepository>();
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();
            var httpContent       = new StringContent("my file");

            moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync((File)null);
            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files"),
                    Content    = httpContent
                },
                Configuration = new HttpConfiguration()
            };

            HttpContext.Current = new HttpContext(
                new HttpRequest("", "http://tempuri.org", ""),
                new HttpResponse(new StringWriter()));


            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            // 1. Upload file
            var response = await controller.PutFileHttpContext(guid.ToString(), new HttpContextWrapper(HttpContext.Current));


            // Assert
            Assert.IsTrue(response.Status == HttpStatusCode.NotFound);
        }
Ejemplo n.º 17
0
        public async Task GetFile_ProperRequestFileStream_Success()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();
            var contentString     = "Test2 content";
            var fileChunk         = new FileChunk()
            {
                ChunkNum     = 1,
                ChunkContent = Encoding.UTF8.GetBytes(contentString),
                ChunkSize    = Encoding.UTF8.GetBytes(contentString).Length
            };
            var file = new File
            {
                FileId       = new Guid("22222222-2222-2222-2222-222222222222"),
                FileName     = "Test2.txt",
                StoredTime   = I18NHelper.DateTimeParseExactInvariant("2015-09-05T22:57:31.7824054-04:00", "o"),
                FileType     = "application/octet-stream",
                FileSize     = fileChunk.ChunkSize,
                ChunkCount   = 1,
                IsLegacyFile = true
            };

            var moqDbConnection = new Mock <DbConnection>();

            moqConfigRepo.Setup(t => t.FileChunkSize).Returns(1 * 1024 * 1024);
            moqConfigRepo.Setup(t => t.LegacyFileChunkSize).Returns(1);

            moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync((File)null);
            moq.Setup(t => t.GetFileInfo(It.IsAny <Guid>())).Returns(file);
            moq.Setup(t => t.ReadChunkContent(moqDbConnection.Object, file.FileId, 1)).Returns(fileChunk.ChunkContent);

            moqFileStreamRepo.Setup(t => t.CreateConnection()).Returns(moqDbConnection.Object);
            moqFileStreamRepo.Setup(t => t.FileExists(It.IsAny <Guid>())).Returns(true);
            moqFileStreamRepo.Setup(t => t.GetFileHead(It.IsAny <Guid>())).Returns(file);
            moqFileStreamRepo.Setup(t => t.ReadChunkContent(moqDbConnection.Object, file.FileId, 1, 0)).Returns(fileChunk.ChunkContent);

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files")
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            var actionResult = await controller.GetFileContent("22222222222222222222222222222222");

            System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
            HttpResponseMessage response = await actionResult.ExecuteAsync(cancellationToken);

            var content = response.Content;

            var contentType = content.Headers.ContentType;
            var fileName    = content.Headers.ContentDisposition.FileName;
            var storedTime  = response.Headers.GetValues("Stored-Date");

            var fileContent = await content.ReadAsStringAsync();

            // Assert
            Assert.IsTrue(response.IsSuccessStatusCode);
            Assert.AreEqual("Test2.txt", fileName);
            Assert.AreEqual("application/octet-stream", contentType.MediaType);
            Assert.AreEqual("Test2 content", fileContent, "Improper content was returned");
            Assert.AreEqual(DateTime.Parse("2015-09-05T22:57:31.7824054-04:00"), DateTime.Parse(storedTime.First()));
        }