def test_send(self): m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_subject('test') m.set_html('WIN') m.set_text('WIN') m.set_from('*****@*****.**') m.add_substitution('subKey', 'subValue') m.add_section('testSection', 'sectionValue') m.add_category('testCategory') m.add_unique_arg('testUnique', 'uniqueValue') m.add_filter('testFilter', 'filter', 'filterValue') m.add_attachment_stream('testFile', 'fileValue') self.sg.send(m) url = self.sg._build_body(m) url.pop('api_key', None) url.pop('api_user', None) url.pop('date', None) testUrl = json.loads('''{"to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**", "headers": "", "fromname": "", "replyto": ""}''') testUrl['x-smtpapi'] = json.dumps(json.loads('''{"sub":{"subKey":["subValue"]}, "section":{"testSection":"sectionValue"}, "category":["testCategory"], "unique_args":{"testUnique":"uniqueValue"}, "filters":{"testFilter":{"settings":{"filter":"filterValue"}}}}''')) self.assertEqual(url, testUrl)
def test_send(self): m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_subject('test') m.set_html('WIN') m.set_text('WIN') m.set_from('*****@*****.**') m.set_asm_group_id(42) m.add_cc('*****@*****.**') m.add_bcc('*****@*****.**') m.add_substitution('subKey', 'subValue') m.add_section('testSection', 'sectionValue') m.add_category('testCategory') m.add_unique_arg('testUnique', 'uniqueValue') m.add_filter('testFilter', 'filter', 'filterValue') m.add_attachment_stream('testFile', 'fileValue') url = self.sg._build_body(m) url.pop('api_key', None) url.pop('api_user', None) url.pop('date', None) test_url = json.loads(''' { "to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**", "cc[]": ["*****@*****.**"], "bcc[]": ["*****@*****.**"] } ''') test_url['x-smtpapi'] = json.dumps(json.loads(''' { "sub": { "subKey": ["subValue"] }, "section": { "testSection":"sectionValue" }, "category": ["testCategory"], "unique_args": { "testUnique":"uniqueValue" }, "filters": { "testFilter": { "settings": { "filter": "filterValue" } } }, "asm_group_id": 42 } ''')) try: self.assertItemsEqual(url, test_url) except: # Python 3+ self.assertCountEqual(url, test_url)
def test_send(self): m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_subject('test') m.set_html('WIN') m.set_text('WIN') m.set_from('*****@*****.**') m.set_asm_group_id(42) m.add_cc('*****@*****.**') m.add_bcc('*****@*****.**') m.add_substitution('subKey', 'subValue') m.add_section('testSection', 'sectionValue') m.add_category('testCategory') m.add_unique_arg('testUnique', 'uniqueValue') m.add_filter('testFilter', 'filter', 'filterValue') m.add_attachment_stream('testFile', 'fileValue') url = self.sg._build_body(m) url.pop('api_key', None) url.pop('api_user', None) url.pop('date', None) test_url = json.loads(''' { "to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**", "cc[]": ["*****@*****.**"], "bcc[]": ["*****@*****.**"] } ''') test_url['x-smtpapi'] = json.dumps( json.loads(''' { "sub": { "subKey": ["subValue"] }, "section": { "testSection":"sectionValue" }, "category": ["testCategory"], "unique_args": { "testUnique":"uniqueValue" }, "filters": { "testFilter": { "settings": { "filter": "filterValue" } } }, "asm_group_id": 42 } ''')) self.assertEqual(url, test_url)
def test_send(self): m = Mail() m.add_to("John, Doe <*****@*****.**>") m.set_subject("test") m.set_html("WIN") m.set_text("WIN") m.set_from("*****@*****.**") m.add_substitution("subKey", "subValue") m.add_section("testSection", "sectionValue") m.add_category("testCategory") m.add_unique_arg("testUnique", "uniqueValue") m.add_filter("testFilter", "filter", "filterValue") m.add_attachment_stream("testFile", "fileValue") url = self.sg._build_body(m) url.pop("api_key", None) url.pop("api_user", None) url.pop("date", None) test_url = json.loads( """ { "to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**" } """ ) test_url["x-smtpapi"] = json.dumps( json.loads( """ { "to" : ["John, Doe <*****@*****.**>"], "sub": { "subKey": ["subValue"] }, "section": { "testSection":"sectionValue" }, "category": ["testCategory"], "unique_args": { "testUnique":"uniqueValue" }, "filters": { "testFilter": { "settings": { "filter": "filterValue" } } } } """ ) ) self.assertEqual(url, test_url)
def post(self): applicant = self.user application_key = ndb.Key(urlsafe=self.request.get('form-key')) application = application_key.get() if self._no_verify() or application.submit_time: logging.info("Attempt to modify verification by %s", applicant.email) self._serve_page() return task = self.request.get('task') if task != 'applicant': user_id = self.user.get_id() token = self.user_model.create_signup_token(user_id) verification_url = self.uri_for('verification', type='v', user_id=user_id, signup_token=token, _full=True) logging.info(verification_url) config = ndb.Key(Settings, 'config').get() sg = SendGridClient(config.sendgrid_username, config.sendgrid_password, secure=True) verification_email = Mail(from_name="NYDKC Awards Committee", from_email="*****@*****.**", subject="Distinguished Key Clubber Application Verification for %s %s" % (applicant.first_name, applicant.last_name) ) verifier = "" if task == 'ltg': application.verification_ltg_email = self.request.get('ltg-email') application.verification_ltg_token = token application.verification_ltg_sent = True verification_email.add_to(application.verification_ltg_email) verifier = "Lieutenant Governor " + applicant.ltg.title() elif task == 'club-president': application.verification_club_president_email = self.request.get('club-president-email') application.verification_club_president_token = token application.verification_club_president_sent = True verification_email.add_to(application.verification_club_president_email) verifier = "Club President " + applicant.club_president.title() elif task == 'faculty-advisor': application.verification_faculty_advisor_email = self.request.get('faculty-advisor-email') application.verification_faculty_advisor_token = token application.verification_faculty_advisor_sent = True verification_email.add_to(application.verification_faculty_advisor_email) verifier = "Faculty Advisor " + applicant.faculty_advisor.title() template_values = { 'applicant': applicant, 'verification_url': verification_url, 'verifier': verifier } verification_email.set_html(JINJA_ENVIRONMENT.get_template('verification-email.html').render(template_values)) htmlhandler = html2text.HTML2Text() verification_email.set_text(htmlhandler.handle(verification_email.html).encode("UTF+8")) verification_email.add_unique_arg('user_id', user_id) code, response = sg.send(verification_email) response = json.loads(response) if response["message"] == "error": logging.error(("Problem with sending email to %s: " % verification_email.to) + str(response["errors"])) self._serve_page() return else: application.verification_applicant = True application.verification_applicant_date = datetime.now() application.put() self._serve_page()