Skip to content

OpenStock is an open-source alternative to expensive market platforms. Track real-time prices, set personalized alerts, and explore detailed company insights — built openly, for everyone, forever free.

test: pin alert actions to the session user; drop unused toggleAlert

ravixalgorithm30dfcf2bb906
diff --git a/__tests__/alert-actions.test.ts b/__tests__/alert-actions.test.ts
new file mode 100644
index 0000000..0dbb50b
--- /dev/null
+++ b/__tests__/alert-actions.test.ts
@@ -0,0 +1,56 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+// Server actions are public endpoints: these tests pin that alerts are always scoped to the
+// signed-in user, never to an id sent by the client. Adapted from #93.
+
+const requireUserId = vi.fn();
+vi.mock('@/lib/better-auth/auth', () => ({ requireUserId: () => requireUserId() }));
+vi.mock('next/cache', () => ({ revalidatePath: vi.fn() }));
+vi.mock('@/database/mongoose', () => ({ connectToDatabase: vi.fn().mockResolvedValue({}) }));
+
+const findOneAndDelete = vi.fn();
+const create = vi.fn();
+vi.mock('@/database/models/alert.model', () => ({
+ Alert: {
+ findOneAndDelete: (...args: unknown[]) => findOneAndDelete(...args),
+ create: (...args: unknown[]) => create(...args),
+ },
+}));
+
+import { createAlert, deleteAlert } from '@/lib/actions/alert.actions';
+
+describe('alert actions are scoped to the session user', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ requireUserId.mockResolvedValue('user-123');
+ findOneAndDelete.mockResolvedValue({ _id: 'alert-1', userId: 'user-123' });
+ create.mockImplementation(async (doc: unknown) => doc);
+ });
+
+ it('deletes the user’s own alert', async () => {
+ await expect(deleteAlert('alert-1')).resolves.toEqual({ success: true });
+ expect(findOneAndDelete).toHaveBeenCalledWith({ _id: 'alert-1', userId: 'user-123' });
+ });
+
+ it('rejects signed-out calls before touching the database', async () => {
+ requireUserId.mockRejectedValue(new Error('Unauthorized'));
+ await expect(deleteAlert('alert-1')).rejects.toThrow('Unauthorized');
+ expect(findOneAndDelete).not.toHaveBeenCalled();
+ });
+
+ it('cannot delete another user’s alert: the query always carries the session user', async () => {
+ findOneAndDelete.mockResolvedValue(null); // someone else's alert matches nothing
+ await deleteAlert('alert-of-someone-else');
+ expect(findOneAndDelete).toHaveBeenCalledWith({ _id: 'alert-of-someone-else', userId: 'user-123' });
+ });
+
+ it('creates alerts for the session user, ignoring any userId sent by the client', async () => {
+ await createAlert({ symbol: 'AAPL', targetPrice: 200, condition: 'ABOVE', userId: 'attacker' } as never);
+ expect(create).toHaveBeenCalledWith(expect.objectContaining({ userId: 'user-123', symbol: 'AAPL' }));
+ });
+
+ it('refuses alerts the checker could never price', async () => {
+ await expect(createAlert({ symbol: 'RELIANCE.NS', targetPrice: 1000, condition: 'ABOVE' })).rejects.toThrow('US stocks and crypto');
+ expect(create).not.toHaveBeenCalled();
+ });
+});
diff --git a/lib/actions/alert.actions.ts b/lib/actions/alert.actions.ts
index 1130d92..980d3fc 100644
--- a/lib/actions/alert.actions.ts
+++ b/lib/actions/alert.actions.ts
@@ -61,17 +61,3 @@ export async function deleteAlert(alertId: string) {
throw new Error('Failed to delete alert');
}
}
-
-// Toggle alert active status (optional utility)
-export async function toggleAlert(alertId: string, active: boolean) {
- const userId = await requireUserId();
- try {
- await connectToDatabase();
- await Alert.findOneAndUpdate({ _id: alertId, userId }, { active });
- revalidatePath('/watchlist');
- return { success: true };
- } catch (error) {
- console.error('Error toggling alert:', error);
- throw new Error('Failed to update alert');
- }
-}