summaryrefslogtreecommitdiff
path: root/test/parent-routes.test.ts
blob: 83734bb7acce7ce59b4df2ec1145db931e76350c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { describe, test, expect, vi, beforeEach } from "vitest";
import request from "supertest";
import express, { Request, Response, NextFunction } from "express";
import { db } from "../src/db/db";
import { linkedDevices, deviceConfig, users } from "../src/db/schema";
import { eq } from "drizzle-orm";
import createParentRouter from "../src/routes/parent";
import { redis } from "../src/db/redis/client";

vi.mock("../src/middleware/auth", () => ({
  authParent: (req: Request, res: Response, next: NextFunction) => {
    req.user = { id: 1, type: "parent" };
    next();
  },
}));

vi.mock("../src/notifications/push", () => ({
  isValidPushToken: vi.fn(() => true),
}));

describe("Parent Routes", () => {
  let app: express.Application;

  beforeEach(async () => {
    const onlineDevices = new Map();

    app = express();
    app.use(express.json());
    app.use("/", createParentRouter(onlineDevices));

    await db.delete(deviceConfig).execute();
    await db.update(users).set({ emailVerified: true }).where(eq(users.id, 1));
    vi.mocked(redis.set).mockReset();
  });

  test("should get devices for parent", async () => {
    const response = await request(app).get("/parent/devices").expect(200);

    expect(response.body.success).toBe(true);
    expect(Array.isArray(response.body.devices)).toBe(true);
  });

  test("should get device config", async () => {
    await db.insert(deviceConfig).values({
      deviceId: 1,
      disableBuddy: false,
      blockAdultSites: true,
      familyLinkAntiCircumvention: false,
      blockStrangers: false,
      notifyDangerousMessages: true,
      notifyNewContactAdded: true,
    });

    const response = await request(app).get("/parent/controls/1").expect(200);

    expect(response.body.success).toBe(true);
    expect(Array.isArray(response.body.safetyControls)).toBe(true);
    const adultSitesControl = response.body.safetyControls.find(
      (c: { key: string }) => c.key === "adult_sites",
    );
    expect(adultSitesControl.defaultValue).toBe(true);
  });

  test("should update device config", async () => {
    await db.insert(deviceConfig).values({
      deviceId: 1,
      disableBuddy: false,
      blockAdultSites: true,
      familyLinkAntiCircumvention: false,
      blockStrangers: false,
      notifyDangerousMessages: true,
      notifyNewContactAdded: true,
    });

    const response = await request(app)
      .post("/parent/controls/1")
      .send({
        key: "block_strangers",
        value: true,
      })
      .expect(200);

    expect(response.body.success).toBe(true);

    const config = await db
      .select()
      .from(deviceConfig)
      .where(eq(deviceConfig.deviceId, 1))
      .limit(1);

    expect(config[0]?.blockStrangers).toBe(true);
  });

  test("should rename device", async () => {
    const response = await request(app)
      .post("/parent/device/1/rename")
      .send({
        name: "Updated Device Name",
      })
      .expect(200);

    expect(response.body.success).toBe(true);

    const device = await db
      .select()
      .from(linkedDevices)
      .where(eq(linkedDevices.id, 1))
      .limit(1);

    expect(device[0]?.nickname).toBe("Updated Device Name");
  });

  test("should validate request body for config update", async () => {
    const response = await request(app)
      .post("/parent/controls/1")
      .send({
        key: "invalid_key",
        value: true,
      })
      .expect(400);

    expect(response.body.success).toBe(false);
  });

  test("should validate device ID parameter", async () => {
    const response = await request(app)
      .get("/parent/controls/invalid")
      .expect(400);

    expect(response.body.success).toBe(false);
  });

  test("should generate one-time kid link code", async () => {
    vi.mocked(redis.set).mockResolvedValueOnce("OK");

    const response = await request(app).post("/parent/kid-link-code").expect(200);

    expect(response.body.success).toBe(true);
    expect(response.body.code).toMatch(/^[A-Z0-9]{3}-[A-Z0-9]{3}$/);
    expect(response.body.expiresInSeconds).toBe(300);

    const firstCall = vi.mocked(redis.set).mock.calls[0];
    expect(firstCall).toBeDefined();
    expect(firstCall?.[0]).toMatch(/^kid-link-code:[A-Z0-9]{3}-[A-Z0-9]{3}$/);
    expect(firstCall?.[1]).toBe("1");
    expect(firstCall?.[2]).toBe("EX");
    expect(firstCall?.[3]).toBe(300);
    expect(firstCall?.[4]).toBe("NX");
  });

  test("should reject kid link code generation when email is not verified", async () => {
    await db
      .update(users)
      .set({ emailVerified: false })
      .where(eq(users.id, 1));

    const response = await request(app).post("/parent/kid-link-code").expect(400);

    expect(response.body.success).toBe(false);
    expect(response.body.reason).toContain("Verify your email");
    expect(redis.set).not.toHaveBeenCalled();
  });
});