def test_api_upload_bytes_invalid(self):
        '''Users should get a useful reply when a uploaded book is invalid and cannot be added.'''
        name = 'invalid-no-title.epub'
        assert library_models.EpubArchive.objects.filter(name=name).count() == 0
        response = self.client.post('/api/documents/', { 'api_key': self.userpref.get_api_key(),
                                                         'epub_data': helper.get_filehandle(name) })        

        self._wellformed(response, UPLOAD_STATUS_CODE_UNACCEPTABLE)

        # We should still have zero documents
        assert library_models.EpubArchive.objects.filter(name=name).count() == 0
    def test_api_upload_bytes(self):
        '''Users should be able to upload a document by sending a stream of epub bytes'''
        name = 'Pride-and-Prejudice_Jane-Austen.epub'
        assert library_models.EpubArchive.objects.filter(name=name).count() == 0
        response = self.client.post('/api/documents/', { 'api_key': self.userpref.get_api_key(),
                                                         'epub_data': helper.get_filehandle(name) })        
        assert response.status_code == UPLOAD_STATUS_CODE
        assert library_models.EpubArchive.objects.filter(name=name).count() == 1

        # Check that it's in their API list now
        response = self.client.get('/api/documents/', { 'api_key': self.userpref.get_api_key()})
        assert 'Pride and Prejudice' in response.content

        # Check that it's in their Bookworm site library too
        self._login()
        response = self.client.get('/library/')
        assert 'Pride and Prejudice' in response.content
 def _upload(self, f):
     self._login()
     fh = helper.get_filehandle(f)
     response = self.client.post('/upload/', {'epub':fh})
     return response