open-law/tests/test_upvote.py

206 lines
5.3 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
from tests.utils import login
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"
interpretation = m.Interpretation(
text="Test Interpretation 1 Text",
user_id=user.id,
).save()
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