username: req.body.username,
password: req.body.password,
email: req.body.email,
+ displayNSFW: false,
role: constants.USER_ROLES.USER
})
db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
if (err) return next(err)
- user.password = req.body.password
+ if (req.body.password) user.password = req.body.password
+ if (req.body.displayNSFW !== undefined) user.displayNSFW = req.body.displayNSFW
+
user.save().asCallback(function (err) {
if (err) return next(err)
const usersValidators = {
isUserPasswordValid,
isUserRoleValid,
- isUserUsernameValid
+ isUserUsernameValid,
+ isUserDisplayNSFWValid
}
function isUserPasswordValid (value) {
return validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`))
}
+function isUserDisplayNSFWValid (value) {
+ return validator.isBoolean(value)
+}
+
// ---------------------------------------------------------------------------
module.exports = usersValidators
// ---------------------------------------------------------------------------
-const LAST_MIGRATION_VERSION = 40
+const LAST_MIGRATION_VERSION = 45
// ---------------------------------------------------------------------------
--- /dev/null
+'use strict'
+
+// utils = { transaction, queryInterface, sequelize, Sequelize }
+exports.up = function (utils, finalCallback) {
+ const q = utils.queryInterface
+ const Sequelize = utils.Sequelize
+
+ const data = {
+ type: Sequelize.BOOLEAN,
+ allowNull: false,
+ defaultValue: false
+ }
+
+ q.addColumn('Users', 'displayNSFW', data, { transaction: utils.transaction }).asCallback(finalCallback)
+}
+
+exports.down = function (options, callback) {
+ throw new Error('Not implemented.')
+}
function usersUpdate (req, res, next) {
req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
// Add old password verification
- req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
+ req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
+ req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid()
logger.debug('Checking usersUpdate parameters', { parameters: req.body })
isEmail: true
}
},
+ displayNSFW: {
+ type: DataTypes.BOOLEAN,
+ allowNull: false,
+ defaultValue: false,
+ validate: {
+ nsfwValid: function (value) {
+ const res = customUsersValidators.isUserDisplayNSFWValid(value)
+ if (res === false) throw new Error('Display NSFW is not valid.')
+ }
+ }
+ },
role: {
type: DataTypes.ENUM(values(constants.USER_ROLES)),
allowNull: false
id: this.id,
username: this.username,
email: this.email,
+ displayNSFW: this.displayNSFW,
role: this.role,
createdAt: this.createdAt
}
requestsUtils.makePutBodyRequest(server.url, path + userId, userAccessToken, data, done)
})
+ it('Should fail with an invalid display NSFW attribute', function (done) {
+ const data = {
+ displayNSFW: -1
+ }
+
+ requestsUtils.makePutBodyRequest(server.url, path + userId, userAccessToken, data, done)
+ })
+
it('Should fail with an non authenticated user', function (done) {
const data = {
password: 'my super password'
it('Should succeed with the correct params', function (done) {
const data = {
- password: 'my super password'
+ password: 'my super password',
+ displayNSFW: true
}
requestsUtils.makePutBodyRequest(server.url, path + userId, userAccessToken, data, done, 204)
expect(user.username).to.equal('user_1')
expect(user.email).to.equal('user_1@example.com')
+ expect(user.displayNSFW).to.be.falsy
expect(user.id).to.exist
done()
const user = users[0]
expect(user.username).to.equal('user_1')
expect(user.email).to.equal('user_1@example.com')
+ expect(user.displayNSFW).to.be.falsy
const rootUser = users[1]
expect(rootUser.username).to.equal('root')
expect(rootUser.email).to.equal('admin1@example.com')
+ expect(rootUser.displayNSFW).to.be.falsy
+
userId = user.id
done()
const user = users[0]
expect(user.username).to.equal('root')
expect(user.email).to.equal('admin1@example.com')
+ expect(user.displayNSFW).to.be.falsy
done()
})
const user = users[0]
expect(user.username).to.equal('user_1')
expect(user.email).to.equal('user_1@example.com')
+ expect(user.displayNSFW).to.be.falsy
done()
})
const user = users[0]
expect(user.username).to.equal('user_1')
expect(user.email).to.equal('user_1@example.com')
+ expect(user.displayNSFW).to.be.falsy
done()
})
expect(users[0].username).to.equal('root')
expect(users[0].email).to.equal('admin1@example.com')
+ expect(users[0].displayNSFW).to.be.falsy
+
expect(users[1].username).to.equal('user_1')
expect(users[1].email).to.equal('user_1@example.com')
+ expect(users[1].displayNSFW).to.be.falsy
done()
})
})
it('Should update the user password', function (done) {
- usersUtils.updateUser(server.url, userId, accessTokenUser, 'new password', function (err, res) {
+ usersUtils.updateUser(server.url, userId, accessTokenUser, 'new password', null, function (err, res) {
if (err) throw err
server.user.password = 'new password'
})
})
+ it('Should be able to change the NSFW display attribute', function (done) {
+ usersUtils.updateUser(server.url, userId, accessTokenUser, null, true, function (err, res) {
+ if (err) throw err
+
+ usersUtils.getUserInformation(server.url, accessTokenUser, function (err, res) {
+ if (err) throw err
+
+ const user = res.body
+
+ expect(user.username).to.equal('user_1')
+ expect(user.email).to.equal('user_1@example.com')
+ expect(user.displayNSFW).to.be.truthy
+ expect(user.id).to.exist
+
+ done()
+ })
+ })
+ })
+
it('Should be able to remove this user', function (done) {
usersUtils.removeUser(server.url, userId, accessToken, done)
})
.end(end)
}
-function updateUser (url, userId, accessToken, newPassword, end) {
+function updateUser (url, userId, accessToken, newPassword, displayNSFW, end) {
const path = '/api/v1/users/' + userId
+ const toSend = {}
+ if (newPassword !== undefined && newPassword !== null) toSend.password = newPassword
+ if (displayNSFW !== undefined && displayNSFW !== null) toSend.displayNSFW = displayNSFW
+
request(url)
.put(path)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
- .send({ password: newPassword })
+ .send(toSend)
.expect(204)
.end(end)
}