def test_get_by_email_id_not_found(self): """If a contact is not found by email_id, an exception is raised.""" ctms = CTMS( mock_interface("GET", 404, {"detail": "Unknown contact_id"})) with self.assertRaises(HTTPError) as context: ctms.get(email_id="unknown-id") assert context.exception.response.status_code == 404
def test_update_use_existing_lang_and_format(self, mock_slugs, mock_langs): """CTMS.update uses the existing language and format""" updated = {"updated": "fake_response"} interface = mock_interface("PATCH", 200, updated) ctms = CTMS(interface) user_data = { "token": "an-existing-user", "email_id": "an-existing-id", "lang": "fr", "format": "T", } update_data = {"newsletters": ["slug1"]} assert ctms.update(user_data, update_data) == updated interface.session.patch.assert_called_once_with( "/ctms/an-existing-id", json={ "newsletters": [{ "name": "slug1", "subscribed": True, "lang": "fr", "format": "T" }] }, )
def test_get_by_email_id(self): """If email_id is passed, GET /ctms/{email_id} is called.""" email_id = self.TEST_CTMS_CONTACT["email"]["email_id"] interface = mock_interface("GET", 200, self.TEST_CTMS_CONTACT) ctms = CTMS(interface) user_data = ctms.get(email_id=email_id) assert user_data == self.TEST_BASKET_FORMAT interface.session.get.assert_called_once_with("/ctms/a-ctms-uuid")
def test_get_by_token(self): """If token is passed, GET /ctms?basket_token={token} is called.""" token = self.TEST_CTMS_CONTACT["email"]["basket_token"] interface = mock_interface("GET", 200, [self.TEST_CTMS_CONTACT]) ctms = CTMS(interface) user_data = ctms.get(token=token) assert user_data == self.TEST_BASKET_FORMAT interface.session.get.assert_called_once_with( "/ctms", params={"basket_token": token})
def test_get_by_amo_id(self): """If amo_id is passed, GET /ctms?amo_id={amo_id} is called.""" amo_id = self.TEST_CTMS_CONTACT["amo"]["user_id"] interface = mock_interface("GET", 200, [self.TEST_CTMS_CONTACT]) ctms = CTMS(interface) user_data = ctms.get(amo_id=amo_id) assert user_data == self.TEST_BASKET_FORMAT interface.session.get.assert_called_once_with( "/ctms", params={"amo_user_id": amo_id})
def test_get_by_mofo_email_id(self): """If mofo_email_id is passed, GET /ctms?mofo_email_id={mofo_email_id} is called.""" mofo_email_id = self.TEST_CTMS_CONTACT["mofo"]["mofo_email_id"] interface = mock_interface("GET", 200, [self.TEST_CTMS_CONTACT]) ctms = CTMS(interface) user_data = ctms.get(mofo_email_id=mofo_email_id) assert user_data == self.TEST_BASKET_FORMAT interface.session.get.assert_called_once_with( "/ctms", params={"mofo_email_id": mofo_email_id})
def test_get_by_email(self): """If email is passed, GET /ctms?primary_email={email} is called.""" email = self.TEST_CTMS_CONTACT["email"]["primary_email"] interface = mock_interface("GET", 200, [self.TEST_CTMS_CONTACT]) ctms = CTMS(interface) user_data = ctms.get(email=email) assert user_data == self.TEST_BASKET_FORMAT interface.session.get.assert_called_once_with( "/ctms", params={"primary_email": email})
def test_get_by_several_ids(self): """If if multiple IDs are passed, the best is used.""" email_id = self.TEST_CTMS_CONTACT["email"]["email_id"] interface = mock_interface("GET", 200, self.TEST_CTMS_CONTACT) ctms = CTMS(interface) user_data = ctms.get( email_id=email_id, token="some-token", email="*****@*****.**" ) assert user_data == self.TEST_BASKET_FORMAT interface.session.get.assert_called_once_with(f"/ctms/{email_id}")
def test_add(self): """CTMS.add calls POST /ctms.""" created = { "email": {"basket_token": "a-new-user", "email_id": "a-new-email_id"} } interface = mock_interface("POST", 201, created) ctms = CTMS(interface) assert ctms.add({"token": "a-new-user"}) == created interface.session.post.assert_called_once_with( "/ctms", json={"email": {"basket_token": "a-new-user"}} )
def test_update_email_id_not_in_existing_data(self): """ CTMS.update requires an email_id in existing data. TODO: This should raise an exception after the SDFC to CTMS transition. However, the calling code is simplier if it does nothing when there is no existing email_id. """ ctms = CTMS("interface should not be called") user_data = {"token": "an-existing-user"} update_data = {"first_name": "Elizabeth", "email_id": "a-new-email-id"} assert ctms.update(user_data, update_data) is None
def test_get_by_several_ids_both_none(self): """If multiple alt IDs are passed and all miss, None is returned.""" interface = Mock(spec_set=["get_by_alternate_id"]) interface.get_by_alternate_id.side_effect = ([], []) ctms = CTMS(interface) user_data = ctms.get(token="some-token", email="*****@*****.**") assert user_data is None interface.get_by_alternate_id.assert_has_calls([ call(basket_token="some-token"), call(primary_email="*****@*****.**"), ])
def test_get_by_several_ids_mult_then_one(self): """If multiple alt IDs are passed, the second is tried on dupes.""" interface = Mock(spec_set=["get_by_alternate_id"]) interface.get_by_alternate_id.side_effect = ( [{"contact": 1}, {"contact": 2}], [self.TEST_CTMS_CONTACT], ) ctms = CTMS(interface) user_data = ctms.get(sfdc_id="sfdc-123", amo_id="amo-123") assert user_data == self.TEST_BASKET_FORMAT interface.get_by_alternate_id.assert_has_calls( [call(amo_user_id="amo-123"), call(sfdc_id="sfdc-123")] )
def test_get_by_several_ids_none_then_one(self): """If multiple alt IDs are passed, the second is tried on miss.""" interface = Mock(spec_set=["get_by_alternate_id"]) interface.get_by_alternate_id.side_effect = ([], [self.TEST_CTMS_CONTACT]) ctms = CTMS(interface) user_data = ctms.get(token="some-token", email="*****@*****.**") assert user_data == self.TEST_BASKET_FORMAT interface.get_by_alternate_id.assert_has_calls( [ call(basket_token="some-token"), call(primary_email="*****@*****.**"), ] )
def test_update(self): """CTMS.update calls PATCH /ctms/{email_id}.""" updated = { "email": { "basket_token": "an-existing-user", "email_id": "an-existing-id", "first_name": "Jane", } } interface = mock_interface("PATCH", 200, updated) ctms = CTMS(interface) user_data = {"token": "an-existing-user", "email_id": "an-existing-id"} update_data = {"first_name": "Jane"} assert ctms.update(user_data, update_data) == updated interface.session.patch.assert_called_once_with( "/ctms/an-existing-id", json={"email": {"first_name": "Jane"}} )
def test_update_by_token_not_found(self): """Updating by unknown basket token raises an exception.""" interface = Mock(spec_set=["get_by_alternate_id"]) interface.get_by_alternate_id.return_value = [] ctms = CTMS(interface) self.assertRaises( CTMSNotFoundByAltIDError, ctms.update_by_alt_id, "token", "the-token", {"email": "*****@*****.**"}, )
def test_update_by_token(self): """CTMS can update a record by basket token.""" record = {"email": {"email_id": "the-email-id", "token": "the-token"}} updated = { "email": { "email_id": "the-email-id", "email": "*****@*****.**", "token": "the-token", } } interface = Mock(spec_set=["get_by_alternate_id", "patch_by_email_id"]) interface.get_by_alternate_id.return_value = [record] interface.patch_by_email_id.return_value = updated ctms = CTMS(interface) resp = ctms.update_by_alt_id( "token", "the-token", {"email": "*****@*****.**"} ) assert resp == updated interface.get_by_alternate_id.assert_called_once_with(basket_token="the-token") interface.patch_by_email_id.assert_called_once_with( "the-email-id", {"email": {"primary_email": "*****@*****.**"}} )
def test_get_by_several_ids_mult_then_none(self): """If multiple alt IDs are passed, the second is tried on dupes.""" interface = Mock(spec_set=["get_by_alternate_id"]) interface.get_by_alternate_id.side_effect = ( [{"contact": 1}, {"contact": 2}], [], ) ctms = CTMS(interface) self.assertRaises( CTMSMultipleContactsError, ctms.get, sfdc_id="sfdc-456", amo_id="amo-456" ) interface.get_by_alternate_id.assert_has_calls( [call(amo_user_id="amo-456"), call(sfdc_id="sfdc-456")] )
def test_get_by_amo_id_multiple_contacts(self): """If an ID returns mutliple contacts, a RuntimeError is raised.""" amo_id = self.TEST_CTMS_CONTACT["amo"]["user_id"] contact2 = { "amo": {"user_id": amo_id}, "email": { "email_id": "other-ctms-uuid", "basket_token": "token", "primary_email": "*****@*****.**", "sfdc_id": "sfdc-id", }, "fxa": {"fxa_id": "fxa-id"}, "mofo": { "mofo_email_id": "mofo-email-id", "mofo_contact_id": "mofo-contact-id", }, } ctms = CTMS(mock_interface("GET", 200, [self.TEST_CTMS_CONTACT, contact2])) self.assertRaises(CTMSMultipleContactsError, ctms.get, amo_id=amo_id)
def test_get_by_token_not_found(self): """If a contact is not found by token, None is returned.""" ctms = CTMS(mock_interface("GET", 200, [])) assert ctms.get(token="unknown-token") is None
def test_get_no_interface(self): """If the interface is None (disabled or other issue), None is returned.""" ctms = CTMS(None) assert ctms.get(token="token") is None
def test_get_no_ids(self): """RuntimeError is raised if all IDs are None.""" ctms = CTMS("interface should not be called") self.assertRaises(CTMSNoIdsError, ctms.get, token=None)
def test_add_no_interface(self): """If the interface is None (disabled or other issue), None is returned.""" ctms = CTMS(None) assert ctms.add({"token": "a-new-user"}) is None
def test_update_no_interface(self): """If the interface is None (disabled or other issue), None is returned.""" ctms = CTMS(None) user_data = {"token": "an-existing-user", "email_id": "an-existing-id"} update_data = {"first_name": "Jane"} assert ctms.update(user_data, update_data) is None