Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions basics/checking-accounts/anchor/tests/litesvm.test.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>, 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', () => {
Expand Down Expand Up @@ -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',
);
});
});
28 changes: 28 additions & 0 deletions basics/checking-accounts/anchor/tests/test.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>, 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);
Expand Down Expand Up @@ -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',
);
});
});
Loading