Skip to content

Commit

Permalink
Merge pull request #417 from AlisProject/ALIS-5265-2
Browse files Browse the repository at this point in the history
add migration checking
  • Loading branch information
keillera authored Apr 28, 2020
2 parents d0ebe4b + 657e452 commit 4da37f6
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,6 @@

* この作業で特に注意する点があれば記載する
* その他、補足事項があれば記載する

# レビュワーに依頼したいこと
- 内容を確認し、問題なければマージしてください。
2 changes: 2 additions & 0 deletions src/common/user_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def verified_phone_and_email(event):

raise NotVerifiedUserError('Not Verified')

# private_eth_addressが「DynamoDBに」存在するか確認。
# Cognitoに存在してもFalseとなる
@staticmethod
def exists_private_eth_address(dynamodb, user_id):
# validate exists private_eth_address
Expand Down
6 changes: 6 additions & 0 deletions src/handlers/me/wallet/balance/me_wallet_balance.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from private_chain_util import PrivateChainUtil
from lambda_base import LambdaBase
from user_util import UserUtil


class MeWalletBalance(LambdaBase):
Expand All @@ -11,11 +12,16 @@ def validate_params(self):
pass

def exec_main_proc(self):
user_id = self.event['requestContext']['authorizer']['claims']['cognito:username']
address = self.event['requestContext']['authorizer']['claims'].get('custom:private_eth_address')

# 現在のトークン量を取得
# まだウォレットアドレスを作成していないユーザには 0 を返す
if address is None:
balance = '0x0'
elif not UserUtil.exists_private_eth_address(self.dynamodb, user_id):
# Cognito上にprivate_eth_addressは存在するが、カストディ規制のウォレット移行が完了していないユーザにも0を返す
balance = '0x0'
else:
balance = PrivateChainUtil.get_balance(address)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def get_schema(self):
def validate_params(self):
UserUtil.verified_phone_and_email(self.event)

# カストディ規制時のウォレット移行が済んでいなければ利用不可
user_id = self.event['requestContext']['authorizer']['claims']['cognito:username']
UserUtil.validate_private_eth_address(self.dynamodb, user_id)

def exec_main_proc(self):
# 必要なパラメーターを取得する
self.web3 = Web3(HTTPProvider(os.environ['PRIVATE_CHAIN_OPERATION_URL']))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def get_schema(self):
def validate_params(self):
UserUtil.verified_phone_and_email(self.event)

# カストディ規制時のウォレット移行が済んでいなければ利用不可
user_id = self.event['requestContext']['authorizer']['claims']['cognito:username']
UserUtil.validate_private_eth_address(self.dynamodb, user_id)

def exec_main_proc(self):
# 履歴取得対象の開始ブロック番号を算出
to_block = self.__get_current_block_number()
Expand Down
37 changes: 35 additions & 2 deletions tests/handlers/me/wallet/balance/test_me_wallet_blance.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import boto3
import os
import json
from unittest import TestCase
from me_wallet_balance import MeWalletBalance
from unittest.mock import patch, MagicMock
from tests_util import TestsUtil


class TestMeWalletBalance(TestCase):
dynamodb = boto3.resource('dynamodb', endpoint_url='http://localhost:4569/')
dynamodb = TestsUtil.get_dynamodb_client()

def setUp(self):
TestsUtil.set_all_tables_name_to_env()
TestsUtil.delete_all_tables(self.dynamodb)

user_configurations_items = [
{
'user_id': 'test-user',
'private_eth_address': '0x1234567890123456789012345678901234567890'
},
]
TestsUtil.create_table(self.dynamodb, os.environ['USER_CONFIGURATIONS_TABLE_NAME'], user_configurations_items)

def test_main_ok(self):
test_address = '0x5d7743a4a6f21593ff6d3d81595f270123456789'
Expand Down Expand Up @@ -43,3 +56,23 @@ def test_main_ok_not_exists_private_eth_address(self):
response = MeWalletBalance(params, {}, dynamodb=self.dynamodb).main()
self.assertEqual(200, response['statusCode'])
self.assertEqual({'result': '0x0'}, json.loads(response['body']))

def test_main_return_zero_if_user_have_not_migrated(self):
test_address = '0x5d7743a4a6f21593ff6d3d81595f270123456789'
params = {
'requestContext': {
'authorizer': {
'claims': {
'cognito:username': 'not-migrated-user',
'custom:private_eth_address': test_address
}
}
}
}

test_balance = '0x10'
magic_lib = MagicMock(return_value=test_balance)
with patch('private_chain_util.PrivateChainUtil.get_balance', magic_lib):
response = MeWalletBalance(params, {}, dynamodb=self.dynamodb).main()
self.assertEqual(200, response['statusCode'])
self.assertEqual({'result': '0x0'}, json.loads(response['body']))
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import json
import boto3
from unittest import TestCase
from me_wallet_token_allhistories_create import MeWalletTokenAllhistoriesCreate
Expand Down Expand Up @@ -44,6 +45,17 @@ def setUp(self):
TestsUtil.create_table(self.dynamodb, os.environ['NOTIFICATION_TABLE_NAME'], self.notification_items)
TestsUtil.create_table(self.dynamodb, os.environ['UNREAD_NOTIFICATION_MANAGER_TABLE_NAME'],
self.unread_notification_manager_items)
user_configurations_items = [
{
'user_id': 'user_01',
'private_eth_address': '0x1234567890123456789012345678901234567890'
},
{
'user_id': 'user_02',
'private_eth_address': '0x1234567890123456789012345678901234567892'
}
]
TestsUtil.create_table(self.dynamodb, os.environ['USER_CONFIGURATIONS_TABLE_NAME'], user_configurations_items)

def TearDown(self):
TestsUtil.delete_all_tables(self.dynamodb)
Expand Down Expand Up @@ -246,6 +258,25 @@ def test_add_type_ok(self):
event, {}, self.dynamodb).add_type('---', None, user_eoa)
self.assertEqual(response, 'unknown')

def test_ng_migration_checking(self):
event = {
'requestContext': {
'authorizer': {
'claims': {
'cognito:username': 'not-migrated-user',
'cognito-identity': 'ap-northeast-1:hogehoge',
'custom:private_eth_address': '0x1111111111111111111111111111111111111111',
'phone_number_verified': 'true',
'email_verified': 'true'
}
}
}
}

response = MeWalletTokenAllhistoriesCreate(event, {}, dynamodb=self.dynamodb, s3=self.s3).main()
self.assertEqual(response['statusCode'], 403)
self.assertEqual(json.loads(response['body'])['message'], 'Not exists private_eth_address')


class PrivateChainEthFilterFakeResponse:
def __init__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import json
import settings
from unittest import TestCase
from me_wallet_token_histories_index import MeWalletTokenHistoriesIndex
Expand All @@ -10,8 +11,18 @@ class TestMeWalletTokenHistoriesIndex(TestCase):
dynamodb = TestsUtil.get_dynamodb_client()

@classmethod
def setUpClass(cls):
def setUpClass(self):
TestsUtil.set_aws_auth_to_env()
TestsUtil.set_all_tables_name_to_env()
TestsUtil.delete_all_tables(self.dynamodb)

user_configurations_items = [
{
'user_id': 'user_01',
'private_eth_address': '0x1234567890123456789012345678901234567890'
},
]
TestsUtil.create_table(self.dynamodb, os.environ['USER_CONFIGURATIONS_TABLE_NAME'], user_configurations_items)

def assert_bad_request(self, params):
target_function = MeWalletTokenHistoriesIndex(params, {}, self.dynamodb, cognito=None)
Expand Down Expand Up @@ -95,3 +106,22 @@ def test_main_ok(self):
}
}
self.assertEqual(mock_send_transaction.call_args_list[3][1], args_apply_relay_events)

def test_ng_migration_checking(self):
event = {
'requestContext': {
'authorizer': {
'claims': {
'cognito:username': 'not-migrated-user',
'cognito-identity': 'ap-northeast-1:hogehoge',
'custom:private_eth_address': '0x1111111111111111111111111111111111111111',
'phone_number_verified': 'true',
'email_verified': 'true'
}
}
}
}

response = MeWalletTokenHistoriesIndex(event, {}, dynamodb=self.dynamodb).main()
self.assertEqual(response['statusCode'], 403)
self.assertEqual(json.loads(response['body'])['message'], 'Not exists private_eth_address')

0 comments on commit 4da37f6

Please sign in to comment.