-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into 248i-cors-imgs
- Loading branch information
Showing
155 changed files
with
3,127 additions
and
2,300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Application, Request } from 'express'; | ||
import { needsAuthorization } from 'api/auth'; | ||
import { validation } from 'api/utils'; | ||
import translations from 'api/i18n'; | ||
import { getTranslationsEntriesV2 } from 'api/i18n/v2_support'; | ||
|
||
const translationsRoutes = (app: Application) => { | ||
app.get('/api/v2/translations', async (_req: Request, res) => { | ||
const translationsV2 = await getTranslationsEntriesV2(); | ||
const translationList = await translationsV2.all(); | ||
res.json(translationList); | ||
}); | ||
|
||
app.post( | ||
'/api/v2/translations', | ||
needsAuthorization(), | ||
validation.validateRequest({ | ||
type: 'object', | ||
properties: { | ||
body: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
properties: { | ||
_id: { type: 'string' }, | ||
language: { type: 'string' }, | ||
key: { type: 'string' }, | ||
value: { type: 'string' }, | ||
context: { | ||
type: 'object', | ||
properties: { | ||
id: { type: 'string' }, | ||
label: { type: 'string' }, | ||
type: { type: 'string' }, | ||
}, | ||
required: ['id', 'label', 'type'], | ||
}, | ||
}, | ||
required: ['language', 'key', 'value', 'context'], | ||
}, | ||
}, | ||
}, | ||
required: ['body'], | ||
}), | ||
async (req, res) => { | ||
await translations.v2StructureSave(req.body); | ||
req.sockets.emitToCurrentTenant('translationKeysChange', req.body); | ||
res.status(200); | ||
res.json({ success: true }); | ||
} | ||
); | ||
}; | ||
|
||
export { translationsRoutes }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import 'isomorphic-fetch'; | ||
import request from 'supertest'; | ||
|
||
import { TranslationDBO } from 'api/i18n.v2/schemas/TranslationDBO'; | ||
import { getFixturesFactory } from 'api/utils/fixturesFactory'; | ||
import { testingEnvironment } from 'api/utils/testingEnvironment'; | ||
import { TestEmitSources, iosocket, setUpApp } from 'api/utils/testingRoutes'; | ||
import { UserRole } from 'shared/types/userSchema'; | ||
import { translationsRoutes } from '..'; | ||
|
||
describe('i18n translations V2 routes', () => { | ||
const createTranslationDBO = getFixturesFactory().v2.database.translationDBO; | ||
const app = setUpApp(translationsRoutes, (req, _res, next) => { | ||
req.user = { | ||
username: 'admin', | ||
role: UserRole.ADMIN, | ||
email: '[email protected]', | ||
}; | ||
// @ts-ignore | ||
req.file = { path: 'filder/filename.ext' }; | ||
next(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
const translationsV2: TranslationDBO[] = [ | ||
createTranslationDBO('Search', 'Buscar', 'es', { | ||
id: 'System', | ||
type: 'Entity', | ||
label: 'User Interface', | ||
}), | ||
createTranslationDBO('Search', 'Search', 'en', { | ||
id: 'System', | ||
type: 'Uwazi UI', | ||
label: 'User Interface', | ||
}), | ||
]; | ||
await testingEnvironment.setUp( | ||
{ | ||
settings: [ | ||
{ | ||
languages: [ | ||
{ key: 'en', label: 'English', default: true }, | ||
{ key: 'es', label: 'Spanish', default: false }, | ||
], | ||
}, | ||
], | ||
translationsV2, | ||
}, | ||
'index_i18n_v2_routes' | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
iosocket.emit.mockReset(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testingEnvironment.tearDown(); | ||
}); | ||
|
||
describe('/api/v2/translations', () => { | ||
it('should update the translations and emit translationKeysChange event', async () => { | ||
const response = await request(app) | ||
.post('/api/v2/translations') | ||
.send([ | ||
{ | ||
language: 'es', | ||
key: 'Search', | ||
value: 'Búsqueda', | ||
context: { | ||
id: 'System', | ||
label: 'User Interface', | ||
type: 'Uwazi UI', | ||
}, | ||
}, | ||
]); | ||
expect(response.status).toEqual(200); | ||
expect(iosocket.emit).toHaveBeenCalledWith( | ||
'translationKeysChange', | ||
TestEmitSources.currentTenant, | ||
[ | ||
{ | ||
context: { id: 'System', label: 'User Interface', type: 'Uwazi UI' }, | ||
key: 'Search', | ||
language: 'es', | ||
value: 'Búsqueda', | ||
}, | ||
] | ||
); | ||
}); | ||
|
||
it('should handle invalid POST request payload', async () => { | ||
const response = await request(app) | ||
.post('/api/v2/translations') | ||
.send({ invalidKey: 'value' }); // Invalid payload | ||
expect(response.status).toBe(400); | ||
expect(response.body).toEqual( | ||
expect.objectContaining({ prettyMessage: 'validation failed' }) | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.