pull test fixes from Gluejar/master

pull/86/head
eric 2018-06-09 15:00:45 -04:00
parent d3b9cfaf3c
commit 6408c878c4
6 changed files with 71 additions and 42 deletions

View File

@ -1,10 +1,11 @@
import requests
import random import random
from django.conf import settings
from urllib import quote
from functools import partial from functools import partial
from urllib import quote
from xml.etree import ElementTree from xml.etree import ElementTree
import requests
from django.conf import settings
from django.apps import apps from django.apps import apps
from . exceptions import BooXtreamError from . exceptions import BooXtreamError
@ -34,10 +35,10 @@ class BooXtream(object):
if not apiuser: if not apiuser:
apiuser = settings.BOOXTREAM_API_USER apiuser = settings.BOOXTREAM_API_USER
self.endpoint = 'https://service.booxtream.com/' self.endpoint = 'https://service.booxtream.com/'
self.postrequest = partial(requests.post, timeout=timeout, auth=(apiuser,apikey)) self.postrequest = partial(requests.post, timeout=timeout, auth=(apiuser, apikey))
def platform(self, epubfile=None, epub=True, kf8mobi=False, **kwargs): def platform(self, epubfile=None, epub=True, kf8mobi=False, **kwargs):
""" Make an API request to BooXtream """ Make an API request to BooXtream
``self.apikey``, ``epubfile`` and the supplied ``kwargs``. ``self.apikey``, ``epubfile`` and the supplied ``kwargs``.
Attempts to deserialize the XML response and return the download link. Attempts to deserialize the XML response and return the download link.
@ -48,25 +49,25 @@ class BooXtream(object):
Boox = apps.get_model('booxtream', 'Boox') Boox = apps.get_model('booxtream', 'Boox')
url = self.endpoint + 'booxtream.xml' url = self.endpoint + 'booxtream.xml'
kwargs['epub'] = '1' if epub else '0' kwargs['epub'] = '1' if epub else '0'
kwargs['kf8mobi'] = '1' if kf8mobi else '0' kwargs['kf8mobi'] = '1' if kf8mobi else '0'
if epubfile: if epubfile:
if hasattr(epubfile,'name') and str(epubfile.name).endswith('.epub'): if hasattr(epubfile, 'name') and str(epubfile.name).endswith('.epub'):
files= {'epubfile': (str(epubfile.name),epubfile)} files = {'epubfile': (str(epubfile.name), epubfile)}
else: else:
# give it a random file name so that kindlegen doesn't choke # give it a random file name so that kindlegen doesn't choke
# needed for in-memory (StringIO) epubs # needed for in-memory (StringIO) epubs
files= {'epubfile': ('%012x.epub' % random.randrange(16**12),epubfile)} files= {'epubfile': ('%012x.epub' % random.randrange(16**12), epubfile)}
else: else:
files={} files = {}
if settings.LOCAL_TEST: if settings.LOCAL_TEST:
# fake it, so you can test other functions without hitting booxtream # fake it, so you can test other functions without hitting booxtream
boox = Boox.objects.create( boox = Boox.objects.create(
download_link_epub='https://github.com/eshellman/42_ebook/blob/master/download/42.epub?raw=true&extra=download.booxtream.com/', download_link_epub='https://github.com/eshellman/42_ebook/blob/master/download/42.epub?raw=true&extra=download.booxtream.com/',
download_link_mobi='https://github.com/eshellman/42_ebook/blob/master/download/42.mobi?raw=true', download_link_mobi='https://github.com/eshellman/42_ebook/blob/master/download/42.mobi?raw=true',
referenceid= kwargs.get('referenceid'), referenceid= kwargs.get('referenceid', '42'),
downloads_remaining= kwargs.get('downloadlimit'), downloads_remaining=kwargs.get('downloadlimit', 10),
expirydays=kwargs.get('expirydays'), expirydays=kwargs.get('expirydays', 30),
) )
return boox return boox
@ -86,9 +87,8 @@ class BooXtream(object):
boox = Boox.objects.create( boox = Boox.objects.create(
download_link_epub=download_link_epub, download_link_epub=download_link_epub,
download_link_mobi=download_link_mobi, download_link_mobi=download_link_mobi,
referenceid= kwargs.get('referenceid'), referenceid=kwargs.get('referenceid'),
downloads_remaining= kwargs.get('downloadlimit'), downloads_remaining=kwargs.get('downloadlimit'),
expirydays=kwargs.get('expirydays'), expirydays=kwargs.get('expirydays'),
) )
return boox return boox

View File

@ -7,7 +7,7 @@ class BooXtreamError(Exception):
self.errors = errors self.errors = errors
def __str__(self): def __str__(self):
errormsg='BooXtream errors:' errormsg = 'BooXtream errors:'
for error in self.errors: for error in self.errors:
errormsg += 'Error %s: %s\n'% (error.find('Code').text,error.find('Msg').text) errormsg += 'Error %s: %s\n'% (error.find('Code').text, error.find('Msg').text)
return errormsg return errormsg

View File

@ -6,22 +6,21 @@ class Boox(models.Model):
""" """
keeps a record of a file that's been watermarked keeps a record of a file that's been watermarked
""" """
download_link_epub = models.URLField(null=True) download_link_epub = models.URLField(null=True)
download_link_mobi = models.URLField(null=True) download_link_mobi = models.URLField(null=True)
referenceid = models.CharField(max_length=32) referenceid = models.CharField(max_length=32)
downloads_remaining = models.PositiveSmallIntegerField(default = 0) downloads_remaining = models.PositiveSmallIntegerField(default=0)
expirydays = models.PositiveSmallIntegerField() expirydays = models.PositiveSmallIntegerField()
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
@property @property
def expired(self): def expired(self):
return self.created+timedelta(days=self.expirydays) < datetime.now() return self.created+timedelta(days=self.expirydays) < datetime.now()
def download_link(self, format): def download_link(self, format):
if format == 'epub': if format == 'epub':
return self.download_link_epub return self.download_link_epub
elif format == 'mobi': elif format == 'mobi':
return self.download_link_mobi return self.download_link_mobi
else: return None
return None

View File

@ -1,6 +1,6 @@
import unittest import unittest
import time import time
import urllib2 import urllib2
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from StringIO import StringIO from StringIO import StringIO
from django.conf import settings from django.conf import settings
@ -23,14 +23,16 @@ class TestBooXtream(unittest.TestCase):
return manager return manager
def test_booxtream_errors(self): def test_booxtream_errors(self):
if settings.LOCAL_TEST:
return
from .exceptions import BooXtreamError from .exceptions import BooXtreamError
inst = self._makeOne() inst = self._makeOne()
if not settings.BOOXTREAM_API_KEY: if not settings.BOOXTREAM_API_KEY:
return return
with self.assertRaises(BooXtreamError) as cm: with self.assertRaises(BooXtreamError) as cm:
inst.platform() inst.platform()
self.assertIn( 'expirydays not set',str(cm.exception)) self.assertIn('expirydays not set', str(cm.exception))
params={ params = {
'customername': 'Jane Test', 'customername': 'Jane Test',
'languagecode':'1043', 'languagecode':'1043',
'expirydays': 1, 'expirydays': 1,
@ -47,7 +49,7 @@ class TestBooXtream(unittest.TestCase):
def test_booxtream_good(self): def test_booxtream_good(self):
inst = self._makeOne() inst = self._makeOne()
params={ params = {
'customeremailaddress':'jane@example.com', 'customeremailaddress':'jane@example.com',
'customername': 'Jane Test', 'customername': 'Jane Test',
'languagecode':'1043', 'languagecode':'1043',
@ -57,16 +59,16 @@ class TestBooXtream(unittest.TestCase):
'chapterfooter':1, 'chapterfooter':1,
'disclaimer':1, 'disclaimer':1,
} }
params['referenceid']= 'order'+str(time.time()) params['referenceid'] = 'order' + str(time.time())
boox=inst.platform(epubfile=self.epub2file, **params) boox = inst.platform(epubfile=self.epub2file, **params)
self.assertRegexpMatches(boox.download_link_epub,'download.booxtream.com/') self.assertRegexpMatches(boox.download_link_epub, 'download.booxtream.com/')
self.assertFalse(boox.expired) self.assertFalse(boox.expired)
self.assertEqual(boox.downloads_remaining,3) self.assertEqual(boox.downloads_remaining, 3)
# make sure it works with an in-memory file # make sure it works with an in-memory file
self.epub2file.seek(0) self.epub2file.seek(0)
in_mem_epub = StringIO() in_mem_epub = StringIO()
in_mem_epub.write(self.epub2file.read()) in_mem_epub.write(self.epub2file.read())
in_mem_epub.seek(0) in_mem_epub.seek(0)
boox2=inst.platform(epubfile=in_mem_epub, **params) boox2 = inst.platform(epubfile=in_mem_epub, **params)
self.assertRegexpMatches(boox2.download_link_epub,'download.booxtream.com/') self.assertRegexpMatches(boox2.download_link_epub, 'download.booxtream.com/')

View File

@ -1,6 +1,7 @@
""" """
Utilities that manipulate pdf files Utilities that manipulate pdf files
""" """
import logging
import requests import requests
from xhtml2pdf import pisa # import python module from xhtml2pdf import pisa # import python module
from PyPDF2 import PdfFileMerger,PdfFileReader from PyPDF2 import PdfFileMerger,PdfFileReader
@ -9,6 +10,7 @@ from tempfile import NamedTemporaryFile
from django.template.loader import render_to_string from django.template.loader import render_to_string
from regluit import settings from regluit import settings
logger = logging.getLogger(__name__)
# Utility function # Utility function
def ask_pdf(context={}): def ask_pdf(context={}):
@ -35,7 +37,7 @@ def pdf_append( file1, file2, file_out ):
def test_pdf(pdf_file): def test_pdf(pdf_file):
temp = None temp = None
try: try:
if isinstance(pdf_file , str): if isinstance(pdf_file , (str, unicode)):
if pdf_file.startswith('http:') or pdf_file.startswith('https:'): if pdf_file.startswith('http:') or pdf_file.startswith('https:'):
temp = NamedTemporaryFile(delete=False) temp = NamedTemporaryFile(delete=False)
test_file_content = requests.get(pdf_file).content test_file_content = requests.get(pdf_file).content
@ -53,7 +55,9 @@ def test_pdf(pdf_file):
except: except:
success = False success = False
return success return success
except: except Exception:
pdf_file = unicode(pdf_file)
logger.exception('error testing a pdf: %s' % pdf_file[:100])
return False return False
def test_test_pdf(self): def test_test_pdf(self):

View File

@ -1119,7 +1119,8 @@ class EbookFileTests(TestCase):
#test the ask-appender #test the ask-appender
c.add_ask_to_ebfs() c.add_ask_to_ebfs()
if settings.AWS_SECRET_ACCESS_KEY: if settings.AWS_SECRET_ACCESS_KEY:
assert test_pdf(c.work.ebookfiles().filter(asking=True)[0].file.url) askingpdfurl = c.work.ebookfiles().filter(asking=True)[0].file.url
assert test_pdf(askingpdfurl)
else: else:
assert test_pdf(c.work.ebookfiles().filter(asking=True)[0].file) assert test_pdf(c.work.ebookfiles().filter(asking=True)[0].file)
@ -1157,6 +1158,29 @@ class EbookFileTests(TestCase):
self.assertTrue(c.work.ebookfiles().filter(asking=True, ebook__active=True).count() == 0) self.assertTrue(c.work.ebookfiles().filter(asking=True, ebook__active=True).count() == 0)
self.assertTrue(c.work.ebookfiles().filter(asking=False, ebook__active=True).count() > 0) self.assertTrue(c.work.ebookfiles().filter(asking=False, ebook__active=True).count() > 0)
def test_bad_ebookfile(self):
w = Work.objects.create(title="Work 3")
e = Edition.objects.create(title=w.title, work=w)
temp = NamedTemporaryFile(delete=False)
test_file_content = "bad text file"
temp.write(test_file_content)
temp.close()
try:
# put the bad file into Django storage
temp_file = open(temp.name)
dj_file = DjangoFile(temp_file)
ebf = EbookFile(format='epub', edition=e, file=dj_file)
ebf.save()
temp_file.close()
ebf.make_mobi()
finally:
# make sure we get rid of temp file
os.remove(temp.name)
self.assertTrue(ebf.mobied < 0)
class MobigenTests(TestCase): class MobigenTests(TestCase):
def test_convert_to_mobi(self): def test_convert_to_mobi(self):
""" """