diff --git a/hc/accounts/middleware.py b/hc/accounts/middleware.py index 589e5307..3118e216 100644 --- a/hc/accounts/middleware.py +++ b/hc/accounts/middleware.py @@ -10,7 +10,12 @@ class TeamAccessMiddleware(object): teams_q = teams_q.select_related("user") request.teams = list(teams_q) - profile = request.user.profile + try: + profile = request.user.profile + except Profile.DoesNotExist: + profile = Profile(user=request.user) + profile.save() + if profile.current_team: request.team = profile.current_team else: diff --git a/hc/accounts/tests/test_team_access_middleware.py b/hc/accounts/tests/test_team_access_middleware.py new file mode 100644 index 00000000..231a002c --- /dev/null +++ b/hc/accounts/tests/test_team_access_middleware.py @@ -0,0 +1,17 @@ +from django.contrib.auth.models import User +from django.test import TestCase +from hc.accounts.models import Profile + + +class TeamAccessMiddlewareTestCase(TestCase): + + def test_it_handles_missing_profile(self): + user = User(username="ned", email="ned@example.org") + user.set_password("password") + user.save() + + self.client.login(username="ned@example.org", password="password") + r = self.client.get("/about/") + self.assertEqual(r.status_code, 200) + + self.assertEqual(Profile.objects.count(), 1)