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
|
2012-01-04 22:14:37 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from regluit.core.models import Work, Campaign
|
|
|
|
|
|
|
|
from decimal import Decimal as D
|
2012-03-07 19:42:16 +00:00
|
|
|
from regluit.utils.localdatetime import now
|
|
|
|
from datetime import timedelta
|
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)
|
2012-02-07 20:47:32 +00:00
|
|
|
wished= self.user.wishlist.works.all()[0]
|
2011-12-19 22:09:01 +00:00
|
|
|
# test the work page
|
2012-02-07 20:47:32 +00:00
|
|
|
r = self.client.get("/work/%s/" % wished.id)
|
2011-12-19 22:09:01 +00:00
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
anon_client = Client()
|
2012-02-07 20:47:32 +00:00
|
|
|
r = anon_client.get("/work/%s/" % wished.id)
|
2011-12-19 22:09:01 +00:00
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
2011-10-10 16:57:10 +00:00
|
|
|
# remove the book
|
2012-02-07 20:47:32 +00:00
|
|
|
r = self.client.post("/wishlist/", {"remove_work_id": wished.id},
|
2011-10-10 16:57:10 +00:00
|
|
|
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)
|
2012-01-10 20:20:02 +00:00
|
|
|
r = self.client.get("/search/?q=sverige")
|
|
|
|
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)
|
2012-01-10 20:20:02 +00:00
|
|
|
r = anon_client.get("/search/?q=sverige")
|
|
|
|
self.assertEqual(r.status_code, 200)
|
2012-03-08 03:06:30 +00:00
|
|
|
r = anon_client.get("/info/metrics.html")
|
|
|
|
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)
|
2012-01-04 22:14:37 +00:00
|
|
|
|
|
|
|
class CampaignUiTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.user = User.objects.create_user('test', 'test@example.org', 'test')
|
|
|
|
self.client = Client()
|
|
|
|
# load a Work and a Campaign to create a Pledge page
|
|
|
|
self.work = Work(title="test Work")
|
|
|
|
self.work.save()
|
2012-03-07 19:42:16 +00:00
|
|
|
self.campaign = Campaign(target=D('1000.00'), deadline=now() + timedelta(days=180),
|
2012-01-04 22:14:37 +00:00
|
|
|
work=self.work)
|
|
|
|
self.campaign.save()
|
|
|
|
self.campaign.activate()
|
|
|
|
|
|
|
|
def test_login_required_for_pledge(self):
|
|
|
|
""" Make sure that the user has to be logged in to be able to access a pledge page"""
|
|
|
|
pledge_path = reverse("pledge", kwargs={'work_id': self.work.id})
|
|
|
|
r = self.client.get(pledge_path)
|
|
|
|
self.assertEqual(r.status_code, 302)
|
|
|
|
|
|
|
|
# now login and see whether the pledge page is accessible
|
|
|
|
self.client.login(username='test', password='test')
|
|
|
|
r = self.client.get(pledge_path)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|