Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Tests for features.pubsub.""" |
| 6 | |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | from mock import Mock |
| 14 | |
| 15 | from features import pubsub |
| 16 | from services import service_manager |
| 17 | from testing import fake |
| 18 | from testing import testing_helpers |
| 19 | |
| 20 | |
| 21 | class 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 40 | task = pubsub.PublishPubsubIssueChangeTask(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 41 | 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 55 | task = pubsub.PublishPubsubIssueChangeTask(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 56 | 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 69 | task = pubsub.PublishPubsubIssueChangeTask(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 70 | 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 86 | task = pubsub.PublishPubsubIssueChangeTask(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 87 | 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, {}) |