/
mockserver.js
358 lines (319 loc) · 9.73 KB
/
mockserver.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
let express = require("express");
let app = express();
let bodyParser = require("body-parser");
let MongoClient = require("mongodb").MongoClient;
let admin = require("firebase-admin")
let serviceAccount = require('./firebasekeyservice.json')
let cookieParser = require('cookie-parser')
let url = "mongodb://admin:password123@ds121282.mlab.com:21282/finalapp";
let dbo;
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://final-app-63dc4.firebaseio.com"
})
app.use(cookieParser())
app.use(bodyParser.raw({ type: "*/*" }));
let serverState = {
users: [],
sessions: {}
}
let testUser = {
username: "Bobster",
email: "bob@gmail.com",
userId: 12345,
firstName: "Bob",
lastName: "Ford",
location: "Montreal",
instruments: ["guitar", "piano"],
styles: ["classical", "flamenco"],
skillLevel: "intermediate",
experience: "string describing playing experience",
seeking: ["session", "gig"],
connections: [
{ userId: 3 },
{ userId: 456 }
],
reviews: [
{ reviewerId: 3, review: { overall: 4, skill: 5, reliability: 3, comment: "some string of feedback" } },
{ reviewerId: 456, review: { overall: 3, skill: 3, reliability: 3, comment: "pretty average" } }
]
}
let otherUsers = [
{
username: "franco345",
email: "ffc@gmail.com",
userId: 3,
firstName: "Francis",
lastName: "Copolla",
location: "Los Angeles",
instruments: ["piano", "vuvuzela"],
styles: ["experimental", "psychedelic"],
skillLevel: "intermediate",
experience: "years playing with touring psychedelic symphonies",
seeking: ["jam", "gig"],
connections: [
{ userId: 12345 },
{ userId: 456 }
],
reviews: [
{ reviewerId: 12345, review: { overall: 3, skill: 1, reliability: 5, comment: "Francis is always present ahead of time and ready to go, but vastly overestimates his abilities" } },
{ reviewerId: 456, review: { overall: 3, skill: 5, reliability: 1, comment: "Super unreliable, but a musical genius" } }
]
},
{
username: "tommyknocker",
email: "tomasfellows@gmail.com",
userId: 456,
firstName: "Tomas",
lastName: "Fellows",
location: "Montreal",
instruments: ["bass", "guitar", "keyboard"],
styles: ["indie", "rock", "electronic", "experimental"],
skillLevel: "intermediate",
experience: "7 years playing, writing, and recording in a band",
seeking: ["jam", "gig", "project"],
connections: [
{ userId: 12345 },
{ userId: 3 }
],
reviews: [
{ reviewerId: 12345, review: { overall: 5, skill: 3, reliability: 5, comment: "Perfect last minute fill-in for our gig" } },
{ reviewerId: 3, review: { overall: 3, skill: 2, reliability: 4, comment: "Struggled with the material, but was dedicated to getting it right" } }
]
}
]
// Connection to Mongo Database
MongoClient.connect(url, { useNewUrlParser: true }, (err, db) => {
if (err) throw err;
dbo = db.db("finalapp")
app.listen(4000, () => {
console.log("Listening on port 4000");
})
})
app.get('/createAccount', (req, res) => {
if (req) {
res.send(JSON.stringify({
success: true,
userId: 12345
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "account could not be created"
}))
}
})
app.get('/login', (req, res) => {
if (req) {
res.send(JSON.stringify({
success: true,
userId: 12345
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "could not log in"
}))
}
})
app.get('/logout', (req, res) => {
if (req) {
res.send(JSON.stringify({
success: true
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "couldn't log out"
}))
}
})
app.post('/getUsersByCriteria', (req, res) => {
let search = JSON.parse(req.body)
if (search.instrument || search.style || search.seeking) {
res.send(JSON.stringify({
success: true,
result: [
testUser, otherUsers[0]
]
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "couldn't search"
}))
}
})
app.post('/getUserById', (req, res) => {
let account = JSON.parse(req.body)
if (account.userId) {
res.send(JSON.stringify({
success: true,
user: otherUsers[1]
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "couldn't get user"
}))
}
})
app.post('/modifyProfile', (req, res) => {
let account = JSON.parse(req.body)
if (account.userId || account.instruments || account.styles || account.seeking) {
res.send(JSON.stringify({
success: true
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "couldn't modify profile"
}))
}
})
app.post('/addConnection', (req, res) => {
let account = JSON.parse(req.body)
if (account.userId && account.connectionUserId) {
res.send(JSON.stringify({
success: true
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "couldn't make connection"
}))
}
})
app.post('/removeConnection', (req, res) => {
let account = JSON.parse(req.body)
if (account.userId && account.connectionUserId) {
res.send(JSON.stringify({
success: true
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "couldn't remove connection"
}))
}
})
app.get('/getAllConnections', (req, res) => {
if (req) {
res.send(JSON.stringify({
success: true,
connectedUsers: [otherUsers[0], otherUsers[1]]
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "couldn't get connections"
}))
}
})
app.post('/getConnectionsByUserId', (req, res) => {
let parsedBody = JSON.parse(req.body)
if (parsedBody.userId) {
res.send(JSON.stringify({
success: true,
connectedUsers: [testUser, otherUsers[1]]
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "couldn't get connections"
}))
}
})
app.post('/reviewUser', (req, res) => {
let account = JSON.parse(req.body)
if (account.userId && account.revieweeId && account.review) {
res.send(JSON.stringify({
success: true
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "couldn't post review"
}))
}
})
app.post('/globalSearch', (req, res) => {
let search = JSON.parse(req.body)
if (search.keyword) {
res.send(JSON.stringify({
success: true,
users: [
otherUsers[0]
]
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "could not search"
}))
}
});
app.get('/getCurrentUser', (req, res) => {
// const sessionCookie = req.cookies.session
// let uid = serverState.sessions[sessionCookie]
let query = { userId: 12345 }
if (req) {
dbo.collection("users").findOne(query, (err, result) => {
if (err) throw err;
console.log("got userById" + result)
res.send(JSON.stringify({
success: true,
user: testUser
}))
})
} else {
res.send(JSON.stringify({
success: false,
reason: "no session ID"
}))
}
})
app.post('/getUserByUsername', (req, res) => {
let parsedBody = JSON.parse(req.body)
if (parsedBody.username) {
res.send(JSON.stringify({
success: true,
user: otherUsers[0]
}))
} else {
res.send(JSON.stringify({
success: false,
reason: "could not search"
}))
}
})
app.post('/sessionLogin', (req, res) => {
// Get the ID token passed and the CSRF token.
const idToken = JSON.parse(req.body).idToken
let uid = ''
admin.auth().verifyIdToken(idToken)
.then(function (decodedToken) {
uid = decodedToken.uid
})
.then(() => {
// Set session expiration to 5 days.
const expiresIn = 60 * 60 * 24 * 5 * 1000;
// Create the session cookie. This will also verify the ID token in the process.
// The session cookie will have the same claims as the ID token.
// To only allow session cookie setting on recent sign-in, auth_time in ID token
// can be checked to ensure user was recently signed in before creating a session cookie.
admin.auth().createSessionCookie(idToken, { expiresIn }).then((sessionCookie) => {
// Set cookie policy for session cookie.
const options = { maxAge: expiresIn, httpOnly: true };
//Link the cookie to the userId
serverState.sessions[sessionCookie] = uid
res.cookie('session', sessionCookie, options);
res.send(JSON.stringify({ status: 'success' }));
});
})
})
app.post('/getInfo', (req, res) => {
const sessionCookie = req.cookies.session
let response = users[serverState.sessions[sessionCookie]]
res.send(JSON.stringify(response))
})