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

View file

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