open-law/tests/test_book.py

1224 lines
37 KiB
Python
Raw Normal View History

2023-06-01 04:59:37 +00:00
from flask import current_app as Response
2023-04-25 08:56:51 +00:00
from flask.testing import FlaskClient, FlaskCliRunner
2023-04-24 06:57:38 +00:00
2023-04-25 08:56:51 +00:00
from app import models as m, db
from app.controllers.create_access_groups import create_moderator_group
2023-05-15 14:55:36 +00:00
from tests.utils import (
login,
logout,
2023-05-15 15:09:03 +00:00
check_if_nested_book_entities_is_deleted,
2023-05-15 14:55:36 +00:00
check_if_nested_collection_entities_is_deleted,
check_if_nested_section_entities_is_deleted,
check_if_nested_interpretation_entities_is_deleted,
2023-05-25 07:10:22 +00:00
create_test_book,
2023-05-15 14:55:36 +00:00
)
2023-04-24 06:57:38 +00:00
def test_create_edit_delete_book(client: FlaskClient):
2023-04-24 06:57:38 +00:00
login(client)
BOOK_NAME = "Test Book"
# label len < 6
response: Response = client.post(
"/book/create",
data=dict(
label="12345",
),
follow_redirects=True,
)
assert response.status_code == 200
2023-04-27 13:17:25 +00:00
assert b"Label must be between 6 and 256 characters long." in response.data
2023-04-24 06:57:38 +00:00
2023-05-25 07:10:22 +00:00
book: m.Book = m.Book.query.filter_by(label=BOOK_NAME).first()
2023-04-24 06:57:38 +00:00
assert not book
assert not m.Book.query.count()
2023-04-27 13:17:25 +00:00
# label len > 256
2023-04-24 06:57:38 +00:00
response: Response = client.post(
"/book/create",
data=dict(
label="".join(["0" for _ in range(1025)]),
),
follow_redirects=True,
)
assert response.status_code == 200
2023-04-27 13:17:25 +00:00
assert b"Label must be between 6 and 256 characters long." in response.data
2023-04-24 06:57:38 +00:00
2023-05-25 07:10:22 +00:00
book: m.Book = m.Book.query.filter_by(label=BOOK_NAME).first()
2023-04-24 06:57:38 +00:00
assert not book
assert not m.Book.query.count()
response: Response = client.post(
"/book/create",
data=dict(
label=BOOK_NAME,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Book added!" in response.data
2023-05-25 07:10:22 +00:00
book: m.Book = m.Book.query.filter_by(label=BOOK_NAME).first()
2023-04-24 06:57:38 +00:00
assert book
2023-04-24 07:20:31 +00:00
assert book.versions
assert len(book.versions) == 1
2023-05-25 07:10:22 +00:00
assert book.access_groups
assert len(book.access_groups) == 2
root_collection: m.Collection = book.last_version.collections[0]
assert root_collection
assert root_collection.access_groups
assert len(root_collection.access_groups) == 2
2023-04-24 07:20:31 +00:00
2023-05-01 15:17:46 +00:00
response: Response = client.post(
2023-05-02 06:48:10 +00:00
"/book/999/edit",
2023-05-01 15:17:46 +00:00
data=dict(
2023-05-02 06:48:10 +00:00
book_id=999,
label="Book 1",
2023-05-01 15:17:46 +00:00
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Book not found!" in response.data
2023-05-01 15:17:46 +00:00
response: Response = client.post(
f"/book/{book.id}/edit",
data=dict(
2023-05-02 06:48:10 +00:00
book_id=book.id,
label=BOOK_NAME + " EDITED",
2023-05-01 15:17:46 +00:00
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
book = db.session.get(m.Book, book.id)
assert book.label != BOOK_NAME
2023-05-01 15:17:46 +00:00
response: Response = client.post(
f"/book/{book.id}/delete",
2023-05-01 15:17:46 +00:00
data=dict(
2023-05-02 06:48:10 +00:00
book_id=book.id,
2023-05-01 15:17:46 +00:00
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
book = db.session.get(m.Book, book.id)
2023-06-01 04:59:37 +00:00
assert book.is_deleted
2023-05-15 15:09:03 +00:00
check_if_nested_book_entities_is_deleted(book)
2023-05-01 15:17:46 +00:00
2023-04-24 07:20:31 +00:00
2023-05-25 14:13:40 +00:00
def test_add_delete_contributor(client: FlaskClient):
2023-04-24 08:30:44 +00:00
_, user = login(client)
user: m.User
2023-04-24 07:20:31 +00:00
2023-04-24 08:30:44 +00:00
moderator = m.User(username="Moderator", password="test").save()
2023-04-24 07:20:31 +00:00
2023-05-25 14:13:40 +00:00
moderators_book: m.Book = create_test_book(moderator.id)
2023-04-24 08:30:44 +00:00
response: Response = client.post(
f"/book/{moderators_book.id}/add_contributor",
data=dict(user_id=moderator.id, role=m.BookContributor.Roles.MODERATOR),
follow_redirects=True,
)
assert response.status_code == 200
assert b"You do not have permission" in response.data
2023-04-24 07:20:31 +00:00
2023-05-25 14:13:40 +00:00
book: m.Book = create_test_book(user.id)
2023-05-24 14:10:50 +00:00
m.BookVersion(semver="1.0.0", book_id=book.id).save()
2023-04-24 08:30:44 +00:00
response: Response = client.post(
f"/book/{book.id}/add_contributor",
data=dict(user_id=moderator.id, role=m.BookContributor.Roles.MODERATOR),
follow_redirects=True,
)
2023-04-24 07:20:31 +00:00
2023-04-24 08:30:44 +00:00
assert response.status_code == 200
assert b"Contributor was added!" in response.data
2023-05-25 14:13:40 +00:00
moderator: m.User = db.session.get(m.User, moderator.id)
assert moderator.access_groups
for access_group in moderator.access_groups:
access_group: m.AccessGroup
assert access_group.book_id == book.id
response: Response = client.post(
f"/book/{book.id}/add_contributor",
data=dict(user_id=moderator.id, role=m.BookContributor.Roles.MODERATOR),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Already exists!" in response.data
2023-04-24 08:30:44 +00:00
contributor: m.BookContributor = m.BookContributor.query.filter_by(
user=moderator, book=book
).first()
assert contributor.role == m.BookContributor.Roles.MODERATOR
assert len(book.contributors) == 2
2023-04-24 08:30:44 +00:00
editor = m.User(username="Editor", password="test").save()
response: Response = client.post(
f"/book/{book.id}/add_contributor",
data=dict(user_id=editor.id, role=m.BookContributor.Roles.EDITOR),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Contributor was added!" in response.data
2023-04-24 07:20:31 +00:00
2023-04-24 08:30:44 +00:00
contributor: m.BookContributor = m.BookContributor.query.filter_by(
user=editor, book=book
).first()
assert contributor.role == m.BookContributor.Roles.EDITOR
assert len(book.contributors) == 3
2023-04-25 08:56:51 +00:00
2023-05-25 14:13:40 +00:00
contributor_to_delete = m.BookContributor.query.filter_by(
user_id=moderator.id, book_id=book.id
).first()
2023-04-25 08:56:51 +00:00
2023-05-25 14:13:40 +00:00
assert moderator.access_groups
2023-04-25 08:56:51 +00:00
response: Response = client.post(
f"/book/{book.id}/delete_contributor",
data=dict(user_id=contributor_to_delete.user_id),
follow_redirects=True,
)
2023-05-25 14:13:40 +00:00
moderator: m.User = db.session.get(m.User, moderator.id)
assert not moderator.access_groups
2023-04-25 08:56:51 +00:00
assert response.status_code == 200
assert b"Success!" in response.data
response: Response = client.post(
f"/book/{book.id}/delete_contributor",
data=dict(user_id=contributor_to_delete.user_id),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Does not exists!" in response.data
2023-04-25 09:39:26 +00:00
2023-05-11 09:32:59 +00:00
response: Response = client.post(
f"/book/{123}/add_contributor",
data=dict(user_id=1, role=m.BookContributor.Roles.MODERATOR),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Book not found!" in response.data
2023-05-11 09:32:59 +00:00
2023-04-25 09:39:26 +00:00
def test_edit_contributor_role(client: FlaskClient, runner: FlaskCliRunner):
_, user = login(client)
user: m.User
book = create_test_book(user.id)
# for contributor in m.BookContributor.query.all():
# db.session.delete(contributor)
# db.session.commit()
2023-04-25 09:39:26 +00:00
book.user_id = user.id
book.save()
contributors_len = len(book.contributors)
assert contributors_len
contributor_edit = book.contributors[0]
assert contributor_edit.role == m.BookContributor.Roles.MODERATOR
response: Response = client.post(
f"/book/{book.id}/edit_contributor_role",
data=dict(
user_id=contributor_edit.user_id,
role=m.BookContributor.Roles.MODERATOR.value,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
2023-05-11 08:14:22 +00:00
moderator = m.User(username="Moderator", password="test").save()
moderators_book: m.Book = create_test_book(moderator.id)
2023-05-11 08:14:22 +00:00
response: Response = client.post(
f"/book/{moderators_book.id}/add_contributor",
data=dict(user_id=moderator.id, role=m.BookContributor.Roles.MODERATOR),
follow_redirects=True,
)
assert response.status_code == 200
assert b"You do not have permission" in response.data
2023-05-11 08:14:22 +00:00
response: Response = client.post(
2023-06-01 04:59:37 +00:00
"/book/999/add_contributor",
2023-05-11 08:14:22 +00:00
data=dict(user_id=moderator.id, role=m.BookContributor.Roles.MODERATOR),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Book not found!" in response.data
2023-04-26 13:14:57 +00:00
2023-05-25 07:10:22 +00:00
def test_crud_collection(client: FlaskClient):
2023-04-26 13:14:57 +00:00
_, user = login(client)
user: m.User
2023-05-25 07:10:22 +00:00
book = create_test_book(user.id)
2023-04-26 13:14:57 +00:00
response: Response = client.post(
f"/book/{book.id}/create_collection",
data=dict(label="Test Collection #1 Label", about="Test Collection #1 About"),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
response: Response = client.post(
f"/book/{book.id}/create_collection",
data=dict(label="Test Collection #1 Label", about="Test Collection #1 About"),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection label must be unique!" in response.data
2023-05-11 09:32:59 +00:00
response: Response = client.post(
2023-06-01 04:59:37 +00:00
"/book/999/create_collection",
2023-05-11 09:32:59 +00:00
data=dict(label="Test Collection #1 Label", about="Test Collection #1 About"),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Book not found!" in response.data
2023-05-11 09:32:59 +00:00
2023-04-26 13:14:57 +00:00
collection: m.Collection = m.Collection.query.filter_by(
label="Test Collection #1 Label"
).first()
2023-05-25 07:10:22 +00:00
assert collection
assert collection.access_groups
assert len(collection.access_groups) == 2
for access_group in collection.access_groups:
access_group: m.AccessGroup
assert access_group.book_id == collection.version.book_id
2023-04-26 14:57:40 +00:00
m.Collection(
2023-04-28 07:09:42 +00:00
label="Test Collection #2 Label",
version_id=collection.version_id,
2023-04-28 07:10:55 +00:00
parent_id=collection.parent_id,
2023-04-26 14:57:40 +00:00
).save()
2023-04-26 13:14:57 +00:00
response: Response = client.post(
f"/book/{book.id}/{collection.id}/edit",
data=dict(
2023-04-26 14:57:40 +00:00
label="Test Collection #2 Label",
2023-04-26 13:14:57 +00:00
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection label must be unique!" in response.data
new_label = "Test Collection #1 Label(edited)"
new_about = "Test Collection #1 About(edited)"
response: Response = client.post(
f"/book/{book.id}/{collection.id}/edit",
data=dict(
label=new_label,
about=new_about,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
2023-05-11 09:32:59 +00:00
response: Response = client.post(
f"/book/999/{collection.id}/edit",
data=dict(label="Test Collection #1 Label", about="Test Collection #1 About"),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Book not found!" in response.data
2023-05-11 09:32:59 +00:00
2023-04-26 13:14:57 +00:00
edited_collection: m.Collection = m.Collection.query.filter_by(
label=new_label, about=new_about
).first()
assert edited_collection
response: Response = client.post(
2023-04-27 13:35:55 +00:00
f"/book/{book.id}/999/edit",
2023-04-26 13:14:57 +00:00
data=dict(
label=new_label,
about=new_about,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/delete",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
deleted_collection: m.Collection = db.session.get(m.Collection, collection.id)
assert deleted_collection.is_deleted
2023-05-15 14:55:36 +00:00
check_if_nested_collection_entities_is_deleted(deleted_collection)
2023-04-26 13:14:57 +00:00
response: Response = client.post(
f"/book/{book.id}/{collection.id}/delete",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection not found" in response.data
2023-04-27 12:45:13 +00:00
2023-05-11 09:32:59 +00:00
response: Response = client.post(
f"/book/999/{collection.id}/delete",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Book not found!" in response.data
2023-05-11 09:32:59 +00:00
2023-04-27 12:45:13 +00:00
def test_crud_subcollection(client: FlaskClient):
2023-04-27 12:45:13 +00:00
_, user = login(client)
user: m.User
2023-05-25 07:10:22 +00:00
book = create_test_book(user.id)
2023-04-27 12:45:13 +00:00
collection: m.Collection = m.Collection.query.filter_by(
2023-05-02 14:07:37 +00:00
version_id=book.last_version.id,
is_leaf=False,
2023-05-02 14:07:37 +00:00
parent_id=book.last_version.root_collection.id,
).first()
leaf_collection: m.Collection = m.Collection.query.filter_by(
version_id=book.last_version.id,
is_leaf=True,
parent_id=collection.id,
).first()
2023-04-27 12:45:13 +00:00
2023-05-11 09:32:59 +00:00
response: Response = client.post(
f"/book/999/{leaf_collection.id}/create_sub_collection",
data=dict(
label="Test SubCollection #1 Label", about="Test SubCollection #1 About"
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Book not found!" in response.data
2023-05-11 09:32:59 +00:00
2023-04-27 12:45:13 +00:00
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/create_sub_collection",
data=dict(
label="Test SubCollection #1 Label", about="Test SubCollection #1 About"
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"You can't create subcollection for this collection" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/create_sub_collection",
data=dict(
label="Test SubCollection #1 Label", about="Test SubCollection #1 About"
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/create_sub_collection",
2023-04-28 07:09:42 +00:00
data=dict(
label="Test SubCollection #1 Label", about="Test SubCollection #1 About"
),
2023-04-27 12:45:13 +00:00
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection label must be unique!" in response.data
sub_collection: m.Collection = m.Collection.query.filter_by(
label="Test SubCollection #1 Label"
).first()
assert sub_collection
2023-05-30 13:45:32 +00:00
assert not sub_collection.is_leaf
2023-04-28 07:10:55 +00:00
assert sub_collection.parent_id == collection.id
2023-04-27 12:45:13 +00:00
2023-05-25 07:10:22 +00:00
assert sub_collection.access_groups
assert len(sub_collection.access_groups) == 2
for access_group in sub_collection.access_groups:
access_group: m.AccessGroup
assert access_group.book_id == sub_collection.version.book_id
2023-04-27 13:35:55 +00:00
m.Collection(
label="Test SubCollection #2 Label",
version_id=collection.version_id,
2023-04-28 07:10:55 +00:00
parent_id=collection.id,
2023-04-27 13:35:55 +00:00
).save()
response: Response = client.post(
2023-05-29 15:07:40 +00:00
f"/book/{book.id}/{sub_collection.id}/edit",
2023-04-27 13:35:55 +00:00
data=dict(
label="Test SubCollection #2 Label",
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection label must be unique!" in response.data
new_label = "Test SubCollection #1 Label(edited)"
new_about = "Test SubCollection #1 About(edited)"
response: Response = client.post(
2023-05-29 15:07:40 +00:00
f"/book/{book.id}/{sub_collection.id}/edit",
2023-04-27 13:35:55 +00:00
data=dict(
label=new_label,
about=new_about,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
edited_collection: m.Collection = m.Collection.query.filter_by(
label=new_label, about=new_about
).first()
assert edited_collection
response: Response = client.post(
2023-05-29 15:07:40 +00:00
f"/book/{book.id}/9999/edit",
2023-04-27 13:35:55 +00:00
data=dict(
label=new_label,
about=new_about,
),
follow_redirects=True,
)
assert response.status_code == 200
2023-05-29 15:07:40 +00:00
assert b"Collection not found" in response.data
2023-04-27 13:35:55 +00:00
response: Response = client.post(
2023-05-29 15:07:40 +00:00
f"/book/{book.id}/{sub_collection.id}/delete",
2023-04-27 13:35:55 +00:00
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
2023-04-28 07:09:42 +00:00
deleted_collection: m.Collection = db.session.get(m.Collection, sub_collection.id)
2023-04-27 13:35:55 +00:00
assert deleted_collection.is_deleted
2023-05-15 14:55:36 +00:00
check_if_nested_collection_entities_is_deleted(deleted_collection)
2023-04-27 13:35:55 +00:00
response: Response = client.post(
2023-05-29 15:07:40 +00:00
f"/book/{book.id}/{sub_collection.id}/delete",
2023-04-27 13:35:55 +00:00
follow_redirects=True,
)
assert response.status_code == 200
2023-05-29 15:07:40 +00:00
assert b"Collection not found" in response.data
2023-05-02 14:07:37 +00:00
def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
_, user = login(client)
user: m.User
book = create_test_book(user.id)
2023-05-02 14:07:37 +00:00
collection: m.Collection = m.Collection.query.filter_by(
2023-05-02 14:07:37 +00:00
version_id=book.last_version.id,
is_leaf=False,
2023-05-02 14:07:37 +00:00
parent_id=book.last_version.root_collection.id,
).first()
sub_collection: m.Collection = m.Collection.query.filter_by(
2023-05-02 14:07:37 +00:00
version_id=book.last_version.id,
is_leaf=True,
parent_id=collection.id,
).first()
2023-05-02 14:07:37 +00:00
response: Response = client.post(
f"/book/{book.id}/{collection.id}/create_section",
2023-05-02 14:07:37 +00:00
data=dict(
2023-05-02 14:38:27 +00:00
collection_id=collection.id,
2023-05-02 14:07:37 +00:00
label="Test Section",
about="Test Section #1 About",
),
follow_redirects=True,
)
assert b"You can't create section for this collection" in response.data
label_1 = "Test Section #1 Label"
response: Response = client.post(
f"/book/{book.id}/{sub_collection.id}/create_section",
2023-05-02 14:07:37 +00:00
data=dict(
collection_id=sub_collection.id,
2023-05-02 14:07:37 +00:00
label=label_1,
about="Test Section #1 About",
),
follow_redirects=True,
)
assert response.status_code == 200
section: m.Section = m.Section.query.filter_by(
label=label_1, collection_id=sub_collection.id
2023-05-02 14:07:37 +00:00
).first()
assert section
assert section.collection_id == sub_collection.id
2023-05-02 14:07:37 +00:00
assert section.version_id == book.last_version.id
assert not section.interpretations
assert section.access_groups
assert len(section.access_groups) == 2
for access_group in section.access_groups:
access_group: m.AccessGroup
assert access_group.book_id == section.version.book_id
2023-05-02 14:07:37 +00:00
response: Response = client.post(
f"/book/{book.id}/{sub_collection.id}/create_section",
2023-05-02 14:07:37 +00:00
data=dict(
collection_id=sub_collection.id,
2023-05-02 14:07:37 +00:00
label=label_1,
about="Test Section #1 About",
),
follow_redirects=True,
)
assert b"Section label must be unique!" in response.data
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/{sub_collection.id}/create_section",
2023-05-02 14:07:37 +00:00
data=dict(
collection_id=sub_collection.id,
label=label_1,
about="Test Section #1 About",
),
follow_redirects=True,
)
assert response.status_code == 200
section: m.Section = m.Section.query.filter_by(
label=label_1, collection_id=sub_collection.id
).first()
assert section
assert section.collection_id == sub_collection.id
assert section.version_id == book.last_version.id
assert not section.interpretations
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/{sub_collection.id}/create_section",
2023-05-02 14:07:37 +00:00
data=dict(
collection_id=sub_collection.id,
label=label_1,
about="Test Section #1 About",
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section label must be unique!" in response.data
response: Response = client.post(
f"/book/{book.id}/999/create_section",
data=dict(
collection_id=999,
label=label_1,
about="Test Section #1 About",
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection not found" in response.data
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/999/create_section",
2023-05-02 14:07:37 +00:00
data=dict(collection_id=999, label=label_1, about="Test Section #1 About"),
follow_redirects=True,
)
assert response.status_code == 200
2023-05-30 06:51:58 +00:00
assert b"Collection not found" in response.data
2023-05-02 14:07:37 +00:00
# edit
m.Section(
label="Test",
collection_id=sub_collection.id,
2023-05-02 14:07:37 +00:00
version_id=book.last_version.id,
).save()
m.Section(
label="Test",
collection_id=sub_collection.id,
version_id=book.last_version.id,
).save()
section: m.Section = m.Section.query.filter_by(
label=label_1, collection_id=sub_collection.id
2023-05-02 14:07:37 +00:00
).first()
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/{section.id}/edit_section",
2023-05-02 14:07:37 +00:00
data=dict(
section_id=section.id,
label="Test",
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section label must be unique!" in response.data
new_label = "Test Section #1 Label(edited)"
new_about = "Test Section #1 About(edited)"
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/{section.id}/edit_section",
2023-05-02 14:07:37 +00:00
data=dict(section_id=section.id, label=new_label, about=new_about),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
edited_section: m.Section = m.Section.query.filter_by(
2023-05-18 12:44:14 +00:00
label=new_label, id=section.id
2023-05-02 14:07:37 +00:00
).first()
assert edited_section
#
section_2: m.Section = m.Section.query.filter_by(
label="Test Section #1 Label(edited)", collection_id=sub_collection.id
2023-05-02 14:07:37 +00:00
).first()
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/{section_2.id}/edit_section",
2023-05-02 14:07:37 +00:00
data=dict(
section_id=section_2.id,
label="Test",
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section label must be unique!" in response.data
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/{section_2.id}/edit_section",
2023-05-02 14:07:37 +00:00
data=dict(section_id=section_2.id, label=new_label, about=new_about),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
edited_section: m.Section = m.Section.query.filter_by(
2023-05-18 12:44:14 +00:00
label=new_label, id=section_2.id
2023-05-02 14:07:37 +00:00
).first()
assert edited_section
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/999/edit_section",
2023-05-02 14:07:37 +00:00
data=dict(section_id=section_2.id, label=new_label, about=new_about),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section not found" in response.data
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/{section.id}/delete_section",
2023-05-02 14:07:37 +00:00
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
deleted_section: m.Section = db.session.get(m.Section, section.id)
assert deleted_section.is_deleted
2023-05-15 14:55:36 +00:00
check_if_nested_section_entities_is_deleted(deleted_section)
2023-05-02 14:07:37 +00:00
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/{section_2.id}/delete_section",
2023-05-02 14:07:37 +00:00
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
deleted_section: m.Section = db.session.get(m.Section, section_2.id)
assert deleted_section.is_deleted
2023-05-15 14:55:36 +00:00
check_if_nested_section_entities_is_deleted(deleted_section)
2023-05-02 14:07:37 +00:00
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/999/delete_section",
2023-05-02 14:07:37 +00:00
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section not found" in response.data
2023-05-04 11:18:41 +00:00
2023-05-25 09:38:08 +00:00
def test_crud_interpretation(client: FlaskClient):
2023-05-04 11:18:41 +00:00
_, user = login(client)
user: m.User
2023-05-25 09:38:08 +00:00
book = create_test_book(user.id)
2023-05-04 11:18:41 +00:00
2023-05-25 09:38:08 +00:00
collection: m.Collection = m.Collection.query.filter_by(
2023-05-04 11:18:41 +00:00
version_id=book.last_version.id,
is_leaf=True,
parent_id=book.last_version.root_collection.id,
2023-05-25 09:38:08 +00:00
).first()
section_in_collection: m.Section = m.Section.query.filter_by(
collection_id=collection.id,
2023-05-04 11:18:41 +00:00
version_id=book.last_version.id,
2023-05-25 09:38:08 +00:00
).first()
2023-05-04 11:18:41 +00:00
2023-05-25 09:38:08 +00:00
collection: m.Collection = m.Collection.query.filter_by(
version_id=book.last_version.id,
is_leaf=False,
parent_id=book.last_version.root_collection.id,
).first()
sub_collection: m.Collection = m.Collection.query.filter_by(
2023-05-04 11:18:41 +00:00
version_id=book.last_version.id,
is_leaf=True,
2023-05-25 09:38:08 +00:00
parent_id=collection.id,
).first()
section_in_subcollection: m.Section = m.Section.query.filter_by(
2023-05-04 11:18:41 +00:00
collection_id=sub_collection.id,
version_id=book.last_version.id,
2023-05-25 09:38:08 +00:00
).first()
2023-05-04 11:18:41 +00:00
2023-05-04 12:14:17 +00:00
text_1 = "Test Interpretation #1 Text"
2023-05-04 11:18:41 +00:00
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/{section_in_subcollection.id}/create_interpretation",
2023-05-18 12:37:55 +00:00
data=dict(section_id=section_in_subcollection.id, text=text_1),
2023-05-04 11:18:41 +00:00
follow_redirects=True,
)
assert response.status_code == 200
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
2023-05-18 12:37:55 +00:00
section_id=section_in_subcollection.id, text=text_1
2023-05-04 11:18:41 +00:00
).first()
assert interpretation
2023-05-04 12:14:17 +00:00
assert interpretation.section_id == section_in_subcollection.id
2023-05-04 11:18:41 +00:00
assert not interpretation.comments
2023-05-25 09:38:08 +00:00
assert interpretation.access_groups
assert len(interpretation.access_groups) == 2
for access_group in interpretation.access_groups:
access_group: m.AccessGroup
assert access_group.book_id == interpretation.section.version.book_id
2023-05-04 11:18:41 +00:00
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/{section_in_collection.id}/create_interpretation",
2023-05-18 12:37:55 +00:00
data=dict(section_id=section_in_collection.id, text=text_1),
2023-05-04 11:18:41 +00:00
follow_redirects=True,
)
assert response.status_code == 200
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
2023-05-18 12:37:55 +00:00
text=text_1, section_id=section_in_collection.id
2023-05-04 11:18:41 +00:00
).first()
assert interpretation
2023-05-04 12:14:17 +00:00
assert interpretation.section_id == section_in_collection.id
2023-05-04 11:18:41 +00:00
assert not interpretation.comments
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/999/create_section",
2023-05-18 12:37:55 +00:00
data=dict(collection_id=999, text=text_1),
2023-05-04 11:18:41 +00:00
follow_redirects=True,
)
assert response.status_code == 200
2023-05-30 06:51:58 +00:00
assert b"Collection not found" in response.data
2023-05-04 11:18:41 +00:00
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/999/create_interpretation",
2023-05-18 12:37:55 +00:00
data=dict(collection_id=999, text=text_1),
2023-05-04 11:18:41 +00:00
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section not found" in response.data
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/888/create_interpretation",
2023-05-18 12:37:55 +00:00
data=dict(collection_id=999, text=text_1),
2023-05-04 11:18:41 +00:00
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section not found" in response.data
2023-05-04 13:32:56 +00:00
# edit
i_1 = m.Interpretation(
2023-05-18 12:37:55 +00:00
text="Test", section_id=section_in_collection.id, user_id=user.id
2023-05-04 13:32:56 +00:00
).save()
i_2 = m.Interpretation(
2023-05-04 13:32:56 +00:00
text="Test",
section_id=section_in_subcollection.id,
).save()
group = create_moderator_group(book.id)
m.InterpretationAccessGroups(
interpretation_id=i_1.id, access_group_id=group.id
).save()
m.InterpretationAccessGroups(
interpretation_id=i_2.id, access_group_id=group.id
).save()
2023-05-04 13:32:56 +00:00
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
2023-05-18 12:37:55 +00:00
section_id=section_in_collection.id
2023-05-04 13:32:56 +00:00
).first()
new_text = "Test Interpretation #1 Text(edited)"
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/{interpretation.id}/edit_interpretation",
2023-05-04 13:32:56 +00:00
data=dict(
interpretation_id=interpretation.id,
text=new_text,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
edited_interpretation: m.Interpretation = m.Interpretation.query.filter_by(
2023-05-18 12:37:55 +00:00
text=new_text, id=interpretation.id
2023-05-04 13:32:56 +00:00
).first()
assert edited_interpretation
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/999/edit_interpretation",
2023-05-04 13:32:56 +00:00
data=dict(
2023-05-29 08:07:04 +00:00
interpretation_id="999",
2023-05-04 13:32:56 +00:00
text=new_text,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Interpretation not found" in response.data
2023-05-04 11:18:41 +00:00
2023-05-04 13:57:47 +00:00
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/999/delete_interpretation",
2023-05-04 13:57:47 +00:00
follow_redirects=True,
)
2023-05-04 11:18:41 +00:00
2023-05-04 13:57:47 +00:00
assert response.status_code == 200
assert b"Interpretation not found" in response.data
2023-05-04 11:18:41 +00:00
2023-05-04 13:57:47 +00:00
response: Response = client.post(
(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/{section_in_subcollection.interpretations[0].id}/delete_interpretation"
2023-05-04 13:57:47 +00:00
),
2023-05-29 08:07:04 +00:00
data=dict(interpretation_id=section_in_subcollection.interpretations[0].id),
2023-05-04 13:57:47 +00:00
follow_redirects=True,
)
2023-05-04 11:18:41 +00:00
2023-05-04 13:57:47 +00:00
assert response.status_code == 200
assert b"Success!" in response.data
2023-05-04 11:18:41 +00:00
2023-05-04 13:57:47 +00:00
deleted_interpretation: m.Interpretation = db.session.get(
m.Interpretation, section_in_subcollection.interpretations[0].id
)
assert deleted_interpretation.is_deleted
2023-05-15 14:55:36 +00:00
check_if_nested_interpretation_entities_is_deleted(deleted_interpretation)
2023-05-04 11:18:41 +00:00
2023-05-04 13:57:47 +00:00
response: Response = client.post(
(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/{section_in_collection.interpretations[0].id}/delete_interpretation"
2023-05-04 13:57:47 +00:00
),
2023-05-31 08:50:51 +00:00
data=dict(interpretation_id=section_in_subcollection.interpretations[0].id),
2023-05-04 13:57:47 +00:00
follow_redirects=True,
)
assert response.status_code == 200
2023-05-09 11:37:09 +00:00
def test_crud_comment(client: FlaskClient, runner: FlaskCliRunner):
_, user = login(client)
user: m.User
# add dummmy data
runner.invoke(args=["db-populate"])
book: m.Book = db.session.get(m.Book, 1)
book.user_id = user.id
book.save()
leaf_collection: m.Collection = m.Collection(
label="Test Leaf Collection #1 Label",
version_id=book.last_version.id,
is_leaf=True,
parent_id=book.last_version.root_collection.id,
).save()
2023-06-01 04:59:37 +00:00
m.Section(
2023-05-09 11:37:09 +00:00
label="Test Section in Collection #1 Label",
collection_id=leaf_collection.id,
version_id=book.last_version.id,
).save()
collection: m.Collection = m.Collection(
label="Test Collection #1 Label", version_id=book.last_version.id
).save()
sub_collection: m.Collection = m.Collection(
label="Test SubCollection #1 Label",
version_id=book.last_version.id,
parent_id=collection.id,
is_leaf=True,
).save()
section_in_subcollection: m.Section = m.Section(
label="Test Section in Subcollection #1 Label",
collection_id=sub_collection.id,
version_id=book.last_version.id,
).save()
group = create_moderator_group(book.id)
m.SectionAccessGroups(
section_id=section_in_subcollection.id, access_group_id=group.id
).save()
2023-05-09 11:37:09 +00:00
label_1 = "Test Interpretation #1 Label"
text_1 = "Test Interpretation #1 Text"
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/{section_in_subcollection.id}/create_interpretation",
2023-05-09 11:37:09 +00:00
data=dict(section_id=section_in_subcollection.id, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
2023-05-18 12:37:55 +00:00
section_id=section_in_subcollection.id
2023-05-09 11:37:09 +00:00
).first()
assert interpretation
assert interpretation.section_id == section_in_subcollection.id
assert not interpretation.comments
comment_text = "Some comment text"
response: Response = client.post(
2023-05-30 07:14:59 +00:00
f"/book/{book.id}/{interpretation.id}/create_comment",
2023-05-09 11:37:09 +00:00
data=dict(
text=comment_text,
interpretation_id=interpretation.id,
),
follow_redirects=True,
)
assert response
assert response.status_code == 200
assert b"Success" in response.data
assert str.encode(comment_text) in response.data
comment: m.Comment = m.Comment.query.filter_by(text=comment_text).first()
assert comment
2023-05-09 14:32:07 +00:00
new_text = "Some new text"
# edit
response: Response = client.post(
2023-05-30 07:24:25 +00:00
f"/book/{book.id}/{interpretation.id}/comment_edit",
2023-05-09 14:32:07 +00:00
data=dict(
text=new_text,
interpretation_id=interpretation.id,
comment_id=comment.id,
),
follow_redirects=True,
)
assert response
assert response.status_code == 200
assert b"Success" in response.data
assert str.encode(new_text) in response.data
assert str.encode(comment_text) not in response.data
2023-05-09 11:37:09 +00:00
# delete
response: Response = client.post(
2023-05-30 07:24:25 +00:00
f"/book/{book.id}/{interpretation.id}/comment_delete",
2023-05-09 11:37:09 +00:00
data=dict(
text=comment_text,
interpretation_id=interpretation.id,
comment_id=comment.id,
),
follow_redirects=True,
)
assert response
assert response.status_code == 200
assert b"Success" in response.data
assert str.encode(comment_text) not in response.data
2023-05-11 08:08:19 +00:00
def test_access_to_settings_page(client: FlaskClient):
_, user = login(client)
2023-05-17 13:34:20 +00:00
book_1 = m.Book(label="test", about="test", user_id=user.id).save()
2023-05-15 09:01:44 +00:00
m.BookVersion(semver="1.0.0", book_id=book_1.id).save()
2023-05-11 08:08:19 +00:00
book_2 = m.Book(label="test", about="test", user_id=user.id).save()
2023-05-15 09:01:44 +00:00
m.BookVersion(semver="1.0.0", book_id=book_2.id).save()
2023-05-11 08:08:19 +00:00
response: Response = client.get(
f"/book/{book_1.id}/settings",
follow_redirects=True,
)
assert response.status_code == 200
response: Response = client.get(
f"/book/{book_2.id}/settings",
follow_redirects=True,
)
assert response.status_code == 200
logout(client)
response: Response = client.get(
f"/book/{book_2.id}/settings",
follow_redirects=True,
)
assert response.status_code == 200
assert b"You do not have permission" in response.data
2023-05-12 06:55:10 +00:00
def test_interpretation_in_home_last_inter_section(
client: FlaskClient, runner: FlaskCliRunner
):
_, user = login(client)
user: m.User
# add dummmy data
runner.invoke(args=["db-populate"])
book: m.Book = db.session.get(m.Book, 1)
book.user_id = user.id
book.save()
leaf_collection: m.Collection = m.Collection(
label="Test Leaf Collection #1 Label",
version_id=book.last_version.id,
is_leaf=True,
parent_id=book.last_version.root_collection.id,
).save()
section_in_collection: m.Section = m.Section(
label="Test Section in Collection #1 Label",
collection_id=leaf_collection.id,
version_id=book.last_version.id,
).save()
collection: m.Collection = m.Collection(
label="Test Collection #1 Label", version_id=book.last_version.id
).save()
sub_collection: m.Collection = m.Collection(
label="Test SubCollection #1 Label",
version_id=book.last_version.id,
parent_id=collection.id,
is_leaf=True,
).save()
section_in_subcollection: m.Section = m.Section(
label="Test Section in Subcollection #1 Label",
collection_id=sub_collection.id,
version_id=book.last_version.id,
).save()
group = create_moderator_group(book.id)
m.SectionAccessGroups(
section_id=section_in_subcollection.id, access_group_id=group.id
).save()
m.SectionAccessGroups(
section_id=section_in_collection.id, access_group_id=group.id
).save()
2023-05-12 06:55:10 +00:00
label_1 = "Test Interpretation no1 Label"
text_1 = "Test Interpretation no1 Text"
2023-05-12 06:55:10 +00:00
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/{section_in_subcollection.id}/create_interpretation",
2023-05-12 06:55:10 +00:00
data=dict(section_id=section_in_subcollection.id, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
2023-05-18 12:37:55 +00:00
section_id=section_in_subcollection.id
2023-05-12 06:55:10 +00:00
).first()
assert interpretation
assert interpretation.section_id == section_in_subcollection.id
assert not interpretation.comments
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/{section_in_collection.id}/create_interpretation",
2023-05-12 06:55:10 +00:00
data=dict(section_id=section_in_collection.id, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
2023-05-18 12:37:55 +00:00
section_id=section_in_collection.id
2023-05-12 06:55:10 +00:00
).first()
assert interpretation
assert interpretation.section_id == section_in_collection.id
assert not interpretation.comments
response: Response = client.post(
2023-05-30 06:51:58 +00:00
f"/book/{book.id}/999/create_section",
2023-05-12 06:55:10 +00:00
data=dict(collection_id=999, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
2023-05-30 06:51:58 +00:00
assert b"Collection not found" in response.data
2023-05-12 06:55:10 +00:00
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/999/create_interpretation",
2023-05-12 06:55:10 +00:00
data=dict(collection_id=999, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section not found" in response.data
response: Response = client.post(
2023-05-30 07:12:43 +00:00
f"/book/{book.id}/888/create_interpretation",
2023-05-12 06:55:10 +00:00
data=dict(collection_id=999, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section not found" in response.data
response: Response = client.get(
2023-06-01 04:59:37 +00:00
"/home",
2023-05-12 06:55:10 +00:00
follow_redirects=True,
)
assert response
assert response.status_code == 200
assert str.encode(text_1) in response.data