2013-08-23 21:14:48 +00:00
|
|
|
import unittest
|
|
|
|
import time
|
2013-09-16 01:41:53 +00:00
|
|
|
import urllib2
|
|
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
from StringIO import StringIO
|
2016-12-31 03:26:16 +00:00
|
|
|
from django.conf import settings
|
2013-08-23 21:14:48 +00:00
|
|
|
|
|
|
|
class TestBooXtream(unittest.TestCase):
|
2013-09-16 01:41:53 +00:00
|
|
|
def setUp(self):
|
|
|
|
# get a small epub test file as a file-like object
|
|
|
|
self.epub2file = NamedTemporaryFile(delete=False)
|
|
|
|
test_file_content = urllib2.urlopen('http://www.hxa.name/articles/content/EpubGuide-hxa7241.epub')
|
|
|
|
self.epub2file.write(test_file_content.read())
|
|
|
|
self.epub2file.seek(0)
|
2014-01-15 13:32:55 +00:00
|
|
|
self.textfile = NamedTemporaryFile(delete=False)
|
|
|
|
self.textfile.write("bad text file")
|
|
|
|
self.textfile.seek(0)
|
2013-09-16 01:41:53 +00:00
|
|
|
|
|
|
|
|
2013-08-23 21:14:48 +00:00
|
|
|
def _makeOne(self):
|
|
|
|
from . import BooXtream
|
|
|
|
manager = BooXtream()
|
|
|
|
return manager
|
|
|
|
|
|
|
|
def test_booxtream_errors(self):
|
|
|
|
from .exceptions import BooXtreamError
|
|
|
|
inst = self._makeOne()
|
2016-12-31 03:26:16 +00:00
|
|
|
if not settings.BOOXTREAM_API_KEY:
|
|
|
|
return
|
2013-08-23 21:14:48 +00:00
|
|
|
with self.assertRaises(BooXtreamError) as cm:
|
|
|
|
inst.platform()
|
|
|
|
self.assertIn( 'expirydays not set',str(cm.exception))
|
2014-01-15 13:32:55 +00:00
|
|
|
params={
|
|
|
|
'customername': 'Jane Test',
|
|
|
|
'languagecode':'1043',
|
|
|
|
'expirydays': 1,
|
|
|
|
'downloadlimit': 3,
|
|
|
|
'exlibris':1,
|
|
|
|
'chapterfooter':1,
|
|
|
|
'disclaimer':1,
|
|
|
|
'referenceid':'bad_file_check'
|
|
|
|
}
|
|
|
|
with self.assertRaises(BooXtreamError) as cm:
|
|
|
|
inst.platform(epubfile=self.textfile, **params)
|
|
|
|
|
2013-08-23 21:14:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_booxtream_good(self):
|
|
|
|
inst = self._makeOne()
|
|
|
|
params={
|
|
|
|
'customeremailaddress':'jane@example.com',
|
|
|
|
'customername': 'Jane Test',
|
|
|
|
'languagecode':'1043',
|
|
|
|
'expirydays': 1,
|
|
|
|
'downloadlimit': 3,
|
|
|
|
'exlibris':1,
|
|
|
|
'chapterfooter':1,
|
|
|
|
'disclaimer':1,
|
|
|
|
}
|
|
|
|
params['referenceid']= 'order'+str(time.time())
|
2013-09-16 01:41:53 +00:00
|
|
|
boox=inst.platform(epubfile=self.epub2file, **params)
|
2013-08-26 03:35:27 +00:00
|
|
|
self.assertRegexpMatches(boox.download_link_epub,'download.booxtream.com/')
|
|
|
|
self.assertFalse(boox.expired)
|
2013-09-16 01:41:53 +00:00
|
|
|
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/')
|