aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/routers/webhooks.test.ts
blob: 5a136a31516b5cb1b6d33bd4f70ae2264a0b2f76 (plain) (blame)
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
import { beforeEach, describe, expect, test } from "vitest";

import type { CustomTestContext } from "../testUtils";
import { defaultBeforeEach } from "../testUtils";

beforeEach<CustomTestContext>(defaultBeforeEach(true));

describe("Webhook Routes", () => {
  test<CustomTestContext>("create webhook", async ({ apiCallers }) => {
    const api = apiCallers[0].webhooks;
    const newWebhook = await api.create({
      url: "https://example.com/webhook",
      events: ["created", "edited"],
    });

    expect(newWebhook).toBeDefined();
    expect(newWebhook.url).toEqual("https://example.com/webhook");
    expect(newWebhook.events).toEqual(["created", "edited"]);
    expect(newWebhook.hasToken).toBe(false); // Assuming token is not set by default
  });

  test<CustomTestContext>("update webhook", async ({ apiCallers }) => {
    const api = apiCallers[0].webhooks;

    // First, create a webhook to update
    const createdWebhook = await api.create({
      url: "https://example.com/webhook",
      events: ["created"],
    });

    // Update it
    const updatedWebhook = await api.update({
      webhookId: createdWebhook.id,
      url: "https://updated-example.com/webhook",
      events: ["created", "edited"],
      token: "test-token",
    });

    expect(updatedWebhook.url).toEqual("https://updated-example.com/webhook");
    expect(updatedWebhook.events).toEqual(["created", "edited"]);
    expect(updatedWebhook.hasToken).toBe(true);

    // Test updating a non-existent webhook
    await expect(() =>
      api.update({
        webhookId: "non-existent-id",
        url: "https://fail.com",
        events: ["created"],
      }),
    ).rejects.toThrow(/Webhook not found/);
  });

  test<CustomTestContext>("list webhooks", async ({ apiCallers }) => {
    const api = apiCallers[0].webhooks;

    // Create a couple of webhooks
    await api.create({
      url: "https://example1.com/webhook",
      events: ["created"],
    });
    await api.create({
      url: "https://example2.com/webhook",
      events: ["edited"],
    });

    const result = await api.list();
    expect(result.webhooks).toBeDefined();
    expect(result.webhooks.length).toBeGreaterThanOrEqual(2);
    expect(
      result.webhooks.some((w) => w.url === "https://example1.com/webhook"),
    ).toBe(true);
    expect(
      result.webhooks.some((w) => w.url === "https://example2.com/webhook"),
    ).toBe(true);
  });

  test<CustomTestContext>("delete webhook", async ({ apiCallers }) => {
    const api = apiCallers[0].webhooks;

    // Create a webhook to delete
    const createdWebhook = await api.create({
      url: "https://example.com/webhook",
      events: ["created"],
    });

    // Delete it
    await api.delete({ webhookId: createdWebhook.id });

    // Verify it's deleted
    await expect(() =>
      api.update({
        webhookId: createdWebhook.id,
        url: "https://updated.com",
        events: ["created"],
      }),
    ).rejects.toThrow(/Webhook not found/);
  });

  test<CustomTestContext>("privacy for webhooks", async ({ apiCallers }) => {
    const user1Webhook = await apiCallers[0].webhooks.create({
      url: "https://user1-webhook.com",
      events: ["created"],
    });
    const user2Webhook = await apiCallers[1].webhooks.create({
      url: "https://user2-webhook.com",
      events: ["created"],
    });

    // User 1 should not access User 2's webhook
    await expect(() =>
      apiCallers[0].webhooks.delete({ webhookId: user2Webhook.id }),
    ).rejects.toThrow(/User is not allowed to access resource/);
    await expect(() =>
      apiCallers[0].webhooks.update({
        webhookId: user2Webhook.id,
        url: "https://fail.com",
        events: ["created"],
      }),
    ).rejects.toThrow(/User is not allowed to access resource/);

    // List should only show the correct user's webhooks
    const user1List = await apiCallers[0].webhooks.list();
    expect(user1List.webhooks.some((w) => w.id === user1Webhook.id)).toBe(true);
    expect(user1List.webhooks.some((w) => w.id === user2Webhook.id)).toBe(
      false,
    );
  });
});