move get mailboxes to mailbox.py

This commit is contained in:
Son NK 2020-05-23 16:46:10 +02:00
parent 3eb6700232
commit 6280512adf
4 changed files with 49 additions and 49 deletions

View file

@ -120,27 +120,6 @@ def get_aliases_v2():
)
@api_bp.route("/mailboxes", methods=["GET"])
@cross_origin()
@require_api_auth
def get_mailboxes():
"""
Get mailboxes
Output:
- mailboxes: list of alias:
- id
- email
"""
user = g.user
return (
jsonify(
mailboxes=[{"id": mb.id, "email": mb.email} for mb in user.mailboxes()]
),
200,
)
@api_bp.route("/aliases/<int:alias_id>", methods=["DELETE"])
@cross_origin()
@require_api_auth

View file

@ -1,6 +1,6 @@
from smtplib import SMTPRecipientsRefused
from flask import g
from flask import g, jsonify
from flask import jsonify
from flask import request
from flask_cors import cross_origin
@ -149,3 +149,24 @@ def update_mailbox(mailbox_id):
db.session.commit()
return jsonify(updated=True), 200
@api_bp.route("/mailboxes", methods=["GET"])
@cross_origin()
@require_api_auth
def get_mailboxes():
"""
Get mailboxes
Output:
- mailboxes: list of alias:
- id
- email
"""
user = g.user
return (
jsonify(
mailboxes=[{"id": mb.id, "email": mb.email} for mb in user.mailboxes()]
),
200,
)

View file

@ -1,5 +1,3 @@
import json
from flask import url_for
from app.config import PAGE_LIMIT
@ -571,28 +569,3 @@ def test_get_alias(flask_client):
assert "nb_reply" in res
assert "enabled" in res
assert "note" in res
def test_get_mailboxes(flask_client):
user = User.create(
email="a@b.c", password="password", name="Test User", activated=True
)
db.session.commit()
# create api_key
api_key = ApiKey.create(user.id, "for test")
db.session.commit()
Mailbox.create(user_id=user.id, email="m1@example.com", verified=True)
Mailbox.create(user_id=user.id, email="m2@example.com", verified=False)
db.session.commit()
r = flask_client.get(
url_for("api.get_mailboxes"), headers={"Authentication": api_key.code},
)
assert r.status_code == 200
# m2@example.com is not returned as it's not verified
assert r.json == {
"mailboxes": [{"email": "a@b.c", "id": 1}, {"email": "m1@example.com", "id": 2}]
}
print(json.dumps(r.json, indent=2))

View file

@ -1,3 +1,5 @@
import json
from flask import url_for
from flask import url_for
@ -158,3 +160,28 @@ def test_cancel_mailbox_email_change(flask_client):
mb = Mailbox.get(mb.id)
assert mb.new_email is None
def test_get_mailboxes(flask_client):
user = User.create(
email="a@b.c", password="password", name="Test User", activated=True
)
db.session.commit()
# create api_key
api_key = ApiKey.create(user.id, "for test")
db.session.commit()
Mailbox.create(user_id=user.id, email="m1@example.com", verified=True)
Mailbox.create(user_id=user.id, email="m2@example.com", verified=False)
db.session.commit()
r = flask_client.get(
url_for("api.get_mailboxes"), headers={"Authentication": api_key.code},
)
assert r.status_code == 200
# m2@example.com is not returned as it's not verified
assert r.json == {
"mailboxes": [{"email": "a@b.c", "id": 1}, {"email": "m1@example.com", "id": 2}]
}
print(json.dumps(r.json, indent=2))