2011-11-22 01:12:13 +00:00
|
|
|
import re
|
|
|
|
|
2011-08-31 03:46:55 +00:00
|
|
|
from django.test import TestCase
|
2011-10-10 16:57:10 +00:00
|
|
|
from django.test.client import Client
|
|
|
|
from django.contrib.auth.models import User
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-10-10 16:57:10 +00:00
|
|
|
class WishlistTests(TestCase):
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-10-10 16:57:10 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.user = User.objects.create_user('test', 'test@example.org', 'test')
|
|
|
|
self.client = Client()
|
|
|
|
self.client.login(username='test', password='test')
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-10-10 16:57:10 +00:00
|
|
|
def test_add_remove(self):
|
|
|
|
# add a book to the wishlist
|
|
|
|
r = self.client.post("/wishlist/", {"googlebooks_id": "2NyiPwAACAAJ"},
|
|
|
|
HTTP_X_REQUESTED_WITH="XMLHttpRequest")
|
|
|
|
self.assertEqual(r.status_code, 302)
|
|
|
|
self.assertEqual(self.user.wishlist.works.all().count(), 1)
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-12-19 22:09:01 +00:00
|
|
|
# test the work page
|
|
|
|
r = self.client.get("/work/1/")
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
anon_client = Client()
|
|
|
|
r = anon_client.get("/work/1/")
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
2011-10-10 16:57:10 +00:00
|
|
|
# remove the book
|
|
|
|
r = self.client.post("/wishlist/", {"remove_work_id": "1"},
|
|
|
|
HTTP_X_REQUESTED_WITH="XMLHttpRequest")
|
|
|
|
self.assertEqual(self.user.wishlist.works.all().count(), 0)
|
2011-10-17 18:37:31 +00:00
|
|
|
|
2011-12-19 22:09:01 +00:00
|
|
|
class PageTests(TestCase):
|
2011-10-17 18:37:31 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.user = User.objects.create_user('test', 'test@example.org', 'test')
|
2011-12-19 22:09:01 +00:00
|
|
|
self.user = User.objects.create_user('test_other', 'test@example.org', 'test_other')
|
2011-10-17 18:37:31 +00:00
|
|
|
self.client = Client()
|
|
|
|
self.client.login(username='test', password='test')
|
|
|
|
|
2011-12-19 22:09:01 +00:00
|
|
|
def test_view_by_self(self):
|
2011-10-17 18:37:31 +00:00
|
|
|
# logged in
|
|
|
|
r = self.client.get("/supporter/test/")
|
|
|
|
self.assertEqual(r.status_code, 200)
|
2011-12-19 22:09:01 +00:00
|
|
|
|
|
|
|
def test_view_by_other(self):
|
|
|
|
# someone else's supporter page
|
|
|
|
r = self.client.get("/supporter/test_other/")
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_view_by_anonymous(self):
|
2011-10-17 18:37:31 +00:00
|
|
|
# not logged in
|
|
|
|
anon_client = Client()
|
2011-12-19 22:09:01 +00:00
|
|
|
r = anon_client.get("/supporter/test/")
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
r = anon_client.get("/")
|
2011-10-17 18:37:31 +00:00
|
|
|
self.assertEqual(r.status_code, 200)
|
2011-11-22 01:12:13 +00:00
|
|
|
|
|
|
|
class GoogleBooksTest(TestCase):
|
|
|
|
|
|
|
|
def test_googlebooks_id(self):
|
|
|
|
r = self.client.get("/googlebooks/wtPxGztYx-UC/")
|
|
|
|
self.assertEqual(r.status_code, 302)
|
|
|
|
work_url = r['location']
|
|
|
|
self.assertTrue(re.match('.*/work/\d+/$', work_url))
|
|
|
|
|
|
|
|
r = self.client.get("/googlebooks/wtPxGztYx-UC/")
|
|
|
|
self.assertEqual(r.status_code, 302)
|
|
|
|
self.assertEqual(r['location'], work_url)
|