Merge branch 'main' into avm99963-monorail

Merged commit cd4b3b336f1f14afa02990fdc2eec5d9467a827e

GitOrigin-RevId: e67bbf185d5538e1472bb42e0abb2a141f88bac1
diff --git a/framework/test/ts_mon_js_test.py b/framework/test/ts_mon_js_test.py
index bcd4060..4231b76 100644
--- a/framework/test/ts_mon_js_test.py
+++ b/framework/test/ts_mon_js_test.py
@@ -12,9 +12,11 @@
 import unittest
 from mock import patch
 
+import flask
 import webapp2
 from google.appengine.ext import testbed
 
+from framework.ts_mon_js import FlaskMonorailTSMonJSHandler
 from framework.ts_mon_js import MonorailTSMonJSHandler
 from services import service_manager
 
@@ -71,3 +73,69 @@
 
     self.assertEqual(res.status_int, 201)
     self.assertEqual(res.body, 'Ok.')
+
+
+class FlaskMonorailTSMonJSHandlerTest(unittest.TestCase):
+
+  def setUp(self):
+    self.testbed = testbed.Testbed()
+    self.testbed.activate()
+    self.testbed.init_user_stub()
+    self.services = service_manager.Services()
+    self.app = flask.Flask('test_app')
+    self.app.config['TESTING'] = True
+    self.app.add_url_rule(
+        '/_/ts_mon_js.do',
+        view_func=FlaskMonorailTSMonJSHandler(
+            services=self.services).PostMonorailTSMonJSHandler,
+        methods=['POST'])
+
+  def tearDown(self):
+    self.testbed.deactivate()
+
+  @patch('framework.xsrf.ValidateToken')
+  @patch('time.time')
+  def testFlaskSubmitMetrics(self, _mockTime, _mockValidateToken):
+    """Test normal case POSTing metrics."""
+    _mockTime.return_value = 1537821859
+    res = self.app.test_client().post(
+        '/_/ts_mon_js.do',
+        data = json.dumps(
+            {
+                'metrics':
+                    [
+                        {
+                            'MetricInfo':
+                                {
+                                    'Name':
+                                      'monorail/frontend/issue_update_latency',
+                                    'ValueType':
+                                      2,
+                                },
+                            'Cells':
+                                [
+                                    {
+                                        'value':
+                                            {
+                                                'sum': 1234,
+                                                'count': 4321,
+                                                'buckets':
+                                                    {
+                                                        0: 123,
+                                                        1: 321,
+                                                        2: 213,
+                                                    },
+                                            },
+                                        'fields':
+                                            {
+                                                'client_id': '789',
+                                                'host_name': 'rutabaga',
+                                                'document_visible': True,
+                                            },
+                                        'start_time': 1537821799,
+                                    }
+                                ],
+                        }
+                    ],
+            }))
+    self.assertEqual(res.status_code, 201)