Fix file uploads; add upload tests

dependabot/go_modules/src/go/github.com/microcosm-cc/bluemonday-1.0.5
Alinson S. Xavier 5 years ago
parent c1dd129bf9
commit c0b5da211f

@ -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

@ -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
}

@ -0,0 +1 @@
This is a file.

@ -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")

@ -81,6 +81,38 @@
{{ if .EditPage }}
<div id="pad">
<script>
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
}
Dropzone.options.userInputForm = {
clickable: false,
maxFilesize: {{ if .MaxUploadSizeInMB }} {{.MaxUploadSizeInMB}} {{ else }} 10 {{end }}, // MB

Loading…
Cancel
Save