SC Test demo example
Table of contents
- Create a Sol Cerberus instance
- Create Sol Cerberus app
- Assign role to a wallet
- Grant some permission to the role
- Fetch the updated permissions
- Login using the allowed wallet
- Verify access
The easiest to understand testing is by looking at the tests examples of the Sol Cerberus Demo program.
Create a Sol Cerberus instance
You can use either a random generated SC app ID or a fixed public key.
solCerberus = new SolCerberus(PROVIDER.connection, PROVIDER.wallet, {
appId: new PublicKey("REPLACE_BY_YOUR_SC_APP_ID"),
});
Create Sol Cerberus app
Creates a SC app in the block-chain for your Anchor program, using the app ID defined on the previous step.
await solCerberus.initializeApp("SolCerberusRBAC", null, {
cached: false,
});
Assign role to a wallet
await solCerberus.assignRole(
"MyRole",
addressTypes.Wallet,
new PublicKey("HERE_THE_ADDRESS_OF_THE_ALLOWED_WALLET")
);
Grant some permission to the role
await solCerberus.addRule("MyRole", "Square", "Add");
Fetch the updated permissions
await solCerberus.fetchPerms();
Login using the allowed wallet
await solCerberus.login({ wallet: new PublicKey("HERE_THE_ADDRESS_OF_THE_ALLOWED_WALLET") });
Verify access
Check an instruction of your program using the allowed wallet to verify access
await DEMO_PROGRAM.methods
.addSquare("ff0000", 50)
.accounts({
demo: demoPda,
signer: new PublicKey("HERE_THE_ADDRESS_OF_THE_ALLOWED_WALLET"),
...(await solCerberus.accounts("Square", "Add")),
})
.signers([ALLOWED_WALLET_KEY_PAIR])
.rpc();