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
from django.conf import settings
from urllib import quote
from functools import partial
from urllib import quote
from xml.etree import ElementTree
import requests
from django.conf import settings
from django.apps import apps
from . exceptions import BooXtreamError
@ -34,7 +35,7 @@ class BooXtream(object):
if not apiuser:
apiuser = settings.BOOXTREAM_API_USER
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):
@ -51,22 +52,22 @@ class BooXtream(object):
kwargs['epub'] = '1' if epub else '0'
kwargs['kf8mobi'] = '1' if kf8mobi else '0'
if epubfile:
if hasattr(epubfile,'name') and str(epubfile.name).endswith('.epub'):
files= {'epubfile': (str(epubfile.name),epubfile)}
if hasattr(epubfile, 'name') and str(epubfile.name).endswith('.epub'):
files = {'epubfile': (str(epubfile.name), epubfile)}
else:
# give it a random file name so that kindlegen doesn't choke
# 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:
files={}
files = {}
if settings.LOCAL_TEST:
# fake it, so you can test other functions without hitting booxtream
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_mobi='https://github.com/eshellman/42_ebook/blob/master/download/42.mobi?raw=true',
referenceid= kwargs.get('referenceid'),
downloads_remaining= kwargs.get('downloadlimit'),
expirydays=kwargs.get('expirydays'),
referenceid= kwargs.get('referenceid', '42'),
downloads_remaining=kwargs.get('downloadlimit', 10),
expirydays=kwargs.get('expirydays', 30),
)
return boox
@ -86,9 +87,8 @@ class BooXtream(object):
boox = Boox.objects.create(
download_link_epub=download_link_epub,
download_link_mobi=download_link_mobi,
referenceid= kwargs.get('referenceid'),
downloads_remaining= kwargs.get('downloadlimit'),
referenceid=kwargs.get('referenceid'),
downloads_remaining=kwargs.get('downloadlimit'),
expirydays=kwargs.get('expirydays'),
)
return boox

View File

@ -7,7 +7,7 @@ class BooXtreamError(Exception):
self.errors = errors
def __str__(self):
errormsg='BooXtream errors:'
errormsg = 'BooXtream 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

View File

@ -9,7 +9,7 @@ class Boox(models.Model):
download_link_epub = models.URLField(null=True)
download_link_mobi = models.URLField(null=True)
referenceid = models.CharField(max_length=32)
downloads_remaining = models.PositiveSmallIntegerField(default = 0)
downloads_remaining = models.PositiveSmallIntegerField(default=0)
expirydays = models.PositiveSmallIntegerField()
created = models.DateTimeField(auto_now_add=True)
@ -22,6 +22,5 @@ class Boox(models.Model):
return self.download_link_epub
elif format == 'mobi':
return self.download_link_mobi
else:
return None

View File

@ -23,14 +23,16 @@ class TestBooXtream(unittest.TestCase):
return manager
def test_booxtream_errors(self):
if settings.LOCAL_TEST:
return
from .exceptions import BooXtreamError
inst = self._makeOne()
if not settings.BOOXTREAM_API_KEY:
return
with self.assertRaises(BooXtreamError) as cm:
inst.platform()
self.assertIn( 'expirydays not set',str(cm.exception))
params={
self.assertIn('expirydays not set', str(cm.exception))
params = {
'customername': 'Jane Test',
'languagecode':'1043',
'expirydays': 1,
@ -47,7 +49,7 @@ class TestBooXtream(unittest.TestCase):
def test_booxtream_good(self):
inst = self._makeOne()
params={
params = {
'customeremailaddress':'jane@example.com',
'customername': 'Jane Test',
'languagecode':'1043',
@ -57,16 +59,16 @@ class TestBooXtream(unittest.TestCase):
'chapterfooter':1,
'disclaimer':1,
}
params['referenceid']= 'order'+str(time.time())
boox=inst.platform(epubfile=self.epub2file, **params)
self.assertRegexpMatches(boox.download_link_epub,'download.booxtream.com/')
params['referenceid'] = 'order' + str(time.time())
boox = inst.platform(epubfile=self.epub2file, **params)
self.assertRegexpMatches(boox.download_link_epub, 'download.booxtream.com/')
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
self.epub2file.seek(0)
in_mem_epub = StringIO()
in_mem_epub.write(self.epub2file.read())
in_mem_epub.seek(0)
boox2=inst.platform(epubfile=in_mem_epub, **params)
self.assertRegexpMatches(boox2.download_link_epub,'download.booxtream.com/')
boox2 = inst.platform(epubfile=in_mem_epub, **params)
self.assertRegexpMatches(boox2.download_link_epub, 'download.booxtream.com/')

View File

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

View File

@ -1119,7 +1119,8 @@ class EbookFileTests(TestCase):
#test the ask-appender
c.add_ask_to_ebfs()
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:
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=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):
def test_convert_to_mobi(self):
"""