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

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

beforeEach<CustomTestContext>(defaultBeforeEach(true));

describe("Feed Routes", () => {
  test<CustomTestContext>("create feed", async ({ apiCallers }) => {
    const api = apiCallers[0].feeds;
    const newFeed = await api.create({
      name: "Test Feed",
      url: "https://example.com/feed.xml",
      enabled: true,
    });

    expect(newFeed).toBeDefined();
    expect(newFeed.name).toEqual("Test Feed");
    expect(newFeed.url).toEqual("https://example.com/feed.xml");
    expect(newFeed.enabled).toBe(true);
  });

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

    // First, create a feed to update
    const createdFeed = await api.create({
      name: "Test Feed",
      url: "https://example.com/feed.xml",
      enabled: true,
    });

    // Update it
    const updatedFeed = await api.update({
      feedId: createdFeed.id,
      name: "Updated Feed",
      url: "https://updated-example.com/feed.xml",
      enabled: false,
    });

    expect(updatedFeed.name).toEqual("Updated Feed");
    expect(updatedFeed.url).toEqual("https://updated-example.com/feed.xml");
    expect(updatedFeed.enabled).toBe(false);

    // Test updating a non-existent feed
    await expect(() =>
      api.update({
        feedId: "non-existent-id",
        name: "Fail",
        url: "https://fail.com",
        enabled: true,
      }),
    ).rejects.toThrow(/Feed not found/);
  });

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

    // Create a couple of feeds
    await api.create({
      name: "Feed 1",
      url: "https://example1.com/feed.xml",
      enabled: true,
    });
    await api.create({
      name: "Feed 2",
      url: "https://example2.com/feed.xml",
      enabled: true,
    });

    const result = await api.list();
    expect(result.feeds).toBeDefined();
    expect(result.feeds.length).toBeGreaterThanOrEqual(2);
    expect(result.feeds.some((f) => f.name === "Feed 1")).toBe(true);
    expect(result.feeds.some((f) => f.name === "Feed 2")).toBe(true);
  });

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

    // Create a feed to delete
    const createdFeed = await api.create({
      name: "Test Feed",
      url: "https://example.com/feed.xml",
      enabled: true,
    });

    // Delete it
    await api.delete({ feedId: createdFeed.id });

    // Verify it's deleted
    await expect(() =>
      api.update({
        feedId: createdFeed.id,
        name: "Updated",
        url: "https://updated.com",
        enabled: true,
      }),
    ).rejects.toThrow(/Feed not found/);
  });

  test<CustomTestContext>("privacy for feeds", async ({ apiCallers }) => {
    const user1Feed = await apiCallers[0].feeds.create({
      name: "User 1 Feed",
      url: "https://user1-feed.com/feed.xml",
      enabled: true,
    });
    const user2Feed = await apiCallers[1].feeds.create({
      name: "User 2 Feed",
      url: "https://user2-feed.com/feed.xml",
      enabled: true,
    });

    // User 1 should not access User 2's feed
    await expect(() =>
      apiCallers[0].feeds.delete({ feedId: user2Feed.id }),
    ).rejects.toThrow(/User is not allowed to access resource/);
    await expect(() =>
      apiCallers[0].feeds.update({
        feedId: user2Feed.id,
        name: "Fail",
        url: "https://fail.com",
        enabled: true,
      }),
    ).rejects.toThrow(/User is not allowed to access resource/);

    // List should only show the correct user's feeds
    const user1List = await apiCallers[0].feeds.list();
    expect(user1List.feeds.some((f) => f.id === user1Feed.id)).toBe(true);
    expect(user1List.feeds.some((f) => f.id === user2Feed.id)).toBe(false);
  });

  test<CustomTestContext>("feed limit enforcement", async ({ apiCallers }) => {
    const api = apiCallers[0].feeds;

    // Create 1000 feeds (the maximum)
    for (let i = 0; i < 1000; i++) {
      await api.create({
        name: `Feed ${i}`,
        url: `https://example${i}.com/feed.xml`,
        enabled: true,
      });
    }

    // The 1001st feed should fail
    await expect(() =>
      api.create({
        name: "Feed 1001",
        url: "https://example1001.com/feed.xml",
        enabled: true,
      }),
    ).rejects.toThrow(/Maximum number of RSS feeds \(1000\) reached/);
  });
});