2013-06-03 16:31:39 +00:00
|
|
|
"""
|
|
|
|
external library imports
|
|
|
|
"""
|
2011-09-16 02:05:47 +00:00
|
|
|
import json
|
2011-10-10 19:39:25 +00:00
|
|
|
from decimal import Decimal
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2013-06-03 16:31:39 +00:00
|
|
|
"""
|
|
|
|
django imports
|
|
|
|
"""
|
|
|
|
from django.contrib.auth.models import User
|
2011-08-31 03:46:55 +00:00
|
|
|
from django.test import TestCase
|
2011-09-16 02:05:47 +00:00
|
|
|
from django.test.client import Client
|
|
|
|
|
2013-06-03 16:31:39 +00:00
|
|
|
"""
|
|
|
|
regluit imports
|
|
|
|
"""
|
2011-12-20 04:26:55 +00:00
|
|
|
import regluit.core.isbn
|
2011-09-16 02:05:47 +00:00
|
|
|
|
2016-12-29 18:44:26 +00:00
|
|
|
from regluit.core import models
|
2013-06-03 16:31:39 +00:00
|
|
|
from regluit.utils.localdatetime import now
|
2015-08-04 16:02:06 +00:00
|
|
|
from regluit.api import models as apimodels
|
2013-06-03 16:31:39 +00:00
|
|
|
|
2011-09-16 02:53:44 +00:00
|
|
|
class ApiTests(TestCase):
|
2016-12-29 18:44:26 +00:00
|
|
|
fixtures = ['initial_data.json', 'neuromancer.json']
|
2012-02-08 03:23:19 +00:00
|
|
|
work_id=None
|
|
|
|
|
2011-09-16 02:05:47 +00:00
|
|
|
def setUp(self):
|
2016-12-29 18:44:26 +00:00
|
|
|
edition = models.Edition.objects.get(pk=1)
|
2017-10-28 22:33:58 +00:00
|
|
|
self.work_id=edition.work_id
|
2011-09-16 02:05:47 +00:00
|
|
|
campaign = models.Campaign.objects.create(
|
|
|
|
name=edition.work.title,
|
|
|
|
work=edition.work,
|
|
|
|
description='Test Campaign',
|
2012-03-07 19:42:16 +00:00
|
|
|
deadline=now(),
|
2011-10-10 19:39:25 +00:00
|
|
|
target=Decimal('1000.00'),
|
2011-09-16 02:05:47 +00:00
|
|
|
)
|
2013-03-06 14:39:06 +00:00
|
|
|
self.user = User.objects.create_user('test', 'test@example.org', 'testpass')
|
2011-09-16 02:05:47 +00:00
|
|
|
self.client = Client()
|
2015-12-19 01:21:28 +00:00
|
|
|
ebook = models.Ebook.objects.create(
|
|
|
|
url="http://example.com/ebook",
|
|
|
|
provider="github",
|
|
|
|
rights='CC BY',
|
|
|
|
format='epub',
|
|
|
|
edition=edition,
|
|
|
|
)
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-09-16 02:53:44 +00:00
|
|
|
def test_user(self):
|
|
|
|
self.assertEqual(User.objects.all().count(), 1)
|
|
|
|
self.assertTrue(User.objects.all()[0].api_key.key)
|
|
|
|
|
|
|
|
def test_no_auth(self):
|
2011-09-16 02:05:47 +00:00
|
|
|
r = self.client.get('/api/v1/campaign/', data={'format': 'json'})
|
2011-09-16 02:53:44 +00:00
|
|
|
self.assertEqual(r.status_code, 401)
|
|
|
|
|
|
|
|
def test_campaigns(self):
|
|
|
|
r = self.client.get('/api/v1/campaign/', data={
|
|
|
|
'format': 'json',
|
|
|
|
'username': self.user.username,
|
|
|
|
'api_key': self.user.api_key.key
|
|
|
|
})
|
2011-09-16 02:05:47 +00:00
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
j = json.loads(r.content)
|
|
|
|
self.assertEqual(len(j['objects']), 1)
|
|
|
|
self.assertEqual(j['objects'][0]['name'], 'Neuromancer')
|
2012-02-08 03:23:19 +00:00
|
|
|
self.assertEqual(j['objects'][0]['work'], '/api/v1/work/%s/' % self.work_id)
|
|
|
|
resource_uri=j['objects'][0]['resource_uri']
|
|
|
|
r = self.client.get( resource_uri, data={
|
2011-09-16 02:53:44 +00:00
|
|
|
'format': 'json',
|
|
|
|
'username': self.user.username,
|
|
|
|
'api_key': self.user.api_key.key
|
|
|
|
})
|
2011-09-16 02:05:47 +00:00
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
j = json.loads(r.content)
|
|
|
|
self.assertEqual(j['name'], 'Neuromancer')
|
2012-02-08 03:23:19 +00:00
|
|
|
self.assertEqual(j['work'], '/api/v1/work/%s/' % self.work_id)
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-09-16 02:05:47 +00:00
|
|
|
def test_campaign_lookup_by_isbn(self):
|
2011-09-16 02:53:44 +00:00
|
|
|
r = self.client.get('/api/v1/campaign/', data={
|
|
|
|
'format': 'json',
|
2012-07-20 15:40:11 +00:00
|
|
|
'work__identifiers__value': regluit.core.isbn.convert_10_to_13('0441007465'),
|
2012-01-10 20:20:02 +00:00
|
|
|
'work__identifiers__type': 'isbn',
|
2011-09-16 02:53:44 +00:00
|
|
|
'username': self.user.username,
|
|
|
|
'api_key': self.user.api_key.key
|
|
|
|
})
|
2011-09-16 02:05:47 +00:00
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
j = json.loads(r.content)
|
|
|
|
self.assertEqual(len(j['objects']), 1)
|
|
|
|
self.assertEqual(j['objects'][0]['name'], 'Neuromancer')
|
2011-09-16 04:51:46 +00:00
|
|
|
self.assertEqual(j['meta']['logged_in_username'], None)
|
|
|
|
self.assertEqual(j['objects'][0]['in_wishlist'], False)
|
|
|
|
|
2012-05-20 17:35:06 +00:00
|
|
|
def test_identifier_lookup(self):
|
|
|
|
r = self.client.get('/api/v1/identifier/', data={
|
|
|
|
'format': 'json',
|
2012-07-20 15:40:11 +00:00
|
|
|
'value': regluit.core.isbn.convert_10_to_13('0441007465'),
|
2012-05-20 17:35:06 +00:00
|
|
|
'type': 'isbn',
|
|
|
|
'username': self.user.username,
|
|
|
|
'api_key': self.user.api_key.key
|
|
|
|
})
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
2011-09-16 04:51:46 +00:00
|
|
|
def test_logged_in_user_info(self):
|
|
|
|
# login and see if adding a work to the users wishlist causes
|
|
|
|
# it to show up as in_wishlist in the campaign info
|
|
|
|
self.client.login(username='test', password='testpass')
|
|
|
|
|
|
|
|
r = self.client.get('/api/v1/campaign/', data={
|
|
|
|
'format': 'json',
|
2012-07-20 15:40:11 +00:00
|
|
|
'work__identifiers__value': regluit.core.isbn.convert_10_to_13('0441007465'),
|
2012-01-10 20:20:02 +00:00
|
|
|
'work__identifiers__type': 'isbn',
|
2011-09-16 04:51:46 +00:00
|
|
|
'username': self.user.username,
|
|
|
|
'api_key': self.user.api_key.key
|
|
|
|
})
|
|
|
|
j = json.loads(r.content)
|
|
|
|
self.assertEqual(j['meta']['logged_in_username'], 'test')
|
|
|
|
self.assertEqual(j['objects'][0]['in_wishlist'], False)
|
|
|
|
|
2012-07-20 15:40:11 +00:00
|
|
|
w = models.Work.objects.get(identifiers__value=regluit.core.isbn.convert_10_to_13('0441007465'), identifiers__type='isbn')
|
2011-12-08 23:22:05 +00:00
|
|
|
self.user.wishlist.add_work(w,'test')
|
2011-09-16 04:51:46 +00:00
|
|
|
r = self.client.get('/api/v1/campaign/', data={
|
|
|
|
'format': 'json',
|
2012-07-20 15:40:11 +00:00
|
|
|
'work__identifiers__value': regluit.core.isbn.convert_10_to_13('0441007465'),
|
2012-01-10 20:20:02 +00:00
|
|
|
'work__identifiers__type': 'isbn',
|
2011-09-16 04:51:46 +00:00
|
|
|
'username': self.user.username,
|
|
|
|
'api_key': self.user.api_key.key
|
|
|
|
})
|
|
|
|
j = json.loads(r.content)
|
|
|
|
self.assertEqual(j['meta']['logged_in_username'], 'test')
|
|
|
|
self.assertEqual(j['objects'][0]['in_wishlist'], True)
|
|
|
|
|
2015-12-19 01:21:28 +00:00
|
|
|
r = self.client.get('/api/v1/free/', data={
|
|
|
|
'format': 'json',
|
|
|
|
'isbn': '0441007465',
|
|
|
|
'username': self.user.username,
|
|
|
|
'api_key': self.user.api_key.key
|
|
|
|
})
|
|
|
|
j = json.loads(r.content)
|
|
|
|
self.assertEqual(j['objects'][0]['filetype'], 'epub')
|
2016-04-09 17:21:12 +00:00
|
|
|
r = self.client.get('/api/v1/free/', data={
|
|
|
|
'format': 'xml',
|
|
|
|
'isbn': '0441007465',
|
|
|
|
'username': self.user.username,
|
|
|
|
'api_key': self.user.api_key.key
|
|
|
|
})
|
|
|
|
self.assertTrue(r.content.find('<rights>CC BY</rights>')>0)
|
2015-12-19 01:21:28 +00:00
|
|
|
|
2014-04-24 11:55:32 +00:00
|
|
|
def test_widget(self):
|
|
|
|
r = self.client.get('/api/widget/0441007465/')
|
|
|
|
self.assertEqual(r.status_code, 200)
|
2015-02-18 18:09:43 +00:00
|
|
|
r = self.client.get('/api/widget/%s/'%self.work_id)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
2014-04-24 11:55:32 +00:00
|
|
|
|
2015-08-27 19:23:52 +00:00
|
|
|
class FeedTests(TestCase):
|
2016-12-29 18:44:26 +00:00
|
|
|
fixtures = ['initial_data.json', 'neuromancer.json']
|
2015-08-27 19:23:52 +00:00
|
|
|
def setUp(self):
|
2016-12-29 18:44:26 +00:00
|
|
|
edition = models.Edition.objects.get(pk=1)
|
2015-08-27 19:23:52 +00:00
|
|
|
ebook = models.Ebook.objects.create(edition=edition, url='http://example.org/', format='epub', rights='CC BY')
|
2017-10-28 22:33:58 +00:00
|
|
|
self.test_work_id = edition.work_id
|
2015-08-18 13:14:19 +00:00
|
|
|
|
|
|
|
def test_opds(self):
|
|
|
|
r = self.client.get('/api/opds/creative_commons/')
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
r = self.client.get('/api/opds/epub/?order_by=featured')
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
r = self.client.get('/api/opds/by/pdf/?order_by=popular')
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
r = self.client.get('/api/opds/active_campaigns/?order_by=title')
|
|
|
|
self.assertEqual(r.status_code, 200)
|
2015-08-27 19:23:52 +00:00
|
|
|
r = self.client.get('/api/opds/?work=%s' % self.test_work_id)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_nix(self):
|
|
|
|
r = self.client.get('/api/onix/by/')
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
r = self.client.get('/api/onix/cc0/')
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
r = self.client.get('/api/onix/epub/?max=10')
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
r = self.client.get('/api/onix/?work=%s' % self.test_work_id)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
2015-08-18 13:14:19 +00:00
|
|
|
|
2015-08-04 16:02:06 +00:00
|
|
|
class AllowedRepoTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
apimodels.AllowedRepo.objects.create(org='test',repo_name='test')
|
|
|
|
apimodels.AllowedRepo.objects.create(org='star',repo_name='*')
|
|
|
|
|
|
|
|
def test_urls(self):
|
|
|
|
#good
|
|
|
|
self.assertTrue(apimodels.repo_allowed('https://github.com/test/test/raw/master/metadata.yaml')[0])
|
|
|
|
self.assertTrue(apimodels.repo_allowed('https://github.com/star/test/raw/master/metadata.yaml')[0])
|
|
|
|
# bad urls
|
|
|
|
self.assertFalse(apimodels.repo_allowed('auhf8peargp8ge')[0])
|
|
|
|
self.assertFalse(apimodels.repo_allowed('')[0])
|
|
|
|
self.assertFalse(apimodels.repo_allowed('http://github.com/test/test/raw/master/metadata.yaml')[0])
|
|
|
|
self.assertFalse(apimodels.repo_allowed('https://github.com/test/test/raw/master/samples/metadata.yaml')[0])
|
|
|
|
self.assertFalse(apimodels.repo_allowed('https://github.com/test/test/raw/branch/metadata.yaml')[0])
|
|
|
|
self.assertFalse(apimodels.repo_allowed('https://github.com/test/test/master/metadata.yaml')[0])
|
|
|
|
self.assertFalse(apimodels.repo_allowed('https://github.com/test/test/raw/master/metadata.json')[0])
|
|
|
|
|
2016-02-23 18:32:51 +00:00
|
|
|
class WebHookTests(TestCase):
|
2016-07-23 16:08:28 +00:00
|
|
|
fixtures = ['initial_data.json']
|
2016-02-23 18:32:51 +00:00
|
|
|
def test_travisci_webhook(self):
|
|
|
|
"""
|
|
|
|
test of api.views.travisci_webhook
|
|
|
|
"""
|
|
|
|
|
|
|
|
payload = json.dumps({
|
|
|
|
"repository":{
|
|
|
|
"id":4651401,
|
|
|
|
"name":"Adventures-of-Huckleberry-Finn_76",
|
|
|
|
"owner_name":"GITenberg",
|
|
|
|
"url":"http://GITenberg.github.com/"
|
|
|
|
},
|
|
|
|
"status_message": "Passed",
|
|
|
|
"type": "push"
|
|
|
|
})
|
|
|
|
|
|
|
|
invalid_payload = json.dumps({
|
|
|
|
"repository":{
|
|
|
|
"id":4651401,
|
|
|
|
"name":"",
|
|
|
|
"url":"http://GITenberg.github.com/"
|
|
|
|
},
|
|
|
|
"status_message": "Passed",
|
|
|
|
"type": "push"
|
|
|
|
})
|
|
|
|
|
|
|
|
url = "/api/travisci/webhook"
|
|
|
|
|
|
|
|
# 200 if a simple get
|
|
|
|
r = self.client.get(url)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
# 200 when we actually load a valid repo (should we use 201?)
|
|
|
|
r = self.client.post(url, data={'payload':payload}, headers={}, allow_redirects=True)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
# 400 error if we get exception when trying to load a book
|
|
|
|
r = self.client.post(url, data={'payload':invalid_payload}, headers={}, allow_redirects=True)
|
|
|
|
self.assertEqual(r.status_code, 400)
|
|
|
|
|
|
|
|
|