-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathredirector.ts
128 lines (114 loc) · 3.86 KB
/
redirector.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
import appFunc from '../src';
import { FastifyInstance } from 'fastify';
import { saveFixtures, TEST_UA } from './helpers';
import { ArticlePost, Source, User, YouTubePost } from '../src/entity';
import { sourcesFixture } from './fixture/source';
import request from 'supertest';
import { postsFixture, videoPostsFixture } from './fixture/post';
import { notifyView } from '../src/common';
import { DataSource } from 'typeorm';
import createOrGetConnection from '../src/db';
import { fallbackImages } from '../src/config';
jest.mock('../src/common', () => ({
...(jest.requireActual('../src/common') as Record<string, unknown>),
notifyView: jest.fn(),
}));
let app: FastifyInstance;
let con: DataSource;
beforeAll(async () => {
con = await createOrGetConnection();
app = await appFunc();
return app.ready();
});
afterAll(() => app.close());
beforeEach(async () => {
jest.resetAllMocks();
await saveFixtures(con, Source, sourcesFixture);
await saveFixtures(con, ArticlePost, postsFixture);
await saveFixtures(con, YouTubePost, videoPostsFixture);
});
describe('GET /r/:postId', () => {
it('should return not found', () => {
return request(app.server).get('/r/not').expect(404);
});
it('should redirect to post url', () => {
return request(app.server)
.get('/r/p1')
.expect(302)
.expect('Location', 'http://p1.com/?ref=dailydev');
});
it('should redirect to youtube post url', () => {
return request(app.server)
.get('/r/yt1')
.expect(302)
.expect('Location', 'https://youtu.be/T_AbQGe7fuU?ref=dailydev');
});
it('should render redirect html and notify view event', async () => {
await request(app.server)
.get('/r/p1')
.set('user-agent', TEST_UA)
.set('cookie', 'da2=u1')
.set('referer', 'https://daily.dev')
.expect(200)
.expect('content-type', 'text/html')
.expect('referrer-policy', 'origin, origin-when-cross-origin')
.expect('link', `<http://p1.com/?ref=dailydev>; rel="preconnect"`)
.expect(
'<html><head><meta name="robots" content="noindex,nofollow"><meta http-equiv="refresh" content="0;URL=http://p1.com/?ref=dailydev"></head></html>',
);
expect(notifyView).toBeCalledWith(
expect.anything(),
'p1',
'u1',
'https://daily.dev',
expect.anything(),
['javascript', 'webdev'],
);
});
it('should render redirect html with hash value', async () => {
await request(app.server)
.get('/r/p1?a=id')
.set('user-agent', TEST_UA)
.expect(200)
.expect('content-type', 'text/html')
.expect('referrer-policy', 'origin, origin-when-cross-origin')
.expect('link', `<http://p1.com/?ref=dailydev>; rel="preconnect"`)
.expect(
'<html><head><meta name="robots" content="noindex,nofollow"><meta http-equiv="refresh" content="0;URL=http://p1.com/?ref=dailydev#id"></head></html>',
);
});
it('should concat query params correctly', async () => {
await con
.getRepository(ArticlePost)
.update({ id: 'p1' }, { url: 'http://p1.com/?a=b' });
return request(app.server)
.get('/r/p1')
.expect(302)
.expect('Location', 'http://p1.com/?a=b&ref=dailydev');
});
});
describe('GET /:id/profile-image', () => {
beforeEach(async () => {
await con.getRepository(User).save([
{
id: '1',
name: 'Ido',
image: 'https://daily.dev/ido.jpg',
timezone: 'utc',
createdAt: new Date(),
},
]);
});
it('should return profile picture for user', async () => {
return request(app.server)
.get('/1/profile-image')
.expect(302)
.expect('Location', 'https://daily.dev/ido.jpg');
});
it('should return default image for non existing user', async () => {
return request(app.server)
.get('/123/profile-image')
.expect(302)
.expect('Location', fallbackImages.avatar);
});
});