Skip to content

Commit

Permalink
Merge pull request #31 from deltachat/mailcow-tags
Browse files Browse the repository at this point in the history
Add token name as tag in mailcow
  • Loading branch information
missytake authored Jun 21, 2022
2 parents 2d03c0a + f1e5ed8 commit 50f04f5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/mailadm/conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def add_email_account(self, token_info, addr=None, password=None):
user_info.password = password

# seems that everything is fine so far, so let's invoke mailcow:
self.get_mailcow_connection().add_user_mailcow(addr, password)
self.get_mailcow_connection().add_user_mailcow(addr, password, token_info.name)

return user_info

Expand Down
11 changes: 9 additions & 2 deletions src/mailadm/mailcow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
class MailcowConnection:
"""Class to manage requests to the mailcow instance.
:param config: the mailadm config
:param mailcow_endpoint: the URL to the mailcow API
:param mailcow_token: the access token to the mailcow API
"""
def __init__(self, mailcow_endpoint, mailcow_token):
self.mailcow_endpoint = mailcow_endpoint
self.auth = {"X-API-Key": mailcow_token}

def add_user_mailcow(self, addr, password, quota=0):
def add_user_mailcow(self, addr, password, token, quota=0):
"""HTTP Request to add a user to the mailcow instance.
:param addr: the email address of the new account
:param password: the password of the new account
:param token: the mailadm token used for account creation
:param quota: the maximum mailbox storage in MB. default: unlimited
"""
url = self.mailcow_endpoint + "add/mailbox"
Expand All @@ -28,6 +30,7 @@ def add_user_mailcow(self, addr, password, quota=0):
"force_pw_update": False,
"tls_enforce_in": False,
"tls_enforce_out": False,
"tags": ["mailadm:" + token]
}
result = r.post(url, json=payload, headers=self.auth)
if type(result.json()) != list or result.json()[0].get("type") != "success":
Expand Down Expand Up @@ -75,6 +78,10 @@ class MailcowUser(object):
def __init__(self, json):
self.addr = json.get("username")
self.quota = json.get("quota")
for tag in json.get("tags", []):
if "mailadm:" in tag:
self.token = tag.strip("mailadm:")
break


class MailcowError(Exception):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_adduser_mailcow_exists(conn, mailcow):
token_info = conn.add_token("burner1", expiry="1w", token="1w_7wDioPeeXyZx96v3", prefix="tmp.")
addr = "pytest.%[email protected]" % (randint(0, 999),)

mailcow.add_user_mailcow(addr, "asdf1234")
mailcow.add_user_mailcow(addr, "asdf1234", token_info.name)
with pytest.raises(MailcowError):
conn.add_email_account(token_info, addr=addr)
for user in conn.get_user_list():
Expand Down
5 changes: 3 additions & 2 deletions tests/test_mailcow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ def test_get_users(self, mailcow):

def test_add_del_user(self, mailcow):
addr = "pytest.%[email protected]" % (randint(0, 999),)
mailcow.add_user_mailcow(addr, "asdf1234")
mailcow.add_user_mailcow(addr, "asdf1234", "pytest")

user = mailcow.get_user(addr)
assert user.addr == addr
assert "pytest" == user.token

mailcow.del_user_mailcow(addr)
assert mailcow.get_user(addr) is None
Expand All @@ -24,7 +25,7 @@ def test_wrong_token(self, mailcow):
with pytest.raises(MailcowError):
mailcow.get_user_list()
with pytest.raises(MailcowError):
mailcow.add_user_mailcow(addr, "asdf1234")
mailcow.add_user_mailcow(addr, "asdf1234", "pytest")
with pytest.raises(MailcowError):
mailcow.get_user(addr)
with pytest.raises(MailcowError):
Expand Down

0 comments on commit 50f04f5

Please sign in to comment.