Skip to content

Commit b278fa6

Browse files
authored
Make a notebook writable after successful save-as (#5296)
* add failing test for readonly notebook * test typing after save-as * make notebook writable after save-as fixes #1324 * fix failing test in travis
1 parent 7563c54 commit b278fa6

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

notebook/static/notebook/js/notebook.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,13 +2913,16 @@ define([
29132913
'content': that.toJSON(),
29142914
'name': nb_name
29152915
};
2916+
var start = new Date().getTime();
29162917
return that.contents.save(nb_path, model)
29172918
.then(function(data) {
29182919
d.modal('hide');
2920+
that.writable = true;
29192921
that.notebook_name = data.name;
29202922
that.notebook_path = data.path;
29212923
that.session.rename_notebook(data.path);
29222924
that.events.trigger('notebook_renamed.Notebook', data);
2925+
that.save_notebook_success(start, data);
29232926
}, function(error) {
29242927
var msg = i18n.msg._(error.message || 'Unknown error saving notebook');
29252928
$(".save-message").html(
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from notebook.tests.selenium.utils import wait_for_selector, Notebook
2+
from selenium.webdriver.common.keys import Keys
3+
from selenium.webdriver.support.ui import WebDriverWait
4+
5+
6+
promise_js = """
7+
var done = arguments[arguments.length - 1];
8+
(%s).then(
9+
data => { done(["success", data]); },
10+
error => { done(["error", error]); }
11+
);
12+
"""
13+
14+
def execute_promise(js, browser):
15+
state, data = browser.execute_async_script(promise_js % js)
16+
if state == 'success':
17+
return data
18+
raise Exception(data)
19+
20+
def wait_for_rename(browser, nbname, timeout=10):
21+
wait = WebDriverWait(browser, timeout)
22+
def notebook_renamed(browser):
23+
elem = browser.find_element_by_id('notebook_name')
24+
current_name = browser.execute_script('return arguments[0].innerText', elem)
25+
return current_name == nbname
26+
return wait.until(notebook_renamed)
27+
28+
def save_as(nb):
29+
JS = 'Jupyter.notebook.save_notebook_as()'
30+
return nb.browser.execute_script(JS)
31+
32+
def get_notebook_name(nb):
33+
JS = 'return Jupyter.notebook.notebook_name'
34+
return nb.browser.execute_script(JS)
35+
36+
def refresh_notebook(nb):
37+
nb.browser.refresh()
38+
nb.__init__(nb.browser)
39+
40+
def test_save_readonly_notebook_as(notebook):
41+
# Make notebook read-only
42+
notebook.edit_cell(index=0, content='import os\nimport stat\nos.chmod("'
43+
+ notebook.browser.current_url.split('?')[0].split('/')[-1] + '", stat.S_IREAD)\nprint(0)')
44+
notebook.browser.execute_script("Jupyter.notebook.get_cell(0).execute();")
45+
notebook.wait_for_cell_output(0)
46+
refresh_notebook(notebook)
47+
# Test that the notebook is read-only
48+
assert notebook.browser.execute_script('return Jupyter.notebook.writable') == False
49+
50+
# Add some content
51+
test_content_0 = "print('a simple')\nprint('test script')"
52+
notebook.edit_cell(index=0, content=test_content_0)
53+
54+
# Wait for Save As modal, save
55+
save_as(notebook)
56+
wait_for_selector(notebook.browser, '.save-message')
57+
inp = notebook.browser.find_element_by_xpath('//input[@data-testid="save-as"]')
58+
inp.send_keys('writable_notebook.ipynb')
59+
inp.send_keys(Keys.RETURN)
60+
wait_for_rename(notebook.browser, "writable_notebook")
61+
# Test that the name changed
62+
assert get_notebook_name(notebook) == "writable_notebook.ipynb"
63+
# Test that address bar was updated (TODO: get the base url)
64+
assert "writable_notebook.ipynb" in notebook.browser.current_url
65+
# Test that it is no longer read-only
66+
assert notebook.browser.execute_script('return Jupyter.notebook.writable') == True
67+
68+
# Add some more content
69+
test_content_1 = "print('a second simple')\nprint('script to test save feature')"
70+
notebook.add_and_execute_cell(content=test_content_1)
71+
# and save the notebook
72+
execute_promise("Jupyter.notebook.save_notebook()", notebook.browser)
73+
74+
# Test that it still contains the content
75+
assert notebook.get_cell_contents(index=0) == test_content_0
76+
assert notebook.get_cell_contents(index=1) == test_content_1
77+
# even after a refresh
78+
refresh_notebook(notebook)
79+
assert notebook.get_cell_contents(index=0) == test_content_0
80+
assert notebook.get_cell_contents(index=1) == test_content_1

0 commit comments

Comments
 (0)