Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # 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 | |
| 8 | from __future__ import print_function |
| 9 | from __future__ import division |
| 10 | from __future__ import absolute_import |
| 11 | |
| 12 | import unittest |
| 13 | |
| 14 | from mock import Mock |
| 15 | |
| 16 | from features import pubsub |
| 17 | from services import service_manager |
| 18 | from testing import fake |
| 19 | from testing import testing_helpers |
| 20 | |
| 21 | |
| 22 | class 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 41 | task = pubsub.PublishPubsubIssueChangeTask(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 42 | 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 56 | task = pubsub.PublishPubsubIssueChangeTask(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 57 | 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 70 | task = pubsub.PublishPubsubIssueChangeTask(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 71 | 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 87 | task = pubsub.PublishPubsubIssueChangeTask(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 88 | 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, {}) |