Skip to content

Commit 5a24eea

Browse files
committed
Add all the unit tests
1 parent c4fe79f commit 5a24eea

12 files changed

+938
-13
lines changed

sdk/tests/accounts_tests.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import time
2+
import unittest
3+
import random
4+
5+
from web3 import Web3
6+
from eth_keys import keys
7+
8+
from sdk.kit import Kit
9+
from sdk.tests import test_data
10+
11+
12+
class TestAccountsWrapper(unittest.TestCase):
13+
14+
@classmethod
15+
def setUpClass(self):
16+
# https://alfajores-forno.celo-testnet.org
17+
# http://localhost:8545
18+
self.kit = Kit('https://alfajores-forno.celo-testnet.org')
19+
self.accounts_wrapper = self.kit.base_wrapper.create_and_get_contract_by_name(
20+
'Accounts')
21+
self.kit.wallet_add_new_key = test_data.pk1
22+
self.kit.wallet_add_new_key = test_data.pk2
23+
24+
self.validators_contract = self.kit.base_wrapper.create_and_get_contract_by_name(
25+
'Validators')
26+
self.locked_gold_contract = self.kit.base_wrapper.create_and_get_contract_by_name(
27+
'LockedGold')
28+
29+
self.min_locked_gold_value = self.kit.w3.toWei(10000, 'ether')
30+
31+
self.bls_public_key = '0x4fa3f67fc913878b068d1fa1cdddc54913d3bf988dbe5a36a20fa888f20d4894c408a6773f3d7bde11154f2a3076b700d345a42fd25a0e5e83f4db5586ac7979ac2053cd95d8f2efd3e959571ceccaa743e02cf4be3f5d7aaddb0b06fc9aff00'
32+
self.bls_pop = '0xcdb77255037eb68897cd487fdd85388cbda448f617f874449d4b11588b0b7ad8ddc20d9bb450b513bb35664ea3923900'
33+
34+
def test_authorize_validator_key_not_validator(self):
35+
accounts = list(self.kit.wallet.accounts.values())[1:]
36+
37+
account = accounts[0]
38+
signer = accounts[1]
39+
40+
self.kit.wallet_change_account = account.address
41+
42+
self.accounts_wrapper.create_account()
43+
44+
sig = self.get_parsed_signature_of_address(account.address, signer.address)
45+
46+
print(self.accounts_wrapper.authorize_validator_signer(signer.address, sig))
47+
48+
# def test_authorize_validator_key_validator(self):
49+
# accounts = list(self.kit.wallet.accounts.values())[1:]
50+
51+
# account = accounts[0]
52+
# signer = accounts[1]
53+
54+
# self.kit.wallet_change_account = account.address
55+
56+
# self.accounts_wrapper.create_account()
57+
58+
# self.setup_validator(account.address)
59+
60+
# sig = self.get_parsed_signature_of_address(account.address, signer.address)
61+
62+
# print(self.accounts_wrapper.authorize_validator_signer(signer.address, sig))
63+
64+
# def test_authorize_validator_key_change_bls_key(self):
65+
# hex_characters = '0123456789abcdef'
66+
# hex_sample = [random.choice(hex_characters) for _ in range(96)]
67+
# new_bls_public_key = '0x'+''.join(hex_sample)
68+
69+
# hex_sample = [random.choice(hex_characters) for _ in range(48)]
70+
# new_bls_pop = '0x'+''.join(hex_sample)
71+
72+
# accounts = list(self.kit.wallet.accounts.values())[1:]
73+
74+
# account = accounts[0]
75+
# signer = accounts[1]
76+
77+
# self.accounts_wrapper.create_account()
78+
79+
# self.setup_validator(account.address)
80+
81+
# sig = self.get_parsed_signature_of_address(account.address, signer.address)
82+
83+
# print(self.accounts_wrapper.authorize_validator_signer_and_bls(signer.address, sig, new_bls_public_key, new_bls_pop))
84+
85+
# def test_set_wallet_address_to_caller(self):
86+
# accounts = list(self.kit.wallet.accounts.values())[1:]
87+
88+
# self.accounts_wrapper.create_account()
89+
# print(self.accounts_wrapper.set_wallet_address(accounts[0].address))
90+
91+
# def test_set_wallet_address_to_different_address(self):
92+
# accounts = list(self.kit.wallet.accounts.values())[1:]
93+
94+
# account = accounts[0]
95+
# signer = accounts[1]
96+
97+
# self.accounts_wrapper.create_account()
98+
99+
# signature = self.accounts_wrapper.generate_proof_of_key_possession(account.address, signer.address)
100+
101+
# print(self.accounts_wrapper.set_wallet_address(signer.address, signature))
102+
103+
# def test_set_wallet_address_without_signature(self):
104+
# """
105+
# Should fail
106+
# """
107+
# accounts = list(self.kit.wallet.accounts.values())[1:]
108+
# print(self.accounts_wrapper.set_wallet_address(accounts[1].address))
109+
110+
# def register_account_with_locked_gold(self, account: str):
111+
# if not self.accounts_wrapper.is_account(account):
112+
# _ = self.accounts_wrapper.create_account({'from': account})
113+
# _ = self.locked_gold_contract.lock(
114+
# {'from': account, 'value': self.min_locked_gold_value})
115+
116+
def get_parsed_signature_of_address(self, address: str, signer: 'Account object') -> 'Signature object':
117+
message = self.kit.w3.soliditySha3(['address'], [address]).hex()
118+
signature = self.kit.wallet.active_account.signHash(message)
119+
return signature
120+
121+
# def setup_validator(self, validator_account: str):
122+
# """
123+
# validator_account should be an address of active account in wallet now
124+
# """
125+
# self.register_account_with_locked_gold(validator_account)
126+
# priv_key = keys.PrivateKey(self.kit.wallet.active_account.privateKey)
127+
# pub_key = priv_key.public_key
128+
# _ = self.validators_contract.register_validator(pub_key, self.bls_public_key, self.bls_pop)

sdk/tests/attestations_tests.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import time
22
import unittest
3+
from unittest.mock import Mock
34

45
from web3 import Web3
56

67
from sdk.kit import Kit
78
from sdk.tests import test_data
9+
from sdk.utils import phone_number_utils
810

911

1012
class TestAttestationsWrapper(unittest.TestCase):
@@ -14,8 +16,50 @@ def setUpClass(self):
1416
self.kit = Kit('https://alfajores-forno.celo-testnet.org')
1517
self.attestations_wrapper = self.kit.base_wrapper.create_and_get_contract_by_name(
1618
'Attestations')
19+
self.kit.wallet_add_new_key = test_data.pk1
1720
self.kit.wallet_add_new_key = test_data.pk2
21+
22+
self.phone_number = '+15555555555'
23+
self.identifier = phone_number_utils.get_phone_hash(self.kit.w3.soliditySha3, self.phone_number)
24+
25+
def test_no_completions(self):
26+
accounts = list(self.kit.wallet.accounts.values())[1:]
27+
mock = Mock()
28+
mock.return_value = {'completed': 0, 'total': 3}
29+
self.attestations_wrapper.get_attestation_stat = mock
30+
31+
result = self.attestations_wrapper.get_verified_status(self.identifier, accounts[0].address)
32+
33+
self.assertFalse(result['is_verified'])
34+
self.assertEqual(result['num_attestations_remaining'], 3)
35+
36+
def test_not_enough_completions(self):
37+
accounts = list(self.kit.wallet.accounts.values())[1:]
38+
mock = Mock()
39+
mock.return_value = {'completed': 2, 'total': 6}
40+
self.attestations_wrapper.get_attestation_stat = mock
41+
42+
result = self.attestations_wrapper.get_verified_status(self.identifier, accounts[0].address)
43+
44+
self.assertFalse(result['is_verified'])
45+
self.assertEqual(result['num_attestations_remaining'], 1)
46+
47+
def test_fraction_too_low(self):
48+
accounts = list(self.kit.wallet.accounts.values())[1:]
49+
mock = Mock()
50+
mock.return_value = {'completed': 3, 'total': 30}
51+
self.attestations_wrapper.get_attestation_stat = mock
52+
53+
result = self.attestations_wrapper.get_verified_status(self.identifier, accounts[0].address)
54+
55+
self.assertFalse(result['is_verified'])
1856

19-
def test_attestation_expiry_blocks(self):
20-
call = self.attestations_wrapper.attestation_expiry_blocks()
21-
self.assertEqual(type(call), int)
57+
def test_fraction_pass_threshold(self):
58+
accounts = list(self.kit.wallet.accounts.values())[1:]
59+
mock = Mock()
60+
mock.return_value = {'completed': 3, 'total': 9}
61+
self.attestations_wrapper.get_attestation_stat = mock
62+
63+
result = self.attestations_wrapper.get_verified_status(self.identifier, accounts[0].address)
64+
65+
self.assertTrue(result['is_verified'])

sdk/tests/dev_net_conf.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"downtimeSlasher": {
3+
"slashableDowntime": 60
4+
},
5+
"epochRewards": {
6+
"frozen": false
7+
},
8+
"exchange": {
9+
"frozen": false,
10+
"minimumReports": 1
11+
},
12+
"goldToken": {
13+
"frozen": false
14+
},
15+
"governance": {
16+
"dequeueFrequency": 30,
17+
"queueExpiry": 1000,
18+
"approvalStageDuration": 100,
19+
"referendumStageDuration": 100,
20+
"executionStageDuration": 100,
21+
"minDeposit": 1,
22+
"concurrentProposals": 5
23+
},
24+
"governanceApproverMultiSig": {
25+
"signatories": [
26+
"0x5409ed021d9299bf6814279a6a1411a7e866a631"
27+
],
28+
"numRequiredConfirmations": 1
29+
},
30+
"oracles": {
31+
"reportExpiry": 300
32+
},
33+
"reserve": {
34+
"initialBalance": 100000000,
35+
"otherAddresses": ["0x91c987bf62D25945dB517BDAa840A6c661374402"]
36+
37+
},
38+
"reserveSpenderMultiSig": {
39+
"signatories": ["0x5409ed021d9299bf6814279a6a1411a7e866a631", "0x4404ac8bd8F9618D27Ad2f1485AA1B2cFD82482D"],
40+
"numRequiredConfirmations": 2
41+
},
42+
"stableToken": {
43+
"goldPrice": 1,
44+
"initialBalances": {
45+
"addresses": [
46+
"0x5409ED021D9299bf6814279A6A1411A7e866A631",
47+
"0x6Ecbe1DB9EF729CBe972C83Fb886247691Fb6beb",
48+
"0xE36Ea790bc9d7AB70C55260C66D52b1eca985f84",
49+
"0xE834EC434DABA538cd1b9Fe1582052B880BD7e63"
50+
],
51+
"values": [
52+
"50000000000000000000000",
53+
"50000000000000000000000",
54+
"50000000000000000000000",
55+
"50000000000000000000000"
56+
]
57+
},
58+
"oracles": [
59+
"0x5409ED021D9299bf6814279A6A1411A7e866A631",
60+
"0xE36Ea790bc9d7AB70C55260C66D52b1eca985f84",
61+
"0x06cEf8E666768cC40Cc78CF93d9611019dDcB628",
62+
"0x7457d5E02197480Db681D3fdF256c7acA21bDc12"
63+
],
64+
"frozen": false
65+
},
66+
"validators": {
67+
"commissionUpdateDelay": 3
68+
}
69+
}

sdk/tests/exchange_tests.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import time
2+
import unittest
3+
from unittest.mock import Mock
4+
5+
from web3 import Web3
6+
7+
from sdk.kit import Kit
8+
from sdk.tests import test_data
9+
10+
11+
class TestExchangeWrapper(unittest.TestCase):
12+
13+
@classmethod
14+
def setUpClass(self):
15+
self.kit = Kit('https://alfajores-forno.celo-testnet.org') # https://alfajores-forno.celo-testnet.org
16+
self.exchange_wrapper = self.kit.base_wrapper.create_and_get_contract_by_name(
17+
'Exchange')
18+
self.kit.wallet_add_new_key = test_data.pk1
19+
self.kit.wallet_add_new_key = test_data.pk2
20+
21+
self.one = self.kit.w3.toWei(1, 'ether')
22+
self.large_buy_amount = self.kit.w3.toWei(1000, 'ether')
23+
24+
def test_check_buckets(self):
25+
buy_bucket, sell_bucket = self.exchange_wrapper.get_buy_and_sell_buckets(True)
26+
27+
self.assertEqual(type(buy_bucket), int)
28+
self.assertEqual(type(sell_bucket), int)
29+
30+
self.assertTrue(buy_bucket > 0)
31+
self.assertTrue(sell_bucket > 0)
32+
33+
def test_quote_usd_sell(self):
34+
self.assertEqual(type(self.exchange_wrapper.quote_usd_sell(self.one)), int)
35+
36+
def test_quote_gold_sell(self):
37+
self.assertEqual(type(self.exchange_wrapper.quote_gold_sell(self.one)), int)
38+
39+
def test_quote_usd_buy(self):
40+
self.assertEqual(type(self.exchange_wrapper.quote_usd_buy(self.one)), int)
41+
42+
def test_quote_gold_buy(self):
43+
self.assertEqual(type(self.exchange_wrapper.quote_gold_buy(self.one)), int)
44+
45+
def test_sell_dollar(self):
46+
gold_amount = self.exchange_wrapper.quote_usd_sell(self.one)
47+
stable_token_wrapper = self.kit.base_wrapper.create_and_get_contract_by_name('StableToken')
48+
approve_tx = stable_token_wrapper.approve(self.exchange_wrapper.address, self.one)
49+
time.sleep(3)
50+
sell_tx = self.exchange_wrapper.sell_dollar(self.one, gold_amount)
51+
52+
self.assertEqual(type(sell_tx), bytes)
53+
54+
# TODO: this test is failing for some unknown reason
55+
def test_sell_gold(self):
56+
usd_amount = self.exchange_wrapper.quote_gold_sell(self.one)
57+
gold_token_wrapper = self.kit.base_wrapper.create_and_get_contract_by_name('GoldToken')
58+
approve_tx = gold_token_wrapper.approve(self.exchange_wrapper.address, self.one)
59+
time.sleep(3)
60+
sell_tx = self.exchange_wrapper.sell_gold(self.one, usd_amount)
61+
62+
self.assertEqual(type(sell_tx), bytes)
63+
64+
def test_get_gold_exchange_rate(self):
65+
self.assertTrue(self.exchange_wrapper.get_exchange_rate(self.large_buy_amount, True) > 0)
66+
67+
def test_get_dollar_exchange_rate(self):
68+
self.assertTrue(self.exchange_wrapper.get_usd_exchange_rate(self.large_buy_amount) > 0)

0 commit comments

Comments
 (0)