profile: store preferred language in the database
This commit is contained in:
@@ -8,6 +8,7 @@ exports = module.exports = {
|
||||
setFallbackEmail,
|
||||
getAvatar,
|
||||
setAvatar,
|
||||
setLanguage,
|
||||
getBackgroundImage,
|
||||
setBackgroundImage,
|
||||
setPassword,
|
||||
@@ -65,6 +66,7 @@ async function get(req, res, next) {
|
||||
role: req.user.role,
|
||||
source: req.user.source,
|
||||
hasBackgroundImage: !!backgroundImage,
|
||||
language: req.user.language,
|
||||
avatarUrl: `https://${dashboardFqdn}/api/v1/profile/avatar/${req.user.id}`,
|
||||
avatarType: avatarType.toString() // this is a Buffer
|
||||
}));
|
||||
@@ -106,6 +108,18 @@ async function setDisplayName(req, res, next) {
|
||||
next(new HttpSuccess(204));
|
||||
}
|
||||
|
||||
async function setLanguage(req, res, next) {
|
||||
assert.strictEqual(typeof req.user, 'object');
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
|
||||
if ('language' in req.body && typeof req.body.language !== 'string') return next(new HttpError(400, 'language must be string'));
|
||||
|
||||
const [error] = await safe(users.update(req.user, { language: req.body.language }, AuditSource.fromRequest(req)));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204));
|
||||
}
|
||||
|
||||
async function setAvatar(req, res, next) {
|
||||
assert.strictEqual(typeof req.user, 'object');
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@ describe('Profile API', function () {
|
||||
expect(response.body.displayName).to.be.a('string');
|
||||
expect(response.body.password).to.not.be.ok();
|
||||
expect(response.body.salt).to.not.be.ok();
|
||||
expect(response.body.language).to.be('');
|
||||
});
|
||||
|
||||
it('fails with expired token', async function () {
|
||||
@@ -370,4 +371,54 @@ describe('Profile API', function () {
|
||||
expect(response.body).to.eql(defaultAvatar);
|
||||
});
|
||||
});
|
||||
|
||||
describe('language', function () {
|
||||
it('fails to set unknown language', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/profile/language`)
|
||||
.query({ access_token: user.token })
|
||||
.send({ language: 'ta' })
|
||||
.ok(() => true);
|
||||
expect(response.statusCode).to.be(400);
|
||||
});
|
||||
|
||||
it('fails to set bad language', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/profile/language`)
|
||||
.query({ access_token: user.token })
|
||||
.send({ language: 123 })
|
||||
.ok(() => true);
|
||||
expect(response.statusCode).to.be(400);
|
||||
});
|
||||
|
||||
it('fails to set unknown language', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/profile/language`)
|
||||
.query({ access_token: user.token })
|
||||
.send({ language: 'ta' })
|
||||
.ok(() => true);
|
||||
expect(response.statusCode).to.be(400);
|
||||
});
|
||||
|
||||
it('set valid language', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/profile/language`)
|
||||
.query({ access_token: user.token })
|
||||
.send({ language: 'en' });
|
||||
expect(response.statusCode).to.be(204);
|
||||
});
|
||||
|
||||
it('did set language', async function () {
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/profile`).query({ access_token: user.token });
|
||||
expect(response.body.language).to.contain('en');
|
||||
});
|
||||
|
||||
it('reset valid language', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/profile/language`)
|
||||
.query({ access_token: user.token })
|
||||
.send({ language: '' });
|
||||
expect(response.statusCode).to.be(204);
|
||||
});
|
||||
|
||||
it('did reset language', async function () {
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/profile`).query({ access_token: user.token });
|
||||
expect(response.body.language).to.contain('');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user