open-law/tests/test_upvote.py

219 lines
5.7 KiB
Python
Raw Normal View History

2023-05-09 14:56:29 +00:00
from flask import current_app as Response
from flask.testing import FlaskClient
from app import models as m
2023-06-08 14:36:40 +00:00
from tests.utils import (
login,
create_interpretation,
create_section,
create_book,
create_collection,
)
2023-05-09 14:56:29 +00:00
2023-05-10 08:43:17 +00:00
def test_upvote_interpretation(client: FlaskClient):
2023-05-09 14:56:29 +00:00
_, user = login(client)
response: Response = client.post(
"/vote/interpretation/999",
headers={"Content-Type": "application/json"},
json=dict(
2023-05-09 14:56:29 +00:00
positive=True,
),
follow_redirects=True,
)
assert response
assert response.status_code == 404
assert response.json["message"] == "Interpretation not found"
2023-06-08 14:36:40 +00:00
book = create_book(client)
assert book
collection, _ = create_collection(client=client, book_id=book.id)
assert collection
section, _ = create_section(
client=client, book_id=book.id, collection_id=collection.id
)
assert section
interpretation, _ = create_interpretation(
client=client, book_id=book.id, section_id=section.id
)
2023-05-09 14:56:29 +00:00
assert interpretation.vote_count == 0
response: Response = client.post(
f"/vote/interpretation/{interpretation.id}",
headers={"Content-Type": "application/json"},
json=dict(
2023-05-09 14:56:29 +00:00
positive=True,
),
follow_redirects=True,
)
assert response
assert response.status_code == 200
json = response.json
assert json
assert "vote_count" in json
assert json["vote_count"] == 1
2023-05-10 12:07:07 +00:00
assert "current_user_vote" in json
assert json["current_user_vote"]
2023-05-09 14:56:29 +00:00
assert interpretation.vote_count == 1
response: Response = client.post(
f"/vote/interpretation/{interpretation.id}",
headers={"Content-Type": "application/json"},
json=dict(
2023-05-09 14:56:29 +00:00
positive=True,
),
follow_redirects=True,
)
assert response
assert response.status_code == 200
json = response.json
assert json
assert "vote_count" in json
assert json["vote_count"] == 0
2023-05-10 12:07:07 +00:00
assert "current_user_vote" in json
assert json["current_user_vote"] is None
2023-05-09 14:56:29 +00:00
assert interpretation.vote_count == 0
response: Response = client.post(
f"/vote/interpretation/{interpretation.id}",
headers={"Content-Type": "application/json"},
json=dict(
2023-05-09 14:56:29 +00:00
positive=False,
),
follow_redirects=True,
)
assert response
assert response.status_code == 200
json = response.json
assert json
assert "vote_count" in json
assert json["vote_count"] == -1
2023-05-10 12:07:07 +00:00
assert "current_user_vote" in json
assert not json["current_user_vote"]
2023-05-09 14:56:29 +00:00
assert interpretation.vote_count == -1
response: Response = client.post(
f"/vote/interpretation/{interpretation.id}",
headers={"Content-Type": "application/json"},
json=dict(
2023-05-10 08:43:17 +00:00
positive=False,
2023-05-09 14:56:29 +00:00
),
follow_redirects=True,
)
assert response
assert response.status_code == 200
json = response.json
assert json
assert "vote_count" in json
assert json["vote_count"] == 0
2023-05-10 12:07:07 +00:00
assert "current_user_vote" in json
assert json["current_user_vote"] is None
2023-05-09 14:56:29 +00:00
assert interpretation.vote_count == 0
2023-05-10 08:43:17 +00:00
def test_upvote_comment(client: FlaskClient):
_, user = login(client)
response: Response = client.post(
"/vote/comment/999",
headers={"Content-Type": "application/json"},
json=dict(
2023-05-10 08:43:17 +00:00
positive=True,
),
follow_redirects=True,
)
assert response
assert response.status_code == 404
assert response.json["message"] == "Comment not found"
comment = m.Comment(
text="Test Comment 1 Text",
user_id=user.id,
).save()
assert comment.vote_count == 0
response: Response = client.post(
f"/vote/comment/{comment.id}",
headers={"Content-Type": "application/json"},
json=dict(
2023-05-10 08:43:17 +00:00
positive=True,
),
follow_redirects=True,
)
assert response
assert response.status_code == 200
json = response.json
assert json
assert "vote_count" in json
assert json["vote_count"] == 1
2023-05-10 12:07:07 +00:00
assert "current_user_vote" in json
assert json["current_user_vote"]
2023-05-10 08:43:17 +00:00
assert comment.vote_count == 1
response: Response = client.post(
f"/vote/comment/{comment.id}",
headers={"Content-Type": "application/json"},
json=dict(
2023-05-10 08:43:17 +00:00
positive=True,
),
follow_redirects=True,
)
assert response
assert response.status_code == 200
json = response.json
assert json
assert "vote_count" in json
assert json["vote_count"] == 0
2023-05-10 12:07:07 +00:00
assert "current_user_vote" in json
assert json["current_user_vote"] is None
2023-05-10 08:43:17 +00:00
assert comment.vote_count == 0
response: Response = client.post(
f"/vote/comment/{comment.id}",
headers={"Content-Type": "application/json"},
json=dict(
2023-05-10 08:43:17 +00:00
positive=False,
),
follow_redirects=True,
)
assert response
assert response.status_code == 200
json = response.json
assert json
assert "vote_count" in json
assert json["vote_count"] == -1
2023-05-10 12:07:07 +00:00
assert "current_user_vote" in json
assert not json["current_user_vote"]
2023-05-10 08:43:17 +00:00
assert comment.vote_count == -1
response: Response = client.post(
f"/vote/comment/{comment.id}",
headers={"Content-Type": "application/json"},
json=dict(
2023-05-10 08:43:17 +00:00
positive=False,
),
follow_redirects=True,
)
assert response
assert response.status_code == 200
json = response.json
assert json
assert "vote_count" in json
assert json["vote_count"] == 0
2023-05-10 12:07:07 +00:00
assert "current_user_vote" in json
assert json["current_user_vote"] is None
2023-05-10 08:43:17 +00:00
assert comment.vote_count == 0