2011-09-16 02:05:47 +00:00
|
|
|
import json
|
|
|
|
import datetime
|
2011-10-10 19:39:25 +00:00
|
|
|
from decimal import Decimal
|
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
|
2011-09-16 02:53:44 +00:00
|
|
|
from django.contrib.auth.models import User
|
2011-09-16 02:05:47 +00:00
|
|
|
|
|
|
|
from regluit.core import bookloader, models
|
2011-12-20 04:26:55 +00:00
|
|
|
import regluit.core.isbn
|
2011-09-16 02:05:47 +00:00
|
|
|
|
2011-09-16 02:53:44 +00:00
|
|
|
class ApiTests(TestCase):
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-09-16 02:05:47 +00:00
|
|
|
def setUp(self):
|
2012-01-09 18:55:22 +00:00
|
|
|
edition = bookloader.add_by_isbn_from_google(isbn='0441012035')
|
2011-09-16 02:05:47 +00:00
|
|
|
campaign = models.Campaign.objects.create(
|
|
|
|
name=edition.work.title,
|
|
|
|
work=edition.work,
|
|
|
|
description='Test Campaign',
|
|
|
|
deadline=datetime.datetime.now(),
|
2011-10-10 19:39:25 +00:00
|
|
|
target=Decimal('1000.00'),
|
2011-09-16 02:05:47 +00:00
|
|
|
)
|
2011-09-16 02:53:44 +00:00
|
|
|
self.user = User.objects.create_user('test', 'test@example.com', 'testpass')
|
2011-09-16 02:05:47 +00:00
|
|
|
self.client = Client()
|
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')
|
|
|
|
self.assertEqual(j['objects'][0]['work'], '/api/v1/work/1/')
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-09-16 02:05:47 +00:00
|
|
|
def test_campaign(self):
|
2011-09-16 02:53:44 +00:00
|
|
|
r = self.client.get('/api/v1/campaign/1/', 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(j['name'], 'Neuromancer')
|
|
|
|
self.assertEqual(j['work'], '/api/v1/work/1/')
|
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-01-10 20:20:02 +00:00
|
|
|
'work__identifiers__value': regluit.core.isbn.convert_10_to_13('0441012035'),
|
|
|
|
'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)
|
|
|
|
|
|
|
|
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-01-10 20:20:02 +00:00
|
|
|
'work__identifiers__value': regluit.core.isbn.convert_10_to_13('0441012035'),
|
|
|
|
'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-01-10 20:20:02 +00:00
|
|
|
w = models.Work.objects.get(identifiers__value=regluit.core.isbn.convert_10_to_13('0441012035'), 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-01-10 20:20:02 +00:00
|
|
|
'work__identifiers__value': regluit.core.isbn.convert_10_to_13('0441012035'),
|
|
|
|
'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)
|
|
|
|
|