regluit/booxtream/tests.py

75 lines
2.5 KiB
Python
Raw Normal View History

2013-08-23 21:14:48 +00:00
import unittest
import time
2020-02-12 22:57:35 +00:00
from urllib.request import urlopen
2013-09-16 01:41:53 +00:00
from tempfile import NamedTemporaryFile
2020-02-13 04:22:11 +00:00
from io import BytesIO
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)
2020-02-12 22:57:35 +00:00
test_file_content = urlopen('http://www.hxa.name/articles/content/EpubGuide-hxa7241.epub')
2013-09-16 01:41:53 +00:00
self.epub2file.write(test_file_content.read())
self.epub2file.seek(0)
2014-01-15 13:32:55 +00:00
self.textfile = NamedTemporaryFile(delete=False)
2020-02-15 17:23:28 +00:00
self.textfile.write(b'bad text file')
2014-01-15 13:32:55 +00:00
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):
2018-06-08 19:01:22 +00:00
if settings.LOCAL_TEST:
return
2013-08-23 21:14:48 +00:00
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()
2018-06-08 19:01:22 +00:00
self.assertIn('expirydays not set', str(cm.exception))
2018-06-08 19:17:39 +00:00
params = {
2014-01-15 13:32:55 +00:00
'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()
2018-06-08 19:17:39 +00:00
params = {
2013-08-23 21:14:48 +00:00
'customeremailaddress':'jane@example.com',
'customername': 'Jane Test',
'languagecode':'1043',
'expirydays': 1,
'downloadlimit': 3,
'exlibris':1,
'chapterfooter':1,
'disclaimer':1,
}
2018-06-08 19:17:39 +00:00
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)
2018-06-08 19:17:39 +00:00
self.assertEqual(boox.downloads_remaining, 3)
2013-09-16 01:41:53 +00:00
# make sure it works with an in-memory file
self.epub2file.seek(0)
2020-02-13 04:22:11 +00:00
in_mem_epub = BytesIO()
2013-09-16 01:41:53 +00:00
in_mem_epub.write(self.epub2file.read())
in_mem_epub.seek(0)
2018-06-08 19:17:39 +00:00
boox2 = inst.platform(epubfile=in_mem_epub, **params)
self.assertRegexpMatches(boox2.download_link_epub, 'download.booxtream.com/')