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

Side by Side 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, 3 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 unified diff | Download patch
« no previous file with comments | « app.yaml ('k') | gatekeeper_mailer.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 import ast 6 import ast
7 import datetime 7 import datetime
8 import hashlib
9 import hmac
10 import json
8 import os 11 import os
12 import random
13 import time
9 import unittest 14 import unittest
10 15
11 import app 16 import app
17 from third_party.BeautifulSoup.BeautifulSoup import BeautifulSoup
12 18
13 19
14 TEST_DIR = os.path.join(os.path.dirname(__file__), 'tests') 20 TEST_DIR = os.path.join(os.path.dirname(__file__), 'tests')
15 21
16 22
17 class GaeTestCase(unittest.TestCase): 23 class GaeTestCase(unittest.TestCase):
18 def setUp(self, *args, **kwargs): 24 def setUp(self, *args, **kwargs):
19 self.clear_datastore() 25 self.clear_datastore()
20 super(GaeTestCase, self).setUp(*args, **kwargs) 26 super(GaeTestCase, self).setUp(*args, **kwargs)
21 27
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 page = app.get_and_cache_pagedata('chromium/console') 472 page = app.get_and_cache_pagedata('chromium/console')
467 # Uncomment if deeper inspection is needed of the returned console. 473 # Uncomment if deeper inspection is needed of the returned console.
468 # with open(os.path.join(test_dir, 'expected.html'), 'w') as fh: 474 # with open(os.path.join(test_dir, 'expected.html'), 'w') as fh:
469 # fh.write(page['content']) 475 # fh.write(page['content'])
470 self.assertEquals('interface', page['body_class']) 476 self.assertEquals('interface', page['body_class'])
471 self.assertEquals(expected_content, page['content']) 477 self.assertEquals(expected_content, page['content'])
472 self.assertEquals( 478 self.assertEquals(
473 'http://build.chromium.org/p/chromium/console/../', 479 'http://build.chromium.org/p/chromium/console/../',
474 page['offsite_base']) 480 page['offsite_base'])
475 self.assertEquals('BuildBot: Chromium', page['title']) 481 self.assertEquals('BuildBot: Chromium', page['title'])
482
483
484 class MailTestCase(GaeTestCase):
485 def setUp(self):
486 self.test_dir = os.path.join(TEST_DIR, 'test_mailer')
487 with open(os.path.join(self.test_dir, 'input.json')) as f:
488 self.input_json = json.load(f)
489 self.build_data = json.loads(self.input_json['message'])
490
491 @staticmethod
492 def _hash_message(mytime, message, url, secret):
493 salt = random.getrandbits(32)
494 hasher = hmac.new(secret, message, hashlib.sha256)
495 hasher.update(str(mytime))
496 hasher.update(str(salt))
497 client_hash = hasher.hexdigest()
498
499 return {'message': message,
500 'time': mytime,
501 'salt': salt,
502 'url': url,
503 'hmac-sha256': client_hash,
504 }
505
506 def test_html_format(self):
507 import gatekeeper_mailer
508 template = gatekeeper_mailer.MailTemplate(self.build_data['waterfall_url'],
509 self.build_data['build_url'],
510 self.build_data['project_name'],
511 'test@chromium.org')
512
513 _, html_content, _ = template.genMessageContent(self.build_data)
514
515 with open(os.path.join(self.test_dir, 'expected.html')) as f:
516 expected_html = ' '.join(f.read().splitlines())
517
518 saw = str(BeautifulSoup(html_content)).split()
519 expected = str(BeautifulSoup(expected_html)).split()
520
521 self.assertEqual(saw, expected)
522
523 def test_hmac_validation(self):
524 from mailer import Email
525 message = self.input_json['message']
526 url = 'http://invalid.chromium.org'
527 secret = 'pajamas'
528
529 test_json = self._hash_message(time.time(), message, url, secret)
530 # pylint: disable=W0212
531 self.assertTrue(Email._validate_message(test_json, url, secret))
532
533 # Test that a trailing slash doesn't affect URL parsing.
534 test_json = self._hash_message(time.time(), message, url + '/', secret)
535 # pylint: disable=W0212
536 self.assertTrue(Email._validate_message(test_json, url, secret))
537
538 tests = [
539 self._hash_message(time.time() + 61, message, url, secret),
540 self._hash_message(time.time() - 61, message, url, secret),
541 self._hash_message(time.time(), message, url + 'hey', secret),
542 self._hash_message(time.time(), message, url, secret + 'hey'),
543 ]
544
545 for test_json in tests:
546 # pylint: disable=W0212
547 self.assertFalse(Email._validate_message(test_json, url, secret))
548
549 test_json = self._hash_message(time.time(), message, url, secret)
550 test_json['message'] = test_json['message'] + 'hey'
551 # pylint: disable=W0212
552 self.assertFalse(Email._validate_message(test_json, url, secret))
OLDNEW
« 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