def test_vsp_authorize(http_get_post): pool = vsp.VotingServiceProvider(**votingServiceProvider) success = {"status": "success", "data": purchaseInfo} addressNotSet = { "status": "error", "code": 9, "message": "no address submitted", } # ok addr = "TkKmVKG7u7PwhQaYr7wgMqBwHneJ2cN4e5YpMVUsWSopx81NFXEzK" http_get_post(pool.apiPath("getpurchaseinfo"), success) pool.authorize(addr) # address not submitted addr = "TkKmVKG7u7PwhQaYr7wgMqBwHneJ2cN4e5YpMVUsWSopx81NFXEzK" http_get_post(pool.apiPath("getpurchaseinfo"), addressNotSet) http_get_post(pool.apiPath("getpurchaseinfo"), success) http_get_post((pool.apiPath("address"), repr({"UserPubKeyAddr": addr})), success) pool.authorize(addr) # other error systemErr = {"status": "error", "code": 14, "message": "system error"} addr = "TkKmVKG7u7PwhQaYr7wgMqBwHneJ2cN4e5YpMVUsWSopx81NFXEzK" http_get_post(pool.apiPath("getpurchaseinfo"), systemErr) with pytest.raises(DecredError): pool.authorize(addr) # wrong address addr = "TkQ4jEVTpGn1LZBPrAoUJ15fDGGHubzd1DDkMSc4hxtHXpHjW1BJ8" http_get_post(pool.apiPath("getpurchaseinfo"), systemErr) with pytest.raises(DecredError): pool.authorize(addr)
def test_vsp_get_stats(http_get_post): pool = vsp.VotingServiceProvider(**votingServiceProvider) success = {"status": "success", "data": poolStats} # ok http_get_post(pool.apiPath("stats"), success) pool.getStats() # pool error systemErr = {"status": "error", "code": 14, "message": "system error"} http_get_post(pool.apiPath("stats"), systemErr) with pytest.raises(DecredError): pool.getStats()
def test_vsp_live(): the_vsp = vsp.VotingServiceProvider(VSP_URL, API_KEY, testnet.Name) the_vsp.authorize(SIGNING_ADDRESS) the_vsp.getStats() purc_info = the_vsp.getPurchaseInfo() # Test voting. if purc_info.voteBits & (1 << 1) != 0: nextVote = 1 | (1 << 2) else: nextVote = 1 | (1 << 1) the_vsp.setVoteBits(nextVote) purc_info = the_vsp.getPurchaseInfo() assert purc_info.voteBits == nextVote
def test_vsp_update_purchase_info(http_get_post): pool = vsp.VotingServiceProvider(**votingServiceProvider) success = {"status": "success", "data": purchaseInfo} # updated pool.purchaseInfo.unixTimestamp = 0 http_get_post(pool.apiPath("getpurchaseinfo"), success) pool.updatePurchaseInfo() assert pool.purchaseInfo.unixTimestamp != 0 # not updated # within the update threshhold before = int(time.time() - vsp.PURCHASE_INFO_LIFE / 2) pool.purchaseInfo.unixTimestamp = before pool.updatePurchaseInfo() assert pool.purchaseInfo.unixTimestamp == before
def test_vsp_set_vote_bits(http_get_post): pool = vsp.VotingServiceProvider(**votingServiceProvider) success = {"status": "success", "data": "ok"} # votebits are 5 assert pool.purchaseInfo.voteBits == 5 # ok http_get_post((pool.apiPath("voting"), repr({"VoteBits": 7})), success) pool.setVoteBits(7) # set to 7 assert pool.purchaseInfo.voteBits == 7 # pool error systemErr = {"status": "error", "code": 14, "message": "system error"} http_get_post((pool.apiPath("voting"), repr({"VoteBits": 3})), systemErr) with pytest.raises(DecredError): pool.setVoteBits(3) # no change assert pool.purchaseInfo.voteBits == 7
def test_vsp_get_purchase_info(http_get_post): pool = vsp.VotingServiceProvider(**votingServiceProvider) success = {"status": "success", "data": purchaseInfo} addressNotSet = { "status": "error", "code": 9, "message": "no address submitted", } # ok http_get_post(pool.apiPath("getpurchaseinfo"), success) pool.getPurchaseInfo() assert not pool.err # error http_get_post(pool.apiPath("getpurchaseinfo"), addressNotSet) with pytest.raises(DecredError): pool.getPurchaseInfo() assert pool.err
def test_vsp_validate(): pool = vsp.VotingServiceProvider(**votingServiceProvider) # correct address addr = "TkKmVKG7u7PwhQaYr7wgMqBwHneJ2cN4e5YpMVUsWSopx81NFXEzK" pool.validate(addr) # valid but wrong address addr = "TkQ4jEVTpGn1LZBPrAoUJ15fDGGHubzd1DDkMSc4hxtHXpHjW1BJ8" with pytest.raises(DecredError): pool.validate(addr) # invalid address addr = "ASDF" with pytest.raises(DecredError): pool.validate(addr) # no address addr = "" with pytest.raises(DecredError): pool.validate(addr)
def test_vsp_blobbing(): pool = vsp.VotingServiceProvider(**votingServiceProvider) b = vsp.VotingServiceProvider.blob(pool) assert isinstance(b, bytearray) rePool = vsp.VotingServiceProvider.unblob(b) assertVspIsEqual(rePool) ts = rePool.purchaseInfo.unixTimestamp assert isinstance(ts, int) and ts == pool.purchaseInfo.unixTimestamp # bad version bCopy = encode.ByteArray(b, copy=True) bCopy[0] = 255 with pytest.raises(NotImplementedError): vsp.VotingServiceProvider.unblob(bCopy.bytes()) # too long bCopy = encode.ByteArray(b, copy=True) bCopy += b"\x00" with pytest.raises(DecredError): vsp.VotingServiceProvider.unblob(bCopy.bytes())
def test_vsp_headers(): pool = vsp.VotingServiceProvider(**votingServiceProvider) headers = pool.headers() assert headers == { "Authorization": "Bearer " + votingServiceProvider["apiKey"] }
def test_vsp_api_path(): pool = vsp.VotingServiceProvider(**votingServiceProvider) path = pool.apiPath("stakeinfo") assert path == "https://www.dcrstakedinner.com/api/v2/stakeinfo"
def test_vsp_serialize(): pool = vsp.VotingServiceProvider(**votingServiceProvider) b = vsp.VotingServiceProvider.blob(pool) assert pool.serialize() == encode.ByteArray(b)
def test_vsp_init(): pool = vsp.VotingServiceProvider(**votingServiceProvider) assertVspIsEqual(pool) ts = pool.purchaseInfo.unixTimestamp assert isinstance(ts, int) and ts >= now