def test_main_ng_not_exists_private_eth_address(self): test_tip_value = 100 to_address = format(10, '064x') burn_value = int(test_tip_value / Decimal(10)) raw_transactions = self.create_singed_transactions( to_address, test_tip_value, burn_value) with patch('me_wallet_tip.UserUtil.get_cognito_user_info' ) as mock_get_cognito_user_info: mock_get_cognito_user_info.return_value = { 'UserAttributes': [{ 'Name': 'hoge', 'Value': 'piyo' }] } target_article_id = self.article_info_table_items[0]['article_id'] event = { 'body': { 'article_id': target_article_id, 'tip_signed_transaction': raw_transactions['tip'].rawTransaction.hex(), 'burn_signed_transaction': raw_transactions['burn'].rawTransaction.hex() }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'custom:private_eth_address': self.test_account.address, 'phone_number_verified': 'true', 'email_verified': 'true' } } } } event['body'] = json.dumps(event['body']) response = MeWalletTip(event, {}, self.dynamodb, cognito=None).main() self.assertEqual(response['statusCode'], 404) self.assertEqual( response['body'], '{"message": "Record Not Found: private_eth_address"}') tip_table = self.dynamodb.Table(os.environ['TIP_TABLE_NAME']) tips = tip_table.scan()['Items'] self.assertEqual(len(tips), 0)
def test_main_ng_same_user(self): test_tip_value = 100 to_address = format(10, '064x') burn_value = int(test_tip_value / Decimal(10)) raw_transactions = self.create_singed_transactions( to_address, test_tip_value, burn_value) with patch('me_wallet_tip.UserUtil.get_private_eth_address' ) as mock_get_private_eth_address: mock_get_private_eth_address.return_value = '0x' + to_address[24:] target_article_id = self.article_info_table_items[0]['article_id'] event = { 'body': { 'article_id': target_article_id, 'tip_signed_transaction': raw_transactions['tip'].rawTransaction.hex(), 'burn_signed_transaction': raw_transactions['burn'].rawTransaction.hex() }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': self.article_info_table_items[0]['user_id'], 'custom:private_eth_address': self.test_account.address, 'phone_number_verified': 'true', 'email_verified': 'true' } } } } event['body'] = json.dumps(event['body']) response = MeWalletTip(event, {}, self.dynamodb, cognito=None).main() self.assertEqual(response['statusCode'], 400) self.assertEqual( response['body'], '{"message": "Invalid parameter: Can not tip to myself"}') tip_table = self.dynamodb.Table(os.environ['TIP_TABLE_NAME']) tips = tip_table.scan()['Items'] self.assertEqual(len(tips), 0)
def test_main_ng_same_user(self): with patch('me_wallet_tip.UserUtil') as user_util_mock: user_util_mock.get_cognito_user_info.return_value = { 'UserAttributes': [{ 'Name': 'custom:private_eth_address', 'Value': '0x1111111111111111111111111111111111111111' }] } target_article_id = self.article_info_table_items[0]['article_id'] target_tip_value = str(1 * (10**18)) event = { 'body': { 'article_id': target_article_id, 'tip_value': target_tip_value }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': self.article_info_table_items[0]['user_id'], 'custom:private_eth_address': '0x5d7743a4a6f21593ff6d3d81595f270123456789', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } event['body'] = json.dumps(event['body']) response = MeWalletTip(event, {}, self.dynamodb, cognito=None).main() self.assertEqual(response['statusCode'], 400) self.assertEqual( response['body'], '{"message": "Invalid parameter: Can not tip to myself"}') tip_table = self.dynamodb.Table(os.environ['TIP_TABLE_NAME']) tips = tip_table.scan()['Items'] self.assertEqual(len(tips), 0)
def test_main_ng_with_not_burnable_user(self): with patch('me_wallet_tip.UserUtil') as user_util_mock, \ patch('me_wallet_tip.MeWalletTip._MeWalletTip__burn_transaction') as mock_burn_transaction: user_util_mock.get_cognito_user_info.return_value = { 'UserAttributes': [{ 'Name': 'custom:private_eth_address', 'Value': '0x1111111111111111111111111111111111111111' }] } mock_burn_transaction.return_value = 'burn_transaction_hash' target_article_id = self.article_info_table_items[0]['article_id'] target_tip_value = str(100) event = { 'body': { 'article_id': target_article_id, 'tip_value': target_tip_value }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'custom:private_eth_address': '0x5d7743a4a6f21593ff6d3d81595f270123456789', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } event['body'] = json.dumps(event['body']) response = MeWalletTip(event, {}, self.dynamodb, cognito=None).main() self.assertEqual(response['statusCode'], 400) self.assertEqual( json.loads(response['body'])['message'], 'Invalid parameter: Required at least 110 token')
def test_main_ok_min_value(self): with patch('me_wallet_tip.UserUtil') as user_util_mock: user_util_mock.get_cognito_user_info.return_value = { 'UserAttributes': [{ 'Name': 'custom:private_eth_address', 'Value': '0x1111111111111111111111111111111111111111' }] } target_article_id = self.article_info_table_items[0]['article_id'] target_tip_value = str(settings.parameters['tip_value']['minimum']) event = { 'body': { 'article_id': target_article_id, 'tip_value': target_tip_value }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'custom:private_eth_address': '0x5d7743a4a6f21593ff6d3d81595f270123456789', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } event['body'] = json.dumps(event['body']) response = MeWalletTip(event, {}, self.dynamodb, cognito=None).main() self.assertEqual(response['statusCode'], 200) tip_table = self.dynamodb.Table(os.environ['TIP_TABLE_NAME']) tips = tip_table.scan()['Items'] self.assertEqual(len(tips), 1) expected_tip = { 'user_id': event['requestContext']['authorizer']['claims'] ['cognito:username'], 'to_user_id': self.article_info_table_items[0]['user_id'], 'tip_value': Decimal(target_tip_value), 'article_id': target_article_id, 'article_title': self.article_info_table_items[0]['title'], 'transaction': '0x0000000000000000000000000000000000000000', 'burn_transaction': 'burn_transaction_hash', 'uncompleted': Decimal(1), 'sort_key': Decimal(1520150552000003), 'target_date': '2018-03-04', 'created_at': Decimal(int(1520150552.000003)) } self.assertEqual(expected_tip, tips[0])
def assert_bad_request(self, params): target_function = MeWalletTip(params, {}, self.dynamodb, cognito=None) response = target_function.main() self.assertEqual(response['statusCode'], 400)
def test_main_ok_min_value(self): test_tip_value = 10 to_address = format(10, '064x') burn_value = int(test_tip_value / Decimal(10)) raw_transactions = self.create_singed_transactions( to_address, test_tip_value, burn_value) with patch('me_wallet_tip.UserUtil.get_private_eth_address' ) as mock_get_private_eth_address: mock_get_private_eth_address.return_value = '0x' + to_address[24:] target_article_id = self.article_info_table_items[0]['article_id'] event = { 'body': { 'article_id': target_article_id, 'tip_signed_transaction': raw_transactions['tip'].rawTransaction.hex(), 'burn_signed_transaction': raw_transactions['burn'].rawTransaction.hex() }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'custom:private_eth_address': self.test_account.address, 'phone_number_verified': 'true', 'email_verified': 'true' } } } } event['body'] = json.dumps(event['body']) response = MeWalletTip(event, {}, self.dynamodb, cognito=None).main() self.assertEqual(response['statusCode'], 200) tip_table = self.dynamodb.Table(os.environ['TIP_TABLE_NAME']) tips = tip_table.scan()['Items'] self.assertEqual(len(tips), 1) expected_tip = { 'user_id': event['requestContext']['authorizer']['claims'] ['cognito:username'], 'to_user_id': self.article_info_table_items[0]['user_id'], 'tip_value': Decimal(test_tip_value), 'article_id': target_article_id, 'article_title': self.article_info_table_items[0]['title'], 'transaction': 'tip_transaction_hash', 'burn_transaction': 'burn_transaction_hash', 'uncompleted': Decimal(1), 'sort_key': Decimal(1520150552000003), 'target_date': '2018-03-04', 'created_at': Decimal(int(1520150552.000003)) } self.assertEqual(expected_tip, tips[0])
def test_main_ok_call_validate_methods(self): test_tip_value = 10 to_address = format(10, '064x') burn_value = int(test_tip_value / Decimal(10)) raw_transactions = self.create_singed_transactions( to_address, test_tip_value, burn_value) with patch('me_wallet_tip.UserUtil.get_private_eth_address') as mock_get_private_eth_address, \ patch('me_wallet_tip.UserUtil.verified_phone_and_email') as mock_verified_phone_and_email, \ patch('me_wallet_tip.UserUtil.validate_private_eth_address') as mock_validate_private_eth_address, \ patch('me_wallet_tip.PrivateChainUtil.validate_raw_transaction_signature') as mock_validate_signature, \ patch('me_wallet_tip.PrivateChainUtil.validate_erc20_transfer_data') \ as mock_validate_erc20_transfer_data: mock_get_private_eth_address.return_value = '0x' + to_address[24:] target_article_id = self.article_info_table_items[0]['article_id'] event = { 'body': { 'article_id': target_article_id, 'tip_signed_transaction': raw_transactions['tip'].rawTransaction.hex(), 'burn_signed_transaction': raw_transactions['burn'].rawTransaction.hex() }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'custom:private_eth_address': self.test_account.address, 'phone_number_verified': 'true', 'email_verified': 'true' } } } } event['body'] = json.dumps(event['body']) response = MeWalletTip(event, {}, self.dynamodb, cognito=None).main() self.assertEqual(response['statusCode'], 200) # verified_phone_and_email args, _ = mock_verified_phone_and_email.call_args self.assertEqual(event, args[0]) # validate_private_eth_address args, _ = mock_validate_private_eth_address.call_args self.assertEqual(self.dynamodb, args[0]) self.assertEqual('act_user_01', args[1]) # validate_raw_transaction_signature args, _ = mock_validate_signature.call_args_list[0] self.assertEqual(raw_transactions['tip'].rawTransaction.hex(), args[0]) self.assertEqual(self.test_account.address, args[1]) args, _ = mock_validate_signature.call_args_list[1] self.assertEqual(raw_transactions['burn'].rawTransaction.hex(), args[0]) self.assertEqual(self.test_account.address, args[1]) # validate_erc20_transfer_data args, _ = mock_validate_erc20_transfer_data.call_args_list[0] tip_data = PrivateChainUtil.get_data_from_raw_transaction( raw_transactions['tip'].rawTransaction.hex(), '0x5') self.assertEqual(tip_data, args[0]) self.assertEqual('0x' + to_address[24:], args[1]) args, _ = mock_validate_erc20_transfer_data.call_args_list[1] burn_data = PrivateChainUtil.get_data_from_raw_transaction( raw_transactions['burn'].rawTransaction.hex(), '0x6') self.assertEqual(burn_data, args[0]) self.assertEqual('0x' + os.environ['BURN_ADDRESS'], args[1])
def assert_bad_request(self, event): response = MeWalletTip(event, {}, dynamodb=self.dynamodb).main() self.assertEqual(response['statusCode'], 400)
def lambda_handler(event, context): me_wallet_tip = MeWalletTip(event, context, dynamodb, cognito=cognito) return me_wallet_tip.main()