From c0b5da211fbdb7b815dd1ce01a358953d5f6a6e5 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Sun, 14 Feb 2021 12:02:33 -0600 Subject: [PATCH] Fix file uploads; add upload tests --- Makefile | 19 ++++++++++-------- src/js/notes.js | 36 +---------------------------------- src/python/assets/readme.txt | 1 + src/python/notes_test.py | 37 +++++++++++++++++++++++++++--------- src/templates/index.tmpl | 32 +++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 52 deletions(-) create mode 100644 src/python/assets/readme.txt diff --git a/Makefile b/Makefile index eac874c..cc333f5 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ VERSION=0.1.0 +DEFAULT_ARGS := --allow-file-uploads + LDFLAGS=-ldflags "-X main.version=${VERSION}" ROLLUP := node_modules/.bin/rollup @@ -63,15 +65,16 @@ install-deps: pip install -r src/python/requirements.txt .PHONY: run -run: - cd build && ./notes +run: all + cd build && ./notes $(DEFAULT_ARGS) .PHONY: test test: all - @cd build ;\ - ./notes --port=8040 &\ - NOTES_PID=$$! ;\ - pytest .. ;\ - PYTEST_RESULT=$$? ;\ - kill $$NOTES_PID ;\ + @cd build ;\ + ./notes $(DEFAULT_ARGS) --port=8040 &\ + NOTES_PID=$$! ;\ + cd .. ;\ + pytest ;\ + PYTEST_RESULT=$$? ;\ + kill $$NOTES_PID ;\ exit $$PYTEST_RESULT diff --git a/src/js/notes.js b/src/js/notes.js index f66dfa6..5db8ca7 100644 --- a/src/js/notes.js +++ b/src/js/notes.js @@ -113,40 +113,6 @@ function retagContent() { } // Re-render Mermaid diagrams - mermaid.init() - + mermaid.init(); } -// noinspection JSUnusedLocalSymbols -function onUploadFinished(file) { - let userInput = $('#userInput'); - this.removeFile(file); - var cursorPos = userInput.prop('selectionStart'); - var cursorEnd = userInput.prop('selectionEnd'); - var v = userInput.val(); - var textBefore = v.substring(0, cursorPos); - var textAfter = v.substring(cursorPos, v.length); - var message = 'uploaded file'; - if (cursorEnd > cursorPos) { - message = v.substring(cursorPos, cursorEnd); - textAfter = v.substring(cursorEnd, v.length); - } - var prefix = ''; - if (file.type.startsWith("image")) { - prefix = '!'; - } - var extraText = prefix + '[' + file.xhr.getResponseHeader("Location").split('filename=')[1] + '](' + - file.xhr.getResponseHeader("Location") + - ')'; - - userInput.val( - textBefore + - extraText + - textAfter - ); - - // Select the newly-inserted link - userInput.prop('selectionStart', cursorPos); - userInput.prop('selectionEnd', cursorPos + extraText.length); - userInput.trigger('keyup'); // trigger a save -} \ No newline at end of file diff --git a/src/python/assets/readme.txt b/src/python/assets/readme.txt new file mode 100644 index 0000000..ee3b914 --- /dev/null +++ b/src/python/assets/readme.txt @@ -0,0 +1 @@ +This is a file. \ No newline at end of file diff --git a/src/python/notes_test.py b/src/python/notes_test.py index 200138d..cc4ed90 100644 --- a/src/python/notes_test.py +++ b/src/python/notes_test.py @@ -1,17 +1,29 @@ from time import sleep +import pytest from selenium import webdriver from selenium.webdriver.chrome.options import Options INDEX_URL = "http://localhost:8040" -def _launch(): +@pytest.fixture() +def browser(): options = Options() options.add_argument("--headless") options.add_argument("window-size=1920,1080") browser = webdriver.Chrome(options=options) - return browser + yield browser + browser.quit() + + +def upload_file(browser, name, path): + content = open(path).read() + browser.execute_script(""" + var dropzone_instance = Dropzone.forElement("#userInputForm") + var new_file = new File(['%s'], '%s', {type: 'application/octet-binary'}) + dropzone_instance.addFile(new_file) + """ % (content, name)) def assert_no_javascript_errors(browser): @@ -20,15 +32,12 @@ def assert_no_javascript_errors(browser): ) -def test_index_should_redirect_to_edit(): - browser = _launch() +def test_index_should_redirect_to_edit(browser): browser.get(INDEX_URL) assert "/edit" in browser.current_url - browser.close() -def test_should_edit(): - browser = _launch() +def test_should_edit(browser): browser.get(INDEX_URL) assert_no_javascript_errors(browser) @@ -70,5 +79,15 @@ def test_should_edit(): assert h1.text == "Hello world" assert "Hello world" in browser.title - # End session - browser.close() + +def test_upload(browser): + browser.get(INDEX_URL) + assert_no_javascript_errors(browser) + + # Upload a file + upload_file(browser, "readme.txt", "src/python/assets/readme.txt") + sleep(1) + assert_no_javascript_errors(browser) + + # Should create a link + browser.find_element_by_link_text("readme.txt") diff --git a/src/templates/index.tmpl b/src/templates/index.tmpl index 21c1eb4..2e6291d 100755 --- a/src/templates/index.tmpl +++ b/src/templates/index.tmpl @@ -81,6 +81,38 @@ {{ if .EditPage }}