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,6 +12,9 @@ module.exports = {
proxyRequestToSpellingApi(req, res) { proxyRequestToSpellingApi(req, res) {
const { language } = req.body const { language } = req.body
let url = req.url.slice('/spelling'.length)
if (url === '/check') {
if (!language) { if (!language) {
logger.error('"language" field should be included for spell checking') logger.error('"language" field should be included for spell checking')
return res.status(422).send(JSON.stringify({ misspellings: [] })) return res.status(422).send(JSON.stringify({ misspellings: [] }))
@ -23,9 +26,9 @@ module.exports = {
logger.info({ language }, 'language not supported') logger.info({ language }, 'language not supported')
return res.status(422).send(JSON.stringify({ misspellings: [] })) 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,6 +97,10 @@ 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'] }
})
describe('when the request is a check request', function() {
beforeEach(function() {
this.controller.proxyRequestToSpellingApi(this.req, this.res) this.controller.proxyRequestToSpellingApi(this.req, this.res)
}) })
@ -115,10 +119,26 @@ describe('SpellingController', function() {
}) })
}) })
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 send a request to the spelling host', function() {
this.request.called.should.equal(true)
})
})
})
describe('when no language is indicated', function() { describe('when no language is indicated', 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'] }
})
describe('when the request is a check request', function() {
beforeEach(function() {
this.controller.proxyRequestToSpellingApi(this.req, this.res) this.controller.proxyRequestToSpellingApi(this.req, this.res)
}) })
@ -136,5 +156,17 @@ describe('SpellingController', function() {
this.res.status.calledWith(422).should.equal(true) this.res.status.calledWith(422).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 send a request to the spelling host', function() {
this.request.called.should.equal(true)
})
})
})
}) })
}) })