PUT /api/mailboxes/:mailbox_id: update mailbox
This commit is contained in:
parent
5ae39c85c6
commit
a76ad0485f
13
README.md
13
README.md
|
@ -1189,6 +1189,19 @@ Output:
|
|||
- 200 if deleted successfully
|
||||
- 400 if error
|
||||
|
||||
#### PUT /api/mailboxes/:mailbox_id
|
||||
|
||||
Update a mailbox.
|
||||
|
||||
Input:
|
||||
- `Authentication` header that contains the api key
|
||||
- `mailbox_id`: in url
|
||||
- (optional) `default`: boolean. Set a mailbox as default mailbox.
|
||||
|
||||
Output:
|
||||
- 200 if updated successfully
|
||||
- 400 if error
|
||||
|
||||
|
||||
### Contact endpoints
|
||||
|
||||
|
|
|
@ -83,3 +83,36 @@ def delete_mailbox(mailbox_id):
|
|||
db.session.commit()
|
||||
|
||||
return jsonify(deleted=True), 200
|
||||
|
||||
|
||||
@api_bp.route("/mailboxes/<mailbox_id>", methods=["PUT"])
|
||||
@cross_origin()
|
||||
@require_api_auth
|
||||
def update_mailbox(mailbox_id):
|
||||
"""
|
||||
Update mailbox
|
||||
Input:
|
||||
mailbox_id: in url
|
||||
default (optional): in body
|
||||
Output:
|
||||
200 if updated successfully
|
||||
|
||||
"""
|
||||
user = g.user
|
||||
mailbox = Mailbox.get(mailbox_id)
|
||||
|
||||
if not mailbox or mailbox.user_id != user.id:
|
||||
return jsonify(error="Forbidden"), 403
|
||||
|
||||
data = request.get_json() or {}
|
||||
changed = False
|
||||
if "default" in data:
|
||||
is_default = data.get("default")
|
||||
if is_default:
|
||||
user.default_mailbox_id = mailbox.id
|
||||
changed = True
|
||||
|
||||
if changed:
|
||||
db.session.commit()
|
||||
|
||||
return jsonify(deleted=True), 200
|
||||
|
|
|
@ -68,3 +68,30 @@ def test_delete_default_mailbox(flask_client):
|
|||
)
|
||||
|
||||
assert r.status_code == 400
|
||||
|
||||
|
||||
def test_update_mailbox(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()
|
||||
|
||||
# create a mailbox
|
||||
mb = Mailbox.create(user_id=user.id, email="mb@gmail.com")
|
||||
db.session.commit()
|
||||
assert user.default_mailbox_id != mb.id
|
||||
|
||||
r = flask_client.put(
|
||||
url_for("api.delete_mailbox", mailbox_id=mb.id),
|
||||
headers={"Authentication": api_key.code},
|
||||
json={"default": True},
|
||||
)
|
||||
|
||||
assert r.status_code == 200
|
||||
|
||||
mb = Mailbox.get(mb.id)
|
||||
assert user.default_mailbox_id == mb.id
|
||||
|
|
Loading…
Reference in a new issue