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