🐛 Fixed bad redirects yaml overriding backed up working yaml file (#20555)

ref ENG-945

- Fixed an issue where upload a broken redirects yaml will override the
last working yaml.
- Instead it will now do the validation before saving and overriding the
yaml.
This commit is contained in:
Ronald Langeveld 2024-07-08 16:45:20 +07:00 committed by GitHub
parent bd15ce5c03
commit 3818445a18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 4 deletions

View File

@ -211,6 +211,9 @@ class CustomRedirectsAPI {
*/
async setFromFilePath(filePath, ext = '.json') {
const redirectsFilePath = await this.getRedirectsFilePath();
const content = await readRedirectsFile(filePath);
const parsed = parseRedirectsFile(content, ext);
this.validate(parsed);
if (redirectsFilePath) {
const backupRedirectsPath = this.getBackupFilePath(redirectsFilePath);
@ -223,10 +226,6 @@ class CustomRedirectsAPI {
await fs.move(redirectsFilePath, backupRedirectsPath);
}
const content = await readRedirectsFile(filePath);
const parsed = parseRedirectsFile(content, ext);
this.validate(parsed);
if (ext === '.json') {
await fs.writeFile(this.createRedirectsFilePath('.json'), JSON.stringify(parsed), 'utf-8');
} else if (ext === '.yaml') {

View File

@ -259,5 +259,48 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
// two redirects in total
redirectManager.addRedirect.calledTwice.should.be.true();
});
it('does not create a backup file from a bad redirect yaml file', async function () {
const incomingFilePath = path.join(__dirname, '/invalid/path/redirects_incoming.yaml');
const backupFilePath = path.join(basePath, 'backup.yaml');
const invalidYaml = `
301:
/my-old-blog-post/: /revamped-url/
/my-old-blog-post/: /revamped-url/
302:
/another-old-blog-post/: /hello-there/
`;
// redirects.json file already exits
fs.pathExists.withArgs(`${basePath}redirects.json`).resolves(false);
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(true);
// incoming redirects file
fs.readFile.withArgs(incomingFilePath, 'utf-8').resolves(invalidYaml);
// backup file DOES not exists yet
fs.pathExists.withArgs(backupFilePath).resolves(false);
// should not be called
fs.unlink.withArgs(backupFilePath).resolves(false);
fs.move.withArgs(`${basePath}redirects.yaml`, backupFilePath).resolves(true);
customRedirectsAPI = new CustomRedirectsAPI({
basePath,
redirectManager,
getBackupFilePath: () => backupFilePath,
validate: () => {}
});
try {
await customRedirectsAPI.setFromFilePath(incomingFilePath, '.yaml');
should.fail('setFromFilePath did not throw');
} catch (err) {
should.exist(err);
err.errorType.should.eql('BadRequestError');
}
fs.unlink.called.should.not.be.true();
fs.move.called.should.not.be.true();
});
});
});