public void WriteResponse_Should_Throw_404_Exception_For_Bad_Application_Path()
		{
			// Arrange
			AttachmentFileHandler handler = new AttachmentFileHandler(_applicationSettings,_fileService);

			string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Unit", "Attachments", "afile.jpg");
			File.WriteAllText(fullPath, "fake content");

			string localPath = "/wiki/Attachments/afile.jpg";
			string applicationPath = "/wookie";
			string modifiedSince = "";

			ResponseWrapperMock wrapper = new ResponseWrapperMock();

			try
			{
				// Act + Assert
				handler.WriteResponse(localPath, applicationPath, modifiedSince, wrapper, null);

				Assert.Fail("No 500 HttpException thrown");
			}
			catch (HttpException e)
			{
				Assert.That(e.GetHttpCode(), Is.EqualTo(404));
			}
		}
 /// <summary>
 /// Constructor for the file manager.
 /// </summary>
 /// <remarks>This action requires editor rights.</remarks>
 public FileManagerController(ApplicationSettings settings, UserServiceBase userManager, IUserContext context,
     SettingsService settingsService, AttachmentFileHandler attachment)
     : base(settings, userManager, context, settingsService)
 {
     _attachmentHandler = attachment;
     _attachmentPathUtil = new AttachmentPathUtil(settings);
 }
        public void TranslateLocalPathToFilePath(string localPath, string appPath, string expectedPath)
        {
            // Arrange
            _applicationSettings.AttachmentsFolder = @"C:\Attachments\";
            AttachmentFileHandler handler = new AttachmentFileHandler(_applicationSettings, _fileService);

            // Act
            string actualPath = handler.TranslateUrlPathToFilePath(localPath, appPath);

            // Assert
            Assert.That(actualPath, Is.EqualTo(expectedPath), "Failed with {0} {1} {2}", localPath, appPath, expectedPath);
        }
        public void TranslateLocalPathToFilePath_Should_Be_Case_Sensitive()
        {
            // Arrange
            _applicationSettings.AttachmentsFolder = @"C:\attachments\";
            AttachmentFileHandler handler = new AttachmentFileHandler(_applicationSettings, _fileService);

            // Act
            string actualPath = handler.TranslateUrlPathToFilePath("/Attachments/a.jpg", "/");

            // Assert
            Assert.That(actualPath, Is.Not.EqualTo(@"c:\Attachments\a.jpg"), "TranslateLocalPathToFilePath does a case sensitive url" +
                                                                             " replacement (this is for Apache compatibility");
        }
		public void Setup()
		{
			_container = new MocksAndStubsContainer();

			_applicationSettings = _container.ApplicationSettings;
			_context = _container.UserContext;
			_repository = _container.Repository;
			_pluginFactory = _container.PluginFactory;
			_settingsService = _container.SettingsService;
			_userService = _container.UserService;
			_historyService = _container.HistoryService;
			_pageService = _container.PageService;
			_attachmentFileHandler = new AttachmentFileHandler(_applicationSettings,_container.FileService);
			_fileService = _container.FileService as FileServiceMock;

			try
			{
				// Delete any existing attachments folder
				DirectoryInfo directoryInfo = new DirectoryInfo(_applicationSettings.AttachmentsFolder);
				if (directoryInfo.Exists)
				{
					directoryInfo.Attributes = FileAttributes.Normal;
					directoryInfo.Delete(true);
				}

				Directory.CreateDirectory(_applicationSettings.AttachmentsFolder);
			}
			catch (IOException e)
			{
				Assert.Fail("Unable to delete the attachments folder " + _applicationSettings.AttachmentsFolder + ", does it have a lock/explorer window open, or Mercurial open?" + e.ToString());
			}
			catch (ArgumentException e)
			{
				Assert.Fail("Unable to delete the attachments folder " + _applicationSettings.AttachmentsFolder + ", is EasyMercurial open?" + e.ToString());
			}

			_filesController = new FileManagerController(_applicationSettings, _userService, _context, _settingsService, _attachmentFileHandler, _fileService);
			_mvcMockContainer = _filesController.SetFakeControllerContext();
		}
		public void WriteResponse_Should_Throw_404_Exception_For_Missing_File()
		{
			// Arrange
			AttachmentFileHandler handler = new AttachmentFileHandler(_applicationSettings,_fileService);

			string localPath = "/wiki/Attachments/doesntexist404.jpg";
			string applicationPath = "/wiki";
			string modifiedSince = "";

			ResponseWrapperMock wrapper = new ResponseWrapperMock();

			try
			{
				// Act + Assert
				handler.WriteResponse(localPath, applicationPath, modifiedSince, wrapper, null);

				Assert.Fail("No 404 HttpException thrown");
			}
			catch (HttpException e)
			{
				Assert.That(e.GetHttpCode(), Is.EqualTo(404));
			}
		}
        public void WriteResponse_Should_Set_200_Status_And_MimeType_And_Write_Bytes()
        {
            // Arrange
            AttachmentFileHandler handler = new AttachmentFileHandler(_applicationSettings,_fileService);

            string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Unit", "Attachments", "afile.jpg");
            File.WriteAllText(fullPath, "fake content");
            byte[] expectedBytes = File.ReadAllBytes(fullPath);
            string expectedMimeType = "image/jpeg";

            string localPath = "/wiki/Attachments/afile.jpg";
            string applicationPath = "/wiki";
            string modifiedSince = "";

            ResponseWrapperMock wrapper = new ResponseWrapperMock();

            // Act
            handler.WriteResponse(localPath, applicationPath, modifiedSince, wrapper, null);

            // Assert
            Assert.That(wrapper.StatusCode, Is.EqualTo(200));
            Assert.That(wrapper.ContentType, Is.EqualTo(expectedMimeType));
            Assert.That(wrapper.Buffer, Is.EqualTo(expectedBytes));
        }