Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | """Tests for MonorailTSMonJSHandler.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import json |
| 12 | import unittest |
| 13 | from mock import patch |
| 14 | |
| 15 | import webapp2 |
| 16 | from google.appengine.ext import testbed |
| 17 | |
| 18 | from framework.ts_mon_js import MonorailTSMonJSHandler |
| 19 | from services import service_manager |
| 20 | |
| 21 | |
| 22 | class MonorailTSMonJSHandlerTest(unittest.TestCase): |
| 23 | |
| 24 | def setUp(self): |
| 25 | self.testbed = testbed.Testbed() |
| 26 | self.testbed.activate() |
| 27 | self.testbed.init_user_stub() |
| 28 | |
| 29 | def tearDown(self): |
| 30 | self.testbed.deactivate() |
| 31 | |
| 32 | @patch('framework.xsrf.ValidateToken') |
| 33 | @patch('time.time') |
| 34 | def testSubmitMetrics(self, _mockTime, _mockValidateToken): |
| 35 | """Test normal case POSTing metrics.""" |
| 36 | _mockTime.return_value = 1537821859 |
| 37 | req = webapp2.Request.blank('/_/ts_mon_js') |
| 38 | req.body = json.dumps({ |
| 39 | 'metrics': [{ |
| 40 | 'MetricInfo': { |
| 41 | 'Name': 'monorail/frontend/issue_update_latency', |
| 42 | 'ValueType': 2, |
| 43 | }, |
| 44 | 'Cells': [{ |
| 45 | 'value': { |
| 46 | 'sum': 1234, |
| 47 | 'count': 4321, |
| 48 | 'buckets': { |
| 49 | 0: 123, |
| 50 | 1: 321, |
| 51 | 2: 213, |
| 52 | }, |
| 53 | }, |
| 54 | 'fields': { |
| 55 | 'client_id': '789', |
| 56 | 'host_name': 'rutabaga', |
| 57 | 'document_visible': True, |
| 58 | }, |
| 59 | 'start_time': 1537821859 - 60, |
| 60 | }], |
| 61 | }], |
| 62 | }) |
| 63 | res = webapp2.Response() |
| 64 | ts_mon_handler = MonorailTSMonJSHandler(request=req, response=res) |
| 65 | class MockApp(object): |
| 66 | def __init__(self): |
| 67 | self.config = {'services': service_manager.Services()} |
| 68 | ts_mon_handler.app = MockApp() |
| 69 | |
| 70 | ts_mon_handler.post() |
| 71 | |
| 72 | self.assertEqual(res.status_int, 201) |
| 73 | self.assertEqual(res.body, 'Ok.') |