def test_upload_start_new_files(self):
        """ Tests upload_start on new files """
        import os

        request = self.factory.post(
            '/plupload/',
            {'model': 'test.IssueSubmission', 'pk': 2, 'name': 'test.png'}
        )

        with mock.patch('os.makedirs', mock.MagicMock(spec=os.makedirs)):

            upload_file(request)

            resumable_file_count = ResumableFile.objects.filter(
                path=path_for_upload(
                    "test.IssueSubmission",
                    "2",
                    "test.png",
                ),
                status=ResumableFileStatus.NEW
            ).count()

            self.assertEquals(
                resumable_file_count,
                1,
                "A ResumableFile should have been created"
            )
    def test_upload_error(self):
        """ Test that posting to upload_error update the model """
        import os

        request = self.factory.post(
            '/plupload/',
            {'model': 'test.IssueSubmission', 'pk': 1, 'name': 'test.png'}
        )

        with mock.patch('os.makedirs', mock.MagicMock(spec=os.makedirs)):
            upload_file(request)

        request = self.factory.post(
            'plupload/upload_error',
            {'model': 'test.IssueSubmission', 'pk': 1, 'name': 'test.png'}
        )

        response = upload_error(request)
        resumable_file_count = ResumableFile.objects.filter(
            path=path_for_upload(
                "test.IssueSubmission",
                "1",
                "test.png",
            ),
            status=ResumableFileStatus.ERROR
        ).count()

        self.assertEquals(
            resumable_file_count,
            1,
            "The ResumableFile should have failed"
        )
    def test_create_file(self):
        """ Test that files are created when no chunk is sent """
        request = self.factory.post(
            '/plupload/',
            {
                'model': 'test.IssueSubmission',
                'pk': 1,
                'name': 'test.png',
                "chunk": 0
            }
        )

        request.FILES['file'] = mock.MagicMock(spec=UploadedFile)

        with mock.patch("builtins.open", mock.MagicMock()) as mock_file:
            upload_file(request)

        mock_file.assert_called_once_with(
            path_for_upload('test.IssueSubmission', '1', 'test.png'),
            'wb'
        )
    def test_upload_file_raises_400_when_malformed(self):
        request = self.factory.post(
            '/plupload/',
            {'model': 'test.IssueSubmission', 'pk': 1, 'name': 'test.png'}
        )

        result = upload_file(request)

        self.assertEquals(
            result,
            HttpResponseBadRequest,
            "A request with no file should return an HttpBadRequest"
        )