Skip to content
This repository was archived by the owner on Sep 7, 2023. It is now read-only.

adding testcases #1

Merged
merged 1 commit into from
Jan 19, 2023
Merged
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
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
"@project-serum/anchor": "^0.25.0"
},
"devDependencies": {
"chai": "^4.3.4",
"mocha": "^9.0.3",
"ts-mocha": "^10.0.0",
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.0",
"@types/mocha": "^9.0.0",
"typescript": "^4.3.5",
"prettier": "^2.6.2"
"@types/randombytes": "^2.0.0",
"chai": "^4.3.4",
"mocha": "^9.0.3",
"prettier": "^2.6.2",
"ts-mocha": "^10.0.0",
"typescript": "^4.3.5"
}
}
87 changes: 87 additions & 0 deletions tests/connection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as anchor from "@project-serum/anchor";
import NodeWallet from "@project-serum/anchor/dist/cjs/nodewallet";
import randombytes from "randombytes";
import { airdrop } from "./utils";
import { expect } from "chai";
import { sendAndConfirmTransaction } from "@solana/web3.js";
import { GplCore } from "../target/types/gpl_core";

const program = anchor.workspace.GplCore as anchor.Program<GplCore>;

anchor.setProvider(anchor.AnchorProvider.env());

describe("Connection", async () => {
let rpcConnection: anchor.web3.Connection;
let testUser: anchor.web3.Keypair;
let testUserWallet: NodeWallet;
let userPDA: anchor.web3.PublicKey;
let testUserPDA: anchor.web3.PublicKey;
let profilePDA: anchor.web3.PublicKey;
let testProfilePDA: anchor.web3.PublicKey;
let connectionPDA: anchor.web3.PublicKey;

before(async () => {
rpcConnection = new anchor.web3.Connection("http://localhost:8899", "confirmed");
// Create a user
const randomHash = randombytes(32);
const userTx = program.methods.createUser(randomHash);
const userPubKeys = await userTx.pubkeys();
userPDA = userPubKeys.user as anchor.web3.PublicKey;
await userTx.rpc();

// Create a profile
const profileTx = program.methods.createProfile("Personal").accounts({ user: userPDA });
const profilePubKeys = await profileTx.pubkeys();
profilePDA = profilePubKeys.profile as anchor.web3.PublicKey;
await profileTx.rpc();

// Create a testUser
testUser = anchor.web3.Keypair.generate();
testUserWallet = new NodeWallet(testUser);
await airdrop(testUser.publicKey);

const randomTestHash = randombytes(32);
const createTestUser = program.methods.createUser(randomTestHash).accounts({ authority: testUser.publicKey });
const testUserPubKeys = await createTestUser.pubkeys();
testUserPDA = testUserPubKeys.user as anchor.web3.PublicKey;
const testUserTx = await createTestUser.transaction();
testUserTx.recentBlockhash = (await rpcConnection.getLatestBlockhash()).blockhash;
testUserTx.feePayer = testUser.publicKey;
const signedTestUserTransaction = await testUserWallet.signTransaction(testUserTx);
await sendAndConfirmTransaction(rpcConnection, signedTestUserTransaction, [testUser]);

// Create a testProfile
const testProfile = program.methods.createProfile("Personal").accounts({ user: testUserPDA, authority: testUser.publicKey });
const testProfilePubKeys = await testProfile.pubkeys();
testProfilePDA = testProfilePubKeys.profile as anchor.web3.PublicKey;
const testProfileTx = await testProfile.transaction();
testProfileTx.recentBlockhash = (await rpcConnection.getLatestBlockhash()).blockhash;
testProfileTx.feePayer = testUser.publicKey;
const signedTransaction = await testUserWallet.signTransaction(testProfileTx);
await sendAndConfirmTransaction(rpcConnection, signedTransaction, [testUser]);
});

it("should create a connection", async () => {
const connection = program.methods.createConnection().accounts({ fromProfile: profilePDA, toProfile: testProfilePDA, user: userPDA });
const pubKeys = await connection.pubkeys();
connectionPDA = pubKeys.connection as anchor.web3.PublicKey;
await connection.rpc();

const connectionAccount = await program.account.connection.fetch(connectionPDA);
expect(connectionAccount.fromProfile.toBase58()).to.equal(profilePDA.toBase58());
expect(connectionAccount.toProfile.toBase58()).to.equal(testProfilePDA.toBase58());
});

it("should delete a connection", async () => {
const connection = program.methods.deleteConnection().accounts({ fromProfile: profilePDA, toProfile: testProfilePDA, connection: connectionPDA, user: userPDA });
await connection.rpc();

try {
await program.account.connection.fetch(connectionPDA);
}
catch (error: any) {
expect(error).to.be.an("error");
expect(error.toString()).to.contain(`Account does not exist ${connectionPDA.toString()}`);
}
});
});
13 changes: 0 additions & 13 deletions tests/gpl.ts

This file was deleted.

61 changes: 61 additions & 0 deletions tests/post.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as anchor from "@project-serum/anchor";
import randombytes from "randombytes";
import { expect } from "chai";
import { GplCore } from "../target/types/gpl_core";

const program = anchor.workspace.GplCore as anchor.Program<GplCore>;

anchor.setProvider(anchor.AnchorProvider.env());

describe("Post", async () => {
let userPDA: anchor.web3.PublicKey;
let profilePDA: anchor.web3.PublicKey;
let postPDA: anchor.web3.PublicKey;

before(async () => {
// Create a user
const randomHash = randombytes(32);
const userTx = program.methods.createUser(randomHash);
const userPubKeys = await userTx.pubkeys();
userPDA = userPubKeys.user as anchor.web3.PublicKey;
await userTx.rpc();

// Create a profile
const profileTx = program.methods.createProfile("Personal").accounts({ user: userPDA });
const profilePubKeys = await profileTx.pubkeys();
profilePDA = profilePubKeys.profile as anchor.web3.PublicKey;
await profileTx.rpc();
});

it("should create a post", async () => {
const randomHash = randombytes(32);
const metadataUri = "This is a test post";
const post = program.methods.createPost(metadataUri, randomHash).accounts({ user: userPDA, profile: profilePDA });
const postPubKeys = await post.pubkeys();
postPDA = postPubKeys.post as anchor.web3.PublicKey;
await post.rpc();
const postAccount = await program.account.post.fetch(postPDA);
expect(postAccount.metadataUri).is.equal(metadataUri);
expect(postAccount.profile.toString()).is.equal(profilePDA.toString());
});

it("should update a post", async () => {
const metadataUri = "This is an updated test post";
const post = program.methods.updatePost(metadataUri).accounts({ user: userPDA, profile: profilePDA, post: postPDA });
await post.rpc();
const postAccount = await program.account.post.fetch(postPDA);
expect(postAccount.metadataUri).is.equal(metadataUri);
expect(postAccount.profile.toString()).is.equal(profilePDA.toString());
});

it("should delete a post", async () => {
const post = program.methods.deletePost().accounts({ user: userPDA, profile: profilePDA, post: postPDA });
await post.rpc();
try {
await program.account.post.fetch(postPDA);
} catch (error: any) {
expect(error).to.be.an("error");
expect(error.toString()).to.contain(`Account does not exist ${postPDA.toString()}`);
}
});
});
43 changes: 43 additions & 0 deletions tests/profile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as anchor from "@project-serum/anchor";
import randombytes from "randombytes";
import { expect } from "chai";
import { GplCore } from "../target/types/gpl_core";

const program = anchor.workspace.GplCore as anchor.Program<GplCore>;

anchor.setProvider(anchor.AnchorProvider.env());

describe("Profile", async () => {
let userPDA: anchor.web3.PublicKey;
let profilePDA: anchor.web3.PublicKey;

before(async () => {
// Create a user
const randomHash = randombytes(32);
const tx = program.methods.createUser(randomHash);
const pubKeys = await tx.pubkeys();
userPDA = pubKeys.user as anchor.web3.PublicKey;
await tx.rpc();
});

it("should create a profile", async () => {
const tx = program.methods.createProfile("Personal").accounts({ user: userPDA });
const pubKeys = await tx.pubkeys();
profilePDA = pubKeys.profile as anchor.web3.PublicKey;
await tx.rpc();
const profileAccount = await program.account.profile.fetch(profilePDA);
expect(profileAccount.user.toString()).is.equal(userPDA.toString());
expect(profileAccount.namespace.toString()).is.equal({ personal: {} }.toString());
});

it("should delete a profile", async () => {
const tx = program.methods.deleteProfile().accounts({ user: userPDA, profile: profilePDA });
await tx.rpc();
try {
await program.account.profile.fetch(profilePDA);
} catch (error: any) {
expect(error).to.be.an("error");
expect(error.toString()).to.contain(`Account does not exist ${profilePDA.toString()}`);
}
});
});
62 changes: 62 additions & 0 deletions tests/reaction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as anchor from "@project-serum/anchor";
import randombytes from "randombytes";
import { expect } from "chai";
import { GplCore } from "../target/types/gpl_core";

const program = anchor.workspace.GplCore as anchor.Program<GplCore>;

anchor.setProvider(anchor.AnchorProvider.env());

describe("Reaction", async () => {
let userPDA: anchor.web3.PublicKey;
let profilePDA: anchor.web3.PublicKey;
let postPDA: anchor.web3.PublicKey;
let reactionPDA: anchor.web3.PublicKey;

before(async () => {
// Create a user
const randomHash = randombytes(32);
const userTx = program.methods.createUser(randomHash);
const userPubKeys = await userTx.pubkeys();
userPDA = userPubKeys.user as anchor.web3.PublicKey;
await userTx.rpc();

// Create a profile
const profileTx = program.methods.createProfile("Personal").accounts({ user: userPDA });
const profilePubKeys = await profileTx.pubkeys();
profilePDA = profilePubKeys.profile as anchor.web3.PublicKey;
await profileTx.rpc();

// Create a post
const postRandomHash = randombytes(32);
const metadataUri = "This is a test post";
const post = program.methods.createPost(metadataUri, postRandomHash).accounts({ user: userPDA, profile: profilePDA });
const postPubKeys = await post.pubkeys();
postPDA = postPubKeys.post as anchor.web3.PublicKey;
await post.rpc();
});

it("should create a reaction", async () => {
const reaction = program.methods.createReaction("Haha").accounts({ toPost: postPDA, fromProfile: profilePDA, user: userPDA });
const reactionPubKeys = await reaction.pubkeys();
reactionPDA = reactionPubKeys.reaction as anchor.web3.PublicKey;
await reaction.rpc();

const reactionAccount = await program.account.reaction.fetch(reactionPDA);
expect(reactionAccount.toPost.toBase58()).to.equal(postPDA.toBase58());
expect(reactionAccount.fromProfile.toBase58()).to.equal(profilePDA.toBase58());
expect(reactionAccount.reactionType.toString()).to.equal({ haha: {} }.toString());
});

it("should delete a reaction", async () => {
const reaction = program.methods.deleteReaction().accounts({ toPost: postPDA, fromProfile: profilePDA, user: userPDA, reaction: reactionPDA });
await reaction.rpc();

try {
await program.account.reaction.fetch(reactionPDA);
} catch (error: any) {
expect(error).to.be.an("error");
expect(error.toString()).to.contain(`Account does not exist ${reactionPDA.toString()}`);
}
});
});
65 changes: 65 additions & 0 deletions tests/user.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as anchor from "@project-serum/anchor";
import NodeWallet from "@project-serum/anchor/dist/cjs/nodewallet";
import randombytes from "randombytes";
import { airdrop } from "./utils";
import { expect } from "chai";
import { sendAndConfirmTransaction } from "@solana/web3.js";
import { GplCore } from "../target/types/gpl_core";

const program = anchor.workspace.GplCore as anchor.Program<GplCore>;

anchor.setProvider(anchor.AnchorProvider.env());
const user = (anchor.getProvider() as any).wallet.payer;

describe("User", async () => {
let connection: anchor.web3.Connection;
let userPDA: anchor.web3.PublicKey;
let randomUser: anchor.web3.Keypair;

before(async () => {
randomUser = anchor.web3.Keypair.generate();
await airdrop(randomUser.publicKey);
connection = new anchor.web3.Connection("http://localhost:8899", "confirmed");
});

it("should create a user", async () => {
const randomHash = randombytes(32);
const tx = program.methods.createUser(randomHash);
const pubKeys = await tx.pubkeys();
userPDA = pubKeys.user as anchor.web3.PublicKey;
await tx.rpc();
const userAccount = await program.account.user.fetch(userPDA);
expect(userAccount.authority.toString()).is.equal(user.publicKey.toString());
});

it("should update a user", async () => {
const tx = program.methods.updateUser().accounts({ user: userPDA, newAuthority: randomUser.publicKey });
const pubKeys = await tx.pubkeys();
const randomUserPDA = pubKeys.user as anchor.web3.PublicKey;
await tx.rpc();
const userAccount = await program.account.user.fetch(randomUserPDA);
expect(userAccount.authority.toString()).is.equal(randomUser.publicKey.toString());
});

it("should delete a user", async () => {
const randomUserWallet = new NodeWallet(randomUser);
const tx = program.methods.deleteUser().accounts({ user: userPDA, authority: randomUser.publicKey });
const pubKeys = await tx.pubkeys();
const randomUserPDA = pubKeys.user as anchor.web3.PublicKey;
const transaction = await tx.transaction();
transaction.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
transaction.feePayer = randomUser.publicKey;
const signedTransaction = await randomUserWallet.signTransaction(transaction);
await sendAndConfirmTransaction(
connection,
signedTransaction,
[randomUser],
);
try {
await program.account.user.fetch(randomUserPDA);
} catch (error: any) {
expect(error).to.be.an("error");
expect(error.toString()).to.contain(`Account does not exist ${randomUserPDA.toString()}`);
}
});
});
9 changes: 9 additions & 0 deletions tests/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as anchor from '@project-serum/anchor';
import { PublicKey, LAMPORTS_PER_SOL } from '@solana/web3.js';

const provider = anchor.getProvider();

export async function airdrop(key: PublicKey) {
const airdropSig = await provider.connection.requestAirdrop(key, 1 * LAMPORTS_PER_SOL);
return provider.connection.confirmTransaction(airdropSig);
}
Loading