def test_metrics_validate_list(): # validate_list with patch.object(address, 'mail_exchanger_lookup') as mock_method: mock_method.side_effect = mock_exchanger_lookup assert_equal( len( address.validate_list('[email protected], [email protected]', metrics=True)), 2) p, m = address.validate_list('[email protected], [email protected]', metrics=True) assert_equal('parsing' in m, True) assert_equal('mx_lookup' in m, True) assert_equal('dns_lookup' in m, True) assert_equal('mx_conn' in m, True) assert_equal('custom_grammar' in m, True) assert_equal( isinstance( address.validate_list('[email protected], [email protected]', metrics=False), address.AddressList), True) assert_equal( isinstance( address.validate_list('[email protected], [email protected]'), address.AddressList), True)
def test_metrics_validate_list(): # validate_list with patch.object(validate, 'mail_exchanger_lookup') as mock_method: mock_method.side_effect = mock_exchanger_lookup assert_equal(len(address.validate_list('[email protected], [email protected]', metrics=True)), 2) p, m = address.validate_list('[email protected], [email protected]', metrics=True) assert_equal('parsing' in m, True) assert_equal('mx_lookup' in m, True) assert_equal('dns_lookup' in m, True) assert_equal('mx_conn' in m, True) assert_equal('custom_grammar' in m, True) assert_equal(isinstance(address.validate_list('[email protected], [email protected]', metrics=False), address.AddressList), True) assert_equal(isinstance(address.validate_list('[email protected], [email protected]'), address.AddressList), True)
def test_parse_syntax_only_false(): # syntax + validation valid_tld_list = [i + '@ai' for i in valid_localparts()] valid_domain_list = [i + '@mailgun.org' for i in valid_localparts()] valid_subdomain_list = [i + '@fakecompany.mailgun.org' for i in valid_localparts()] invalid_mx_list = [i + '@example.com' for i in valid_localparts(True)] invalid_tld_list = [i + '@com' for i in invalid_localparts(True)] invalid_domain_list = [i + '@example.com' for i in invalid_localparts(True)] invalid_subdomain_list = [i + '@sub.example.com' for i in invalid_localparts(True)] all_valid_list = valid_tld_list + valid_domain_list + valid_subdomain_list all_invalid_list = invalid_mx_list + invalid_tld_list + invalid_domain_list + \ invalid_subdomain_list all_list = all_valid_list + all_invalid_list # all valid with patch.object(validate, 'mail_exchanger_lookup') as mock_method: mock_method.side_effect = mock_exchanger_lookup parse, unpar = address.validate_list(', '.join(valid_tld_list), as_tuple=True) assert_equal(parse, valid_tld_list) assert_equal(unpar, []) parse, unpar = address.validate_list(', '.join(valid_domain_list), as_tuple=True) assert_equal(parse, valid_domain_list) assert_equal(unpar, []) parse, unpar = address.validate_list(', '.join(valid_subdomain_list), as_tuple=True) assert_equal(parse, valid_subdomain_list) assert_equal(unpar, []) # all invalid parse, unpar = address.validate_list(', '.join(invalid_mx_list), as_tuple=True) assert_equal(parse, []) assert_equal(unpar, invalid_mx_list) parse, unpar = address.validate_list(', '.join(invalid_tld_list), as_tuple=True) assert_equal(parse, []) assert_equal(unpar, invalid_tld_list) parse, unpar = address.validate_list(', '.join(invalid_domain_list), as_tuple=True) assert_equal(parse, []) assert_equal(unpar, invalid_domain_list) parse, unpar = address.validate_list(', '.join(invalid_subdomain_list), as_tuple=True) assert_equal(parse, []) assert_equal(unpar, invalid_subdomain_list) parse, unpar = address.validate_list(', '.join(all_list), as_tuple=True) assert_equal(parse, all_valid_list) assert_equal(unpar, all_invalid_list)
def test_parse_syntax_only_false(): # syntax + validation valid_tld_list = [i + '@ai' for i in valid_localparts()] valid_domain_list = [i + '@mailgun.org' for i in valid_localparts()] valid_subdomain_list = [i + '@fakecompany.mailgun.org' for i in valid_localparts()] invalid_mx_list = [i + '@example.com' for i in valid_localparts(True)] invalid_tld_list = [i + '@com' for i in invalid_localparts(True)] invalid_domain_list = [i + '@example.com' for i in invalid_localparts(True)] invalid_subdomain_list = [i + '@sub.example.com' for i in invalid_localparts(True)] all_valid_list = valid_tld_list + valid_domain_list + valid_subdomain_list all_invalid_list = invalid_mx_list + invalid_tld_list + invalid_domain_list + \ invalid_subdomain_list all_list = all_valid_list + all_invalid_list # all valid with patch.object(address, 'mail_exchanger_lookup') as mock_method: mock_method.side_effect = mock_exchanger_lookup parse, unpar = address.validate_list(', '.join(valid_tld_list), as_tuple=True) assert_equal(parse, valid_tld_list) assert_equal(unpar, []) parse, unpar = address.validate_list(', '.join(valid_domain_list), as_tuple=True) assert_equal(parse, valid_domain_list) assert_equal(unpar, []) parse, unpar = address.validate_list(', '.join(valid_subdomain_list), as_tuple=True) assert_equal(parse, valid_subdomain_list) assert_equal(unpar, []) # all invalid parse, unpar = address.validate_list(invalid_mx_list, as_tuple=True) assert_equal(parse, []) assert_equal(unpar, invalid_mx_list) parse, unpar = address.validate_list(invalid_tld_list, as_tuple=True) assert_equal(parse, []) assert_equal(unpar, invalid_tld_list) parse, unpar = address.validate_list(invalid_domain_list, as_tuple=True) assert_equal(parse, []) assert_equal(unpar, invalid_domain_list) parse, unpar = address.validate_list(invalid_subdomain_list, as_tuple=True) assert_equal(parse, []) assert_equal(unpar, invalid_subdomain_list) parse, unpar = address.validate_list(all_list, as_tuple=True) assert_equal(parse, all_valid_list) assert_equal(unpar, all_invalid_list)
def test_mx_aol_dual_lookup(ld, cmx): ld.return_value = ['mailin-01.mx.aol.com', 'mailin-02.mx.aol.com'] cmx.return_value = 'mailin-03.mx.aol.com' # Invalidate managed email response out of pattern mailbox = '*****@*****.**' addr = address.validate_address(mailbox) assert_equal(type(addr), type(None)) # Same test but with validate_list addr = address.validate_list([mailbox]) expected = 'flanker.addresslib.address:' assert_equal(addr, []) # Allow AOL MX unmanaged mailboxes to pass remaining patterns mailbox = '*****@*****.**' addr = address.validate_address(mailbox) assert_equal(addr, mailbox) # Same test but with validate_list expected = 'flanker.addresslib.address:' addr = address.validate_list([mailbox]) assert_equal(addr, mailbox)
def test_mx_yahoo_dual_lookup(ld, cmx): ld.return_value = ['mta7.am0.yahoodns.net', 'mta6.am0.yahoodns.net'] cmx.return_value = 'mta5.am0.yahoodns.net' # Invalidate managed email response out of pattern mailbox = '*****@*****.**' addr = address.validate_address(mailbox) assert_equal(type(addr), type(None)) # Same test but with validate_list addr = address.validate_list([mailbox]) expected = 'flanker.addresslib.address:' assert_equal(addr, []) # Allow Yahoo MX unmanaged mailboxes to pass remaining patterns mailbox = '*****@*****.**' addr = address.validate_address(mailbox) assert_equal(addr, mailbox) # Same test but with validate_list expected = 'flanker.addresslib.address:' addr = address.validate_list([mailbox]) assert_equal(addr, mailbox)
def main_view(): if request.method == 'POST': email = request.form['input'] list_email = list(set(email.split("\r\n"))) #remove duplicate emails and split each email from the new line #sort out the emails with commas, colons and spaces and the ones without list_without_problems, list_with_problems = [], [] for email in list_email: if ' ' in email or ';' in email or ',' in email: list_with_problems.append(email) else: list_without_problems.append(email) unique_emails = ','.join(list_without_problems) #transform list to string #validate the good list of emails valid = address.validate_list(unique_emails, as_tuple=True) valids = valid[0] #first list in tuple is valid emails invalids = valid[1] #second is invalid emails #individually check the list with commas, colons, and spaces if len(list_with_problems) != 0: for email in list_with_problems: if address.validate_address(email): valids.append(valids) else: invalids.append(email) #add suggestions if there are invalid emails suggests = [] if len(invalids) != 0: invalids = [unique_emails.encode("ascii") for unique_emails in invalids] #invalid emails come out as unicode - change to regular str and replace @@@ back to space suggests = [str(validate.suggest_alternate(unique_emails)).replace(" ", "") for unique_emails in invalids] #suggest alternate returns None - cast None to str return render_template('home.html', invalids=invalids, valids=valids, suggests=suggests, valid_length=len(valids), invalid_length=len(invalids)) else: return render_template('home.html')
def put_a_m_n_invite(self, handle, team, rqurl=None, rqform=None, *args, **kargs): d = {} self.EMM = EmailModel() collabraw = rqform.get('emails') valid_emails, invalid_emails = address.validate_list(collabraw, as_tuple=True) self.lggr.debug('valid_emails:%s' % valid_emails) self.lggr.debug('invalid_emails:%s' % invalid_emails) #2. If it is an email, send ring subscription url/token o = urlparse.urlparse(rqurl) host_url = urlparse.urlunparse((o.scheme, o.netloc, '', '', '', '')) #2a PENDING # Subtract team object from org document peopleteams = self.MAM.is_org(handle) if peopleteams: for teamd in peopleteams['teams']: teamfound = False if teamd['teamname'] == team: # This team actually exists in this org. You can continue with the invitations teamfound = True break if not teamfound: # This team doesnt exist. No invitation can be sent #d['redirect'] = '/'+handle+'/_teams/'+team d['redirect'] = url_for('avispa_rest.teams_a_m_n', handle=handle, team=team, _external=True, _scheme=URL_SCHEME) return d # teamd carries the team object #Try to convert invalid_emails (myringIDs) into valid_emails for invite_handle in invalid_emails: user_doc = self.MAM.select_user_doc_view('auth/userbyhandle', invite_handle) if user_doc: if user_doc['email'] not in valid_emails: valid_emails.append(user_doc['email']) for email in valid_emails: email = str(email) invite = {} invite['email'] = email invite['count'] = 1 invite['team'] = team invite['token'] = flask_bcrypt.generate_password_hash( email + str(random.randint(0, 9999))) invite['lasttime'] = str(datetime.now()) invite['author'] = current_user.id user_doc = self.MAM.select_user_doc_view('auth/userbyemail', email) if user_doc: #You are inviting an existing myRing user existinguser = True else: #You are inviting a soon to be myRing user existinguser = False token = invite['token'] to = email subject = handle + " has invited you to collaborate in the following team : " + team # https://avispa.myring.io/_register?h=cdmit&t=staff&k=11111&[email protected] content = "Click here to start working with this team: " + host_url + "/_register?h=" + handle + "&t=" + team + "&k=" + token + "&e=" + email self.lggr.debug('%s,%s,%s' % (to, subject, content)) if self.EMM.send_one_email(to, subject, content): flash("Invitation email sent.", 'UI') self.MAM.append_to_user_field(handle, 'invitations', invite) else: flash("Invitation email failed.", 'UI') #d['redirect'] = '/'+handle+'/_teams/'+team d['redirect'] = url_for('avispa_rest.teams_a_m_n', handle=handle, team=team, _external=True, _scheme=URL_SCHEME) return d
import csv import sys from flanker.addresslib import address print "Validate Emails" print "--------------------" f = open(sys.argv[1], 'rU') mails = [] invalidMails = [] try: reader = csv.DictReader(f, delimiter=',') for row in reader: mails.append(row['email']) print "Validando emails" result = address.validate_list(mails, as_tuple=True) print "--------------------" print "Mail Count: ", len(mails) print "Invalid Mail Count: ", len(result[1]) print "--------------------" print "Percentage of mails invalid: {:.1%}".format(float(len(result[1])) / float(len(mails))) finally: f.close()