open-law/tests/test_book.py

456 lines
13 KiB
Python
Raw Normal View History

2023-04-24 06:57:38 +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
2023-04-24 06:57:38 +00:00
from tests.utils import login
2023-05-01 15:17:46 +00:00
def test_create_edit_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
book = m.Book.query.filter_by(label=BOOK_NAME).first()
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
book = m.Book.query.filter_by(label=BOOK_NAME).first()
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
book = m.Book.query.filter_by(label=BOOK_NAME).first()
assert book
2023-04-24 07:20:31 +00:00
assert book.versions
assert len(book.versions) == 1
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
response: Response = client.post(
f"/book/{book.id}/edit",
data=dict(
2023-05-02 06:48:10 +00:00
book_id=book.id,
2023-05-01 15:17:46 +00:00
label=BOOK_NAME,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Book label must be unique!" in response.data
response: Response = client.post(
f"/book/{book.id}/edit",
data=dict(
2023-05-02 06:48:10 +00:00
book_id=book.id,
2023-05-01 15:17:46 +00:00
label=BOOK_NAME + " EDITED",
),
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-04-24 07:20:31 +00:00
2023-04-24 08:30:44 +00:00
def test_add_contributor(client: FlaskClient):
_, 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-04-24 08:30:44 +00:00
moderators_book = m.Book(label="Test Book", user_id=moderator.id).save()
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 are not owner of this book!" in response.data
2023-04-24 07:20:31 +00:00
2023-04-24 08:30:44 +00:00
book = m.Book(label="Test Book", user_id=user.id).save()
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
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) == 1
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) == 2
2023-04-25 08:56:51 +00:00
def test_delete_contributor(client: FlaskClient, runner: FlaskCliRunner):
_, user = login(client)
user: m.User
# add dummmy data
runner.invoke(args=["db-populate"])
book = db.session.get(m.Book, 1)
book.user_id = user.id
book.save()
contributors_len = len(book.contributors)
assert contributors_len
contributor_to_delete = book.contributors[0]
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"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
def test_edit_contributor_role(client: FlaskClient, runner: FlaskCliRunner):
_, user = login(client)
user: m.User
# add dummmy data
runner.invoke(args=["db-populate"])
book = db.session.get(m.Book, 1)
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-04-26 13:14:57 +00:00
def test_crud_collection(client: FlaskClient, runner: FlaskCliRunner):
_, user = login(client)
user: m.User
# add dummmy data
runner.invoke(args=["db-populate"])
book = db.session.get(m.Book, 1)
book.user_id = user.id
book.save()
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
collection: m.Collection = m.Collection.query.filter_by(
label="Test Collection #1 Label"
).first()
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
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
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
def test_crud_subcollection(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.versions[-1].id,
is_leaf=True,
2023-04-28 07:10:55 +00:00
parent_id=book.versions[-1].root_collection.id,
2023-04-27 12:45:13 +00:00
).save()
collection: m.Collection = m.Collection(
label="Test Collection #1 Label", version_id=book.versions[-1].id
).save()
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
assert 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-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(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit",
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(
f"/book/{book.id}/{collection.id}/{sub_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
edited_collection: m.Collection = m.Collection.query.filter_by(
label=new_label, about=new_about
).first()
assert edited_collection
response: Response = client.post(
f"/book/{book.id}/{collection.id}/9999/edit",
data=dict(
label=new_label,
about=new_about,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"SubCollection not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
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
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection not found" in response.data