Fix response when learning custom words

GitOrigin-RevId: fe7c8a2862532b83e650d3ecfae2ae5245394620
This commit is contained in:
Simon Detheridge 2019-07-20 13:20:35 +01:00 committed by sharelatex
parent e38a86d9f4
commit 0b938927af
2 changed files with 65 additions and 30 deletions

View file

@ -12,20 +12,23 @@ module.exports = {
proxyRequestToSpellingApi(req, res) {
const { language } = req.body
if (!language) {
logger.error('"language" field should be included for spell checking')
return res.status(422).send(JSON.stringify({ misspellings: [] }))
}
let url = req.url.slice('/spelling'.length)
if (!languageCodeIsSupported(language)) {
// this log statement can be changed to 'error' once projects with
// unsupported languages are removed from the DB
logger.info({ language }, 'language not supported')
return res.status(422).send(JSON.stringify({ misspellings: [] }))
if (url === '/check') {
if (!language) {
logger.error('"language" field should be included for spell checking')
return res.status(422).send(JSON.stringify({ misspellings: [] }))
}
if (!languageCodeIsSupported(language)) {
// this log statement can be changed to 'error' once projects with
// unsupported languages are removed from the DB
logger.info({ language }, 'language not supported')
return res.status(422).send(JSON.stringify({ misspellings: [] }))
}
}
const userId = AuthenticationController.getLoggedInUserId(req)
let url = req.url.slice('/spelling'.length)
url = `/user/${userId}${url}`
req.headers['Host'] = Settings.apis.spelling.host
return request({

View file

@ -97,21 +97,37 @@ describe('SpellingController', function() {
beforeEach(function() {
this.req.session.user._id = this.userId = 'user-id-123'
this.req.body = { language: 'fi', words: ['blab'] }
this.controller.proxyRequestToSpellingApi(this.req, this.res)
})
it('should not send a request to the spelling host', function() {
this.request.called.should.equal(false)
describe('when the request is a check request', function() {
beforeEach(function() {
this.controller.proxyRequestToSpellingApi(this.req, this.res)
})
it('should not send a request to the spelling host', function() {
this.request.called.should.equal(false)
})
it('should return an empty misspellings array', function() {
this.res.send
.calledWith(JSON.stringify({ misspellings: [] }))
.should.equal(true)
})
it('should return a 422 status', function() {
this.res.status.calledWith(422).should.equal(true)
})
})
it('should return an empty misspellings array', function() {
this.res.send
.calledWith(JSON.stringify({ misspellings: [] }))
.should.equal(true)
})
describe('when the request is not a check request', function() {
beforeEach(function() {
this.req.url = '/spelling/learn'
this.controller.proxyRequestToSpellingApi(this.req, this.res)
})
it('should return a 422 status', function() {
this.res.status.calledWith(422).should.equal(true)
it('should send a request to the spelling host', function() {
this.request.called.should.equal(true)
})
})
})
@ -119,21 +135,37 @@ describe('SpellingController', function() {
beforeEach(function() {
this.req.session.user._id = this.userId = 'user-id-123'
this.req.body = { words: ['blab'] }
this.controller.proxyRequestToSpellingApi(this.req, this.res)
})
it('should not send a request to the spelling host', function() {
this.request.called.should.equal(false)
describe('when the request is a check request', function() {
beforeEach(function() {
this.controller.proxyRequestToSpellingApi(this.req, this.res)
})
it('should not send a request to the spelling host', function() {
this.request.called.should.equal(false)
})
it('should return an empty misspellings array', function() {
this.res.send
.calledWith(JSON.stringify({ misspellings: [] }))
.should.equal(true)
})
it('should return a 422 status', function() {
this.res.status.calledWith(422).should.equal(true)
})
})
it('should return an empty misspellings array', function() {
this.res.send
.calledWith(JSON.stringify({ misspellings: [] }))
.should.equal(true)
})
describe('when the request is not a check request', function() {
beforeEach(function() {
this.req.url = '/spelling/learn'
this.controller.proxyRequestToSpellingApi(this.req, this.res)
})
it('should return a 422 status', function() {
this.res.status.calledWith(422).should.equal(true)
it('should send a request to the spelling host', function() {
this.request.called.should.equal(true)
})
})
})
})