Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2145)

Unified Diff: app_test.py

Issue 19878007: Add build mailer capability to support gatekeeper_ng. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/chromium-build@master
Patch Set: Rebase to latest master Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « app.yaml ('k') | gatekeeper_mailer.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: app_test.py
diff --git a/app_test.py b/app_test.py
index 5aaaf4d5d47bcffdf8fb350265c59b07b86c3f10..a24e144c438afc7a334baff29285f4d719970016 100644
--- a/app_test.py
+++ b/app_test.py
@@ -5,10 +5,16 @@
import ast
import datetime
+import hashlib
+import hmac
+import json
import os
+import random
+import time
import unittest
import app
+from third_party.BeautifulSoup.BeautifulSoup import BeautifulSoup
TEST_DIR = os.path.join(os.path.dirname(__file__), 'tests')
@@ -473,3 +479,74 @@ class FetchTestCase(GaeTestCase):
'http://build.chromium.org/p/chromium/console/../',
page['offsite_base'])
self.assertEquals('BuildBot: Chromium', page['title'])
+
+
+class MailTestCase(GaeTestCase):
+ def setUp(self):
+ self.test_dir = os.path.join(TEST_DIR, 'test_mailer')
+ with open(os.path.join(self.test_dir, 'input.json')) as f:
+ self.input_json = json.load(f)
+ self.build_data = json.loads(self.input_json['message'])
+
+ @staticmethod
+ def _hash_message(mytime, message, url, secret):
+ salt = random.getrandbits(32)
+ hasher = hmac.new(secret, message, hashlib.sha256)
+ hasher.update(str(mytime))
+ hasher.update(str(salt))
+ client_hash = hasher.hexdigest()
+
+ return {'message': message,
+ 'time': mytime,
+ 'salt': salt,
+ 'url': url,
+ 'hmac-sha256': client_hash,
+ }
+
+ def test_html_format(self):
+ import gatekeeper_mailer
+ template = gatekeeper_mailer.MailTemplate(self.build_data['waterfall_url'],
+ self.build_data['build_url'],
+ self.build_data['project_name'],
+ 'test@chromium.org')
+
+ _, html_content, _ = template.genMessageContent(self.build_data)
+
+ with open(os.path.join(self.test_dir, 'expected.html')) as f:
+ expected_html = ' '.join(f.read().splitlines())
+
+ saw = str(BeautifulSoup(html_content)).split()
+ expected = str(BeautifulSoup(expected_html)).split()
+
+ self.assertEqual(saw, expected)
+
+ def test_hmac_validation(self):
+ from mailer import Email
+ message = self.input_json['message']
+ url = 'http://invalid.chromium.org'
+ secret = 'pajamas'
+
+ test_json = self._hash_message(time.time(), message, url, secret)
+ # pylint: disable=W0212
+ self.assertTrue(Email._validate_message(test_json, url, secret))
+
+ # Test that a trailing slash doesn't affect URL parsing.
+ test_json = self._hash_message(time.time(), message, url + '/', secret)
+ # pylint: disable=W0212
+ self.assertTrue(Email._validate_message(test_json, url, secret))
+
+ tests = [
+ self._hash_message(time.time() + 61, message, url, secret),
+ self._hash_message(time.time() - 61, message, url, secret),
+ self._hash_message(time.time(), message, url + 'hey', secret),
+ self._hash_message(time.time(), message, url, secret + 'hey'),
+ ]
+
+ for test_json in tests:
+ # pylint: disable=W0212
+ self.assertFalse(Email._validate_message(test_json, url, secret))
+
+ test_json = self._hash_message(time.time(), message, url, secret)
+ test_json['message'] = test_json['message'] + 'hey'
+ # pylint: disable=W0212
+ self.assertFalse(Email._validate_message(test_json, url, secret))
« no previous file with comments | « app.yaml ('k') | gatekeeper_mailer.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698