Commit ab990b4d authored by Abhilash Raj's avatar Abhilash Raj

Merge branch 'add-membership-tests' into 'master'

Add membership tests to Mailing List.

See merge request !67
parents 636fa182 9ed7415c
Pipeline #22291285 passed with stage
in 1 minute and 49 seconds
image: maxking/mailman-ci-runner
python2.7:
script:
- tox -e py27
python3.4:
script:
- tox -e py34
python3.5:
script:
- tox -e py35
......
......@@ -350,3 +350,54 @@ class MailingList(RESTObject):
data['username'] = username
data['password'] = password
return self._connection.call(url, data, 'PATCH')[1]
def _check_membership(self, address, allowed_roles):
"""
Given an address and role, check if there is a membership record that
matches the given address with a given role for this Mailing List.
"""
url = 'members/find'
data = {'subscriber': address,
'list_id': self.list_id}
response, content = self._connection.call(url, data=data)
if 'entries' not in content:
return False
for membership in content['entries']:
# We check for all the returned roles for this User and MailingList
if membership['role'] in allowed_roles:
return True
return False
def is_owner(self, address):
"""
Given an address, checks if the given address is an owner of this
mailing list.
"""
return self._check_membership(address=address,
allowed_roles=('owner',))
def is_moderator(self, address):
"""
Given an address, checks if the given address is a moderator of this
mailing list.
"""
return self._check_membership(address=address,
allowed_roles=('moderator',))
def is_member(self, address):
"""
Given an address, checks if the given address is subscribed to this
mailing list.
"""
return self._check_membership(address=address,
allowed_roles=('member',))
def is_owner_or_mod(self, address):
"""
Given an address, checks if the given address is either a owner or
a moderator of this list.
It is possible for them to be both owner and moderator.
"""
return self._check_membership(address=address,
allowed_roles=('owner', 'moderator'))
......@@ -3,16 +3,256 @@ interactions:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [!!python/unicode 'Basic cmVzdGFkbWluOnJlc3RwYXNz']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.1.2a1]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: !!python/unicode '404 Not Found'}
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Sun, 25 Feb 2018 03:57:24 GMT']
server: [WSGIServer/0.2 CPython/3.5.2]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:12:30 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:13:00 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:13:34 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:14:08 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:15:56 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:17:50 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:18:54 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:19:53 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:20:29 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:22:08 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:28:12 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:46:20 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:47:04 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
content-length: ['13']
content-type: [application/json; charset=UTF-8]
date: ['Wed, 16 May 2018 00:48:11 GMT']
server: [WSGIServer/0.2 CPython/3.6.4+]
status: {code: 404, message: Not Found}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
authorization: [Basic cmVzdGFkbWluOnJlc3RwYXNz]
user-agent: [GNU Mailman REST client v3.2.0b3]
method: GET
uri: http://localhost:9001/3.0/domains/example.org
response:
body: {string: 404 Not Found}
headers:
Date: ['Wed, 16 May 2018 00:50:21 GMT']
Server: [WSGIServer/0.2 CPython/3.6.4+]
content-length: ['13']
content-type: [application/json; charset=UTF-8]
status: {code: 404, message: Not Found}
version: 1
This diff is collapsed.
# Copyright (C) 2017-2018 by the Free Software Foundation, Inc.
#
# This file is part of mailman.client.
#
# mailman.client is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# mailman.client 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 Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with mailman.client. If not, see <http://www.gnu.org/licenses/>.
"""Tests for Mailing List."""
from __future__ import absolute_import, print_function, unicode_literals
from urllib.error import HTTPError
from unittest import TestCase
from mailmanclient import Client
class TestMailingListMembershipTests(TestCase):
def setUp(self):
self._client = Client(
'http://localhost:9001/3.1', 'restadmin', 'restpass')
try:
self.domain = self._client.create_domain('example.com')
except HTTPError:
self.domain = self._client.get_domain('example.com')
self.mlist = self.domain.create_list('foo')
def tearDown(self):
self.domain.delete()
def test_list_is_owner(self):
# Tests MailingList.is_owner
# First, we add an owner to the mailing list and then make sure that
# we see it in the owners roster.
anne_addr = '[email protected]'
self.mlist.add_owner(anne_addr)
# Check that the address
owners_list = [owner.email for owner in self.mlist.owners]
self.assertIn(anne_addr, owners_list)
# Now, we make sure that we get the same result in our API.
self.assertTrue(self.mlist.is_owner(anne_addr))
# Make sure we get False for someone who is not a list owner.
self.assertFalse(self.mlist.is_owner('[email protected]'))
# Make sure that a subscriber doesn't return True for is_owner check.
# We are doing this test because of the way is_owner test works. A
# wrong value for `role` could result in a list member being tested
# as owner.
self.mlist.subscribe('[email protected]')
self.assertFalse(self.mlist.is_owner('[email protected]'))
# Now, try the same thing for Moderators.
self.mlist.add_moderator('[email protected]')
self.assertFalse(self.mlist.is_owner('[email protected]'))
def test_list_is_moderator(self):
# Tests MailingList.is_moderator
# First, we add a moderator to the list.
mod_addr = '[email protected]'
self.mlist.add_moderator(mod_addr)
mods_emails = [mod.email for mod in self.mlist.moderators]
self.assertIn(mod_addr, mods_emails)
self.assertFalse(self.mlist.is_owner(mod_addr))
# Owners shouldn't return true for this API.
owner_addr = '[email protected]'
self.mlist.add_owner(owner_addr)
self.assertFalse(self.mlist.is_moderator(owner_addr))
# Subscribers shouldn't return true for this API.
subscriber_addr = '[email protected]'
self.mlist.subscribe(subscriber_addr)
self.assertFalse(self.mlist.is_moderator(subscriber_addr))
def test_list_is_member(self):
# Tests MailingList.is_member
subscriber_addr = '[email protected]'
self.mlist.subscribe(subscriber_addr, pre_verified=True,
pre_confirmed=True, pre_approved=True)
all_subscribers = [member.email for member in self.mlist.members]
self.assertIn(subscriber_addr, all_subscribers)
# Now make sure we get the same result through this API.
self.assertTrue(self.mlist.is_member(subscriber_addr))
# Make sure owners don't pass this check.
owner_addr = '[email protected]'
self.mlist.add_owner(owner_addr)
self.assertFalse(self.mlist.is_member(owner_addr))
# Make sure moderators don't pass this check.
mod_addr = '[email protected]'
self.mlist.add_moderator(mod_addr)
self.assertFalse(self.mlist.is_member(mod_addr))
def test_list_is_owner_or_mod(self):
# Tests MailingList.is_owner_or_mod
# Tests MailingList.is_moderator
# First, we add a moderator to the list.
mod_addr = '[email protected]'
self.mlist.add_moderator(mod_addr)
mods_emails = [mod.email for mod in self.mlist.moderators]
self.assertIn(mod_addr, mods_emails)
self.assertTrue(self.mlist.is_owner_or_mod(mod_addr))
# Owners shouldn't return true for this API.
owner_addr = '[email protected]'
self.mlist.add_owner(owner_addr)
owners_list = [owner.email for owner in self.mlist.owners]
self.assertIn(owner_addr, owners_list)
self.assertTrue(self.mlist.is_owner_or_mod(owner_addr))
# Subscribers shouldn't return true for this API.
subscriber_addr = '[email protected]'
self.mlist.subscribe(subscriber_addr)
self.assertFalse(self.mlist.is_owner_or_mod(subscriber_addr))
......@@ -3,7 +3,7 @@ envlist = py{35,36},lint
[testenv]
usedevelop = True
commands = python -m pytest --vcr-record-mode=none
commands = python -m pytest --vcr-record-mode=once
deps =
WebTest
contextlib2
......@@ -13,7 +13,7 @@ deps =
pytest-vcr
[testenv:record]
basepython = python2
basepython = python3
commands = pytest --vcr-record-mode=once
[testenv:lint]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment