diff --git a/src/postorius/tests/fixtures/vcr_cassettes/mailman_user.yaml b/src/postorius/tests/fixtures/vcr_cassettes/mailman_user.yaml new file mode 100644 index 0000000..db338dd --- /dev/null +++ b/src/postorius/tests/fixtures/vcr_cassettes/mailman_user.yaml @@ -0,0 +1,206 @@ +interactions: +- request: + body: mail_host=example.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/domains + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + location: ['http://localhost:9001/3.0/domains/example.com'] + status: {code: 201, message: Created} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode GET + uri: http://localhost:9001/3.0/domains/example.com + response: + body: {string: !!python/unicode '{"base_url": "http://example.com", "description": + null, "http_etag": "\"e736411818ff1815ca83575e0958c38c5188f0a4\"", "mail_host": + "example.com", "self_link": "http://localhost:9001/3.0/domains/example.com", + "url_host": "example.com"}'} + headers: + content-length: ['233'] + content-type: [application/json; charset=utf-8] + status: {code: 200, message: OK} +- request: + body: fqdn_listname=foo%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/lists + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + location: ['http://localhost:9001/3.0/lists/foo.example.com'] + status: {code: 201, message: Created} +- request: + body: email=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/1'] + status: {code: 201, message: Created} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode GET + uri: http://localhost:9001/3.0/lists/foo.example.com + response: + body: {string: !!python/unicode '{"display_name": "Foo", "fqdn_listname": "foo@example.com", + "http_etag": "\"698a819bbb6b902096a8c5543cc7fac2328960d5\"", "list_id": "foo.example.com", + "list_name": "foo", "mail_host": "example.com", "member_count": 0, "self_link": + "http://localhost:9001/3.0/lists/foo.example.com", "volume": 1}'} + headers: + content-length: ['294'] + content-type: [application/json; charset=utf-8] + status: {code: 200, message: OK} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode DELETE + uri: http://localhost:9001/3.0/lists/foo@example.com + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + status: {code: 204, message: No Content} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode DELETE + uri: http://localhost:9001/3.0/users/1 + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + status: {code: 204, message: No Content} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode DELETE + uri: http://localhost:9001/3.0/domains/example.com + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + status: {code: 204, message: No Content} +- request: + body: mail_host=example.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/domains + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + location: ['http://localhost:9001/3.0/domains/example.com'] + status: {code: 201, message: Created} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode GET + uri: http://localhost:9001/3.0/domains/example.com + response: + body: {string: !!python/unicode '{"base_url": "http://example.com", "description": + null, "http_etag": "\"e736411818ff1815ca83575e0958c38c5188f0a4\"", "mail_host": + "example.com", "self_link": "http://localhost:9001/3.0/domains/example.com", + "url_host": "example.com"}'} + headers: + content-length: ['233'] + content-type: [application/json; charset=utf-8] + status: {code: 200, message: OK} +- request: + body: fqdn_listname=foo%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/lists + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + location: ['http://localhost:9001/3.0/lists/foo.example.com'] + status: {code: 201, message: Created} +- request: + body: email=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/lists/foo.example.com + response: + body: {string: !!python/unicode '{"display_name": "Foo", "fqdn_listname": "foo@example.com", + "http_etag": "\"698a819bbb6b902096a8c5543cc7fac2328960d5\"", "list_id": "foo.example.com", + "list_name": "foo", "mail_host": "example.com", "member_count": 0, "self_link": + "http://localhost:9001/3.0/lists/foo.example.com", "volume": 1}'} + headers: + content-length: ['294'] + content-type: [application/json; charset=utf-8] + status: {code: 200, message: OK} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode DELETE + uri: http://localhost:9001/3.0/lists/foo@example.com + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + status: {code: 204, message: No Content} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode DELETE + uri: http://localhost:9001/3.0/users/2 + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + status: {code: 204, message: No Content} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode DELETE + uri: http://localhost:9001/3.0/domains/example.com + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + status: {code: 204, message: No Content} +version: 1 diff --git a/src/postorius/tests/fixtures/vcr_cassettes/mailman_user_address_prefs.yaml b/src/postorius/tests/fixtures/vcr_cassettes/mailman_user_address_prefs.yaml new file mode 100644 index 0000000..b56f34f --- /dev/null +++ b/src/postorius/tests/fixtures/vcr_cassettes/mailman_user_address_prefs.yaml @@ -0,0 +1,143 @@ +interactions: +- request: + body: email=user2%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/1/addresses + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + location: ['http://localhost:9001/3.0/addresses/user2@example.com'] + status: {code: 201, message: Created} +- request: + body: email=user3%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/1/addresses + response: + body: {string: !!python/unicode ''} + headers: + content-length: ['0'] + location: ['http://localhost:9001/3.0/addresses/user3@example.com'] + status: {code: 201, message: Created} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: !!python/unicode GET + uri: http://localhost:9001/3.0/users/user@example.com + response: + body: {string: !!python/unicode '{"created_on": "2005-08-01T07:49:23", "http_etag": + "\"bdcb404b219b82818fb885be00d5339669d6bb3f\"", "is_server_owner": false, + "password": "$6$rounds=689043$d8uskbfd.VfEs4/o$6TxSFOGOXk3EgmvFCnSgf5Mvc0gAzP4AH8V1zcVfdViIz6WfvkKfMB3cY//dRVxhaP5Uzc7MFKy0v6qIjQJJz0", + "self_link": "http://localhost:9001/3.0/users/1", "user_id": 1}'} + 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/1/addresses + response: + body: {string: !!python/unicode '{"entries": [{"email": "user2@example.com", "http_etag": + "\"b0525102b3bdf4a45112c8066362f4befcc70581\"", "original_email": "user2@example.com", + "registered_on": "2005-08-01T07:49:23", "self_link": "http://localhost:9001/3.0/addresses/user2@example.com", + "user": "http://localhost:9001/3.0/users/1"}, {"email": "user3@example.com", + "http_etag": "\"9703386a61ef8d7ddc31692b30f65e49b041bab6\"", "original_email": + "user3@example.com", "registered_on": "2005-08-01T07:49:23", "self_link": + "http://localhost:9001/3.0/addresses/user3@example.com", "user": "http://localhost:9001/3.0/users/1"}, + {"email": "user@example.com", "http_etag": "\"21ddd5bb988d468856280278646b1da2d10cfa17\"", + "original_email": "user@example.com", "registered_on": "2005-08-01T07:49:23", + "self_link": "http://localhost:9001/3.0/addresses/user@example.com", "user": + "http://localhost:9001/3.0/users/1"}], "http_etag": "\"effec0f366f8fedf2ea3a9dc617f1f4adb3cf783\"", + "start": 0, "total_size": 3}'} + headers: + content-length: ['961'] + 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/user@example.com + response: + body: {string: !!python/unicode '{"created_on": "2005-08-01T07:49:23", "http_etag": + "\"bdcb404b219b82818fb885be00d5339669d6bb3f\"", "is_server_owner": false, + "password": "$6$rounds=689043$d8uskbfd.VfEs4/o$6TxSFOGOXk3EgmvFCnSgf5Mvc0gAzP4AH8V1zcVfdViIz6WfvkKfMB3cY//dRVxhaP5Uzc7MFKy0v6qIjQJJz0", + "self_link": "http://localhost:9001/3.0/users/1", "user_id": 1}'} + 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/1/addresses + response: + body: {string: !!python/unicode '{"entries": [{"email": "user2@example.com", "http_etag": + "\"b0525102b3bdf4a45112c8066362f4befcc70581\"", "original_email": "user2@example.com", + "registered_on": "2005-08-01T07:49:23", "self_link": "http://localhost:9001/3.0/addresses/user2@example.com", + "user": "http://localhost:9001/3.0/users/1"}, {"email": "user3@example.com", + "http_etag": "\"9703386a61ef8d7ddc31692b30f65e49b041bab6\"", "original_email": + "user3@example.com", "registered_on": "2005-08-01T07:49:23", "self_link": + "http://localhost:9001/3.0/addresses/user3@example.com", "user": "http://localhost:9001/3.0/users/1"}, + {"email": "user@example.com", "http_etag": "\"21ddd5bb988d468856280278646b1da2d10cfa17\"", + "original_email": "user@example.com", "registered_on": "2005-08-01T07:49:23", + "self_link": "http://localhost:9001/3.0/addresses/user@example.com", "user": + "http://localhost:9001/3.0/users/1"}], "http_etag": "\"effec0f366f8fedf2ea3a9dc617f1f4adb3cf783\"", + "start": 0, "total_size": 3}'} + headers: + content-length: ['961'] + 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/addresses/user2@example.com/preferences + response: + body: {string: !!python/unicode '{"http_etag": "\"a7666bafbd297a58aa20fc67fed94dc5f29e696c\"", + "self_link": "http://localhost:9001/3.0/addresses/user2@example.com/preferences"}'} + headers: + content-length: ['143'] + 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/addresses/user3@example.com/preferences + response: + body: {string: !!python/unicode '{"http_etag": "\"a99bfb2af4f58fb94d3308d147de24937297fb32\"", + "self_link": "http://localhost:9001/3.0/addresses/user3@example.com/preferences"}'} + headers: + content-length: ['143'] + 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/addresses/user@example.com/preferences + response: + body: {string: !!python/unicode '{"http_etag": "\"c557a0cd7d400f67152a6f6e5e2c14858e632323\"", + "self_link": "http://localhost:9001/3.0/addresses/user@example.com/preferences"}'} + headers: + content-length: ['142'] + 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 new file mode 100644 index 0000000..98a3dfc --- /dev/null +++ b/src/postorius/tests/mailman_api_tests/test_user.py @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2016 by the Free Software Foundation, Inc. +# +# This file is part of Postorius. +# +# Postorius is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# Postorius is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# Postorius. If not, see . + +"""Tests for ban lists""" + +from __future__ import absolute_import, print_function, unicode_literals + +import logging + +from django.conf import settings +from django.contrib.auth.models import User +from django.core.urlresolvers import reverse +from django.test import TestCase +from six.moves.urllib_parse import quote + +from postorius.models import MailmanUser +from postorius.tests import MM_VCR +from postorius.utils import get_client + + +logger = logging.getLogger(__name__) +vcr_log = logging.getLogger('vcr') +vcr_log.setLevel(logging.WARNING) + + +class MailmanUserTest(TestCase): + """ + Tests for the mailman user preferences settings page. + """ + + @MM_VCR.use_cassette('mailman_user.yaml') + def setUp(self): + self.domain = get_client().create_domain('example.com') + self.foo_list = self.domain.create_list('foo') + self.user = User.objects.create_user( + 'user', 'user@example.com', 'testpass') + self.mm_user = MailmanUser.objects.create_from_django(self.user) + + @MM_VCR.use_cassette('mailman_user.yaml') + def tearDown(self): + self.foo_list.delete() + self.mm_user.delete() + self.user.delete() + self.domain.delete() + + def _check_redirect_login(self, url): + response = self.client.get(url) + if '%40' not in url: # Django < 1.8 + url = quote(url) + expected_redirect = 'http://testserver%s?next=%s' % ( + reverse(settings.LOGIN_URL), url) + self.assertEqual(response.status_code, 302) + self.assertEqual(response['location'], expected_redirect) + + @MM_VCR.use_cassette('mailman_user_access.yaml') + def test_page_not_accessible_if_not_logged_in(self): + self._check_redirect_login(reverse('user_address_preferences')) + + @MM_VCR.use_cassette('mailman_user_address_prefs.yaml') + def test_address_based_preferences(self): + self.client.login(username='user', password='testpass') + self.mm_user.add_address('user2@example.com') + self.mm_user.add_address('user3@example.com') + response = self.client.get(reverse('user_address_preferences')) + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.context["formset"]), 3) + self.assertEqual(len(response.context["zipped_data"]), 3) + #self.assertEqual( + # response.context["formset"].initial['archive_policy'], 'public') diff --git a/src/postorius/views/user.py b/src/postorius/views/user.py index 8eefd63..0d2d093 100644 --- a/src/postorius/views/user.py +++ b/src/postorius/views/user.py @@ -106,7 +106,7 @@ helperform = UserPreferences() mm_user = MailmanUser.objects.get(address=request.user.email) addresses = mm_user.addresses - AFormset = formset_factory(UserPreferences, extra=len(addresses._addresses)) + AFormset = formset_factory(UserPreferences, extra=len(addresses)) formset = AFormset() zipped_data = zip(formset.forms, addresses) for form, address in zipped_data: