euscan-ng/euscanwww/djeuscan/tests/test_views.py

297 lines
9.5 KiB
Python
Raw Normal View History

2012-05-07 19:09:08 +02:00
import StringIO
2012-05-12 16:07:49 +02:00
2012-06-08 09:18:59 +02:00
try:
from BeautifulSoup import BeautifulSoup
except ImportError:
from bs4 import BeautifulSoup
2012-05-07 14:27:54 +02:00
from djeuscan.helpers import get_profile
2012-05-07 14:27:54 +02:00
from djeuscan.tests import SystemTestCase
2012-05-13 11:37:05 +02:00
from djeuscan.tests.euscan_factory import PackageFactory, setup_maintainers, \
setup_herds, setup_categories, setup_overlays
2012-05-07 19:09:08 +02:00
class PagesTest(SystemTestCase):
"""
Test main pages
"""
def test_index(self):
response = self.get("index")
self.assertEqual(response.status_code, 200)
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
def test_world(self):
response = self.get("world")
self.assertEqual(response.status_code, 200)
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
def test_about(self):
response = self.get("about")
2012-05-07 14:27:54 +02:00
self.assertEqual(response.status_code, 200)
2012-05-12 17:29:09 +02:00
def test_global_feed(self):
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)
2012-05-07 19:09:08 +02:00
2012-05-12 16:07:49 +02:00
class PackageTests(SystemTestCase):
def setUp(self):
super(PackageTests, self).setUp()
self.package = PackageFactory.create()
def test_package(self):
response = self.get("package", category=self.package.category,
package=self.package.name)
self.assertEqual(response.status_code, 200)
def test_favourite(self):
response = self.get("package", category=self.package.category,
package=self.package.name)
self.assertEqual(response.status_code, 200)
self.assertNotIn("Watch", response.content)
with self.login():
response = self.get("package", category=self.package.category,
package=self.package.name)
self.assertEqual(response.status_code, 200)
user = response.context["user"]
self.assertEquals(get_profile(user).categories.count(), 0)
self.assertIn("Watch", response.content)
self.post("favourite_package", category=self.package.category,
package=self.package.name)
self.assertEquals(get_profile(user).packages.count(), 1)
response = self.get("accounts_packages")
self.assertEqual(response.status_code, 200)
self.assertIn(self.package.name, response.content)
self.post("unfavourite_package", category=self.package.category,
package=self.package.name)
self.assertEquals(get_profile(user).packages.count(), 0)
2012-05-12 16:07:49 +02:00
2012-05-07 19:09:08 +02:00
class SectionTests(SystemTestCase):
def _check_table(self, response, items, attr=None):
2012-05-07 14:27:54 +02:00
soup = BeautifulSoup(response.content)
rows = soup.findAll("tr")
2012-05-07 19:09:08 +02:00
self.assertEqual(len(rows), len(items))
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
for item in items:
if attr:
item_str = getattr(item, attr)
else:
item_str = item
self.assertTrue(item_str in response.content)
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
class CategoriesTests(SectionTests):
def setUp(self):
super(CategoriesTests, self).setUp()
2012-05-13 11:37:05 +02:00
self.categories, self.packages = setup_categories()
2012-05-07 19:09:08 +02:00
def test_categories(self):
response = self.get("categories")
self.assertEqual(response.status_code, 200)
self._check_table(response, self.categories)
2012-05-12 16:07:49 +02:00
def test_category(self):
category = self.categories[0]
response = self.get("category", category=category)
self.assertEqual(response.status_code, 200)
self._check_table(response, self.packages[:1], attr="name")
2012-05-12 17:29:09 +02:00
def test_category_feed(self):
category = self.categories[0]
response = self.get("category_feed", category=category)
self.assertEqual(response.status_code, 200)
def test_favourite(self):
category = self.categories[0]
response = self.get("category", category=category)
self.assertEqual(response.status_code, 200)
self.assertNotIn("Watch", response.content)
with self.login():
response = self.get("category", category=category)
self.assertEqual(response.status_code, 200)
user = response.context["user"]
self.assertEquals(get_profile(user).categories.count(), 0)
self.assertIn("Watch", response.content)
self.post("favourite_category", category=category)
self.assertEquals(get_profile(user).categories.count(), 1)
response = self.get("accounts_categories")
self.assertEqual(response.status_code, 200)
self._check_table(response, [category])
self.post("unfavourite_category", category=category)
self.assertEquals(get_profile(user).categories.count(), 0)
2012-05-07 19:09:08 +02:00
class HerdsTests(SectionTests):
def setUp(self):
super(HerdsTests, self).setUp()
2012-05-13 11:37:05 +02:00
self.herds, self.packages = setup_herds()
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
def test_herds(self):
2012-05-07 14:27:54 +02:00
response = self.get("herds")
self.assertEqual(response.status_code, 200)
2012-05-07 19:09:08 +02:00
self._check_table(response, self.herds, attr="herd")
2012-05-07 14:27:54 +02:00
2012-05-12 16:07:49 +02:00
def test_herd(self):
herd = self.herds[0]
response = self.get("herd", herd=herd.herd)
self.assertEqual(response.status_code, 200)
self._check_table(response, self.packages[:1], attr="name")
2012-05-12 17:29:09 +02:00
def test_herd_feed(self):
herd = self.herds[0]
response = self.get("herd_feed", herd=herd.herd)
self.assertEqual(response.status_code, 200)
def test_favourite(self):
herd = self.herds[0]
response = self.get("herd", herd=herd.herd)
self.assertEqual(response.status_code, 200)
self.assertNotIn("Watch", response.content)
with self.login():
response = self.get("herd", herd=herd.herd)
self.assertEqual(response.status_code, 200)
user = response.context["user"]
self.assertEquals(get_profile(user).herds.count(), 0)
self.assertIn("Watch", response.content)
self.post("favourite_herd", herd=herd.herd)
self.assertEquals(get_profile(user).herds.count(), 1)
response = self.get("accounts_herds")
self.assertEqual(response.status_code, 200)
self._check_table(response, [herd], attr="herd")
self.post("unfavourite_herd", herd=herd.herd)
self.assertEquals(get_profile(user).herds.count(), 0)
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
class MaintainersTests(SectionTests):
def setUp(self):
super(MaintainersTests, self).setUp()
2012-05-13 11:37:05 +02:00
self.maintainers, self.packages = setup_maintainers()
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
def test_maintainers(self):
2012-05-07 14:27:54 +02:00
response = self.get("maintainers")
self.assertEqual(response.status_code, 200)
2012-05-07 19:09:08 +02:00
self._check_table(response, self.maintainers, attr="name")
2012-05-07 14:27:54 +02:00
2012-05-12 16:07:49 +02:00
def test_maintainer(self):
maintainer = self.maintainers[0]
response = self.get("maintainer", maintainer_id=maintainer.pk)
self.assertEqual(response.status_code, 200)
self._check_table(response, self.packages[:1], attr="name")
2012-05-12 17:29:09 +02:00
def test_maintainer_feed(self):
maintainer = self.maintainers[0]
response = self.get("maintainer_feed", maintainer_id=maintainer.pk)
self.assertEqual(response.status_code, 200)
def test_favourite(self):
maintainer = self.maintainers[0]
response = self.get("maintainer", maintainer_id=maintainer.pk)
self.assertEqual(response.status_code, 200)
self.assertNotIn("Watch", response.content)
with self.login():
response = self.get("maintainer", maintainer_id=maintainer.pk)
self.assertEqual(response.status_code, 200)
user = response.context["user"]
self.assertEquals(get_profile(user).maintainers.count(), 0)
self.assertIn("Watch", response.content)
self.post("favourite_maintainer", maintainer_id=maintainer.pk)
self.assertEquals(get_profile(user).maintainers.count(), 1)
response = self.get("accounts_maintainers")
self.assertEqual(response.status_code, 200)
self._check_table(response, [maintainer], attr="name")
self.post("unfavourite_maintainer", maintainer_id=maintainer.pk)
self.assertEquals(get_profile(user).maintainers.count(), 0)
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
class OverlayTests(SectionTests):
def setUp(self):
super(OverlayTests, self).setUp()
2012-05-13 11:37:05 +02:00
self.overlays, self.packages = setup_overlays()
2012-05-07 14:27:54 +02:00
def test_overlays(self):
2012-05-07 19:09:08 +02:00
response = self.get("overlays")
self.assertEqual(response.status_code, 200)
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
self._check_table(response, self.overlays)
2012-05-12 16:07:49 +02:00
def test_overlay(self):
overlay = self.overlays[0]
response = self.get("overlay", overlay=overlay)
self.assertEqual(response.status_code, 200)
self._check_table(response, self.packages[overlay], attr="name")
2012-05-07 19:09:08 +02:00
class WorldScanTests(SectionTests):
def setUp(self):
super(WorldScanTests, self).setUp()
2012-05-07 14:27:54 +02:00
for _ in range(3):
2012-05-07 19:09:08 +02:00
PackageFactory.create()
self.packages = [PackageFactory.create().name for _ in range(3)]
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
def test_world_scan_packages(self):
response = self.post("world_scan",
data={"packages": "\n".join(self.packages)})
2012-05-07 14:27:54 +02:00
self.assertEqual(response.status_code, 200)
2012-05-07 19:09:08 +02:00
self._check_table(response, self.packages)
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
def test_world_scan_world(self):
world_file = StringIO.StringIO()
world_file.write("\n".join(self.packages))
world_file.name = "world"
world_file.read = world_file.getvalue
2012-05-07 14:27:54 +02:00
2012-05-07 19:09:08 +02:00
response = self.post("world_scan", data={"world": world_file})
2012-05-07 14:27:54 +02:00
self.assertEqual(response.status_code, 200)
2012-05-07 19:09:08 +02:00
self._check_table(response, self.packages)