-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathrss.ts
205 lines (194 loc) · 5.27 KB
/
rss.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { DataSource, DeepPartial } from 'typeorm';
import { FastifyInstance } from 'fastify';
import request from 'supertest';
import { v4 as uuidv4 } from 'uuid';
import appFunc from '../src';
import {
Bookmark,
Source,
PostKeyword,
Settings,
User,
ArticlePost,
} from '../src/entity';
import { saveFixtures } from './helpers';
import { sourcesFixture } from './fixture/source';
import { postKeywordsFixture } from './fixture/post';
import { usersFixture } from './fixture/user';
import createOrGetConnection from '../src/db';
let app: FastifyInstance;
let con: DataSource;
const now = new Date(1591192264260);
export const postsFixture: DeepPartial<ArticlePost>[] = [
{
id: 'p1',
shortId: 'p1',
title: 'P1',
url: 'http://p1.com',
score: 0,
sourceId: 'a',
createdAt: now,
},
{
id: 'p2',
shortId: 'p2',
title: 'P2',
url: 'http://p2.com',
score: 7,
sourceId: 'b',
createdAt: new Date(now.getTime() - 1000),
},
{
id: 'p3',
shortId: 'p3',
title: 'P3',
url: 'http://p3.com',
score: 4,
sourceId: 'c',
createdAt: new Date(now.getTime() - 2000),
},
{
id: 'p4',
shortId: 'p4',
title: 'P4',
url: 'http://p4.com',
score: 3,
sourceId: 'a',
createdAt: new Date(now.getTime() - 3000),
},
{
id: 'p5',
shortId: 'p5',
title: 'P5',
url: 'http://p5.com',
score: 10,
sourceId: 'b',
createdAt: new Date(now.getTime() - 4000),
},
];
const bookmarksFixture = [
{
userId: '1',
postId: 'p3',
createdAt: new Date(now.getTime() - 1000),
},
{
userId: '1',
postId: 'p1',
createdAt: new Date(now.getTime() - 2000),
},
{
userId: '1',
postId: 'p5',
createdAt: new Date(now.getTime() - 3000),
},
];
beforeAll(async () => {
con = await createOrGetConnection();
app = await appFunc();
return app.ready();
});
afterAll(() => app.close());
beforeEach(async () => {
await saveFixtures(con, User, usersFixture);
await saveFixtures(con, Source, sourcesFixture);
await saveFixtures(con, ArticlePost, postsFixture);
await saveFixtures(con, PostKeyword, postKeywordsFixture);
await saveFixtures(con, Bookmark, bookmarksFixture);
});
describe('GET /rss/b/:slug', () => {
const slug = uuidv4();
const path = `/rss/b/${slug}`;
it('should fail when user does not exist', async () => {
await con.getRepository(Settings).save({ userId: '1', bookmarkSlug: slug });
await con.getRepository(User).delete({});
return request(app.server).get(path).expect(404);
});
it('should fail when bookmarks are private', async () => {
await con.getRepository(User).save([usersFixture[0]]);
await con.getRepository(Settings).save({ userId: '1', bookmarkSlug: null });
return request(app.server).get(path).expect(404);
});
it('should fail when slug is not a valid uuid', async () => {
return request(app.server).get('/rss/b/1').expect(404);
});
it('should return rss feed of bookmarks', async () => {
await con.getRepository(User).save([usersFixture[0]]);
await con.getRepository(Settings).save({ userId: '1', bookmarkSlug: slug });
const res = await request(app.server)
.get(path)
.expect('Content-Type', 'application/rss+xml')
.expect(200);
expect(
res.text
.replace(/\<lastBuildDate>.*?<\/lastBuildDate>/g, '')
.replace(slug, 'uuid'),
).toMatchSnapshot();
});
});
//
// describe('GET /rss/b/l/:listId', () => {
// const path = (id: string): string => `/rss/b/l/${id}`;
//
// let list: BookmarkList;
//
// beforeEach(async () => {
// list = await con
// .getRepository(BookmarkList)
// .save({ name: 'Gems', userId: '1' });
// });
//
// it('should fail when user does not exist', () => {
// mockFailedUser();
// return request(app.server).get(path(list.id)).expect(403);
// });
//
//
// it('should fail when list does not exist', () => {
// mockUser();
// return request(app.server).get(path('list')).expect(403);
// });
//
// it('should return rss feed of bookmarks', async () => {
// mockUser();
// await con
// .getRepository(Bookmark)
// .update({ postId: In(['p1', 'p3']) }, { listId: list.id });
// const res = await request(app.server)
// .get(path(list.id))
// .expect('Content-Type', 'application/rss+xml')
// .expect(200);
// expect(
// res.text
// .replace(/\<lastBuildDate>.*?<\/lastBuildDate>/g, '')
// .replace(/\<atom:link.*?type=\"application\/rss\+xml\"\/>/g, ''),
// ).toMatchSnapshot();
// });
// });
//
// describe('GET /rss/f/:userId', () => {
// const path = '/rss/f/1';
//
// it('should fail when user does not exist', () => {
// mockFailedUser();
// return request(app.server).get(path).expect(403);
// });
//
//
// it('should fail when feed does not exist', () => {
// mockUser();
// return request(app.server).get(path).expect(403);
// });
//
// it('should return rss feed of bookmarks', async () => {
// mockUser();
// await saveFeedFixtures();
// const res = await request(app.server)
// .get(path)
// .expect('Content-Type', 'application/rss+xml')
// .expect(200);
// expect(
// res.text.replace(/\<lastBuildDate>.*?<\/lastBuildDate>/g, ''),
// ).toMatchSnapshot();
// });
// });