blob: 040cd75a51f1dc895571275b40a70d1ed66164f1 [file] [log] [blame]
Alex Klein104b0672019-06-28 15:43:00 -06001# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""API controller tests."""
7
8from __future__ import print_function
9
Alex Klein231d2da2019-07-22 16:44:45 -060010from chromite.api import api_config
Alex Klein104b0672019-06-28 15:43:00 -060011from chromite.api import router
12from chromite.api.controller import api as api_controller
13from chromite.api.gen.chromite.api import api_pb2
14from chromite.lib import cros_test_lib
15
16
Alex Klein231d2da2019-07-22 16:44:45 -060017class GetMethodsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein104b0672019-06-28 15:43:00 -060018 """GetMethods tests."""
19
Alex Klein231d2da2019-07-22 16:44:45 -060020 def setUp(self):
21 self.request = api_pb2.MethodGetRequest()
22 self.response = api_pb2.MethodGetResponse()
23
Alex Klein104b0672019-06-28 15:43:00 -060024 def testGetMethods(self):
25 """Simple GetMethods sanity check."""
26 methods = ['foo', 'bar']
27 self.PatchObject(router.Router, 'ListMethods', return_value=methods)
28
Alex Klein231d2da2019-07-22 16:44:45 -060029 api_controller.GetMethods(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -060030
Mike Frysinger678735c2019-09-28 18:23:28 -040031 self.assertCountEqual(methods, [m.method for m in self.response.methods])
Alex Klein104b0672019-06-28 15:43:00 -060032
Alex Klein231d2da2019-07-22 16:44:45 -060033 def testValidateOnly(self):
34 """Sanity check validate only calls only validate."""
35 patch = self.PatchObject(router.Router, 'ListMethods')
36
37 api_controller.GetMethods(self.request, self.response,
38 self.validate_only_config)
39
40 patch.assert_not_called()
Alex Klein104b0672019-06-28 15:43:00 -060041
42
Alex Klein231d2da2019-07-22 16:44:45 -060043class GetVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein104b0672019-06-28 15:43:00 -060044 """GetVersion tests."""
45
Alex Klein231d2da2019-07-22 16:44:45 -060046 def setUp(self):
Alex Klein104b0672019-06-28 15:43:00 -060047 self.PatchObject(api_controller, 'VERSION_MAJOR', new=1)
48 self.PatchObject(api_controller, 'VERSION_MINOR', new=2)
49 self.PatchObject(api_controller, 'VERSION_BUG', new=3)
50
Alex Klein231d2da2019-07-22 16:44:45 -060051 self.request = api_pb2.VersionGetRequest()
52 self.response = api_pb2.VersionGetResponse()
Alex Klein104b0672019-06-28 15:43:00 -060053
Alex Klein231d2da2019-07-22 16:44:45 -060054 def testGetVersion(self):
55 """Simple GetVersion sanity check."""
56 api_controller.GetVersion(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -060057
Alex Klein231d2da2019-07-22 16:44:45 -060058 self.assertEqual(self.response.version.major, 1)
59 self.assertEqual(self.response.version.minor, 2)
60 self.assertEqual(self.response.version.bug, 3)
61
62 def testValidateOnly(self):
63 """Sanity check validate only calls only validate."""
64 api_controller.GetVersion(self.request, self.response,
65 self.validate_only_config)
66
67 self.assertFalse(self.response.version.major)
68 self.assertFalse(self.response.version.minor)
69 self.assertFalse(self.response.version.bug)