def assert_bad_request(self, params): test_function = MeInfoUpdate(event=params, context={}, dynamodb=self.dynamodb) response = test_function.main() self.assertEqual(response['statusCode'], 400)
def test_call_validate_user_existence(self): target_user_data = self.users_table_items[2] params = { 'body': { 'user_display_name': 'display_name_03', 'self_introduction': 'self_introduction_03' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': target_user_data['user_id'] } } } } params['body'] = json.dumps(params['body']) mock_lib = MagicMock() with patch('me_info_update.DBUtil', mock_lib): MeInfoUpdate(event=params, context={}, dynamodb=self.dynamodb).main() args, kwargs = mock_lib.validate_user_existence.call_args self.assertTrue(mock_lib.validate_user_existence.called) self.assertTrue(args[0]) self.assertTrue(args[1])
def test_main_ok(self): target_user_data = self.users_table_items[0] params = { 'body': { 'user_display_name': 'display_name_01', 'self_introduction': 'self_introduction_01' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': target_user_data['user_id'] } } } } params['body'] = json.dumps(params['body']) me_infos_update = MeInfoUpdate(event=params, context={}, dynamodb=self.dynamodb) response = me_infos_update.main() users_table = self.dynamodb.Table(os.environ['USERS_TABLE_NAME']) user = users_table.get_item(Key={ 'user_id': target_user_data['user_id'] }).get('Item') expected_items = { 'user_id': target_user_data['user_id'], 'user_display_name': json.loads(params['body'])['user_display_name'], 'self_introduction': json.loads(params['body'])['self_introduction'], 'sync_elasticsearch': 1 } self.assertEqual(response['statusCode'], 200) users_param_names = [ 'user_id', 'user_display_name', 'self_introduction', 'sync_elasticsearch' ] for key in users_param_names: self.assertEqual(expected_items[key], user[key])
def test_main_ok_with_script_tag(self): target_user_data = self.users_table_items[2] params = { 'body': { 'user_display_name': "a<script>alert('ab')</script>b", 'self_introduction': "c<script>alert('cd')</script>d" }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': target_user_data['user_id'] } } } } params['body'] = json.dumps(params['body']) me_infos_update = MeInfoUpdate(event=params, context={}, dynamodb=self.dynamodb) response = me_infos_update.main() users_table = self.dynamodb.Table(os.environ['USERS_TABLE_NAME']) user = users_table.get_item(Key={ 'user_id': target_user_data['user_id'] }).get('Item') expected_items = { 'user_id': target_user_data['user_id'], 'user_display_name': "a<script>alert('ab')</script>b", 'self_introduction': "c<script>alert('cd')</script>d" } self.assertEqual(response['statusCode'], 200) users_param_names = [ 'user_id', 'user_display_name', 'self_introduction' ] for key in users_param_names: self.assertEqual(expected_items[key], user[key])
def lambda_handler(event, context): me_info_update = MeInfoUpdate(event, context, dynamodb) return me_info_update.main()