bgneal@824: """ bgneal@824: Unit tests for the bandmap application views. bgneal@824: bgneal@824: """ bgneal@825: import datetime bgneal@825: bgneal@824: from django.test import TestCase bgneal@824: from django.core.urlresolvers import reverse bgneal@824: from django.contrib.auth.models import User bgneal@824: bgneal@824: from bandmap.models import BandEntry bgneal@824: bgneal@824: bgneal@824: class NotLoggedInTestCase(TestCase): bgneal@824: """Tests for a non-authenticated user. bgneal@824: bgneal@824: """ bgneal@824: LOGIN_URL = reverse('accounts-login') bgneal@824: bgneal@824: def test_map(self): bgneal@824: url = reverse('bandmap-map') bgneal@824: response = self.client.get(url) bgneal@824: self.assertEqual(response.status_code, 200) bgneal@824: bgneal@824: def test_get_add(self): bgneal@824: url = reverse('bandmap-add') bgneal@824: response = self.client.get(url, follow=True) bgneal@824: self.assertRedirects(response, self.LOGIN_URL + '?next=' + url) bgneal@824: bgneal@824: def test_post_add(self): bgneal@824: url = reverse('bandmap-add') bgneal@824: response = self.client.post(url, follow=True) bgneal@824: self.assertRedirects(response, self.LOGIN_URL + '?next=' + url) bgneal@824: bgneal@824: bgneal@824: class BasicTestCase(TestCase): bgneal@824: """Tests for an authenticated user.""" bgneal@824: bgneal@824: def setUp(self): bgneal@824: self.user = User.objects.create_user( bgneal@824: username='pj', email='pj@example.com', password='top_secret') bgneal@824: self.client.login(username='pj', password='top_secret') bgneal@824: bgneal@824: def test_map(self): bgneal@824: url = reverse('bandmap-map') bgneal@824: response = self.client.get(url) bgneal@824: self.assertEqual(response.status_code, 200) bgneal@824: bgneal@824: def test_get_add(self): bgneal@824: url = reverse('bandmap-add') bgneal@824: response = self.client.get(url, follow=True) bgneal@824: self.assertEqual(response.status_code, 200) bgneal@824: bgneal@824: def test_post_add(self): bgneal@825: url = reverse('bandmap-add') bgneal@825: post_data = { bgneal@825: 'name': 'Test Band', bgneal@825: 'url': 'http://example.com/', bgneal@825: 'location': 'Here & There', bgneal@825: 'note': 'They suck', bgneal@825: 'is_active': 'on', bgneal@825: 'lat': '42.0', bgneal@825: 'lon': '-23.5', bgneal@825: } bgneal@825: now = datetime.datetime.now() bgneal@825: delta_t = datetime.timedelta(seconds=1) bgneal@825: response = self.client.post(url, data=post_data, follow=True) bgneal@825: self.assertRedirects(response, url) bgneal@825: bgneal@825: self.assertEqual(BandEntry.objects.all().count(), 1) bgneal@825: entry = BandEntry.objects.get(pk=1) bgneal@825: self.assertEqual(entry.name, post_data['name']) bgneal@825: self.assertEqual(entry.user, self.user) bgneal@825: self.assertEqual(entry.user, self.user) bgneal@825: self.assertTrue(now - entry.date_submitted < delta_t) bgneal@825: self.assertIsNone(entry.date_approved) bgneal@825: self.assertEqual(entry.url, post_data['url']) bgneal@825: self.assertEqual(entry.location, post_data['location']) bgneal@825: self.assertAlmostEqual(entry.lat, float(post_data['lat'])) bgneal@825: self.assertAlmostEqual(entry.lon, float(post_data['lon'])) bgneal@825: self.assertEqual(entry.note, post_data['note']) bgneal@825: self.assertTrue(entry.is_active) bgneal@825: self.assertFalse(entry.is_approved)