def test_make_contact_sync_message(user): """Test make_contact_sync_message serializes a user and returns a properly formatted sync message""" contact_sync_message = api.make_contact_sync_message(user.id) serialized_user = UserSerializer(user).data serialized_user.update(serialized_user.pop("legal_address") or {}) serialized_user.update(serialized_user.pop("profile") or {}) serialized_user["street_address"] = "\n".join(serialized_user.pop("street_address")) serialized_user.pop("unused_coupons") assert contact_sync_message == [ { "integratorObjectId": "{}-{}".format(settings.HUBSPOT_ID_PREFIX, user.id), "action": "UPSERT", "changeOccurredTimestamp": any_instance_of(int), "propertyNameToValues": api.sanitize_properties(serialized_user), } ]
def make_contact_sync_message(user_id): """ Create the body of a sync message for a contact. This will flatten the contained LegalAddress and Profile serialized data into one larger serializable dict Args: user_id (int): User id Returns: list: dict containing serializable sync-message data """ from users.models import User from users.serializers import UserSerializer user = User.objects.get(id=user_id) properties = UserSerializer(user).data properties.update(properties.pop("legal_address") or {}) properties.update(properties.pop("profile") or {}) properties.pop("unused_coupons") if "street_address" in properties: properties["street_address"] = "\n".join( properties.pop("street_address")) return [make_sync_message(user.id, properties)]