class TestIssue(TestJiraModel): def setUp(self): obj = { 'id': 1337, 'self': 'https://example.org/1337', 'key': 'AB-123', 'fields': { 'summary': 'Test summary', 'fixVersions': [] } } self.issue = Issue(obj, MockSession(400, {})) self.obj = obj @patch('apps.jira.models.get_response', return_value=MockResponse(201)) def test_comment(self, mock_response): response = self.issue.comment('Test comment') self.assertTrue(response) @patch('apps.jira.models.get_response', return_value=MockResponse(201)) def test_add_version(self, mock_response): response = self.issue.add_version('Test version') self.assertTrue(response) def test_add_version_already_has_version(self): issue = Issue(self.obj, MockSession(400, {})) issue.fix_versions.append({'name': 'Existing version'}) self.assertFalse(issue.add_version('Test version'))
def test_single_page_result(self): mock_data_one = {'total': 3, 'maxResults': 3, 'issues': [1, 2, 3]} mock_data_two = {'total': 3, 'maxResults': 3, 'issues': []} mock_response_one = MockResponse(200, mock_data_one) mock_response_two = MockResponse(200, mock_data_two) session = MockSession(200, {}) session.request = mock.Mock( side_effect=[mock_response_one, mock_response_two]) results = get_paginated_results(session, 'url', self.params) self.assertEqual([1, 2, 3], results)
def test_send_message_success(mock_post, mock_process): response_data = {'ok': True, 'message': fake.sentence()} mock_post.return_value = MockResponse(status_code=200, json=response_data) chat_id = '1' tg_handler.send_message(chat_id, 'lorem') assert mock_process.call_count == 1 assert response_data in list(mock_process.call_args)[0] assert chat_id in list(mock_process.call_args)[0]
def test_multi_page_result_pagination_api(self): mock_data_one = { 'isLast': False, 'maxResults': 50, 'values': [1, 2, 3] } mock_data_two = {'isLast': True, 'maxResults': 50, 'values': [4, 5, 6]} mock_response_one = MockResponse(200, mock_data_one) mock_response_two = MockResponse(200, mock_data_two) session = MockSession(200, {}) session.request = mock.Mock( side_effect=[mock_response_one, mock_response_two]) results = get_paginated_results(session, 'url', self.params) self.assertEqual([1, 2, 3, 4, 5, 6], results)
def test_send_message_got_error(caplog): response_text = 'Not Found' response_code = 404 with patch('telegram_logger.handlers.requests.post') as mock_post: mock_post.return_value = MockResponse(status_code=response_code, text=response_text) tg_handler.send_message(1, 'lorem') assert str(response_code) in caplog.text assert response_text in caplog.text
def _mocked_requests_get_status_case_4(self): # pylint: disable=no-self-use return MockResponse( {'links': { 'abc': { 'active': True }, 'def': { 'active': True } }}, 200)
def _mocked_requests_get_path_down(self): # pylint: disable=no-self-use return MockResponse( {'links': { 'abc': { 'active': False }, 'def': { 'active': True } }}, 200)
async def test_async_send_humio_structured_message_sad_path(self, mock_post_async): f = asyncio.Future() f.set_result(MockResponse({}, 400)) mock_post_async.return_value = f token = CORRECT_HUMIO attributes = {'status': 'blah', 'eventType': 'failed'} event_sender = build_event_sender(token) event_sender.post_async = mock_post_async with pytest.raises(Exception): await event_sender.send_event_async(attributes)
async def test_get_orders_success(mocker): response = json.loads(load_data(f'client/get_order/success_response.json')) expected_result = Order.parse_raw( load_data(f'client/get_order/success_expected.json')) client = Client(host='http://localhost') mocker.patch.object(client.http_session, 'get', return_value=MockResponse(response)) order = await client.get_order('AAAAAAAA', 'ivanov') assert order == expected_result
async def test_send_humio_structured_message_post_args(self, mock_session, mock_post_async): f = asyncio.Future() f.set_result(MockResponse({}, 200)) mock_post_async.return_value = f token = CORRECT_HUMIO attributes = {'status': 'blah', 'eventType': 'failed'} event_sender = build_event_sender(token) event_sender.post_async = mock_post_async assert mock_post_async.called_once_with(event_sender, mock_session)
async def test_async_send_humio_structured_message_happy_path(self, mock_post_async): f = asyncio.Future() f.set_result(MockResponse({}, 200)) mock_post_async.return_value = f token = CORRECT_HUMIO attributes = {'status': 'blah', 'eventType': 'failed'} event_sender = build_event_sender(token) event_sender.post_async = mock_post_async send_async = await event_sender.send_event_async(attributes) assert send_async.status_code is 200
async def test_add_bags_error(mocker): selection = json.loads(load_data('client/add_bags/selection.json')) selection = [BaggageSelection.parse_obj(i) for i in selection] response = json.loads(load_data('client/add_bags/error_response.json')) client = Client(host='http://localhost') mocker.patch.object(client.http_session, 'put', return_value=MockResponse(response)) try: await client.add_bags(selection) except AddBagsError as exc: assert exc.code == response['error']['code'] assert exc.message == response['error']['message'] else: assert False, 'exception should be raised'
async def test_add_bags_success(mocker): selection = json.loads(load_data('client/add_bags/selection.json')) selection = [BaggageSelection.parse_obj(i) for i in selection] response = json.loads(load_data('client/add_bags/success_response.json')) expected_result = json.loads( load_data('client/add_bags/success_expected.json')) expected_result = [BaggageSelection.parse_obj(i) for i in expected_result] client = Client(host='http://localhost') mocker.patch.object(client.http_session, 'put', return_value=MockResponse(response)) shopping_cart = await client.add_bags(selection) assert shopping_cart == expected_result
class TestJiraClient(unittest.TestCase): def setUp(self): self.client = JiraClient(CLIENT_URL) self.client.authenticate_user(CLIENT_USER, CLIENT_PASSWORD) def test_authenticate(self): self.assertIsInstance(self.client._session.auth, HTTPBasicAuth) @patch('apps.jira.client.Session.request', return_value=MockResponse(200, TEST_DATA)) def test_get_board(self, mock_response): board = self.client.board(123) self.assertIsInstance(board, Board) @patch('apps.jira.client.Session.request', return_value=MockResponse(200, TEST_DATA_LIST)) def test_list_boards(self, mock_response): boards = self.client.list_boards() self.assertIsInstance(boards[0], Board) @patch('apps.jira.client.Session.request', return_value=MockResponse(200, SPRINT_TEST_DATA)) def test_get_sprint(self, mock_response): sprint = self.client.sprint(123) self.assertIsInstance(sprint, Sprint) @patch('apps.jira.client.Session.request', return_value=MockResponse(200, ISSUE_TEST_DATA)) def test_get_issue(self, mock_response): issue = self.client.issue('ABC-123') self.assertIsInstance(issue, Issue) @patch('apps.jira.client.get_paginated_results', return_value=SEARCH_TEST_DATA) def test_search(self, mock_response): issues = self.client.search('status = Done') self.assertIsInstance(issues[0], Issue) self.assertEqual(issues[0].key, 'AB-123') @patch('apps.jira.client.get_paginated_results', return_value=SEARCH_TEST_DATA) def test_issues_done_for_sprint(self, mock_response): issues = self.client.issue_done_for_sprint(3900) self.assertIsInstance(issues[0], Issue) self.assertEqual(issues[0].key, 'AB-123') @patch('apps.jira.client.JiraClient.search', return_value=[1]) def test_version_exists(self, mock_return): self.assertTrue(self.client._version_exists('mock version')) @patch('apps.jira.client.JiraClient.search', return_value=[]) def test_version_exist_empty_value(self, mock_return): self.assertFalse(self.client._version_exists('mock version')) @patch('apps.jira.client.JiraClient.search', return_value=[1]) def test_version_exists_raises_http_error(self, mock): mock.side_effect = HTTPError(Mock(status=404), 'not found') self.assertFalse(self.client._version_exists('mock version')) @patch('apps.jira.client.get_response', return_value=Mock(status_code=201)) @patch('apps.jira.client.JiraClient._version_exists', return_value=False) def test_create_version(self, mock_exists, mock_response): self.assertTrue(self.client.create_version('mock version', 1)) @patch('apps.jira.client.JiraClient._version_exists', return_value=True) def test_create_version_version_already_exists(self, mock_exists): self.assertFalse(self.client.create_version('mock version', 1)) @patch('apps.jira.client.get_response', return_value=Mock(status_code=500)) @patch('apps.jira.client.JiraClient._version_exists', return_value=False) def test_create_version_raises_http_error(self, mock_exists, mock_response): mock_response.side_effect = HTTPError(Mock(status=500), 'Error') self.assertRaises(HTTPError, self.client.create_version, 'mock_version', 1)