mirror of
https://github.com/kind-0/nsecbunkerd.git
synced 2026-05-03 07:00:11 +00:00
feat: Persist created key to database in create_new_key
- Use Prisma to upsert key details upon creation. - Add database mocking for related unit tests. - Ensure `create_new_key` saves keyName and pubkey for future operations.
This commit is contained in:
parent
7ed4835bda
commit
dc86897db4
@ -1,5 +1,5 @@
|
|||||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||||
import { createMockAdmin, createMockRequest, getResponseResult, resetMocks } from './test-utils';
|
import { createMockAdmin, createMockRequest, getResponseResult, resetMocks, mockPrisma } from './test-utils';
|
||||||
|
|
||||||
// Mock the saveEncrypted function
|
// Mock the saveEncrypted function
|
||||||
vi.mock('../../../../commands/add.js', () => ({
|
vi.mock('../../../../commands/add.js', () => ({
|
||||||
@ -11,6 +11,11 @@ vi.mock('../../../lib/profile.js', () => ({
|
|||||||
setupSkeletonProfile: vi.fn(),
|
setupSkeletonProfile: vi.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Mock the prisma module
|
||||||
|
vi.mock('../../../../db.js', () => ({
|
||||||
|
default: mockPrisma,
|
||||||
|
}));
|
||||||
|
|
||||||
import createNewKey from '../create_new_key';
|
import createNewKey from '../create_new_key';
|
||||||
|
|
||||||
describe('create_new_key', () => {
|
describe('create_new_key', () => {
|
||||||
@ -51,6 +56,19 @@ describe('create_new_key', () => {
|
|||||||
expect.stringMatching(/^nsec1/)
|
expect.stringMatching(/^nsec1/)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Verify key was saved to database
|
||||||
|
expect(mockPrisma.key.upsert).toHaveBeenCalledWith({
|
||||||
|
where: { keyName: 'my-key' },
|
||||||
|
update: {
|
||||||
|
pubkey: expect.any(String),
|
||||||
|
deletedAt: null,
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
keyName: 'my-key',
|
||||||
|
pubkey: expect.any(String),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
// Verify response contains npub
|
// Verify response contains npub
|
||||||
expect(admin.rpc.sendResponse).toHaveBeenCalledTimes(1);
|
expect(admin.rpc.sendResponse).toHaveBeenCalledTimes(1);
|
||||||
const result = getResponseResult(admin);
|
const result = getResponseResult(admin);
|
||||||
|
|||||||
@ -9,6 +9,7 @@ export const mockPrisma = {
|
|||||||
findMany: vi.fn(),
|
findMany: vi.fn(),
|
||||||
create: vi.fn(),
|
create: vi.fn(),
|
||||||
update: vi.fn(),
|
update: vi.fn(),
|
||||||
|
upsert: vi.fn(),
|
||||||
delete: vi.fn(),
|
delete: vi.fn(),
|
||||||
},
|
},
|
||||||
keyUser: {
|
keyUser: {
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import { saveEncrypted } from "../../../commands/add.js";
|
|||||||
import { nip19 } from 'nostr-tools';
|
import { nip19 } from 'nostr-tools';
|
||||||
import { setupSkeletonProfile } from "../../lib/profile.js";
|
import { setupSkeletonProfile } from "../../lib/profile.js";
|
||||||
import { bytesToHex, hexToBytes } from "../../../utils/hex.js";
|
import { bytesToHex, hexToBytes } from "../../../utils/hex.js";
|
||||||
|
import prisma from "../../../db.js";
|
||||||
|
|
||||||
export default async function createNewKey(admin: AdminInterface, req: NDKRpcRequest) {
|
export default async function createNewKey(admin: AdminInterface, req: NDKRpcRequest) {
|
||||||
const [ keyName, passphrase, _nsec ] = req.params as [ string, string, string? ];
|
const [ keyName, passphrase, _nsec ] = req.params as [ string, string, string? ];
|
||||||
@ -35,6 +36,19 @@ export default async function createNewKey(admin: AdminInterface, req: NDKRpcReq
|
|||||||
|
|
||||||
await admin.loadNsec(keyName, nsec);
|
await admin.loadNsec(keyName, nsec);
|
||||||
|
|
||||||
|
// Also save to database so delete_key can find it
|
||||||
|
await prisma.key.upsert({
|
||||||
|
where: { keyName },
|
||||||
|
update: {
|
||||||
|
pubkey: user.pubkey,
|
||||||
|
deletedAt: null, // Ensure it's not marked as deleted
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
keyName,
|
||||||
|
pubkey: user.pubkey,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const result = JSON.stringify({
|
const result = JSON.stringify({
|
||||||
npub: user.npub,
|
npub: user.npub,
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user