diff --git a/cypress/e2e/hedgedoc-migrator.cy.js b/cypress/e2e/hedgedoc-migrator.cy.js index 82c5430..6c671ff 100644 --- a/cypress/e2e/hedgedoc-migrator.cy.js +++ b/cypress/e2e/hedgedoc-migrator.cy.js @@ -1,31 +1,56 @@ +String.prototype.replaceLast = function (what, replacement) { + var pcs = this.split(what); + var lastPc = pcs.pop(); + return pcs.join(what) + replacement + lastPc; +} + async function getContent(url) { const res = await fetch(url.concat('/download')); return res.text(); } -async function migrateDocument(url, baseUrl) { - const content = await getContent(url); - cy.request({ - url: baseUrl.concat('/new'), - method: 'POST', - headers: { - 'Content-Type': 'text/markdown', - 'Access-Control-Allow-Origin': new URL(baseUrl).hostname, - }, - body: content, - }).then((res) => { - const redirect = res.redirects[0].split(' ')[1]; - cy.visit(url); - cy.get('#view-mode-toggle-edit').click({force: true}); - cy.get('.CodeMirror-scroll').type('{ctrl}a{backspace}'); - cy.get('.CodeMirror-scroll').type(`Moved to [${redirect}](${redirect})`); - }); +async function migrateDocument(oldUrl, newUrl) { + // get content of old pad + var content = await getContent(oldUrl); + + // replace URLs + content = content.replaceAll(new URL(oldUrl).hostname, new URL(newUrl).hostname); + + // visit new pad url (not possible via post api request because pad may already exists) + // Caution: Content of new pad url will be overwritten! + // Caution: History will not be moved to new pad url + cy.visit(newUrl.concat('?edit')); + cy.window().then((win) => { + + // Write Content + cy.get('.CodeMirror-scroll').type('{ctrl}a{backspace}'); + cy.get('.CodeMirror-scroll').type(content); + + // Visit old pad and replace content + cy.visit(oldUrl); + cy.get('#view-mode-toggle-edit').click({force: true}); + cy.get('.CodeMirror-scroll').type('{ctrl}a{backspace}'); + cy.get('.CodeMirror-scroll').type(`# 301 Pad moved{enter}=> [${newUrl}](${newUrl})`); + + }) + } describe('Migrate document', () => { - it('passes', async () => { - const baseUrl = 'https://md.margau.net'; - const url = 'https://md.margau.net/H0JO3L5DS-6Yhv4RrdS-tw'; - migrateDocument(url, baseUrl); - }); -}); + it('passes', async () => { + + // Read list of pads to migrate + cy.fixture('pads').then(async(pads) => { + + if(pads.length === 0) { + console.log("Didn't find any pad urls to migrate"); + } + + for (const pad of pads) { + await migrateDocument(pad.oldUrl, pad.newUrl); + } + + }) + + }) +}) diff --git a/cypress/fixtures/pads.json b/cypress/fixtures/pads.json new file mode 100644 index 0000000..98ca924 --- /dev/null +++ b/cypress/fixtures/pads.json @@ -0,0 +1,10 @@ +[ + { + "oldUrl":"https://md.margau.net/test", + "newUrl":"https://pad.hacknang.de/test" + }, + { + "oldUrl":"https://md.margau.net/test3", + "newUrl":"https://pad.hacknang.de/test2" + } +]