Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # Copyright 2018 The Chromium Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Tests for MonorailTSMonJSHandler.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import json |
| 11 | import unittest |
| 12 | from mock import patch |
| 13 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 14 | import flask |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 15 | from google.appengine.ext import testbed |
| 16 | |
| 17 | from framework.ts_mon_js import MonorailTSMonJSHandler |
| 18 | from services import service_manager |
| 19 | |
| 20 | |
| 21 | class MonorailTSMonJSHandlerTest(unittest.TestCase): |
| 22 | |
| 23 | def setUp(self): |
| 24 | self.testbed = testbed.Testbed() |
| 25 | self.testbed.activate() |
| 26 | self.testbed.init_user_stub() |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 27 | self.services = service_manager.Services() |
| 28 | self.app = flask.Flask('test_app') |
| 29 | self.app.config['TESTING'] = True |
| 30 | self.app.add_url_rule( |
| 31 | '/_/ts_mon_js.do', |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 32 | view_func=MonorailTSMonJSHandler( |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 33 | services=self.services).PostMonorailTSMonJSHandler, |
| 34 | methods=['POST']) |
| 35 | |
| 36 | def tearDown(self): |
| 37 | self.testbed.deactivate() |
| 38 | |
| 39 | @patch('framework.xsrf.ValidateToken') |
| 40 | @patch('time.time') |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 41 | def testSubmitMetrics(self, _mockTime, _mockValidateToken): |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 42 | """Test normal case POSTing metrics.""" |
| 43 | _mockTime.return_value = 1537821859 |
| 44 | res = self.app.test_client().post( |
| 45 | '/_/ts_mon_js.do', |
| 46 | data = json.dumps( |
| 47 | { |
| 48 | 'metrics': |
| 49 | [ |
| 50 | { |
| 51 | 'MetricInfo': |
| 52 | { |
| 53 | 'Name': |
| 54 | 'monorail/frontend/issue_update_latency', |
| 55 | 'ValueType': |
| 56 | 2, |
| 57 | }, |
| 58 | 'Cells': |
| 59 | [ |
| 60 | { |
| 61 | 'value': |
| 62 | { |
| 63 | 'sum': 1234, |
| 64 | 'count': 4321, |
| 65 | 'buckets': |
| 66 | { |
| 67 | 0: 123, |
| 68 | 1: 321, |
| 69 | 2: 213, |
| 70 | }, |
| 71 | }, |
| 72 | 'fields': |
| 73 | { |
| 74 | 'client_id': '789', |
| 75 | 'host_name': 'rutabaga', |
| 76 | 'document_visible': True, |
| 77 | }, |
| 78 | 'start_time': 1537821799, |
| 79 | } |
| 80 | ], |
| 81 | } |
| 82 | ], |
| 83 | })) |
| 84 | self.assertEqual(res.status_code, 201) |