2016-03-18 11:59:03 -04:00
|
|
|
request = require("./request")
|
|
|
|
settings = require("settings-sharelatex")
|
2016-03-21 13:03:31 -04:00
|
|
|
{db, ObjectId} = require("../../../../app/js/infrastructure/mongojs")
|
2016-03-18 11:59:03 -04:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
class User
|
|
|
|
constructor: (options = {}) ->
|
|
|
|
@email = "acceptance-test-#{count}@example.com"
|
|
|
|
@password = "acceptance-test-#{count}-password"
|
|
|
|
count++
|
|
|
|
@jar = request.jar()
|
|
|
|
@request = request.defaults({
|
|
|
|
jar: @jar
|
|
|
|
})
|
2016-07-28 11:00:18 -04:00
|
|
|
|
2016-03-18 11:59:03 -04:00
|
|
|
login: (callback = (error) ->) ->
|
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.post {
|
|
|
|
url: "/register" # Register will log in, but also ensure user exists
|
|
|
|
json:
|
|
|
|
email: @email
|
|
|
|
password: @password
|
|
|
|
}, (error, response, body) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
db.users.findOne {email: @email}, (error, user) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@id = user?._id?.toString()
|
2016-07-05 05:24:24 -04:00
|
|
|
@_id = user?._id?.toString()
|
2016-07-29 04:52:55 -04:00
|
|
|
@first_name = user?.first_name
|
|
|
|
@referal_id = user?.referal_id
|
2016-03-18 11:59:03 -04:00
|
|
|
callback()
|
2016-07-05 05:24:24 -04:00
|
|
|
|
|
|
|
logout: (callback = (error) ->) ->
|
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.get {
|
2016-07-05 09:55:08 -04:00
|
|
|
url: "/logout"
|
2016-07-05 05:24:24 -04:00
|
|
|
json:
|
|
|
|
email: @email
|
|
|
|
password: @password
|
|
|
|
}, (error, response, body) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
db.users.findOne {email: @email}, (error, user) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@id = user?._id?.toString()
|
|
|
|
@_id = user?._id?.toString()
|
|
|
|
callback()
|
|
|
|
|
2016-03-21 13:03:31 -04:00
|
|
|
ensure_admin: (callback = (error) ->) ->
|
|
|
|
db.users.update {_id: ObjectId(@id)}, { $set: { isAdmin: true }}, callback
|
2016-07-05 05:24:24 -04:00
|
|
|
|
2017-09-01 08:28:11 -04:00
|
|
|
upgradeFeatures: (callback = (error) -> ) ->
|
|
|
|
features = {
|
|
|
|
collaborators: -1 # Infinite
|
|
|
|
versioning: true
|
|
|
|
dropbox:true
|
|
|
|
compileTimeout: 60
|
|
|
|
compileGroup:"priority"
|
|
|
|
templates: true
|
|
|
|
references: true
|
|
|
|
trackChanges: true
|
|
|
|
trackChangesVisible: true
|
|
|
|
}
|
|
|
|
db.users.update {_id: ObjectId(@id)}, { $set: { features: features }}, callback
|
|
|
|
|
|
|
|
downgradeFeatures: (callback = (error) -> ) ->
|
|
|
|
features = {
|
|
|
|
collaborators: 1
|
|
|
|
versioning: false
|
|
|
|
dropbox:false
|
|
|
|
compileTimeout: 60
|
|
|
|
compileGroup:"standard"
|
|
|
|
templates: false
|
|
|
|
references: false
|
|
|
|
trackChanges: false
|
|
|
|
trackChangesVisible: false
|
|
|
|
}
|
|
|
|
db.users.update {_id: ObjectId(@id)}, { $set: { features: features }}, callback
|
|
|
|
|
2017-09-01 09:45:54 -04:00
|
|
|
defaultFeatures: (callback = (error) -> ) ->
|
|
|
|
features = settings.defaultFeatures
|
|
|
|
db.users.update {_id: ObjectId(@id)}, { $set: { features: features }}, callback
|
|
|
|
|
2017-08-29 12:45:16 -04:00
|
|
|
full_delete_user: (email, callback = (error) ->) ->
|
|
|
|
db.users.findOne {email: email}, (error, user) =>
|
|
|
|
if !user?
|
|
|
|
return callback()
|
|
|
|
user_id = user._id
|
|
|
|
db.projects.remove owner_ref:ObjectId(user_id), {multi:true}, (err)->
|
|
|
|
if err?
|
|
|
|
callback(err)
|
|
|
|
db.users.remove {_id: ObjectId(user_id)}, callback
|
|
|
|
|
2016-03-18 11:59:03 -04:00
|
|
|
createProject: (name, callback = (error, project_id) ->) ->
|
|
|
|
@request.post {
|
|
|
|
url: "/project/new",
|
|
|
|
json:
|
|
|
|
projectName: name
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if !body?.project_id?
|
|
|
|
console.error "SOMETHING WENT WRONG CREATING PROJECT", response.statusCode, response.headers["location"], body
|
2016-08-01 04:06:02 -04:00
|
|
|
callback(null, body.project_id)
|
2016-07-28 11:00:18 -04:00
|
|
|
|
|
|
|
deleteProject: (project_id, callback=(error)) ->
|
|
|
|
@request.delete {
|
|
|
|
url: "/project/#{project_id}"
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null)
|
|
|
|
|
2016-07-29 06:04:07 -04:00
|
|
|
openProject: (project_id, callback=(error)) ->
|
|
|
|
@request.get {
|
|
|
|
url: "/project/#{project_id}"
|
|
|
|
}, (error, response, body) ->
|
2016-07-29 06:08:24 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
if response.statusCode != 200
|
|
|
|
err = new Error("Non-success response when opening project: #{response.statusCode}")
|
|
|
|
return callback(err)
|
2016-07-29 06:04:07 -04:00
|
|
|
callback(null)
|
|
|
|
|
2016-03-18 11:59:03 -04:00
|
|
|
addUserToProject: (project_id, email, privileges, callback = (error, user) ->) ->
|
|
|
|
@request.post {
|
|
|
|
url: "/project/#{project_id}/users",
|
|
|
|
json: {email, privileges}
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, body.user)
|
2016-07-28 11:00:18 -04:00
|
|
|
|
2016-03-18 11:59:03 -04:00
|
|
|
makePublic: (project_id, level, callback = (error) ->) ->
|
|
|
|
@request.post {
|
|
|
|
url: "/project/#{project_id}/settings/admin",
|
|
|
|
json:
|
|
|
|
publicAccessLevel: level
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null)
|
|
|
|
|
|
|
|
getCsrfToken: (callback = (error) ->) ->
|
|
|
|
@request.get {
|
|
|
|
url: "/register"
|
|
|
|
}, (err, response, body) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
csrfMatches = body.match("window.csrfToken = \"(.*?)\";")
|
|
|
|
if !csrfMatches?
|
|
|
|
return callback(new Error("no csrf token found"))
|
2017-08-29 12:45:16 -04:00
|
|
|
@csrfToken = csrfMatches[1]
|
2016-03-18 11:59:03 -04:00
|
|
|
@request = @request.defaults({
|
|
|
|
headers:
|
2017-08-29 12:45:16 -04:00
|
|
|
"x-csrf-token": @csrfToken
|
2016-03-18 11:59:03 -04:00
|
|
|
})
|
|
|
|
callback()
|
|
|
|
|
2016-07-05 09:55:08 -04:00
|
|
|
changePassword: (callback = (error) ->) ->
|
2016-07-05 09:21:38 -04:00
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.post {
|
2016-07-05 09:55:08 -04:00
|
|
|
url: "/user/password/update"
|
2016-07-05 09:21:38 -04:00
|
|
|
json:
|
2016-07-05 09:55:08 -04:00
|
|
|
currentPassword: @password
|
|
|
|
newPassword1: @password
|
|
|
|
newPassword2: @password
|
2016-07-05 09:21:38 -04:00
|
|
|
}, (error, response, body) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
db.users.findOne {email: @email}, (error, user) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
callback()
|
|
|
|
|
2016-07-06 07:14:01 -04:00
|
|
|
getUserSettingsPage: (callback = (error, statusCode) ->) ->
|
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.get {
|
|
|
|
url: "/user/settings"
|
|
|
|
}, (error, response, body) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, response.statusCode)
|
|
|
|
|
2017-05-10 08:45:53 -04:00
|
|
|
getProjectListPage: (callback=(error, statusCode)->) ->
|
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.get {
|
|
|
|
url: "/project"
|
|
|
|
}, (error, response, body) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, response.statusCode)
|
|
|
|
|
2016-07-06 07:14:01 -04:00
|
|
|
|
|
|
|
|
2016-07-05 09:21:38 -04:00
|
|
|
module.exports = User
|