# HG changeset patch # User Brian Neal # Date 1390188402 21600 # Node ID 22d158ef2217eda1dfae420e199dcdbc2b368338 # Parent f01bc9500aef16fbf2c85479d6b0b35815941c62 More bio unit tests. diff -r f01bc9500aef -r 22d158ef2217 bio/tests/test_views.py --- a/bio/tests/test_views.py Sun Jan 19 17:23:07 2014 -0600 +++ b/bio/tests/test_views.py Sun Jan 19 21:26:42 2014 -0600 @@ -5,7 +5,6 @@ from django.contrib.auth.models import User from django.test import TestCase from django.core.urlresolvers import reverse, NoReverseMatch -from django.http import Http404 class MemberSearchTest(TestCase): @@ -121,7 +120,7 @@ self._test_get('date', page=99, code=404) -class MyProfileTest(TestCase): +class ProfileTest(TestCase): def setUp(self): u1 = User.objects.create_user('paul', '', 'pw') @@ -129,13 +128,62 @@ u2 = User.objects.create_user('eddie', '', 'pw') u2.save() - def test_anon(self): + def test_anon_my_profile(self): my_url = reverse('bio-me') response = self.client.get(my_url) login_url = reverse('accounts-login') + '?next=' + my_url self.assertRedirects(response, login_url) - def test_authenticated(self): + def test_authenticated_my_profile(self): self.client.login(username='paul', password='pw') response = self.client.get(reverse('bio-me')) self.assertEqual(response.status_code, 200) + + def test_anon_other_profile(self): + url = reverse('bio-view_profile', kwargs={'username': 'eddie'}) + response = self.client.get(url) + login_url = reverse('accounts-login') + '?next=' + url + self.assertRedirects(response, login_url) + + def test_authenticated_other_profile(self): + self.client.login(username='paul', password='pw') + url = reverse('bio-view_profile', kwargs={'username': 'eddie'}) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + def test_view_profile_as_me_redirects(self): + self.client.login(username='paul', password='pw') + url = reverse('bio-view_profile', kwargs={'username': 'paul'}) + response = self.client.get(url) + self.assertRedirects(response, reverse('bio-me')) + + def test_edit_anon(self): + url = reverse('bio-edit_profile') + response = self.client.get(url) + login_url = reverse('accounts-login') + '?next=' + url + self.assertRedirects(response, login_url) + + def test_edit_profile(self): + self.client.login(username='paul', password='pw') + url = reverse('bio-edit_profile') + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + # Save some new data. We don't exhaustively test every field. + response = self.client.post(url, { + 'first_name': 'Paul', + 'last_name': 'Johnson', + 'email': 'pjmoto@example.com', + 'occupation': 'Surf Guitar Pioneer', + 'profile_text': 'Wrote Mr. Moto at age 15.', + 'submit_button': 'Save', + }) + self.assertRedirects(response, reverse('bio-me')) + + u = User.objects.get(username='paul') + self.assertEqual(u.first_name, 'Paul') + self.assertEqual(u.last_name, 'Johnson') + self.assertEqual(u.email, 'pjmoto@example.com') + profile = u.get_profile() + self.assertEqual(profile.occupation, 'Surf Guitar Pioneer') + self.assertEqual(profile.profile_text, 'Wrote Mr. Moto at age 15.') diff -r f01bc9500aef -r 22d158ef2217 bio/views.py --- a/bio/views.py Sun Jan 19 17:23:07 2014 -0600 +++ b/bio/views.py Sun Jan 19 21:26:42 2014 -0600 @@ -95,7 +95,7 @@ user = get_object_or_404(User, username=username) if user == request.user: - return HttpResponseRedirect(reverse('bio.views.my_profile')) + return HttpResponseRedirect(reverse('bio-me')) profile = user.get_profile() hide_email = profile.hide_email @@ -118,7 +118,7 @@ def edit_profile(request): if request.method == 'POST': if request.POST.get('submit_button', 'Cancel') == 'Cancel': - return HttpResponseRedirect(reverse('bio.views.my_profile')) + return HttpResponseRedirect(reverse('bio-me')) profile = request.user.get_profile() user_form = EditUserForm(request.POST, instance=request.user) profile_form = EditUserProfileForm(request.POST, instance=profile) @@ -127,7 +127,7 @@ profile = profile_form.save(commit=False) profile.user = request.user profile.save() - return HttpResponseRedirect(reverse('bio.views.my_profile')) + return HttpResponseRedirect(reverse('bio-me')) else: profile = request.user.get_profile() user_form = EditUserForm(instance=request.user)