mirror of
https://github.com/chatmail/relay.git
synced 2026-05-13 09:24:43 +00:00
dictproxy: make NOCREATE_FILE a constant; log warning if creating account fails
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
@@ -11,6 +12,8 @@ import subprocess
|
||||
|
||||
from .database import Database
|
||||
|
||||
NOCREATE_FILE = "/etc/chatmail-nocreate"
|
||||
|
||||
|
||||
def encrypt_password(password: str):
|
||||
password = password.encode("ascii")
|
||||
@@ -27,10 +30,12 @@ def encrypt_password(password: str):
|
||||
|
||||
|
||||
def create_user(db, user, password):
|
||||
if not os.path.exists("/tmp/nocreate"):
|
||||
with db.write_transaction() as conn:
|
||||
conn.create_user(user, password)
|
||||
return dict(home=f"/home/vmail/{user}", uid="vmail", gid="vmail", password=password)
|
||||
if os.path.exists(NOCREATE_FILE):
|
||||
logging.warning(f"Didn't create account: {NOCREATE_FILE} exists. Delete the file to enable account creation.")
|
||||
return
|
||||
with db.write_transaction() as conn:
|
||||
conn.create_user(user, password)
|
||||
return dict(home=f"/home/vmail/{user}", uid="vmail", gid="vmail", password=password)
|
||||
|
||||
|
||||
def get_user_data(db, user):
|
||||
|
||||
@@ -2,6 +2,7 @@ import os
|
||||
|
||||
import pytest
|
||||
|
||||
import chatmaild.dictproxy
|
||||
from .dictproxy import get_user_data, lookup_passdb
|
||||
from .database import Database, DBError
|
||||
|
||||
@@ -14,8 +15,9 @@ def db(tmpdir):
|
||||
|
||||
|
||||
def test_basic(db):
|
||||
if os.path.exists("/tmp/nocreate"):
|
||||
os.remove("/tmp/nocreate")
|
||||
chatmaild.dictproxy.NOCREATE_FILE = "/tmp/nocreate"
|
||||
if os.path.exists(chatmaild.dictproxy.NOCREATE_FILE):
|
||||
os.remove(chatmaild.dictproxy.NOCREATE_FILE)
|
||||
lookup_passdb(db, "link2xt@c1.testrun.org", "asdf")
|
||||
data = get_user_data(db, "link2xt@c1.testrun.org")
|
||||
assert data
|
||||
@@ -31,12 +33,13 @@ def test_dont_overwrite_password_on_wrong_login(db):
|
||||
|
||||
|
||||
def test_nocreate_file(db):
|
||||
with open("/tmp/nocreate", "w+") as f:
|
||||
chatmaild.dictproxy.NOCREATE_FILE = "/tmp/nocreate"
|
||||
with open(chatmaild.dictproxy.NOCREATE_FILE, "w+") as f:
|
||||
f.write("")
|
||||
assert os.path.exists("/tmp/nocreate")
|
||||
assert os.path.exists(chatmaild.dictproxy.NOCREATE_FILE)
|
||||
lookup_passdb(db, "newuser1@something.org", "kajdlqweqwe")
|
||||
assert not get_user_data(db, "newuser1@something.org")
|
||||
os.remove("/tmp/nocreate")
|
||||
os.remove(chatmaild.dictproxy.NOCREATE_FILE)
|
||||
|
||||
|
||||
def test_db_version(db):
|
||||
|
||||
Reference in New Issue
Block a user