auth_framework.spec.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import * as StellarSdk from '@stellar/stellar-sdk';
  2. import { readFileSync } from 'fs';
  3. import { expect } from 'chai';
  4. import path from 'path';
  5. import { fileURLToPath } from 'url';
  6. import { call_contract_function, extractLogEvent } from './test_helpers.js';
  7. import { assert } from 'console';
  8. const __filename = fileURLToPath(import.meta.url);
  9. const dirname = path.dirname(__filename);
  10. const server = new StellarSdk.SorobanRpc.Server("https://soroban-testnet.stellar.org:443");
  11. function readContractAddress(filename) {
  12. return readFileSync(path.join(dirname, '.soroban', 'contract-ids', filename), 'utf8').trim();
  13. }
  14. describe('Auth Framework', () => {
  15. let keypair, a, b, c, a_invalid;
  16. before(async () => {
  17. console.log('Setting up cross contract tests...');
  18. keypair = StellarSdk.Keypair.fromSecret(readFileSync('alice.txt', 'utf8').trim());
  19. a = new StellarSdk.Contract(readContractAddress('a.txt'));
  20. b = new StellarSdk.Contract(readContractAddress('b.txt'));
  21. c = new StellarSdk.Contract(readContractAddress('c.txt'));
  22. a_invalid = new StellarSdk.Contract(readContractAddress('a_invalid.txt'));
  23. });
  24. it('calls a', async () => {
  25. let values = [
  26. b.address().toScVal(),
  27. c.address().toScVal()
  28. ];
  29. let res = await call_contract_function("call_b", server, keypair, a, ...values);
  30. expect(res.returnValue().value().toString()).to.equal("22");
  31. });
  32. it ('call falis with invalid `a` contract', async () => {
  33. let values = [
  34. b.address().toScVal(),
  35. c.address().toScVal()
  36. ];
  37. let res = await call_contract_function("call_b", server, keypair, a_invalid, ...values);
  38. assert(res.toString().includes("recording authorization only] encountered authorization not tied to the root contract invocation for an address. Use `require_auth()` in the top invocation or enable non-root authorization."));
  39. });
  40. });