def test_call_sanitize_text(self): title_str = 'test text' params = { 'pathParameters': { 'article_id': 'publicId0001' }, 'body': { 'title': title_str }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) mock_lib = MagicMock() with patch('me_articles_public_title_update.TextSanitizer', mock_lib): MeArticlesPublicTitleUpdate(params, {}, self.dynamodb).main() args, kwargs = mock_lib.sanitize_text.call_args self.assertTrue(mock_lib.sanitize_text.called) self.assertEqual(args[0], title_str)
def test_call_validate_article_existence(self): params = { 'pathParameters': { 'article_id': 'publicId0001' }, 'body': { 'title': 'test text' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) mock_lib = MagicMock() with patch('me_articles_public_title_update.DBUtil', mock_lib): MeArticlesPublicTitleUpdate(params, {}, self.dynamodb).main() args, kwargs = mock_lib.validate_article_existence.call_args self.assertTrue(mock_lib.validate_article_existence.called) self.assertTrue(args[0]) self.assertTrue(args[1]) self.assertTrue(kwargs['user_id']) self.assertEqual(kwargs['status'], 'public') self.assertEqual(kwargs['version'], 2)
def test_main_ok(self): params = { 'pathParameters': { 'article_id': 'publicId0001' }, 'body': { 'title': 'title text' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } article_content_edit_before = self.article_content_edit_table.scan( )['Items'] params['body'] = json.dumps(params['body']) response = MeArticlesPublicTitleUpdate(params, {}, self.dynamodb).main() article_content_edit_after = self.article_content_edit_table.scan( )['Items'] self.assertEqual(response['statusCode'], 200) self.assertEqual( len(article_content_edit_after) - len(article_content_edit_before), 0) article_content_edit = self.article_content_edit_table.get_item( Key={'article_id': params['pathParameters']['article_id']})['Item'] self.assertEqual( params['requestContext']['authorizer']['claims'] ['cognito:username'], article_content_edit['user_id']) for key, value in json.loads(params['body']).items(): self.assertEqual(value, article_content_edit[key])
def test_main_ok_with_empty_string(self): params = { 'pathParameters': { 'article_id': 'publicId0001' }, 'body': { 'title': '' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) response = MeArticlesPublicTitleUpdate(params, {}, self.dynamodb).main() article_content_edit = self.article_content_edit_table.get_item( Key={'article_id': params['pathParameters']['article_id']})['Item'] self.assertEqual(response['statusCode'], 200) expected_items = { 'article_id': self.article_content_edit_items[0].get('article_id'), 'eye_catch_url': self.article_content_edit_items[0].get('eye_catch_url'), 'title': None, 'body': self.article_content_edit_items[0].get('body'), 'overview': self.article_content_edit_items[0].get('overview'), 'user_id': self.article_content_edit_items[0].get('user_id'), } self.assertEqual(article_content_edit, expected_items)
def assert_bad_request(self, params): me_articles_public_title_update = MeArticlesPublicTitleUpdate( params, {}, self.dynamodb) response = me_articles_public_title_update.main() self.assertEqual(response['statusCode'], 400)
def lambda_handler(event, context): me_articles_public_title_update = MeArticlesPublicTitleUpdate( event, context, dynamodb) return me_articles_public_title_update.main()