-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(post): simplification du sujet des Post
#748
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
import pytest # noqa | ||
from django.conf import settings | ||
from django.db.models import F | ||
from django.forms import HiddenInput | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
from faker import Faker | ||
from machina.conf import settings as machina_settings | ||
|
||
from lacommunaute.forum_conversation.factories import PostFactory, TopicFactory | ||
from lacommunaute.forum.factories import ForumFactory | ||
from lacommunaute.forum_conversation.factories import ( | ||
AnonymousPostFactory, | ||
AnonymousTopicFactory, | ||
PostFactory, | ||
TopicFactory, | ||
) | ||
from lacommunaute.forum_conversation.forms import PostForm | ||
from lacommunaute.forum_conversation.models import Topic | ||
from lacommunaute.users.factories import UserFactory | ||
from taggit.models import Tag | ||
|
||
|
||
faker = Faker(settings.LANGUAGE_CODE) | ||
|
@@ -37,19 +46,277 @@ def test_post_subject_comes_from_topic_subject(self): | |
post = form.create_post() | ||
self.assertEqual( | ||
post.subject, | ||
f"{machina_settings.TOPIC_ANSWER_SUBJECT_PREFIX} {self.topic.subject}", | ||
self.topic.subject, | ||
) | ||
|
||
def test_reply_as_authenticated_user(self): | ||
post = PostFactory(topic=self.topic, poster=self.user) | ||
form = PostForm( | ||
data=self.form_data, | ||
user=self.user, | ||
forum=self.forum, | ||
topic=self.topic, | ||
instance=post, | ||
|
||
@pytest.fixture(name="public_forum") | ||
def fixture_public_forum(): | ||
return ForumFactory(with_public_perms=True) | ||
|
||
|
||
def get_create_topic_url(forum): | ||
return ( | ||
reverse("forum_conversation:topic_create", kwargs={"forum_pk": forum.pk, "forum_slug": forum.slug}) | ||
+ "?checked" | ||
) | ||
|
||
|
||
def get_update_topic_url(topic): | ||
return reverse( | ||
"forum_conversation:topic_update", | ||
kwargs={"forum_slug": topic.forum.slug, "forum_pk": topic.forum.pk, "pk": topic.pk, "slug": topic.slug}, | ||
) | ||
|
||
|
||
def get_reply_topic_url(topic): | ||
return reverse( | ||
"forum_conversation_extension:post_create", | ||
kwargs={"forum_slug": topic.forum.slug, "forum_pk": topic.forum.pk, "pk": topic.pk, "slug": topic.slug}, | ||
) | ||
|
||
|
||
def get_post_update_url(post): | ||
return reverse( | ||
"forum_conversation:post_update", | ||
kwargs={ | ||
"forum_slug": post.topic.forum.slug, | ||
"forum_pk": post.topic.forum.pk, | ||
"topic_pk": post.topic.pk, | ||
"topic_slug": post.topic.slug, | ||
"pk": post.pk, | ||
}, | ||
) | ||
|
||
|
||
superuser_hidden_fields = { | ||
"poll-TOTAL_FORMS": 2, | ||
"poll-INITIAL_FORMS": 0, | ||
"attachment-TOTAL_FORMS": 1, | ||
"attachment-INITIAL_FORMS": 0, | ||
} | ||
|
||
|
||
class TestTopicForm: | ||
def test_create_topic_as_anonymous(self, db, client, public_forum): | ||
username = faker.email() | ||
|
||
response = client.post( | ||
get_create_topic_url(public_forum), | ||
data={"subject": "Test", "content": faker.sentences(2), "username": username}, | ||
) | ||
form.update_post(post) | ||
self.assertFalse(post.username) | ||
self.assertEqual(post.updated_by, self.user) | ||
self.assertEqual(post.updates_count, F("updates_count") + 1) | ||
assert response.status_code == 302 | ||
|
||
topic = Topic.objects.get() | ||
assert topic.first_post.username == username | ||
assert topic.poster is None | ||
assert topic.first_post.poster is None | ||
assert topic.first_post.updates_count == 0 | ||
assert topic.first_post.updated_by is None | ||
|
||
def test_update_anonymous_topic_as_self(self, db, client, public_forum): | ||
topic = AnonymousTopicFactory(forum=public_forum, with_post=True) | ||
username = topic.first_post.username | ||
session = client.session | ||
session["anonymous_topic"] = topic.first_post.anonymous_key | ||
|
||
data = {"subject": faker.word(), "content": faker.sentences(2), "username": username} | ||
response = client.post(get_update_topic_url(topic), data=data) | ||
assert response.status_code == 302 | ||
|
||
topic.refresh_from_db() | ||
assert topic.first_post.username == username | ||
assert topic.poster is None | ||
assert topic.first_post.poster is None | ||
assert topic.first_post.updates_count == 0 # surprisingly, this is not incremented | ||
assert topic.first_post.updated_by is None # updated_by is a FK on User | ||
|
||
def test_update_anonymous_topic_as_superuser(self, db, client, public_forum): | ||
topic = AnonymousTopicFactory(forum=public_forum, with_post=True) | ||
username = topic.first_post.username | ||
superuser = UserFactory(is_superuser=True) | ||
client.force_login(superuser) | ||
|
||
data = { | ||
"subject": faker.word(), | ||
"content": faker.sentences(2), | ||
**superuser_hidden_fields, | ||
} | ||
response = client.post(get_update_topic_url(topic), data=data) | ||
assert response.status_code == 302 | ||
|
||
topic.refresh_from_db() | ||
assert topic.first_post.username == username | ||
assert topic.poster is None | ||
assert topic.first_post.poster is None | ||
assert topic.first_post.updates_count == 1 | ||
assert topic.first_post.updated_by == superuser | ||
|
||
def test_create_topic_as_authenticated(self, db, client, public_forum): | ||
user = UserFactory() | ||
client.force_login(user) | ||
|
||
response = client.post( | ||
get_create_topic_url(public_forum), data={"subject": "Test", "content": faker.sentences(2)} | ||
) | ||
assert response.status_code == 302 | ||
|
||
topic = Topic.objects.get() | ||
assert topic.poster == user | ||
assert topic.first_post.poster == user | ||
assert topic.first_post.username is None | ||
assert topic.first_post.updates_count == 0 | ||
assert topic.first_post.updated_by is None | ||
|
||
def test_update_authenticated_topic_as_self(self, db, client, public_forum): | ||
topic = TopicFactory(forum=public_forum, with_post=True) | ||
user = topic.poster | ||
client.force_login(user) | ||
|
||
data = {"subject": faker.word(), "content": faker.sentences(2)} | ||
response = client.post(get_update_topic_url(topic), data=data) | ||
assert response.status_code == 302 | ||
|
||
topic.refresh_from_db() | ||
assert topic.first_post.username is None | ||
assert topic.poster == user | ||
assert topic.first_post.poster == user | ||
assert topic.first_post.updates_count == 1 | ||
assert topic.first_post.updated_by == user | ||
|
||
def test_update_authenticated_topic_as_superuser(self, db, client, public_forum): | ||
topic = TopicFactory(forum=public_forum, with_post=True) | ||
user = topic.poster | ||
superuser = UserFactory(is_superuser=True) | ||
client.force_login(superuser) | ||
|
||
data = { | ||
"subject": faker.word(), | ||
"content": faker.sentences(2), | ||
**superuser_hidden_fields, | ||
} | ||
response = client.post(get_update_topic_url(topic), data=data) | ||
assert response.status_code == 302 | ||
|
||
topic.refresh_from_db() | ||
assert topic.first_post.username is None | ||
assert topic.poster == user | ||
assert topic.first_post.poster == user | ||
assert topic.first_post.updates_count == 1 | ||
assert topic.first_post.updated_by == superuser | ||
|
||
def test_init_tags_when_creating_topic(self, db, client, public_forum): | ||
response = client.get(get_create_topic_url(public_forum)) | ||
assert response.status_code == 200 | ||
assert response.context_data["post_form"].fields["tags"].initial is None | ||
|
||
def test_init_tags_when_updating_topic(self, db, client, public_forum): | ||
topic = TopicFactory(forum=public_forum, with_post=True) | ||
client.force_login(topic.poster) | ||
response = client.get(get_update_topic_url(topic)) | ||
assert response.status_code == 200 | ||
assert set(response.context_data["post_form"].fields["tags"].initial) == set(Tag.objects.none()) | ||
|
||
def test_init_tags_when_updating_tagged_topic(self, db, client, public_forum): | ||
topic = TopicFactory(forum=public_forum, with_post=True, with_tags=[faker.word() for _ in range(2)]) | ||
client.force_login(topic.poster) | ||
|
||
response = client.get(get_update_topic_url(topic)) | ||
assert response.status_code == 200 | ||
assert set(response.context_data["post_form"].fields["tags"].initial) == set(Tag.objects.all()) | ||
|
||
|
||
class TestPostForm: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beaux tests :) |
||
def test_reply_as_anonymous(self, db, client, public_forum): | ||
topic = TopicFactory(forum=public_forum, with_post=True) | ||
username = faker.email() | ||
|
||
response = client.post(get_reply_topic_url(topic), data={"content": faker.sentences(2), "username": username}) | ||
assert response.status_code == 200 # htmx view | ||
|
||
topic.refresh_from_db() | ||
post = topic.posts.last() | ||
assert post.username == username | ||
assert post.poster is None | ||
assert post.updates_count == 0 | ||
assert post.updated_by is None | ||
|
||
def test_update_anonymous_reply_as_self(self, db, client, public_forum): | ||
post = AnonymousPostFactory(topic=TopicFactory(forum=public_forum, with_post=True)) | ||
username = post.username | ||
session = client.session | ||
session["anonymous_post"] = post.anonymous_key | ||
|
||
data = {"content": faker.sentences(2), "username": username} | ||
response = client.post(get_post_update_url(post), data=data) | ||
assert response.status_code == 302 | ||
|
||
post.refresh_from_db() | ||
assert post.username == username | ||
assert post.poster is None | ||
assert post.updates_count == 0 # surprisingly, this is not incremented | ||
assert post.updated_by is None | ||
|
||
def test_update_anonymous_reply_as_superuser(self, db, client, public_forum): | ||
post = AnonymousPostFactory(topic=TopicFactory(forum=public_forum, with_post=True)) | ||
username = post.username | ||
superuser = UserFactory(is_superuser=True) | ||
client.force_login(superuser) | ||
|
||
data = {"content": faker.sentences(2), **superuser_hidden_fields} | ||
response = client.post(get_post_update_url(post), data=data) | ||
assert response.status_code == 302 | ||
|
||
post.refresh_from_db() | ||
assert post.username == username | ||
assert post.poster is None | ||
assert post.updates_count == 1 | ||
assert post.updated_by == superuser | ||
|
||
def test_reply_as_authenticated(self, db, client, public_forum): | ||
topic = TopicFactory(forum=public_forum, with_post=True) | ||
user = UserFactory() | ||
client.force_login(user) | ||
|
||
response = client.post(get_reply_topic_url(topic), data={"content": faker.sentences(2)}) | ||
assert response.status_code == 200 | ||
|
||
topic.refresh_from_db() | ||
assert topic.posts.count() == 2 | ||
|
||
post = topic.posts.last() | ||
assert post.username is None | ||
assert post.poster == user | ||
assert post.updates_count == 0 | ||
assert post.updated_by is None | ||
|
||
def test_update_authenticated_reply_as_self(self, db, client, public_forum): | ||
post = PostFactory(topic=TopicFactory(forum=public_forum, with_post=True)) | ||
user = post.poster | ||
client.force_login(user) | ||
|
||
data = {"content": faker.sentences(2)} | ||
response = client.post(get_post_update_url(post), data=data) | ||
assert response.status_code == 302 | ||
|
||
post.refresh_from_db() | ||
assert post.username is None | ||
assert post.poster == user | ||
assert post.updates_count == 1 | ||
assert post.updated_by == user | ||
|
||
def test_update_authenticated_reply_as_superuser(self, db, client, public_forum): | ||
post = PostFactory(topic=TopicFactory(forum=public_forum, with_post=True)) | ||
user = post.poster | ||
superuser = UserFactory(is_superuser=True) | ||
client.force_login(superuser) | ||
|
||
data = {"content": faker.sentences(2), **superuser_hidden_fields} | ||
response = client.post(get_post_update_url(post), data=data) | ||
assert response.status_code == 302 | ||
|
||
post.refresh_from_db() | ||
assert post.username is None | ||
assert post.poster == user | ||
assert post.updates_count == 1 | ||
assert post.updated_by == superuser |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Je vois que il y a un test qui confirme on peut enlever ce code