Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: option to ban user #1238

Merged
merged 18 commits into from
Feb 12, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: more tests
  • Loading branch information
peterpeterparker committed Feb 11, 2025
commit 6cb42fe45670b6d7ab5fb2ee9875d33f116931ad
151 changes: 147 additions & 4 deletions src/tests/specs/satellite.user-ban.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { Doc, _SERVICE as SatelliteActor } from '$declarations/satellite/satellite.did';
import type {
DelDoc,
Doc,
_SERVICE as SatelliteActor,
SetDoc
} from '$declarations/satellite/satellite.did';
import { idlFactory as idlFactorSatellite } from '$declarations/satellite/satellite.factory.did';
import { type Identity } from '@dfinity/agent';
import { Ed25519KeyIdentity } from '@dfinity/identity';
Expand All @@ -7,9 +12,9 @@ import { type Actor, PocketIc } from '@hadronous/pic';
import { toArray } from '@junobuild/utils';
import { nanoid } from 'nanoid';
import { beforeAll, describe, expect, inject } from 'vitest';
import { USER_NOT_ALLOWED } from './constants/satellite-tests.constants';
import { mockSetRule } from './mocks/collection.mocks';
import { controllersInitArgs, SATELLITE_WASM_PATH } from './utils/setup-tests.utils';
import {USER_NOT_ALLOWED} from "./constants/satellite-tests.constants";

describe('Satellite User Usage', () => {
let pic: PocketIc;
Expand Down Expand Up @@ -170,6 +175,56 @@ describe('Satellite User Usage', () => {
});
});

describe('get many documents', () => {
let user: Identity;
let docKey: string;

beforeEach(async () => {
user = Ed25519KeyIdentity.generate();

await createUser(user);

const { set_doc } = actor;

docKey = nanoid();

await set_doc(collection, docKey, {
data: await toArray({
hello: 'world'
}),
description: toNullable(),
version: toNullable()
});
});

it('should not get documents if banned', async () => {
await banUser({ user, version: [1n] });

actor.setIdentity(user);
const { get_many_docs } = actor;

const result = await get_many_docs([[collection, docKey]]);

const doc = fromNullable(result[0][1]);

expect(doc).toBeUndefined();
});

it('should get documents if unbanned', async () => {
await banUser({ user, version: [1n] });
await unbanUser({ user, version: [2n] });

actor.setIdentity(user);
const { get_many_docs } = actor;

const result = await get_many_docs([[collection, docKey]]);

const doc = fromNullable(result[0][1]);

expect(doc).not.toBeUndefined();
});
});

describe('set document', () => {
let user: Identity;

Expand Down Expand Up @@ -215,6 +270,52 @@ describe('Satellite User Usage', () => {
});
});

describe('set many documents', () => {
let user: Identity;

const createDocs = async (): Promise<void> => {
actor.setIdentity(user);

const { set_many_docs } = actor;

const data: SetDoc = {
data: await toArray({
hello: 'world'
}),
description: toNullable(),
version: toNullable()
};

await set_many_docs([
[collection, nanoid(), data],
[collection, nanoid(), data]
]);
};

beforeEach(async () => {
user = Ed25519KeyIdentity.generate();

await createUser(user);
});

it('should not set documents if banned', async () => {
await expect(createDocs()).resolves.not.toThrowError();

await banUser({ user, version: [1n] });

await expect(createDocs()).rejects.toThrow(USER_NOT_ALLOWED);
});

it('should set documents if unbanned', async () => {
await expect(createDocs()).resolves.not.toThrowError();

await banUser({ user, version: [1n] });
await unbanUser({ user, version: [2n] });

await expect(createDocs()).resolves.not.toThrowError();
});
});

describe('delete a document', () => {
let user: Identity;

Expand All @@ -235,21 +336,63 @@ describe('Satellite User Usage', () => {
});

it('should not delete document if banned', async () => {
await deleteDoc();
await expect(deleteDoc()).resolves.not.toThrowError();

await banUser({ user, version: [1n] });

await expect(deleteDoc()).rejects.toThrow(USER_NOT_ALLOWED);
});

it('should get document if unbanned', async () => {
await deleteDoc();
await expect(deleteDoc()).resolves.not.toThrowError();

await banUser({ user, version: [1n] });
await unbanUser({ user, version: [2n] });

await expect(deleteDoc()).resolves.not.toThrowError();
});
});

describe('delete many documents', () => {
let user: Identity;

const deleteDocs = async (): Promise<void> => {
actor.setIdentity(user);

const { del_many_docs } = actor;

const data: DelDoc = {
version: toNullable()
};

await del_many_docs([
[collection, nanoid(), data],
[collection, nanoid(), data]
]);
};

beforeEach(async () => {
user = Ed25519KeyIdentity.generate();

await createUser(user);
});

it('should not delete documents if banned', async () => {
await expect(deleteDocs()).resolves.not.toThrowError();

await banUser({ user, version: [1n] });

await expect(deleteDocs()).rejects.toThrow(USER_NOT_ALLOWED);
});

it('should get documents if unbanned', async () => {
await expect(deleteDocs()).resolves.not.toThrowError();

await banUser({ user, version: [1n] });
await unbanUser({ user, version: [2n] });

await expect(deleteDocs()).resolves.not.toThrowError();
});
});
});
});
Loading