diff --git a/basics/checking-accounts/anchor/tests/litesvm.test.ts b/basics/checking-accounts/anchor/tests/litesvm.test.ts index 1b7859c22..e39b9d68e 100644 --- a/basics/checking-accounts/anchor/tests/litesvm.test.ts +++ b/basics/checking-accounts/anchor/tests/litesvm.test.ts @@ -1,10 +1,22 @@ import * as anchor from '@anchor-lang/core'; import { Keypair, PublicKey, SystemProgram, Transaction } from '@solana/web3.js'; +import { assert } from 'chai'; import { LiteSVMProvider } from 'anchor-litesvm'; import { LiteSVM } from 'litesvm'; import IDL from '../target/idl/checking_account_program.json' with { type: 'json' }; import type { CheckingAccountProgram } from '../target/types/checking_account_program.ts'; +const expectAnchorError = async (promise: Promise, code: string) => { + let caught: any; + try { + await promise; + } catch (error) { + caught = error; + } + assert.isDefined(caught, `expected the transaction to fail with ${code}`); + assert.strictEqual(caught?.error?.errorCode?.code, code, `expected ${code}, got: ${caught}`); +}; + const PROGRAM_ID = new PublicKey(IDL.address); describe('LiteSVM example', () => { @@ -48,4 +60,20 @@ describe('LiteSVM example', () => { }) .rpc(); }); + + it('Rejects an account our program does not own', async () => { + // accountToCreate was never created, so the runtime presents it as + // owned by the System Program — exactly what `owner = id()` must refuse. + await expectAnchorError( + program.methods + .checkAccounts() + .accounts({ + payer: wallet.publicKey, + accountToCreate: accountToCreate.publicKey, + accountToChange: accountToCreate.publicKey, + }) + .rpc(), + 'ConstraintOwner', + ); + }); }); diff --git a/basics/checking-accounts/anchor/tests/test.ts b/basics/checking-accounts/anchor/tests/test.ts index a0ab321cf..f31da3d7a 100644 --- a/basics/checking-accounts/anchor/tests/test.ts +++ b/basics/checking-accounts/anchor/tests/test.ts @@ -1,7 +1,19 @@ import * as anchor from '@anchor-lang/core'; import { Keypair, SystemProgram, sendAndConfirmTransaction, Transaction } from '@solana/web3.js'; +import { assert } from 'chai'; import type { CheckingAccountProgram } from '../target/types/checking_account_program.ts'; +const expectAnchorError = async (promise: Promise, code: string) => { + let caught: any; + try { + await promise; + } catch (error) { + caught = error; + } + assert.isDefined(caught, `expected the transaction to fail with ${code}`); + assert.strictEqual(caught?.error?.errorCode?.code, code, `expected ${code}, got: ${caught}`); +}; + describe('Anchor example', () => { const provider = anchor.AnchorProvider.env(); anchor.setProvider(provider); @@ -38,4 +50,20 @@ describe('Anchor example', () => { }) .rpc(); }); + + it('Rejects an account our program does not own', async () => { + // accountToCreate was never created, so the runtime presents it as + // owned by the System Program — exactly what `owner = id()` must refuse. + await expectAnchorError( + program.methods + .checkAccounts() + .accounts({ + payer: wallet.publicKey, + accountToCreate: accountToCreate.publicKey, + accountToChange: accountToCreate.publicKey, + }) + .rpc(), + 'ConstraintOwner', + ); + }); });