This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 638
/
Copy pathutils.js
143 lines (127 loc) · 4.19 KB
/
utils.js
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
import fs from 'fs';
import jwt from 'jsonwebtoken';
import mongoose from 'mongoose';
import path from 'path';
import redis from 'redis';
import sinon from 'sinon';
import { expect, request } from 'chai';
import StreamClient from 'getstream/lib/lib/client';
import Personalization from 'getstream/lib/lib/personalization';
import Collections from 'getstream/lib/lib/collections';
import api from '../src/server';
import config from '../src/config';
import db from '../src/utils/db';
import logger from '../src/utils/logger';
import { CreateFingerPrints } from '../src/parsers/feed';
var mockClient = null;
const mockFeeds = {};
export function createMockFeed(group, id) {
const mock = {
slug: group,
userId: id,
id: group + ':' + id,
};
for (const method of ['follow', 'addActivity', 'addActivities', 'get', 'unfollow']) {
mock[method] = sinon.spy(sinon.stub().returns(Promise.resolve({ results: [] })));
}
mock['getReadOnlyToken'] = sinon.spy(sinon.stub().returns('faketoken'));
mockFeeds[group + ':' + id] = mock;
return mock;
}
export function getMockFeed(group, id) {
return mockFeeds[group + ':' + id];
}
function setupMocks() {
mockClient = sinon.createStubInstance(StreamClient);
mockClient.collections = sinon.createStubInstance(Collections);
mockClient.personalization = sinon.createStubInstance(Personalization);
mockClient.feed.callsFake((group, id) => {
return mockFeeds[group + ':' + id] || createMockFeed(group, id);
});
}
export function getMockClient() {
if (mockClient == null) {
setupMocks();
}
return mockClient;
}
export function getTestFeed(name) {
return fs.createReadStream(path.join(__dirname, 'data', 'feed', name));
}
export function getTestPodcast(name) {
return fs.createReadStream(path.join(__dirname, 'data', 'podcast-feed', name));
}
export function getTestPage(name) {
return fs.createReadStream(path.join(__dirname, 'data', 'og', name));
}
export async function loadFixture(...fixtures) {
const filters = {
Article: (articles) => {
for (const article of articles) {
article.enclosures = article.enclosures || [];
}
CreateFingerPrints(articles, 'STABLE');
return articles;
},
Episode: (episodes) => {
for (const episode of episodes) {
episode.enclosures = episode.enclosures || [];
}
CreateFingerPrints(episodes, 'STABLE');
return episodes;
},
};
for (const fixture of fixtures) {
const batch = require(`./fixtures/${fixture}.json`);
for (const models of batch) {
for (const modelName in models) {
const fixedData = models[modelName].map((data) => {
//XXX: cloning loaded json to enable filtering without thinking about module cache
data = Object.assign({}, data);
//XXX: convert things that look like ObjectID to actual ObjectID
// instances to enable mongo references
for (const key in data) {
//XXX: reject number attributes (see bug: https://jira.mongodb.org/browse/NODE-1146)
if (
typeof data[key] !== 'number' &&
mongoose.Types.ObjectId.isValid(data[key])
) {
data[key] = mongoose.Types.ObjectId(data[key]);
}
}
return data;
});
const filter = filters[modelName] || ((x) => x);
const filteredData = filter(fixedData);
const modulePath = `../src/models/${modelName.toLowerCase()}`;
//XXX: hack to avoid loading models explicitly before loading fixtures
// also avoids compiling model modules twice as mocked module loader
// with babel forces recompilation of transpiled source code which
// causes double-registration of mongoose models
const cachedModule = require.cache[require.resolve(modulePath)];
const model = cachedModule ? cachedModule.exports : require(modulePath);
await Promise.all(
filteredData.map((f) => {
return model.create(f);
}),
);
}
}
}
}
export function withLogin(
req,
user = {
email: '[email protected]',
sub: '5b0f306d8e147f10f16aceaf',
},
) {
const authToken = jwt.sign(user, config.jwt.secret);
return req.set('Authorization', `Bearer ${authToken}`);
}
export async function dropDBs() {
const redisClient = redis.createClient(config.cache.uri);
const mongo = await db;
await mongo.connection.dropDatabase();
await redisClient.send_command('FLUSHDB');
}