def test_broken_catalog(self, mock_reporter): mockV3Api = MockAPI(os.path.join(self.resources_dir, 'v3_cdn'), 'https://cdn.door43.org/') mockV3Api.add_host(os.path.join(self.resources_dir, 'broken_api'), 'https://api.door43.org/') mockS3 = MockS3Handler('ts_bucket') mockDb = MockDynamodbHandler() mockDb._load_db( os.path.join(TestTsV2Catalog.resources_dir, 'ready_new_db.json')) mockLog = MockLogger() mock_get_url = lambda url, catch_exception: mockV3Api.get_url( url, catch_exception) mock_download = lambda url, dest: mockV3Api.download_file(url, dest) event = self.make_event() converter = TsV2CatalogHandler(event=event, context=None, logger=mockLog, s3_handler=mockS3, dynamodb_handler=mockDb, url_handler=mock_get_url, download_handler=mock_download, url_exists_handler=lambda url: False) result = converter.run() self.assertFalse(result) self.assertIn( 'Failed to load the catalog json: No JSON object could be decoded', mockLog._messages)
def test_webhook_ulb_pull_request(self, mock_reporter, mock_url_exists): request_file = os.path.join(self.resources_dir, 'ulb-pull-request.json') with codecs.open(request_file, 'r', encoding='utf-8') as in_file: request_text = in_file.read() # convert Windows line endings to Linux line endings content = request_text.replace('\r\n', '\n') # deserialized object request_json = json.loads(content) mockLogger = MockLogger() mockDCS = MockAPI(self.resources_dir, 'https://git.door43.org/') urls = { 'https://git.door43.org/Door43-Catalog/en_ulb/archive/2fbfd081f46487e48e49090a95c48d45e04e6bed.zip': 'en_ulb.zip' } mock_download = lambda url, dest: mockDCS.download_file( urls[url], dest) self.MockDynamodbHandler.data = None self.MockS3Handler.reset() handler = WebhookHandler(event=request_json, context=None, logger=mockLogger, s3_handler=self.MockS3Handler, dynamodb_handler=self.MockDynamodbHandler, download_handler=mock_download) with self.assertRaises(Exception) as error_context: handler.run() self.assertIn('Skipping un-merged pull request', str(error_context.exception)) entry = self.MockDynamodbHandler.data self.assertEqual(0, len(self.MockS3Handler.uploads)) self.assertIsNone(entry)
def test_webhook_with_missing_obs_data(self, mock_reporter, mock_url_exists): request_file = os.path.join(self.resources_dir, 'obs-missing-request.json') with codecs.open(request_file, 'r', encoding='utf-8') as in_file: request_text = in_file.read() # convert Windows line endings to Linux line endings content = request_text.replace('\r\n', '\n') # deserialized object request_json = json.loads(content) mockLogger = MockLogger() mockDCS = MockAPI(self.resources_dir, 'https://git.door43.org/') self.MockDynamodbHandler.data = None self.MockS3Handler.reset() urls = { 'https://git.door43.org/Door43-Catalog/ylb_obs/archive/f8a8d8d757e7ea287cf91b266963f8523bdbd5ad.zip': 'ylb_obs_missing_data.zip' } mock_download = lambda url, dest: mockDCS.download_file( urls[url], dest) handler = WebhookHandler(event=request_json, context=None, logger=mockLogger, s3_handler=self.MockS3Handler, dynamodb_handler=self.MockDynamodbHandler, download_handler=mock_download) handler.run() entry = self.MockDynamodbHandler.data self.assertEqual(1, len(self.MockS3Handler.uploads)) self.assertIn('/ylb_obs.zip', self.MockS3Handler.uploads[0]['path']) self.assertIn( 'temp/ylb_obs/{}/ylb/obs/v4.1/obs.zip'.format(entry['commit_id']), self.MockS3Handler.uploads[0]['key']) self.assertEqual('f8a8d8d757', entry['commit_id']) self.assertEqual(False, entry['dirty']) self.assertEqual('ylb', entry['language']) self.assertEqual('2017-04-25T21:46:30+00:00', entry['timestamp']) self.assertEqual(False, entry['signed']) self.assertEqual('ylb_obs', entry['repo_name']) assert_object_equals_file( self, json.loads(entry['package']), os.path.join(self.resources_dir, 'expected_obs_package_missing_data.json'))
def test_webhook_ulb(self, mock_reporter, mock_url_exists): request_file = os.path.join(self.resources_dir, 'ulb-request.json') with codecs.open(request_file, 'r', encoding='utf-8') as in_file: request_text = in_file.read() # convert Windows line endings to Linux line endings content = request_text.replace('\r\n', '\n') # deserialized object request_json = json.loads(content) mockLogger = MockLogger() mockDCS = MockAPI(self.resources_dir, 'https://git.door43.org/') urls = { 'https://git.door43.org/Door43-Catalog/en_ulb/archive/2fbfd081f46487e48e49090a95c48d45e04e6bed.zip': 'en_ulb.zip' } mock_download = lambda url, dest: mockDCS.download_file( urls[url], dest) self.MockDynamodbHandler.data = None self.MockS3Handler.reset() handler = WebhookHandler(event=request_json, context=None, logger=mockLogger, s3_handler=self.MockS3Handler, dynamodb_handler=self.MockDynamodbHandler, download_handler=mock_download) handler.run() entry = self.MockDynamodbHandler.data self.assertEqual(4, len(self.MockS3Handler.uploads)) # books and bundle self.assertIn('/en_ulb.zip', self.MockS3Handler.uploads[0]['path']) self.assertEqual('2fbfd081f4', entry['commit_id']) self.assertEqual(False, entry['dirty']) self.assertEqual('en', entry['language']) self.assertEqual('2017-05-02T22:52:04+00:00', entry['timestamp']) self.assertEqual(False, entry['signed']) self.assertEqual('en_ulb', entry['repo_name']) self.assertIn( 'temp/en_ulb/{}/en/ulb/v7/ulb.zip'.format(entry['commit_id']), self.MockS3Handler.uploads[0]['key']) assert_object_equals_file( self, json.loads(entry['package']), os.path.join(self.resources_dir, 'expected_ulb_package.json'))
def test_webhook_ulb_merged_pull_request(self, mock_reporter, mock_url_exists): request_file = os.path.join(self.resources_dir, 'ulb-merged-pull-request.json') with codecs.open(request_file, 'r', encoding='utf-8') as in_file: request_text = in_file.read() # convert Windows line endings to Linux line endings content = request_text.replace('\r\n', '\n') # deserialized object request_json = json.loads(content) mockLogger = MockLogger() mockDCS = MockAPI(self.resources_dir, 'https://git.door43.org/') urls = { 'https://git.door43.org/Door43-Catalog/ta_ulb/archive/0a7e25cd939f00086262fe94b9d25afc3b5dabd3.zip': 'ta_ulb.zip' } mock_download = lambda url, dest: mockDCS.download_file( urls[url], dest) self.MockDynamodbHandler.data = None self.MockS3Handler.reset() handler = WebhookHandler(event=request_json, context=None, logger=mockLogger, s3_handler=self.MockS3Handler, dynamodb_handler=self.MockDynamodbHandler, download_handler=mock_download) handler.run() entry = self.MockDynamodbHandler.data self.assertEqual(4, len(self.MockS3Handler.uploads)) # books and bundle self.assertIn('/ta_ulb.zip', self.MockS3Handler.uploads[0]['path']) self.assertEqual('0a7e25cd93', entry['commit_id']) self.assertEqual(False, entry['dirty']) self.assertEqual('ta', entry['language']) self.assertEqual('2017-08-17T18:56:52.884140+00:00', entry['timestamp']) self.assertEqual(False, entry['signed']) self.assertEqual('ta_ulb', entry['repo_name']) self.assertIn( 'temp/ta_ulb/{}/ta/ulb/v3/ulb.zip'.format(entry['commit_id']), self.MockS3Handler.uploads[0]['key'])
def test_complete_status(self, mock_reporter): mockV3Api = MockAPI(self.resources_dir, 'https://cdn.door43.org/') mockS3 = MockS3Handler('ts_bucket') mockDb = MockDynamodbHandler() mockDb._load_db( os.path.join(TestTsV2Catalog.resources_dir, 'complete_db.json')) mockLog = MockLogger() mock_get_url = lambda url, catch_exception: mockV3Api.get_url( url, catch_exception) mock_download = lambda url, dest: mockV3Api.download_file(url, dest) event = self.make_event() converter = TsV2CatalogHandler(event=event, context=None, logger=mockLog, s3_handler=mockS3, dynamodb_handler=mockDb, url_handler=mock_get_url, download_handler=mock_download, url_exists_handler=lambda url: True) result = converter.run() self.assertTrue(result) self.assertEqual(0, len(mockS3._recent_uploads)) self.assertIn('Catalog already generated', mockLog._messages)