def create(self, _list_id: str, data: Dict[str, str]) -> None: """Mocks the create function of the mailchimp api. This function just sets the payload data to a private variable to test it. Args: _list_id: str. List Id of mailchimp list. data: dict. Payload received. """ if data['email_address'] == '*****@*****.**': self.users_data.append({ # Email: [email protected]. 'email': 'fedd8b80a7a813966263853b9af72151', 'status': data['status'] }) elif data['email_address'] == '*****@*****.**': raise mailchimpclient.MailChimpError({ 'status': 400, 'title': 'Forgotten Email Not Subscribed' }) else: raise mailchimpclient.MailChimpError({ 'status': 404, 'title': 'Invalid email', 'detail': 'Server Issue' })
def get( self, _list_id: str, subscriber_hash: str ) -> Dict[str, str]: """Mocks the get function of the mailchimp api. Args: _list_id: str. List Id of mailchimp list. subscriber_hash: str. Subscriber hash, which is an MD5 hash of subscriber's email ID. Raises: MailchimpError. Error 404 or 401 to mock API server error. Returns: dict. The updated status dict for users. """ if not self.users_data: raise mailchimpclient.MailChimpError( {'status': 401, 'detail': 'Server Error'}) for user in self.users_data: if user['email_hash'] == subscriber_hash: return user raise mailchimpclient.MailChimpError({'status': 404})
def update_members( self, _audience_id: str, data: MailChimpListsDataDict ) -> Mapping[str, Union[List[str], List[Dict[str, str]]]]: """Mocks the update_members function of the mailchimp api. Args: _audience_id: str. Audience Id of the mailchimp list. data: list(dict(str,str)). Payload received. Returns: dict. Returns correct dict based on whether invalid email was received or not. Raises: MailchimpError. Error 404 to mock API server error. """ emails = [] for user in data['members']: emails.append(user['email_address']) self.parent_emails = emails if 'invalid_email' in emails: updated_members: List[str] = [] invalid_email_dict: Dict[str, Union[List[str], List[Dict[str, str]]]] = { 'new_members': emails[1:], 'updated_members': updated_members, 'errors': [{ 'email_address': 'invalid_email' }] } return invalid_email_dict # Mocking a request issue by throwing an exception for this # particular case. if 'errored_email' in emails: raise mailchimpclient.MailChimpError({ 'status': 404, 'title': 'Server Issue', 'detail': 'Server Issue' }) valid_email_dict: Dict[str, List[str]] = { 'new_members': emails, 'updated_members': [] } return valid_email_dict