blob: e86230ca028a9f7d8e6df75e3bc7a75621cbe086 [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."""
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020041 task = pubsub.PublishPubsubIssueChangeTask(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000042 mr = testing_helpers.MakeMonorailRequest(
43 user_info={'user_id': 1},
44 params={},
45 method='POST',
46 services=self.services)
47 result = task.HandleRequest(mr)
48 expected_body = {
49 'error': 'Cannot proceed without a valid issue ID.',
50 }
51 self.assertEqual(result, expected_body)
52
53 def testPublishPubsubIssueChangeTask_PubSubAPIInitFailure(self):
54 """Test case when pub/sub API fails to init."""
55 pubsub.set_up_pubsub_api = Mock(return_value=None)
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020056 task = pubsub.PublishPubsubIssueChangeTask(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000057 mr = testing_helpers.MakeMonorailRequest(
58 user_info={'user_id': 1},
59 params={},
60 method='POST',
61 services=self.services)
62 result = task.HandleRequest(mr)
63 expected_body = {
64 'error': 'Pub/Sub API init failure.',
65 }
66 self.assertEqual(result, expected_body)
67
68 def testPublishPubsubIssueChangeTask_IssueNotFound(self):
69 """Test case when issue is not found."""
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020070 task = pubsub.PublishPubsubIssueChangeTask(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000071 mr = testing_helpers.MakeMonorailRequest(
72 user_info={'user_id': 1},
73 params={'issue_id': 314159},
74 method='POST',
75 services=self.services)
76 result = task.HandleRequest(mr)
77 expected_body = {
78 'error': 'Could not find issue with ID 314159',
79 }
80 self.assertEqual(result, expected_body)
81
82 def testPublishPubsubIssueChangeTask_Normal(self):
83 """Test normal happy-path case."""
84 issue = fake.MakeTestIssue(789, 543, 'sum', 'New', 111, issue_id=78901,
85 project_name='rutabaga')
86 self.services.issue.TestAddIssue(issue)
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020087 task = pubsub.PublishPubsubIssueChangeTask(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000088 mr = testing_helpers.MakeMonorailRequest(
89 user_info={'user_id': 1},
90 params={'issue_id': 78901},
91 method='POST',
92 services=self.services)
93 result = task.HandleRequest(mr)
94
95 self.pubsub_client_mock.projects().topics().publish.assert_called_once_with(
96 topic='projects/testing-app/topics/issue-updates',
97 body={
98 'messages': [{
99 'attributes': {
100 'local_id': '543',
101 'project_name': 'rutabaga',
102 },
103 }],
104 }
105 )
106 self.assertEqual(result, {})