def test_post_success_account_doesnt_exist(mock_load_account, client): kp = Keypair.random() challenge_xdr = build_challenge_transaction( server_secret=settings.SIGNING_SEED, client_account_id=kp.public_key, home_domain=settings.SEP10_HOME_DOMAINS[0], web_auth_domain=urlparse(settings.HOST_URL).netloc, network_passphrase=settings.STELLAR_NETWORK_PASSPHRASE, ) envelope = TransactionEnvelope.from_xdr( challenge_xdr, network_passphrase=settings.STELLAR_NETWORK_PASSPHRASE) envelope.sign(kp) signed_challenge_xdr = envelope.to_xdr() mock_load_account.side_effect = NotFoundError(MagicMock()) response = client.post(AUTH_PATH, {"transaction": signed_challenge_xdr}) content = response.json() assert response.status_code == 200, json.dumps(content, indent=2) jwt_contents = jwt.decode(content["token"], settings.SERVER_JWT_KEY, algorithms=["HS256"]) iat = jwt_contents.pop("iat") exp = jwt_contents.pop("exp") assert exp - iat == 24 * 60 * 60 assert jwt_contents == { "iss": os.path.join(settings.HOST_URL, "auth"), "sub": kp.public_key, "jti": envelope.hash().hex(), "client_domain": None, }
def test_post_fails_account_doesnt_exist_no_client_attribution_signature( mock_load_account, client): kp = Keypair.random() client_domain_kp = Keypair.random() challenge_xdr = build_challenge_transaction( server_secret=settings.SIGNING_SEED, client_account_id=kp.public_key, home_domain=settings.SEP10_HOME_DOMAINS[0], web_auth_domain=urlparse(settings.HOST_URL).netloc, network_passphrase=settings.STELLAR_NETWORK_PASSPHRASE, client_domain="test.com", client_signing_key=client_domain_kp.public_key, ) envelope = TransactionEnvelope.from_xdr( challenge_xdr, network_passphrase=settings.STELLAR_NETWORK_PASSPHRASE) envelope.sign(kp) signed_challenge_xdr = envelope.to_xdr() mock_load_account.side_effect = NotFoundError(MagicMock()) response = client.post(AUTH_PATH, {"transaction": signed_challenge_xdr}) content = response.json() assert response.status_code == 400, json.dumps(content, indent=2) assert ( content["error"] == "error while validating challenge: " "Transaction not signed by the source account of the 'client_domain' ManageData operation" )
def mock_load_account_no_account(account_id): if isinstance(account_id, Keypair): account_id = account_id.public_key if account_id not in [ Keypair.from_secret(v).public_key for v in [USD_DISTRIBUTION_SEED, ETH_DISTRIBUTION_SEED] ]: raise NotFoundError(response=Response(status_code=404, headers={}, url="", text=json.dumps(dict( status=404)))) account = Account(account_id, 1) account.signers = [] account.thresholds = Thresholds(0, 0, 0) return ( account, [{ "balances": [ { "asset_code": "USD", "asset_issuer": USD_ISSUER_ACCOUNT }, { "asset_code": "ETH", "asset_issuer": ETH_ISSUER_ACCOUNT }, ] }], )
def mock_load_not_exist_account(account_id): if account_id != settings.STELLAR_ISSUER_ACCOUNT_ADDRESS and account_id != settings.STELLAR_DISTRIBUTION_ACCOUNT_ADDRESS: raise NotFoundError(response=Response(status_code=404, headers={}, url="", text=json.dumps(dict( status=404)))) return Account(account_id, 1)
def test_get_account_obj_not_found(mock_load_account, mock_accounts_endpoint): mock_accounts_endpoint.return_value.account_id.return_value.call.side_effect = NotFoundError( Mock()) kp = Keypair.random() with pytest.raises(RuntimeError, match=f"account {kp.public_key} does not exist"): utils.get_account_obj(kp) mock_load_account.assert_not_called()
def mock_load_account_no_account(account_id): if account_id not in [ Keypair.from_secret(v).public_key for v in [USD_DISTRIBUTION_SEED, ETH_DISTRIBUTION_SEED] ]: raise NotFoundError(response=Response(status_code=404, headers={}, url="", text=json.dumps(dict( status=404)))) return Account(account_id, 1)
def mock_load_not_exist_account(account_id): accounts = [] for vals in settings.ASSETS.values(): accounts.extend(vals.values()) if account_id not in accounts: raise NotFoundError( response=Response( status_code=404, headers={}, url="", text=json.dumps(dict(status=404)) ) ) return Account(account_id, 1)
def raise_not_found(*_): response = Response(9, '', {}, '') raise NotFoundError(response)
data={"transaction": envelope.to_xdr()}, content_type="application/json", ) content = json.loads(response.content) assert content["token"] mock_request.META["HTTP_AUTHORIZATION"] = auth_str.format(content["token"]) mock_view_function = Mock() check_auth(mock_request, mock_view_function) mock_view_function.assert_called_once_with(CLIENT_ADDRESS, None, mock_request) @patch( "polaris.sep10.views.settings.HORIZON_SERVER.load_account", Mock(side_effect=NotFoundError(Mock())), ) def test_auth_post_success_account_does_not_exist(client): """`POST <auth>` succeeds when given a proper JSON-encoded transaction.""" response = client.get(f"{endpoint}?account={CLIENT_ADDRESS}", follow=True) content = json.loads(response.content) # Sign the XDR with the client. envelope_xdr = content["transaction"] envelope = TransactionEnvelope.from_xdr( envelope_xdr, network_passphrase=settings.STELLAR_NETWORK_PASSPHRASE) envelope.sign(CLIENT_SEED) response = client.post( endpoint, data={"transaction": envelope.to_xdr()}, content_type="application/json",