def test_already_tracked(self, ) -> None: report = self._build_report( report_id=123, tracking_status='T', logs=[ TrackingStatusLog( created_at='2021-01-01T00:00:00+00:00', log_id=1, log_type='tracking-status', private=True, author=Author(username='******', ), message_html='Tracked', attachments=[], tracker_name='my-tracker', tracker_url='http://tracker/issue/1', tracker_id='1', ), ], ) ywh_api_client_mock = create_autospec(YesWeHackApiClient, spec_set=True) tracker_client_mock = create_autospec(TrackerClient, spec_set=True) tracker_client_mock.tracker_type = 'MyTracker' tracker_client_mock.get_tracker_issue.return_value = TrackerIssue( tracker_url='http://tracker/issue/1', project='my-project', issue_id='1', issue_url='http://tracker/issue/1', closed=False, ) Given( case=self, report=report, yeswehack_client=ywh_api_client_mock, tracker_name='my-tracker', tracker_client=tracker_client_mock, synchronize_options=SynchronizeOptions( upload_private_comments=True, upload_public_comments=True, upload_details_updates=True, upload_rewards=True, upload_status_updates=True, ), feedback_options=FeedbackOptions(), message_formatter=SimpleMessageFormatter( tracking_status_update_format= 'issue url: {tracker_issue.issue_url}', synchronization_done_format= 'issue url: {send_logs_result.tracker_issue.issue_url}', download_comment_format='comment: {comment}', status_update_comment_format='comment: {comment}', ), ).when_synchronize_report().then_assert_no_error( ).then_assert_has_result().then_assert_is_existing_issue( ).then_assert_tracker_client_send_report_not_called( ).then_assert_tracker_client_send_logs_not_called( ).then_assert_yeswehack_client_put_report_tracking_status_not_called()
def map_raw_log( # noqa: WPS210,WPS212,WPS231 context: MappingContext, raw_log: YesWeHackRawApiLog, ) -> Log: """ Map a raw API log to a local log. Args: context: a mapping context raw_log: a raw log Returns: a local log """ created_at = raw_log.created_at log_id = raw_log.id log_type = raw_log.type private = raw_log.private author = _map_raw_author( context=context, raw_author=raw_log.author, ) if raw_log.author else Author(username='******', ) message_html = cleanup_ywh_redirects_from_html( ywh_domain=context.yeswehack_domain, html=raw_log.message_html or '', ) attachments = _map_raw_attachments( context=context, raw_attachments=raw_log.attachments, ) if raw_log.type == 'comment': return CommentLog( created_at=created_at, log_id=log_id, log_type=log_type, private=private, author=author, message_html=message_html, attachments=attachments, ) if raw_log.type == 'details-update': return DetailsUpdateLog( created_at=created_at, log_id=log_id, log_type=log_type, private=private, author=author, message_html=message_html, attachments=attachments, old_details=raw_log.old_details, new_details=raw_log.new_details, ) if raw_log.type == 'reward': return RewardLog( created_at=created_at, log_id=log_id, log_type=log_type, private=private, author=author, message_html=message_html, attachments=attachments, reward_type=raw_log.reward_type, ) if raw_log.type == 'status-update': return StatusUpdateLog( created_at=created_at, log_id=log_id, log_type=log_type, private=private, author=author, message_html=message_html, attachments=attachments, old_status=raw_log.old_status, new_status=raw_log.status, ) if raw_log.type == 'tracking-status': return TrackingStatusLog( created_at=created_at, log_id=log_id, log_type=log_type, private=private, author=author, message_html=message_html, attachments=attachments, tracker_name=raw_log.tracker_name, tracker_url=cleanup_ywh_redirects_from_text( ywh_domain=context.yeswehack_domain, text=raw_log.tracker_url, ) if raw_log.tracker_url else None, tracker_id=raw_log.tracker_id, ) if raw_log.type == 'tracker-update': return TrackerUpdateLog( created_at=created_at, log_id=log_id, log_type=log_type, private=private, author=author, message_html=message_html, attachments=attachments, tracker_name=raw_log.tracker_name, tracker_url=cleanup_ywh_redirects_from_text( ywh_domain=context.yeswehack_domain, text=raw_log.tracker_url, ) if raw_log.tracker_url else None, tracker_id=raw_log.tracker_id, tracker_token=raw_log.tracker_token, ) if raw_log.type == 'tracker-message': return TrackerMessageLog( created_at=created_at, log_id=log_id, log_type=log_type, private=private, author=author, message_html=message_html, attachments=attachments, tracker_name=raw_log.tracker_name, tracker_url=cleanup_ywh_redirects_from_text( ywh_domain=context.yeswehack_domain, text=raw_log.tracker_url, ) if raw_log.tracker_url else None, tracker_id=raw_log.tracker_id, ) return Log( created_at=created_at, log_id=log_id, log_type=log_type, private=private, author=author, message_html=message_html, attachments=attachments, )
def test_partially_synced(self, ) -> None: comment_log1 = CommentLog( created_at='2021-01-01T00:00:00+00:00', log_id=1, log_type='comment', private=True, author=Author(username='******', ), message_html='This is a comment', attachments=[], ) tracking_status = TrackingStatusLog( created_at='2021-01-01T00:30:00+00:00', log_id=1, log_type='tracking-status', private=True, author=Author(username='******', ), message_html='Tracked', attachments=[], tracker_name='my-tracker', tracker_url='http://tracker/issue/1', tracker_id='1', ) tracker_update_log1 = TrackerUpdateLog( created_at='2021-01-01T01:00:00+00:00', log_id=2, log_type='tracker-update', private=True, author=Author(username='******', ), message_html='This is a a tracker update', attachments=[], tracker_name='my-tracker', tracker_id='1', tracker_url='http://tracker/issue/1', tracker_token=StateEncryptor.encrypt( key='123', state=TrackerIssueState( closed=False, bugtracker_name='my-tracker', ), ), ) comment_log2 = CommentLog( created_at='2021-01-01T02:00:00+00:00', log_id=3, log_type='comment', private=True, author=Author(username='******', ), message_html='This is another comment', attachments=[], ) report = self._build_report( report_id=123, tracking_status='T', logs=[ comment_log1, tracking_status, tracker_update_log1, comment_log2, ], ) ywh_api_client_mock = create_autospec(YesWeHackApiClient, spec_set=True) tracker_client_mock = create_autospec(TrackerClient, spec_set=True) tracker_client_mock.tracker_type = 'MyTracker' issue = TrackerIssue( tracker_url='http://tracker/issue/1', project='my-project', issue_id='1', issue_url='http://tracker/issue/1', closed=False, ) tracker_client_mock.get_tracker_issue.return_value = issue tracker_client_mock.send_logs.return_value = SendLogsResult( tracker_issue=issue, added_comments=[ TrackerIssueComment( created_at=datetime.datetime( year=2020, month=1, day=1, hour=15, minute=17, second=23, microsecond=420000, tzinfo=datetime.timezone.utc, ), author='tracker-user', comment_id='456', body='', attachments={}, ), ], ) Given( case=self, report=report, yeswehack_client=ywh_api_client_mock, tracker_name='my-tracker', tracker_client=tracker_client_mock, synchronize_options=SynchronizeOptions( upload_private_comments=True, upload_public_comments=True, upload_details_updates=True, upload_rewards=True, upload_status_updates=True, ), feedback_options=FeedbackOptions(), message_formatter=SimpleMessageFormatter( tracking_status_update_format= 'issue url: {tracker_issue.issue_url}', synchronization_done_format= 'issue url: {send_logs_result.tracker_issue.issue_url}', download_comment_format='comment: {comment}', status_update_comment_format='comment: {comment}', ), ).when_synchronize_report().then_assert_no_error( ).then_assert_has_result().then_assert_is_existing_issue( ).then_assert_tracker_client_send_report_not_called( ).then_assert_tracker_client_send_logs_called_once_with( tracker_issue=issue, logs=[ comment_log2, ], ).then_assert_yeswehack_client_post_report_tracker_update_called_once_with( report=report, tracker_name='my-tracker', issue_id='1', issue_url='http://tracker/issue/1', comment='issue url: http://tracker/issue/1', token=StateEncryptor.encrypt( key='123', state=TrackerIssueState(closed=False, bugtracker_name='my-tracker', downloaded_comments=['456']), ), )