2018-03-22 05:49:12 -04:00
|
|
|
express = require("express")
|
|
|
|
app = express()
|
|
|
|
bodyParser = require('body-parser')
|
2018-05-29 12:21:42 -04:00
|
|
|
sinon = require 'sinon'
|
2018-03-22 05:49:12 -04:00
|
|
|
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
|
2018-11-19 10:44:51 -05:00
|
|
|
v1Id = 1000
|
|
|
|
|
2018-03-22 05:49:12 -04:00
|
|
|
module.exports = MockV1Api =
|
2018-11-19 10:44:51 -05:00
|
|
|
nextV1Id: -> v1Id++
|
|
|
|
|
2018-05-16 11:31:28 -04:00
|
|
|
users: { }
|
|
|
|
|
|
|
|
setUser: (id, user) ->
|
|
|
|
@users[id] = user
|
2018-03-22 05:49:12 -04:00
|
|
|
|
|
|
|
exportId: null
|
|
|
|
|
|
|
|
exportParams: null
|
|
|
|
|
|
|
|
setExportId: (id) ->
|
|
|
|
@exportId = id
|
|
|
|
|
|
|
|
getLastExportParams: () ->
|
|
|
|
@exportParams
|
|
|
|
|
|
|
|
clearExportParams: () ->
|
|
|
|
@exportParams = null
|
|
|
|
|
2018-05-29 12:21:42 -04:00
|
|
|
syncUserFeatures: sinon.stub()
|
|
|
|
|
2018-07-10 05:42:17 -04:00
|
|
|
affiliations: []
|
|
|
|
|
2018-07-16 08:26:52 -04:00
|
|
|
updateEmail: sinon.stub()
|
|
|
|
|
2018-07-16 12:25:52 -04:00
|
|
|
existingEmails: []
|
|
|
|
|
2018-12-10 08:12:34 -05:00
|
|
|
brands: {}
|
|
|
|
|
2018-07-10 05:42:17 -04:00
|
|
|
setAffiliations: (affiliations) -> @affiliations = affiliations
|
|
|
|
|
2018-03-22 05:49:12 -04:00
|
|
|
run: () ->
|
2018-05-29 12:21:42 -04:00
|
|
|
app.get "/api/v1/sharelatex/users/:v1_user_id/plan_code", (req, res, next) =>
|
|
|
|
user = @users[req.params.v1_user_id]
|
2018-05-16 11:31:28 -04:00
|
|
|
if user
|
|
|
|
res.json user
|
|
|
|
else
|
|
|
|
res.sendStatus 404
|
|
|
|
|
2018-11-19 10:44:51 -05:00
|
|
|
app.get "/api/v1/sharelatex/users/:v1_user_id/subscriptions", (req, res, next) =>
|
|
|
|
user = @users[req.params.v1_user_id]
|
|
|
|
if user?.subscription?
|
|
|
|
res.json user.subscription
|
|
|
|
else
|
|
|
|
res.sendStatus 404
|
|
|
|
|
2018-11-30 08:03:35 -05:00
|
|
|
app.get "/api/v1/sharelatex/users/:v1_user_id/subscription_status", (req, res, next) =>
|
|
|
|
user = @users[req.params.v1_user_id]
|
|
|
|
if user?.subscription_status?
|
|
|
|
res.json user.subscription_status
|
|
|
|
else
|
|
|
|
res.sendStatus 404
|
|
|
|
|
|
|
|
app.delete "/api/v1/sharelatex/users/:v1_user_id/subscription", (req, res, next) =>
|
|
|
|
user = @users[req.params.v1_user_id]
|
|
|
|
if user?
|
|
|
|
user.canceled = true
|
|
|
|
res.sendStatus 200
|
|
|
|
else
|
|
|
|
res.sendStatus 404
|
2018-11-19 10:44:51 -05:00
|
|
|
|
2018-05-29 12:21:42 -04:00
|
|
|
app.post "/api/v1/sharelatex/users/:v1_user_id/sync", (req, res, next) =>
|
|
|
|
@syncUserFeatures(req.params.v1_user_id)
|
|
|
|
res.sendStatus 200
|
|
|
|
|
2018-03-22 05:49:12 -04:00
|
|
|
app.post "/api/v1/sharelatex/exports", (req, res, next) =>
|
|
|
|
@exportParams = Object.assign({}, req.body)
|
|
|
|
res.json exportId: @exportId
|
|
|
|
|
2018-06-21 06:30:12 -04:00
|
|
|
app.get "/api/v2/users/:userId/affiliations", (req, res, next) =>
|
2018-07-10 05:42:17 -04:00
|
|
|
res.json @affiliations
|
2018-06-21 06:30:12 -04:00
|
|
|
|
2018-06-21 11:59:35 -04:00
|
|
|
app.post "/api/v2/users/:userId/affiliations", (req, res, next) =>
|
|
|
|
res.sendStatus 201
|
|
|
|
|
|
|
|
app.delete "/api/v2/users/:userId/affiliations/:email", (req, res, next) =>
|
|
|
|
res.sendStatus 204
|
2018-05-29 12:21:42 -04:00
|
|
|
|
2018-12-10 08:12:34 -05:00
|
|
|
app.get "/api/v2/brands/:slug", (req, res, next) =>
|
|
|
|
if brand = @brands[req.params.slug]
|
|
|
|
res.json brand
|
|
|
|
else
|
|
|
|
res.sendStatus 404
|
|
|
|
|
2018-07-04 12:59:19 -04:00
|
|
|
app.get '/universities/list', (req, res, next) ->
|
|
|
|
res.json []
|
|
|
|
|
|
|
|
app.get '/universities/list/:id', (req, res, next) ->
|
2018-11-20 05:48:21 -05:00
|
|
|
res.json {
|
|
|
|
id: parseInt(req.params.id)
|
|
|
|
name: "Institution #{req.params.id}"
|
|
|
|
}
|
2018-07-04 12:59:19 -04:00
|
|
|
|
|
|
|
app.get '/university/domains', (req, res, next) ->
|
|
|
|
res.json []
|
|
|
|
|
2018-07-16 08:26:52 -04:00
|
|
|
app.put '/api/v1/sharelatex/users/:id/email', (req, res, next) =>
|
2018-07-17 06:12:09 -04:00
|
|
|
{ email } = req.body?.user
|
2018-07-16 12:25:52 -04:00
|
|
|
if email in @existingEmails
|
|
|
|
return res.sendStatus 409
|
|
|
|
else
|
|
|
|
@updateEmail parseInt(req.params.id), email
|
|
|
|
return res.sendStatus 200
|
2018-07-16 08:26:52 -04:00
|
|
|
|
2018-09-20 09:59:30 -04:00
|
|
|
app.post "/api/v1/sharelatex/login", (req, res, next) =>
|
|
|
|
for id, user of @users
|
2018-09-24 06:32:15 -04:00
|
|
|
if user? && user.email == req.body.email && user.password == req.body.password
|
2018-09-20 09:59:30 -04:00
|
|
|
return res.json {
|
|
|
|
email: user.email,
|
|
|
|
valid: true,
|
|
|
|
user_profile: user.profile
|
|
|
|
}
|
|
|
|
return res.status(403).json {
|
2018-09-24 06:32:15 -04:00
|
|
|
email: req.body.email,
|
2018-09-20 09:59:30 -04:00
|
|
|
valid: false
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:49:12 -04:00
|
|
|
app.listen 5000, (error) ->
|
|
|
|
throw error if error?
|
|
|
|
.on "error", (error) ->
|
2018-05-16 11:31:28 -04:00
|
|
|
console.error "error starting MockV1Api:", error.message
|
2018-03-22 05:49:12 -04:00
|
|
|
process.exit(1)
|
2018-09-24 18:03:28 -04:00
|
|
|
|
2018-10-02 11:16:46 -04:00
|
|
|
app.get '/api/v1/sharelatex/docs/:token/info', (req, res, next) =>
|
|
|
|
res.json { allow: true, exported: false }
|
2018-09-27 12:38:35 -04:00
|
|
|
|
2018-03-22 05:49:12 -04:00
|
|
|
MockV1Api.run()
|