summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAbhilash Raj2015-04-06 03:58:22 +0530
committerAbhilash Raj2015-04-06 03:58:22 +0530
commit17fa7ac10ddd6ca0916cdcdd3a5e8c1414e9bcbc (patch)
tree8d69d19d1c47ef4ba76d75140ef5706223f6cc6b /src
parentfe12351e6f0e11f48bd714357f05aa7a34ec7e90 (diff)
downloadmailman-17fa7ac10ddd6ca0916cdcdd3a5e8c1414e9bcbc.tar.gz
mailman-17fa7ac10ddd6ca0916cdcdd3a5e8c1414e9bcbc.tar.zst
mailman-17fa7ac10ddd6ca0916cdcdd3a5e8c1414e9bcbc.zip
* implement left over methods
* add and remove owners using the address
Diffstat (limited to 'src')
-rw-r--r--src/mailman/interfaces/domain.py4
-rw-r--r--src/mailman/model/docs/domains.rst10
-rw-r--r--src/mailman/model/domain.py32
-rw-r--r--src/mailman/model/tests/test_domain.py25
-rw-r--r--src/mailman/model/user.py1
-rw-r--r--src/mailman/rest/domains.py8
-rw-r--r--src/mailman/rest/lists.py2
-rw-r--r--src/mailman/rest/tests/test_domains.py15
-rw-r--r--src/mailman/rest/users.py25
9 files changed, 67 insertions, 55 deletions
diff --git a/src/mailman/interfaces/domain.py b/src/mailman/interfaces/domain.py
index 94a9eefd3..ee5eafc78 100644
--- a/src/mailman/interfaces/domain.py
+++ b/src/mailman/interfaces/domain.py
@@ -122,8 +122,8 @@ class IDomainManager(Interface):
interface of the domain. If not given, it defaults to
http://`mail_host`/
:type base_url: string
- :param owner: Owner of the domain, defaults to None
- :type owner: `Iuser`
+ :param owners: List of owners of the domain, defaults to None
+ :type owners: list
:return: The new domain object
:rtype: `IDomain`
:raises `BadDomainSpecificationError`: when the `mail_host` is
diff --git a/src/mailman/model/docs/domains.rst b/src/mailman/model/docs/domains.rst
index dd0904f2b..ded52f817 100644
--- a/src/mailman/model/docs/domains.rst
+++ b/src/mailman/model/docs/domains.rst
@@ -58,7 +58,7 @@ Domains can have explicit descriptions.
... 'example.net',
... base_url='http://lists.example.net',
... description='The example domain',
- ... owner=user)
+ ... owners=['user@domain.com'])
<Domain example.net, The example domain,
base_url: http://lists.example.net>
@@ -67,13 +67,13 @@ Domains can have explicit descriptions.
<Domain example.net, The example domain,
base_url: http://lists.example.net>
-Domains can have multiple number of owners, ideally one of the owners
-should have a verified preferred address. However this is not checked
-right now and contact_address from config is used as a fallback.
+Domains can have multiple owners, ideally one of the owners should have a
+verified preferred address. However this is not checked right now and
+contact_address from config can be used as a fallback.
::
>>> net_domain = manager['example.net']
- >>> net_domain.add_owner(user_manager.get_user('test@example.org'))
+ >>> net_domain.add_owner('test@example.org')
Domains can list all associated mailing lists with the mailing_lists property.
diff --git a/src/mailman/model/domain.py b/src/mailman/model/domain.py
index 6809688d6..32ea7db9b 100644
--- a/src/mailman/model/domain.py
+++ b/src/mailman/model/domain.py
@@ -28,6 +28,7 @@ from mailman.database.transaction import dbconnection
from mailman.interfaces.domain import (
BadDomainSpecificationError, DomainCreatedEvent, DomainCreatingEvent,
DomainDeletedEvent, DomainDeletingEvent, IDomain, IDomainManager)
+from mailman.interfaces.usermanager import IUserManager
from mailman.model.mailinglist import MailingList
from mailman.model.user import User, DomainOwner
from urllib.parse import urljoin, urlparse
@@ -35,6 +36,7 @@ from sqlalchemy import Column, Integer, Unicode
from sqlalchemy.orm import relationship, backref
from zope.event import notify
from zope.interface import implementer
+from zope.component import getUtility
@@ -46,7 +48,7 @@ class Domain(Model):
id = Column(Integer, primary_key=True)
- mail_host = Column(Unicode) # TODO: add index?
+ mail_host = Column(Unicode)
base_url = Column(Unicode)
description = Column(Unicode)
owners = relationship("User",
@@ -56,7 +58,7 @@ class Domain(Model):
def __init__(self, mail_host,
description=None,
base_url=None,
- owner=None):
+ owners=[]):
"""Create and register a domain.
:param mail_host: The host name for the email interface.
@@ -67,16 +69,16 @@ class Domain(Model):
scheme. If not given, it will be constructed from the
`mail_host` using the http protocol.
:type base_url: string
- :param owner: The `User` who is the owner of this domain
- :type owner: `IUser`
+ :param owners: List of `User` who are the owners of this domain
+ :type owners: list
"""
self.mail_host = mail_host
self.base_url = (base_url
if base_url is not None
else 'http://' + mail_host)
self.description = description
- if owner is not None:
- self.owners.append(owner)
+ if len(owners):
+ self.add_owners(owners)
@property
def url_host(self):
@@ -112,16 +114,22 @@ class Domain(Model):
def add_owner(self, owner):
"""Add a domain owner"""
- self.owners.append(owner)
+ user_manager = getUtility(IUserManager)
+ user = user_manager.get_user(owner)
+ if user is None:
+ user = user_manager.create_user(owner)
+ self.owners.append(user)
def add_owners(self, owners):
"""Add multiple owners"""
- for each in owners:
- self.owners.append(each)
+ assert(isinstance(owners, list))
+ for owner in owners:
+ self.add_owner(owner)
def remove_owner(self, owner):
""" Remove a domain owner"""
- self.owners.remove(owner)
+ user_manager = getUtility(IUserManager)
+ self.owners.remove(user_manager.get_user(owner))
@implementer(IDomainManager)
@@ -133,7 +141,7 @@ class DomainManager:
mail_host,
description=None,
base_url=None,
- owner=None):
+ owners=[]):
"""See `IDomainManager`."""
# Be sure the mail_host is not already registered. This is probably
# a constraint that should (also) be maintained in the database.
@@ -142,7 +150,7 @@ class DomainManager:
'Duplicate email host: %s' % mail_host)
notify(DomainCreatingEvent(mail_host))
- domain = Domain(mail_host, description, base_url, owner)
+ domain = Domain(mail_host, description, base_url, owners)
store.add(domain)
notify(DomainCreatedEvent(domain))
return domain
diff --git a/src/mailman/model/tests/test_domain.py b/src/mailman/model/tests/test_domain.py
index 950c34969..8223aa00b 100644
--- a/src/mailman/model/tests/test_domain.py
+++ b/src/mailman/model/tests/test_domain.py
@@ -81,25 +81,23 @@ class TestDomainManager(unittest.TestCase):
self.assertRaises(KeyError, self._manager.remove, 'doesnotexist.com')
def test_domain_create_with_owner(self):
- user = getUtility(IUserManager).create_user('someuser@example.org')
- config.db.commit()
- domain = self._manager.add('example.org', owner=user)
+ domain = self._manager.add('example.org',
+ owners=['someuser@example.org'])
self.assertEqual(len(domain.owners), 1)
- self.assertEqual(domain.owners[0].id, user.id)
+ self.assertEqual(domain.owners[0].addresses[0].email,
+ 'someuser@example.org')
def test_add_domain_owner(self):
- user = getUtility(IUserManager).create_user('someuser@example.org')
- config.db.commit()
domain = self._manager.add('example.org')
- domain.add_owner(user)
+ domain.add_owner('someuser@example.org')
self.assertEqual(len(domain.owners), 1)
- self.assertEqual(domain.owners[0].id, user.id)
+ self.assertEqual(domain.owners[0].addresses[0].email,
+ 'someuser@example.org')
def test_remove_domain_owner(self):
- user = getUtility(IUserManager).create_user('someuser@somedomain.org')
- config.db.commit()
- domain = self._manager.add('example.org', owner=user)
- domain.remove_owner(user)
+ domain = self._manager.add('example.org',
+ owners=['someuser@example.org'])
+ domain.remove_owner('someuser@example.org')
self.assertEqual(len(domain.owners), 0)
@@ -134,6 +132,3 @@ class TestDomainLifecycleEvents(unittest.TestCase):
self.assertEqual(listmanager.get('dog@example.org'), None)
self.assertEqual(listmanager.get('ewe@example.com'), ewe)
self.assertEqual(listmanager.get('fly@example.com'), fly)
-
- def test_owners_are_deleted_when_domain_is(self):
- self._domainmanager.remove('example.net')
diff --git a/src/mailman/model/user.py b/src/mailman/model/user.py
index a8bc556cd..5fecc1836 100644
--- a/src/mailman/model/user.py
+++ b/src/mailman/model/user.py
@@ -19,6 +19,7 @@
__all__ = [
'User',
+ 'DomainOwner'
]
diff --git a/src/mailman/rest/domains.py b/src/mailman/rest/domains.py
index 2f41ecfd9..41a1c50bd 100644
--- a/src/mailman/rest/domains.py
+++ b/src/mailman/rest/domains.py
@@ -25,6 +25,7 @@ __all__ = [
from mailman.interfaces.domain import (
BadDomainSpecificationError, IDomainManager)
+from mailman.interfaces.usermanager import IUserManager
from mailman.rest.helpers import (
BadRequest, CollectionMixin, NotFound, bad_request, child, created, etag,
no_content, not_found, okay, path_to)
@@ -110,10 +111,11 @@ class AllDomains(_DomainBase):
validator = Validator(mail_host=str,
description=str,
base_url=str,
- owner=int,
+ owners=list,
_optional=('description', 'base_url',
- 'owner'))
- domain = domain_manager.add(**validator(request))
+ 'owners'))
+ values = validator(request)
+ domain = domain_manager.add(**values)
except BadDomainSpecificationError as error:
bad_request(response, str(error))
except ValueError as error:
diff --git a/src/mailman/rest/lists.py b/src/mailman/rest/lists.py
index f6bc27917..641ddec8e 100644
--- a/src/mailman/rest/lists.py
+++ b/src/mailman/rest/lists.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2010-2015 by the Free Software Foundation, Inc.
+ # Copyright (C) 2010-2015 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
diff --git a/src/mailman/rest/tests/test_domains.py b/src/mailman/rest/tests/test_domains.py
index 9ebc0c0d8..13299516c 100644
--- a/src/mailman/rest/tests/test_domains.py
+++ b/src/mailman/rest/tests/test_domains.py
@@ -27,6 +27,7 @@ import unittest
from mailman.app.lifecycle import create_list
from mailman.database.transaction import transaction
from mailman.interfaces.listmanager import IListManager
+from mailman.interfaces.domain import IDomainManager
from mailman.testing.helpers import call_api
from mailman.testing.layers import RESTLayer
from urllib.error import HTTPError
@@ -41,10 +42,16 @@ class TestDomains(unittest.TestCase):
with transaction():
self._mlist = create_list('test@example.com')
- def test_create_domain(self):
- """Create domain via REST"""
- # TODO: Complete this
- # Tests should be failing with improper REST API.
+ def test_create_domains(self):
+ """Test Create domain via REST"""
+ data = {'mail_host': 'example.org',
+ 'description': 'Example domain',
+ 'base_url': 'http://example.org',
+ 'owners': ['someone@example.com',
+ 'secondowner@example.com',]}
+ content, response = call_api('http://localhost:9001/3.0/domains',
+ data, method="POST")
+ self.assertEqual(response.status, 201)
def test_bogus_endpoint_extension(self):
# /domains/<domain>/lists/<anything> is not a valid endpoint.
diff --git a/src/mailman/rest/users.py b/src/mailman/rest/users.py
index 5a3f0118d..b8eaee448 100644
--- a/src/mailman/rest/users.py
+++ b/src/mailman/rest/users.py
@@ -22,6 +22,7 @@ __all__ = [
'AddressUser',
'AllUsers',
'Login',
+ 'OwnersForDomain',
]
@@ -395,27 +396,25 @@ class OwnersForDomain(_UserBase):
def on_post(self, request, response):
"""POST to /domains/<domain>/owners """
- validator = Validator(owner_id=GetterSetter(int))
+ validator = Validator(owner=GetterSetter(str))
try:
values = validator(request)
except ValueError as error:
bad_request(response, str(error))
return
- owner = getUtility(IUserManager).get_user_by_id(values['owner_id'])
- self._domain.add_owner(owner)
+ self._domain.add_owner(values['owner'])
return no_content(response)
- def on_patch(self, request, response):
- # TODO: complete this
- pass
-
- def on_put(self, request, response):
- # TODO: complete this
- pass
-
def on_delete(self, request, response):
- # TODO: complete this
- pass
+ """DELETE to /domains/<domain>/owners"""
+ validator = Validator(owner=GetterSetter(str))
+ try:
+ values = validator(request)
+ except ValueError as error:
+ bad_request(response, str(error))
+ return
+ self._domain.remove_owner(owner)
+ return no_content(response)
@paginate
def _get_collection(self, request):