blob: c2ad97186367f9928ac8f47c063cf18a97779ea8 [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
Mike Frysingeref94e4c2020-02-10 23:59:54 -050010import sys
11
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
Alex Klein104b0672019-06-28 15:43:00 -060013from chromite.api import router
14from chromite.api.controller import api as api_controller
15from chromite.api.gen.chromite.api import api_pb2
16from chromite.lib import cros_test_lib
17
18
Mike Frysingeref94e4c2020-02-10 23:59:54 -050019assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
20
21
Alex Klein231d2da2019-07-22 16:44:45 -060022class GetMethodsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein104b0672019-06-28 15:43:00 -060023 """GetMethods tests."""
24
Alex Klein231d2da2019-07-22 16:44:45 -060025 def setUp(self):
26 self.request = api_pb2.MethodGetRequest()
27 self.response = api_pb2.MethodGetResponse()
28
Alex Klein104b0672019-06-28 15:43:00 -060029 def testGetMethods(self):
30 """Simple GetMethods sanity check."""
31 methods = ['foo', 'bar']
32 self.PatchObject(router.Router, 'ListMethods', return_value=methods)
33
Alex Klein231d2da2019-07-22 16:44:45 -060034 api_controller.GetMethods(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -060035
Mike Frysinger678735c2019-09-28 18:23:28 -040036 self.assertCountEqual(methods, [m.method for m in self.response.methods])
Alex Klein104b0672019-06-28 15:43:00 -060037
Alex Klein231d2da2019-07-22 16:44:45 -060038 def testValidateOnly(self):
39 """Sanity check validate only calls only validate."""
40 patch = self.PatchObject(router.Router, 'ListMethods')
41
42 api_controller.GetMethods(self.request, self.response,
43 self.validate_only_config)
44
45 patch.assert_not_called()
Alex Klein104b0672019-06-28 15:43:00 -060046
47
Alex Klein231d2da2019-07-22 16:44:45 -060048class GetVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein104b0672019-06-28 15:43:00 -060049 """GetVersion tests."""
50
Alex Klein231d2da2019-07-22 16:44:45 -060051 def setUp(self):
Alex Klein104b0672019-06-28 15:43:00 -060052 self.PatchObject(api_controller, 'VERSION_MAJOR', new=1)
53 self.PatchObject(api_controller, 'VERSION_MINOR', new=2)
54 self.PatchObject(api_controller, 'VERSION_BUG', new=3)
55
Alex Klein231d2da2019-07-22 16:44:45 -060056 self.request = api_pb2.VersionGetRequest()
57 self.response = api_pb2.VersionGetResponse()
Alex Klein104b0672019-06-28 15:43:00 -060058
Alex Klein231d2da2019-07-22 16:44:45 -060059 def testGetVersion(self):
60 """Simple GetVersion sanity check."""
61 api_controller.GetVersion(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -060062
Alex Klein231d2da2019-07-22 16:44:45 -060063 self.assertEqual(self.response.version.major, 1)
64 self.assertEqual(self.response.version.minor, 2)
65 self.assertEqual(self.response.version.bug, 3)
66
67 def testValidateOnly(self):
68 """Sanity check validate only calls only validate."""
69 api_controller.GetVersion(self.request, self.response,
70 self.validate_only_config)
71
72 self.assertFalse(self.response.version.major)
73 self.assertFalse(self.response.version.minor)
74 self.assertFalse(self.response.version.bug)