def test_send_email(self): with patch("smtplib.SMTP") as mock_smtp: to_address = "*****@*****.**" subject = "testSubject" content = "testContent" send_email(content, to_address, subject) instance = mock_smtp.return_value self.assertTrue(instance.sendmail.called)
def test_send_email_no_to_address(self): with patch("smtplib.SMTP") as mock_smtp: to_address = None subject = "testSubject" content = "testContent" try: send_email(content, to_address, subject) assert False except Exception: instance = mock_smtp.return_value self.assertFalse(instance.sendmail.called)
def handle_new_request_action(self, username, request_action): '''Handles sending email to the person who created the request, as well as updating the state of the request depending on the data sent. :param username: The user's name. :type username: string :param request_action: The current action. Can be either reply or reject :type request_action: string :rtype: json ''' data = dict(toolkit.request.POST) counters_data_dict = {'package_id': data['package_id'], 'flag': ''} if 'rejected' in data: data['rejected'] = asbool(data['rejected']) counters_data_dict['flag'] = 'declined' else: counters_data_dict['flag'] = 'replied' message_content = data.get('message_content') if message_content is None or message_content == '': payload = { 'success': False, 'error': { 'message_content': 'Missing value' } } return json.dumps(payload) try: _get_action('requestdata_request_patch', data) except NotAuthorized: abort(403, _('Not authorized to use this action.')) except ValidationError as e: error = {'success': False, 'error': {'fields': e.error_dict}} return json.dumps(error) to = data['send_to'] subject = config.get('ckan.site_title') + ': Data request ' +\ request_action file = data.get('file_upload') response = send_email(message_content, to, subject, file=file) if response['success'] is False: error = { 'success': False, 'error': { 'fields': { 'email': response['message'] } } } return json.dumps(error) success = {'success': True, 'message': 'Message was sent successfully'} get_action('requestdata_increment_request_data_counters')( {}, counters_data_dict) return json.dumps(success)
def send_request(self): '''Send mail to resource owner. :param data: Contact form data. :type data: object :rtype: json ''' context = { 'model': model, 'session': model.Session, 'user': c.user, 'auth_user_obj': c.userobj } try: if p.toolkit.request.method == 'POST': data = dict(toolkit.request.POST) _get_action('requestdata_request_create', data) except NotAuthorized: abort(403, _('Unauthorized to update this dataset.')) except ValidationError as e: error = {'success': False, 'error': {'fields': e.error_dict}} return json.dumps(error) data_dict = {'id': data['package_id']} package = _get_action('package_show', data_dict) sender_name = data.get('sender_name', '') user_obj = context['auth_user_obj'] data_dict = {'id': user_obj.id, 'permission': 'read'} organizations = _get_action('organization_list_for_user', data_dict) orgs = [] for i in organizations: orgs.append(i['display_name']) org = ','.join(orgs) dataset_name = package['name'] dataset_title = package['title'] email = user_obj.email message = data['message_content'] creator_user_id = package['creator_user_id'] data_owner =\ _get_action('user_show', {'id': creator_user_id}).get('name') if len(get_sysadmins()) > 0: sysadmin = get_sysadmins()[0].name context_sysadmin = { 'model': model, 'session': model.Session, 'user': sysadmin, 'auth_user_obj': c.userobj } to = package['maintainer'] if to is None: message = { 'success': False, 'error': { 'fields': { 'email': 'Dataset maintainer email not found.' } } } return json.dumps(message) maintainers = to.split(',') data_dict = {'users': []} users_email = [] only_org_admins = False data_maintainers = [] # Get users objects from maintainers list for id in maintainers: try: user =\ toolkit.get_action('user_show')(context_sysadmin, {'id': id}) data_dict['users'].append(user) users_email.append(user['email']) data_maintainers.append(user['fullname'] or user['name']) except NotFound: pass mail_subject =\ config.get('ckan.site_title') + ': New data request "'\ + dataset_title + '"' if len(users_email) == 0: admins = self._org_admins_for_dataset(dataset_name) for admin in admins: users_email.append(admin.get('email')) data_maintainers.append(admin.get('fullname')) only_org_admins = True content = _get_email_configuration(sender_name, data_owner, dataset_name, email, message, org, data_maintainers, only_org_admins=only_org_admins) response_message = \ emailer.send_email(content, users_email, mail_subject) # notify package creator that new data request was made _get_action('requestdata_notification_create', data_dict) data_dict = {'package_id': data['package_id'], 'flag': 'request'} action_name = 'requestdata_increment_request_data_counters' _get_action(action_name, data_dict) return json.dumps(response_message) else: message = { 'success': True, 'message': 'Request sent, but email message was not sent.' } return json.dumps(message)