From c0aefdef296d6cde4d3e6562777dd5ce2772033d Mon Sep 17 00:00:00 2001 From: volpino Date: Sat, 14 Jul 2012 15:29:42 +0200 Subject: [PATCH] euscanwww: Started writing tests for favourite stuff but login fails Signed-off-by: volpino --- euscanwww/djeuscan/tests/__init__.py | 25 ++++++++++++++++++++++ euscanwww/djeuscan/tests/euscan_factory.py | 8 +++++++ euscanwww/djeuscan/tests/test_views.py | 21 ++++++++++++++++++ setup.py | 2 +- 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/euscanwww/djeuscan/tests/__init__.py b/euscanwww/djeuscan/tests/__init__.py index 0e27290..5e37ff2 100644 --- a/euscanwww/djeuscan/tests/__init__.py +++ b/euscanwww/djeuscan/tests/__init__.py @@ -3,12 +3,16 @@ from urllib import urlencode from django.test import TestCase from django.test.client import Client from django.core.urlresolvers import reverse +from django.contrib.auth.models import User + +from djeuscan.tests.euscan_factory import UserFactory class SystemTestCase(TestCase): """ Base class for system tests """ + fixtures = ["test_data.json"] def setUp(self): self.client = Client() @@ -27,6 +31,27 @@ class SystemTestCase(TestCase): url = reverse(url_name, args=args, kwargs=kwargs) return self.client.post(url, data) + def login(self): + user = UserFactory.build() + user.set_password("pass") + user.save() + return Login(self, user.username, "pass") + + +class Login(object): + def __init__(self, testcase, user, password): + self.testcase = testcase + success = testcase.client.login(username=user, password=password) + self.testcase.assertTrue( + success, + "login with username=%r, password=%r failed" % (user, password) + ) + + def __enter__(self): + pass + + def __exit__(self, *args): + self.testcase.client.logout() from test_models import * from test_views import * diff --git a/euscanwww/djeuscan/tests/euscan_factory.py b/euscanwww/djeuscan/tests/euscan_factory.py index b60fa61..114e3a4 100644 --- a/euscanwww/djeuscan/tests/euscan_factory.py +++ b/euscanwww/djeuscan/tests/euscan_factory.py @@ -3,11 +3,19 @@ from string import letters from datetime import datetime from collections import defaultdict +from django.contrib.auth.models import User + import factory from djeuscan.models import Herd, Maintainer, Package, Version, EuscanResult +class UserFactory(factory.Factory): + FACTORY_FOR = User + + username = factory.Sequence(lambda n: 'user' + n) + + def random_string(length=None): if length is None: length = random.randint(1, 30) diff --git a/euscanwww/djeuscan/tests/test_views.py b/euscanwww/djeuscan/tests/test_views.py index 4139453..bb55faa 100644 --- a/euscanwww/djeuscan/tests/test_views.py +++ b/euscanwww/djeuscan/tests/test_views.py @@ -9,6 +9,8 @@ from djeuscan.tests import SystemTestCase from djeuscan.tests.euscan_factory import PackageFactory, setup_maintainers, \ setup_herds, setup_categories, setup_overlays +from djeuscan.models import CategoryAssociation + class PagesTest(SystemTestCase): """ @@ -31,6 +33,10 @@ class PagesTest(SystemTestCase): response = self.get("global_feed") self.assertEqual(response.status_code, 200) + def test_api(self): + response = self.get("api") + self.assertEqual(response.status_code, 200) + class PackageTests(SystemTestCase): def setUp(self): @@ -81,6 +87,21 @@ class CategoriesTests(SectionTests): response = self.get("category_feed", category=category) self.assertEqual(response.status_code, 200) + def dont_test_favourite(self): + # TODO: understand why login fails + category = self.categories[0] + self.assertEqual(CategoryAssociation.objects.count(), 0) + + response = self.get("category", category=category) + self.assertNotIn("Watch", response.content) + + with self.login(): + response = self.get("category", category=category) + self.assertIn("Watch", response.content) + self.post("favourite_category", category=category) + + self.assertEqual(CategoryAssociation.objects.count(), 1) + class HerdsTests(SectionTests): def setUp(self): diff --git a/setup.py b/setup.py index e9aeb1b..dcc2779 100755 --- a/setup.py +++ b/setup.py @@ -70,7 +70,7 @@ packages = [ ] tests_require = [ - 'factory_boy', + 'factory-boy==1.1.3', ] setup(