|
1 | 1 | from unittest import mock |
2 | 2 |
|
| 3 | +from hubspot_api.utils import ContactException |
| 4 | +from models.contact import Contact |
| 5 | + |
3 | 6 |
|
4 | 7 | @mock.patch("routes.v1.contact.utils.get_contact_by_email") |
5 | 8 | def test_get_contact_by_email(mock_get_contact_by_email, client): |
@@ -43,3 +46,68 @@ def test_create_contact( |
43 | 46 | ) |
44 | 47 | assert response.status_code == 200 |
45 | 48 | assert response.json() == {"contact_id": 12356} |
| 49 | + |
| 50 | + |
| 51 | +def test_create_contact_returns_duplicate_email_error(organization, contact, client): |
| 52 | + response = client.post( |
| 53 | + "/api/v1/contacts/", |
| 54 | + json={ |
| 55 | + "first_name": "raaj", |
| 56 | + "last_name": "das", |
| 57 | + |
| 58 | + "phone": "2547120202002", |
| 59 | + "company_name": "Test org", |
| 60 | + }, |
| 61 | + ) |
| 62 | + assert response.status_code == 200 |
| 63 | + assert response.json() == { |
| 64 | + "detail": "duplicate key value violates unique constraint " |
| 65 | + '"contact_email_key"\nDETAIL: Key (email)=(' |
| 66 | + "[email protected]) already exists.\n" |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | +@mock.patch("routes.v1.contact._contact.delete_contact") |
| 71 | +@mock.patch("routes.v1.contact.utils.create_contact") |
| 72 | +def test_create_contact_raises_hubspot_contactexception( |
| 73 | + mock_create_contact, mock_delete_contact, organization, client |
| 74 | +): |
| 75 | + mock_create_contact.side_effect = ContactException( |
| 76 | + "Email with this contact exists " "in Hubspot" |
| 77 | + ) |
| 78 | + mock_delete_contact.return_value = True |
| 79 | + response = client.post( |
| 80 | + "/api/v1/contacts/", |
| 81 | + json={ |
| 82 | + "first_name": "test", |
| 83 | + "last_name": "user", |
| 84 | + |
| 85 | + "phone": "2547120202002", |
| 86 | + "company_name": "Test org", |
| 87 | + }, |
| 88 | + ) |
| 89 | + assert response.status_code == 200 |
| 90 | + assert response.json() == {"detail": "Email with this contact exists in Hubspot"} |
| 91 | + |
| 92 | + |
| 93 | +@mock.patch("routes.v1.contact.utils.create_contact") |
| 94 | +def test_create_contact_rollbacks_db_contact_for_hubspot_exception( |
| 95 | + mock_create_contact, db, organization, client |
| 96 | +): |
| 97 | + mock_create_contact.side_effect = ContactException( |
| 98 | + "Email with this contact exists " "in Hubspot" |
| 99 | + ) |
| 100 | + response = client.post( |
| 101 | + "/api/v1/contacts/", |
| 102 | + json={ |
| 103 | + "first_name": "test", |
| 104 | + "last_name": "user", |
| 105 | + |
| 106 | + "phone": "2547120202002", |
| 107 | + "company_name": "Test org", |
| 108 | + }, |
| 109 | + ) |
| 110 | + contact = db. query( Contact). filter_by( email="[email protected]"). all() |
| 111 | + assert response.status_code == 200 |
| 112 | + assert response.json() == {"detail": "Email with this contact exists in Hubspot"} |
| 113 | + assert contact == [] |
0 commit comments