blob: da4837823ff70e78fee9e2be0cabf6531888c4ba [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Tests for MonorailTSMonJSHandler."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import json
11import unittest
12from mock import patch
13
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020014import flask
Copybara854996b2021-09-07 19:36:02 +000015from google.appengine.ext import testbed
16
17from framework.ts_mon_js import MonorailTSMonJSHandler
18from services import service_manager
19
20
21class 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ínezde942802022-07-15 14:06:55 +020027 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ínezf19ea432024-01-23 20:20:52 +010032 view_func=MonorailTSMonJSHandler(
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020033 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ínezf19ea432024-01-23 20:20:52 +010041 def testSubmitMetrics(self, _mockTime, _mockValidateToken):
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020042 """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)