blob: 2044cf7e12b90f7cf172906166ab3955380dd792 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2019 The Chromium Authors. All rights reserved.
2# Use of this source code is govered by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Tests for features.pubsub."""
7
8from __future__ import print_function
9from __future__ import division
10from __future__ import absolute_import
11
12import unittest
13
14from mock import Mock
15
16from features import pubsub
17from services import service_manager
18from testing import fake
19from testing import testing_helpers
20
21
22class PublishPubsubIssueChangeTaskTest(unittest.TestCase):
23
24 def setUp(self):
25 self.services = service_manager.Services(
26 user=fake.UserService(),
27 project=fake.ProjectService(),
28 config=fake.ConfigService(),
29 issue=fake.IssueService(),
30 features=fake.FeaturesService())
31 self.services.project.TestAddProject(
32 'test-project', owner_ids=[1, 3],
33 project_id=12345)
34
35 # Stub the pubsub API (there is no pubsub testbed stub).
36 self.pubsub_client_mock = Mock()
37 pubsub.set_up_pubsub_api = Mock(return_value=self.pubsub_client_mock)
38
39 def testPublishPubsubIssueChangeTask_NoIssueIdParam(self):
40 """Test case when issue_id param is not passed."""
41 task = pubsub.PublishPubsubIssueChangeTask(
42 request=None, response=None, services=self.services)
43 mr = testing_helpers.MakeMonorailRequest(
44 user_info={'user_id': 1},
45 params={},
46 method='POST',
47 services=self.services)
48 result = task.HandleRequest(mr)
49 expected_body = {
50 'error': 'Cannot proceed without a valid issue ID.',
51 }
52 self.assertEqual(result, expected_body)
53
54 def testPublishPubsubIssueChangeTask_PubSubAPIInitFailure(self):
55 """Test case when pub/sub API fails to init."""
56 pubsub.set_up_pubsub_api = Mock(return_value=None)
57 task = pubsub.PublishPubsubIssueChangeTask(
58 request=None, response=None, services=self.services)
59 mr = testing_helpers.MakeMonorailRequest(
60 user_info={'user_id': 1},
61 params={},
62 method='POST',
63 services=self.services)
64 result = task.HandleRequest(mr)
65 expected_body = {
66 'error': 'Pub/Sub API init failure.',
67 }
68 self.assertEqual(result, expected_body)
69
70 def testPublishPubsubIssueChangeTask_IssueNotFound(self):
71 """Test case when issue is not found."""
72 task = pubsub.PublishPubsubIssueChangeTask(
73 request=None, response=None, services=self.services)
74 mr = testing_helpers.MakeMonorailRequest(
75 user_info={'user_id': 1},
76 params={'issue_id': 314159},
77 method='POST',
78 services=self.services)
79 result = task.HandleRequest(mr)
80 expected_body = {
81 'error': 'Could not find issue with ID 314159',
82 }
83 self.assertEqual(result, expected_body)
84
85 def testPublishPubsubIssueChangeTask_Normal(self):
86 """Test normal happy-path case."""
87 issue = fake.MakeTestIssue(789, 543, 'sum', 'New', 111, issue_id=78901,
88 project_name='rutabaga')
89 self.services.issue.TestAddIssue(issue)
90 task = pubsub.PublishPubsubIssueChangeTask(
91 request=None, response=None, services=self.services)
92 mr = testing_helpers.MakeMonorailRequest(
93 user_info={'user_id': 1},
94 params={'issue_id': 78901},
95 method='POST',
96 services=self.services)
97 result = task.HandleRequest(mr)
98
99 self.pubsub_client_mock.projects().topics().publish.assert_called_once_with(
100 topic='projects/testing-app/topics/issue-updates',
101 body={
102 'messages': [{
103 'attributes': {
104 'local_id': '543',
105 'project_name': 'rutabaga',
106 },
107 }],
108 }
109 )
110 self.assertEqual(result, {})