<commit_before>from . import models
from content import models as content
from core import tests
import entities.models
from features.associations import models as associations
from features.gestalten import tests as gestalten
from features.groups import tests as groups
from features.memberships import test_mixins as memberships
# from features.subscriptions import test_mixins as subscriptions


class Conversation(gestalten.GestaltMixin, groups.GroupMixin):
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        conversation = models.Conversation.objects.create(subject='Test Thema')
        conversation.texts.create(author=cls.gestalt, text='Test Text')
        cls.association = associations.Association.objects.create(
                container=conversation, entity=cls.group)


class ExternalConversationMixin(memberships.MemberMixin, gestalten.OtherGestaltMixin):
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        cls.content = content.Article.objects.create(
                author=cls.other_gestalt, public=False, title='Test Message')
        entities.models.GroupContent.objects.create(content=cls.content, group=cls.group)


class MessageMixin:
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        cls.content = content.Article.objects.create(
                author=cls.gestalt, public=False, title='Test Message')


class NoNotificationOnExternalConversation:
    def test_external_conversation(self):
        conversation = content.Article.objects.create(
                author=self.other_gestalt, public=False, title='Test Message')
        entities.models.GroupContent.objects.create(content=conversation, group=self.group)
        self.assertNoNotificationSent()


class GroupPageHasCreateLink:
    def test_group_page_creation(self):
        response = self.client.get(self.group.get_absolute_url())
        self.assertContainsLink(response, 'message-create', key=self.group.pk)


class CanCreateConversationWithEmail:
    def test_create_conversation(self):
        response = self.client.get(self.get_url('message-create', key=self.group.pk))
        self.assertEqual(response.status_code, 200)
        response = self.client.post(
                self.get_url('message-create', key=self.group.pk),
                {
                    'author': '<EMAIL>',
                    'subject': 'Temp Test Thema',
                    'text': 'Test Text',
                })
        self.assertExists(associations.Association, conversation__subject='Temp Test Thema')
        self.assertRedirects(response, self.group.get_absolute_url())


class CanCreateConversation:
    def test_create_conversation(self):
        response = self.client.get(self.get_url('message-create', key=self.group.pk))
        self.assertEqual(response.status_code, 200)
        response = self.client.post(
                self.get_url('message-create', key=self.group.pk),
                {
                    'subject': 'Temp Test Thema',
                    'text': 'Test Text',
                })
        self.assertExists(associations.Association, conversation__subject='Temp Test Thema')
        association = associations.Association.objects.get(
                conversation__subject='Temp Test Thema')
        self.assertRedirects(response, self.get_url('conversation', key=association.pk))


class GroupPageHasConversationLink:
    def test_group_page_conversation(self):
        response = self.client.get(self.group.get_absolute_url())
        self.assertContainsLink(response, 'conversation', key=self.association.pk)


class GroupPageDoesNotHaveConversationLink:
    def test_group_page_conversation(self):
        response = self.client.get(self.group.get_absolute_url())
        self.assertNotContainsLink(response, 'conversation', key=self.association.pk)


class CanViewConversation:
    def test_view_conversation(self):
        response = self.client.get(self.get_url('conversation', key=self.association.pk))
        self.assertEquals(response.status_code, 200)


class CanNotViewConversation:
    def test_view_conversation(self):
        conversation_url = self.get_url('conversation', key=self.association.pk)
        response = self.client.get(conversation_url)
        self.assertForbiddenOrLogin(response, conversation_url)


class OtherGestaltCanNotViewConversation:
    def test_view_conversation_as_other(self):
        self.client.logout()
        self.client.force_login(self.other_gestalt.user)
        conversation_url = self.get_url('conversation', key=self.association.pk)
        response = self.client.get(conversation_url)
        self.assertForbiddenOrLogin(response, conversation_url)


class Anonymous(
        GroupPageHasCreateLink,
        CanCreateConversationWithEmail,
        GroupPageDoesNotHaveConversationLink,
        CanNotViewConversation,
        Conversation, tests.Test):
    '''
    An anonymous visitor
    * should see a message creation link on the group page
    '''


class Authenticated(
        GroupPageHasCreateLink,
        CanCreateConversation,
        GroupPageHasConversationLink,
        CanViewConversation,
        Conversation, gestalten.AuthenticatedMixin, tests.Test):
    '''
    An authenticated visitor
    * should see a message creation link on the group page
    '''


class GroupMember(
        GroupPageHasCreateLink,
        CanCreateConversation,
        GroupPageHasConversationLink,
        CanViewConversation,
        OtherGestaltCanNotViewConversation,
        Conversation, memberships.MemberMixin, gestalten.OtherGestaltMixin, tests.Test):
    '''
    A group member
    * should see a message creation link on the group page
    '''

<commit_msg>Add view conversation tests<commit_after>from . import models
from content import models as content
from core import tests
import entities.models
from features.associations import models as associations
from features.gestalten import tests as gestalten
from features.groups import tests as groups
from features.memberships import test_mixins as memberships
# from features.subscriptions import test_mixins as subscriptions


class Conversation(gestalten.GestaltMixin, groups.GroupMixin):
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        conversation = models.Conversation.objects.create(subject='Test Thema')
        conversation.texts.create(author=cls.gestalt, text='Test Text')
        cls.association = associations.Association.objects.create(
                container=conversation, entity=cls.group)


class ExternalConversationMixin(memberships.MemberMixin, gestalten.OtherGestaltMixin):
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        cls.content = content.Article.objects.create(
                author=cls.other_gestalt, public=False, title='Test Message')
        entities.models.GroupContent.objects.create(content=cls.content, group=cls.group)


class MessageMixin:
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        cls.content = content.Article.objects.create(
                author=cls.gestalt, public=False, title='Test Message')


class NoNotificationOnExternalConversation:
    def test_external_conversation(self):
        conversation = content.Article.objects.create(
                author=self.other_gestalt, public=False, title='Test Message')
        entities.models.GroupContent.objects.create(content=conversation, group=self.group)
        self.assertNoNotificationSent()


class GroupPageHasCreateLink:
    def test_group_page_creation(self):
        response = self.client.get(self.group.get_absolute_url())
        self.assertContainsLink(response, 'message-create', key=self.group.pk)


class CanCreateConversationWithEmail:
    def test_create_conversation(self):
        response = self.client.get(self.get_url('message-create', key=self.group.pk))
        self.assertEqual(response.status_code, 200)
        response = self.client.post(
                self.get_url('message-create', key=self.group.pk),
                {
                    'author': '<EMAIL>',
                    'subject': 'Temp Test Thema',
                    'text': 'Test Text',
                })
        self.assertExists(associations.Association, conversation__subject='Temp Test Thema')
        self.assertRedirects(response, self.group.get_absolute_url())


class CanCreateConversation:
    def test_create_conversation(self):
        response = self.client.get(self.get_url('message-create', key=self.group.pk))
        self.assertEqual(response.status_code, 200)
        response = self.client.post(
                self.get_url('message-create', key=self.group.pk),
                {
                    'subject': 'Temp Test Thema',
                    'text': 'Test Text',
                })
        self.assertExists(associations.Association, conversation__subject='Temp Test Thema')
        association = associations.Association.objects.get(
                conversation__subject='Temp Test Thema')
        self.assertRedirects(response, self.get_url('conversation', key=association.pk))


class GroupPageHasConversationLink:
    def test_group_page_conversation(self):
        response = self.client.get(self.group.get_absolute_url())
        self.assertContainsLink(response, 'conversation', key=self.association.pk)


class GroupPageDoesNotHaveConversationLink:
    def test_group_page_conversation(self):
        response = self.client.get(self.group.get_absolute_url())
        self.assertNotContainsLink(response, 'conversation', key=self.association.pk)


class CanViewConversation:
    def test_view_conversation(self):
        response = self.client.get(self.get_url('conversation', key=self.association.pk))
        self.assertEquals(response.status_code, 200)


class CanNotViewConversation:
    def test_view_conversation(self):
        conversation_url = self.get_url('conversation', key=self.association.pk)
        response = self.client.get(conversation_url)
        self.assertForbiddenOrLogin(response, conversation_url)


class OtherGestaltCanNotViewConversation:
    def test_view_conversation_as_other(self):
        self.client.logout()
        self.client.force_login(self.other_gestalt.user)
        conversation_url = self.get_url('conversation', key=self.association.pk)
        response = self.client.get(conversation_url)
        self.assertForbiddenOrLogin(response, conversation_url)


class CanReplyToConversation:
    def test_reply(self):
        response = self.client.post(
                self.get_url('conversation', key=self.group.pk),
                {
                    'text': 'Test Reply',
                })
        self.assertExists(self.association.conversation.texts, text='Test Reply')
        self.assertRedirects(response, self.get_url('conversation', key=self.association.pk))


class Anonymous(
        GroupPageHasCreateLink,
        CanCreateConversationWithEmail,
        GroupPageDoesNotHaveConversationLink,
        CanNotViewConversation,
        Conversation, tests.Test):
    '''
    An anonymous visitor
    * should see a message creation link on the group page
    '''


class Authenticated(
        GroupPageHasCreateLink,
        CanCreateConversation,
        GroupPageHasConversationLink,
        CanViewConversation,
        CanReplyToConversation,
        Conversation, gestalten.AuthenticatedMixin, tests.Test):
    '''
    An authenticated visitor
    * should see a message creation link on the group page
    '''


class GroupMember(
        GroupPageHasCreateLink,
        CanCreateConversation,
        GroupPageHasConversationLink,
        CanViewConversation,
        OtherGestaltCanNotViewConversation,
        CanReplyToConversation,
        Conversation, memberships.MemberMixin, gestalten.OtherGestaltMixin, tests.Test):
    '''
    A group member
    * should see a message creation link on the group page
    '''


{'fraction_non_alphanumeric': 0.09091668755744965, 'fraction_numerical': 0.0015041363750313362, 'mean_word_length': 3.2788702180908116, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4799907', 'n_tokens_mistral': 3310, 'n_tokens_neox': 3213, 'n_words': 600}