Browse Source

Add DELETE /api/contacts/:contact_id

Son NK 5 years ago
parent
commit
0d725588ae
3 changed files with 72 additions and 0 deletions
  1. 18 0
      README.md
  2. 25 0
      app/api/views/alias.py
  3. 29 0
      tests/api/test_alias.py

+ 18 - 0
README.md

@@ -978,6 +978,24 @@ Return 409 if contact is already added.
 }
 }
 ```
 ```
 
 
+#### DELETE /api/contacts/:contact_id
+
+Delete a contact
+
+Input:
+- `Authentication` header that contains the api key
+- `contact_id` in url.
+
+Output:
+If success, 200.
+
+
+```json
+{
+    "deleted": true
+}
+```
+
 ### Database migration
 ### Database migration
 
 
 The database migration is handled by `alembic`
 The database migration is handled by `alembic`

+ 25 - 0
app/api/views/alias.py

@@ -320,3 +320,28 @@ def create_contact_route(alias_id):
     db.session.commit()
     db.session.commit()
 
 
     return jsonify(**serialize_contact(contact)), 201
     return jsonify(**serialize_contact(contact)), 201
+
+
+@api_bp.route("/contacts/<int:contact_id>", methods=["DELETE"])
+@cross_origin()
+@verify_api_key
+def delete_contact(contact_id):
+    """
+    Delete contact
+    Input:
+        contact_id: in url
+    Output:
+        200
+
+
+    """
+    user = g.user
+    contact = Contact.get(contact_id)
+
+    if not contact or contact.alias.user_id != user.id:
+        return jsonify(error="Forbidden"), 403
+
+    Contact.delete(contact_id)
+    db.session.commit()
+
+    return jsonify(deleted=True), 200

+ 29 - 0
tests/api/test_alias.py

@@ -267,3 +267,32 @@ def test_create_contact_route(flask_client):
         json={"contact": "First2 Last2 <first@example.com>"},
         json={"contact": "First2 Last2 <first@example.com>"},
     )
     )
     assert r.status_code == 409
     assert r.status_code == 409
+
+
+def test_delete_contact(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()
+
+    alias = Alias.create_new_random(user)
+    db.session.commit()
+
+    contact = Contact.create(
+        alias_id=alias.id,
+        website_email="contact@example.com",
+        reply_email="reply+random@sl.io",
+    )
+    db.session.commit()
+
+    r = flask_client.delete(
+        url_for("api.delete_contact", contact_id=contact.id),
+        headers={"Authentication": api_key.code},
+    )
+
+    assert r.status_code == 200
+    assert r.json == {"deleted": True}