diff --git a/src/postorius/tests/fixtures/vcr_cassettes/mailman_user_subscriptions_no_mm_user.yaml b/src/postorius/tests/fixtures/vcr_cassettes/mailman_user_subscriptions_no_mm_user.yaml new file mode 100644 index 0000000..3dfdac7 --- /dev/null +++ b/src/postorius/tests/fixtures/vcr_cassettes/mailman_user_subscriptions_no_mm_user.yaml @@ -0,0 +1,112 @@ +interactions: +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode GET + uri: http://localhost:9001/3.0/users/old-user@example.com + response: + body: {string: !!python/unicode 404 Not Found} + headers: + content-length: ['13'] + content-type: [application/json; charset=utf-8] + status: {code: 404, message: Not Found} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode GET + uri: http://localhost:9001/3.0/users/old-user@example.com + response: + body: {string: !!python/unicode 404 Not Found} + headers: + content-length: ['13'] + content-type: [application/json; charset=utf-8] + status: {code: 404, message: Not Found} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode GET + uri: http://localhost:9001/3.0/users/old-user@example.com + response: + body: {string: !!python/unicode 404 Not Found} + headers: + content-length: ['13'] + content-type: [application/json; charset=utf-8] + status: {code: 404, message: Not Found} +- request: + body: email=old-user%40example.com + headers: + accept-encoding: ['gzip, deflate'] + !!python/unicode content-type: [!!python/unicode application/x-www-form-urlencoded] + method: !!python/unicode POST + uri: http://localhost:9001/3.0/users + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + location: ['http://localhost:9001/3.0/users/2'] + status: {code: 201, message: Created} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode GET + uri: http://localhost:9001/3.0/users/2 + response: + body: {string: !!python/unicode '{"created_on": "2005-08-01T07:49:23", "http_etag": + "\"5d9a2125862a97678d31d374d4563c58e962ba81\"", "is_server_owner": false, + "password": "$6$rounds=674177$NUcFsfsljYExmeOr$Ia81UVDT8ZEIx2anUItM/BKFAOxAoCjewD0hHs66c7GOqskRRGdE6TtwpUXkJ1xo.rJ2Jzbn5.cwXut31W/Ci1", + "self_link": "http://localhost:9001/3.0/users/2", "user_id": 2}'} + headers: + content-length: ['324'] + content-type: [application/json; charset=utf-8] + status: {code: 200, message: OK} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode GET + uri: http://localhost:9001/3.0/users/2/addresses + response: + body: {string: !!python/unicode '{"entries": [{"email": "old-user@example.com", + "http_etag": "\"c0525c55f8389234b6835f13e2da185819e94507\"", "original_email": + "old-user@example.com", "registered_on": "2005-08-01T07:49:23", "self_link": + "http://localhost:9001/3.0/addresses/old-user@example.com", "user": "http://localhost:9001/3.0/users/2"}], + "http_etag": "\"11b8bff1cda2496f3a21cf476da2766fb6fae519\"", "start": 0, "total_size": + 1}'} + headers: + content-length: ['399'] + content-type: [application/json; charset=utf-8] + status: {code: 200, message: OK} +- request: + body: subscriber=old-user%40example.com + headers: + accept-encoding: ['gzip, deflate'] + !!python/unicode content-type: [!!python/unicode application/x-www-form-urlencoded] + method: !!python/unicode POST + uri: http://localhost:9001/3.0/members/find + response: + body: {string: !!python/unicode '{"http_etag": "\"32223434a0f3af4cdc4673d1fbc5bac1f6d98fd3\"", + "start": 0, "total_size": 0}'} + headers: + content-length: ['90'] + content-type: [application/json; charset=utf-8] + status: {code: 200, message: OK} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode GET + uri: http://localhost:9001/3.0/users/old-user@example.com + response: + body: {string: !!python/unicode '{"created_on": "2005-08-01T07:49:23", "http_etag": + "\"5d9a2125862a97678d31d374d4563c58e962ba81\"", "is_server_owner": false, + "password": "$6$rounds=674177$NUcFsfsljYExmeOr$Ia81UVDT8ZEIx2anUItM/BKFAOxAoCjewD0hHs66c7GOqskRRGdE6TtwpUXkJ1xo.rJ2Jzbn5.cwXut31W/Ci1", + "self_link": "http://localhost:9001/3.0/users/2", "user_id": 2}'} + headers: + content-length: ['324'] + content-type: [application/json; charset=utf-8] + status: {code: 200, message: OK} +version: 1 diff --git a/src/postorius/tests/mailman_api_tests/test_user.py b/src/postorius/tests/mailman_api_tests/test_user.py index 3adf81d..9b88e09 100644 --- a/src/postorius/tests/mailman_api_tests/test_user.py +++ b/src/postorius/tests/mailman_api_tests/test_user.py @@ -25,10 +25,11 @@ from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.test import TestCase +from django.test.utils import override_settings from mock import patch from six.moves.urllib_parse import quote -from postorius.models import MailmanUser +from postorius.models import MailmanUser, Mailman404Error from postorius.tests import MM_VCR from postorius.utils import get_client @@ -123,3 +124,17 @@ }) response = self.client.post(url, post_data) self.assertEqual(response.status_code, 302) + + @MM_VCR.use_cassette('mailman_user_subscriptions_no_mm_user.yaml') + @override_settings(AUTOCREATE_MAILMAN_USER=False) + def test_subscriptions_no_mailman_user(self): + # Existing Django users without a corresponding Mailman user must not + # cause views to crash. + user = User.objects.create_user( + 'old-user', 'old-user@example.com', 'testpass') + self.client.login(username='old-user', password='testpass') + self.assertRaises(Mailman404Error, MailmanUser.objects.get, address=user.email) + response = self.client.get(reverse('user_subscriptions')) + self.assertEqual(response.status_code, 200) + # The Mailman user must have been created + self.assertIsNotNone(MailmanUser.objects.get(address=user.email)) diff --git a/src/postorius/urls.py b/src/postorius/urls.py index d09d921..b22c656 100644 --- a/src/postorius/urls.py +++ b/src/postorius/urls.py @@ -64,7 +64,7 @@ {"template_name": "postorius/login.html"}, name='user_login'), url(r'^accounts/logout/$', logout_view, name='user_logout'), url(r'^accounts/profile/$', user_views.user_profile, name='user_profile'), - url(r'^accounts/subscriptions/$', user_views.UserSubscriptionsView.as_view(), + url(r'^accounts/subscriptions/$', user_views.user_subscriptions, name='user_subscriptions'), url(r'^accounts/per-address-preferences/$', user_views.UserAddressPreferencesView.as_view(), name='user_address_preferences'), diff --git a/src/postorius/views/user.py b/src/postorius/views/user.py index 0761ae2..0b4f0c4 100644 --- a/src/postorius/views/user.py +++ b/src/postorius/views/user.py @@ -208,14 +208,17 @@ {'zipped_data': zipped_data, 'formset': formset}) -class UserSubscriptionsView(MailmanUserView): - - """Shows the subscriptions of a user. - """ - @method_decorator(login_required) - def get(self, request): - memberships = self._get_memberships() - return render(request, 'postorius/user/subscriptions.html', {'memberships': memberships}) +@login_required +def user_subscriptions(request): + """Shows the subscriptions of a user.""" + utils.set_other_emails(request.user) + try: + mm_user = MailmanUser.objects.get_or_create_from_django(request.user) + except MailmanApiError: + return utils.render_api_error(request) + memberships = [m for m in mm_user.subscriptions if m.role == 'member'] + return render(request, 'postorius/user/subscriptions.html', + {'memberships': memberships}) class AddressActivationView(TemplateView):