mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-11-06 08:54:57 +05:30
test(privatebin): add unit tests for create and retrieve paste functionality
This commit is contained in:
parent
0ce4be47df
commit
ee122d32fe
1 changed files with 80 additions and 6 deletions
|
|
@ -1,6 +1,80 @@
|
||||||
import { expect, describe, it } from 'vitest';
|
import { expect, describe, it, vi, beforeEach } from 'vitest';
|
||||||
// import { main } from './service';
|
import { createPaste, retrievePaste } from './service';
|
||||||
//
|
import { InitialValuesType } from './types';
|
||||||
// describe('privatebin', () => {
|
|
||||||
//
|
globalThis.crypto = require('crypto').webcrypto;
|
||||||
// })
|
|
||||||
|
describe('PrivateBin Service', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
localStorage.clear();
|
||||||
|
});
|
||||||
|
|
||||||
|
const defaultOptions: InitialValuesType = {
|
||||||
|
expiration: '1day',
|
||||||
|
burnAfterReading: false,
|
||||||
|
password: 'testpassword'
|
||||||
|
};
|
||||||
|
|
||||||
|
it('should create and retrieve a paste', async () => {
|
||||||
|
const content = 'Hello, PrivateBin!';
|
||||||
|
const pasteId = await createPaste(content, defaultOptions);
|
||||||
|
expect(typeof pasteId).toBe('string');
|
||||||
|
|
||||||
|
const retrieved = await retrievePaste(pasteId, defaultOptions.password);
|
||||||
|
expect(retrieved).toBe(content);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not retrieve with wrong password', async () => {
|
||||||
|
const content = 'Secret Message';
|
||||||
|
const pasteId = await createPaste(content, defaultOptions);
|
||||||
|
await expect(retrievePaste(pasteId, 'wrongpassword')).rejects.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should expire a paste after the set time', async () => {
|
||||||
|
const content = 'Expiring Message';
|
||||||
|
defaultOptions.expiration = '1hour';
|
||||||
|
const options = { ...defaultOptions };
|
||||||
|
const pasteId = await createPaste(content, options);
|
||||||
|
// Simulate expiration by manipulating createdAt
|
||||||
|
const pastes = JSON.parse(
|
||||||
|
localStorage.getItem('privatebin_pastes') || '{}'
|
||||||
|
);
|
||||||
|
pastes[pasteId].createdAt = Date.now() - 2 * 60 * 60 * 1000; // 2 hours ago
|
||||||
|
localStorage.setItem('privatebin_pastes', JSON.stringify(pastes));
|
||||||
|
await expect(retrievePaste(pasteId, options.password)).rejects.toThrow(
|
||||||
|
'Paste has expired'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should burn after reading if option is set', async () => {
|
||||||
|
const content = 'Burn after reading';
|
||||||
|
const options = { ...defaultOptions, burnAfterReading: true };
|
||||||
|
const pasteId = await createPaste(content, options);
|
||||||
|
const retrieved = await retrievePaste(pasteId, options.password);
|
||||||
|
expect(retrieved).toBe(content);
|
||||||
|
// Should be deleted after reading
|
||||||
|
await expect(retrievePaste(pasteId, options.password)).rejects.toThrow(
|
||||||
|
'Paste not found'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should store and retrieve multiple pastes independently', async () => {
|
||||||
|
const content1 = 'First';
|
||||||
|
const content2 = 'Second';
|
||||||
|
const id1 = await createPaste(content1, defaultOptions);
|
||||||
|
const id2 = await createPaste(content2, {
|
||||||
|
...defaultOptions,
|
||||||
|
password: 'another'
|
||||||
|
});
|
||||||
|
const r1 = await retrievePaste(id1, defaultOptions.password);
|
||||||
|
const r2 = await retrievePaste(id2, 'another');
|
||||||
|
expect(r1).toBe(content1);
|
||||||
|
expect(r2).toBe(content2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw if paste not found', async () => {
|
||||||
|
await expect(retrievePaste('nonexistent', 'any')).rejects.toThrow(
|
||||||
|
'Paste not found'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue