"""Server tests for user/view-specific document rendering."""
from timApp.auth.accesstype import AccessType
from timApp.tests.server.timroutetest import TimRouteTest
from timApp.timdb.sqa import db
[docs]class ParVisibilityTest(TimRouteTest):
[docs] def test_belongs(self):
self.login_test1()
d = self.create_doc(
initial_par="""
#-
a
#- {nocache=true visible="%%'testuser1'|belongs%%"}
testuser1 only
#-
anyone
"""
)
self.assert_content(
self.get(d.url, as_tree=True), ["a", "testuser1 only", "anyone"]
)
p = d.document.get_paragraphs()[1]
p.set_markdown("testuser1 only edited")
d.document.modify_paragraph_obj(p.get_id(), p)
self.assert_content(
self.get(d.url, as_tree=True), ["a", "testuser1 only edited", "anyone"]
)
self.test_user_2.grant_access(d, AccessType.view)
db.session.commit()
self.login_test2()
self.assert_content(self.get(d.url, as_tree=True), ["a", "anyone"])
self.test_user_2.grant_access(d, AccessType.edit)
db.session.commit()
self.assert_content(
self.get(d.url, as_tree=True), ["a", "anyone"]
) # TODO shouldn't editors always see everything?
p = d.document.get_paragraphs()[1]
p.set_attr("visible", "%%'testuser2'|belongs%%")
p.set_markdown("testuser2 only")
d.document.modify_paragraph_obj(p.get_id(), p)
self.assert_content(
self.get(d.url, as_tree=True), ["a", "testuser2 only", "anyone"]
)
self.login_test1()
self.assert_content(self.get(d.url, as_tree=True), ["a", "anyone"])
[docs] def test_isview(self):
self.login_test1()
d = self.create_doc(
initial_par="""
#- {nocache=true visible="%%False|isview%%"}
only teacher
"""
)
self.assert_content(self.get(d.url, as_tree=True), [])
self.assert_content(self.get(d.get_url_for_view("slide"), as_tree=True), [])
self.assert_content(
self.get(d.get_url_for_view("answers"), as_tree=True), ["only teacher"]
)
self.assert_content(
self.get(d.get_url_for_view("teacher"), as_tree=True), ["only teacher"]
)
self.assert_content(
self.get(d.url, as_tree=True), []
) # make sure the teacher route is not cached incorrectly
[docs] def test_belongs_in_text(self):
self.login_test1()
d = self.create_doc(
initial_par="""
#- {nocache=true}
I am testuser1: %%'testuser1'|belongs%%
#-
I am testuser1: %%'testuser1'|belongs%%
"""
)
self.assert_content(
self.get(d.url, as_tree=True),
[
"I am testuser1: True",
"I am testuser1: The belongs filter requires nocache=true attribute.",
],
)
[docs] def test_area_visible(self):
self.login_test1()
d = self.create_doc(
initial_par="""
1
#- {area=a visible="%%'testuser1'|belongs%%" nocache=true}
a title
#-
2
#- {area_end=a}
#-
3
"""
)
self.test_user_2.grant_access(d, AccessType.view)
db.session.commit()
self.assert_content(
self.get(d.url, as_tree=True),
[
"1",
"a title",
"2",
"",
"3",
],
)
self.login_test2()
self.assert_content(
self.get(d.url, as_tree=True),
[
"1",
"",
"",
"3",
],
)