<commit_before>import django
from django.test import TestCase  # , Client
from htmlvalidator.client import ValidatingClient
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse

from paiji2_shoutbox.models import (
    Note as Message,
)
# from paiji2_shoutbox.views import (
#     NoteListView as MessageListView,
#     NoteCreateView as MessageCreateView,
#     NoteEditView as MessageEditView,
#     NoteDeleteView as MessageDeleteView,
# )
from paiji2_utils.templatetags.urlize2 import (
    # display_bulletin_board as display_shoutbox,
    urlize2,
)


def reload_object(obj):
    if django.VERSION[0] >= 1 and django.VERSION[1] >= 8:
        obj.refresh_from_db()
        return obj
    else:
        return obj._meta.model.objects.get(pk=obj.pk)


User = get_user_model()


class BaseTestCase(TestCase):
    def setUp(self):
        self.alice = User.objects.create_user(
            'alice',
            password='<PASSWORD>',
        )
        self.chuck = User.objects.create_user(
            'chuck',
            password='<PASSWORD>',
        )
        self.message = Message.objects.create(
            author=self.alice,
            message='test',
        )
        self.client = ValidatingClient()


class PagesTestCase(BaseTestCase):
    def test_list(self):
        url = reverse('message-list')

        # Unauthenticated user
        response = self.client.get(url)
        self.assertEqual(response.status_code, 302)

        # Authenticated user
        self.client.login(username='alice', password='<PASSWORD>')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_create(self):
        url = reverse('message-add')
        # Unauthenticated user
        response = self.client.get(url)
        self.assertEqual(response.status_code, 302)

        # Authenticated user
        self.client.login(username='alice', password='test')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

        response = self.client.post(url, {
            'message': 'test',
        })
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Message.objects.count(), 2)

    def test_edit(self):
        url = reverse('message-edit', kwargs={
            'pk': self.message.pk,
        })

        # Non owner
        self.client.login(username='chuck', password='<PASSWORD>')
        response = self.client.get(url)

        self.assertEqual(response.status_code, 404)

        # Owner
        self.client.login(username='alice', password='<PASSWORD>')
        response = self.client.get(url)

        self.assertEqual(response.status_code, 200)

        response = self.client.post(url, {
            'message': 'retest',
            'next': reverse('message-list'),
        })
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Message.objects.count(), 1)
        self.message = reload_object(self.message)
        self.assertEqual(self.message.message, 'retest')

    def test_delete(self):
        url = reverse('message-delete', kwargs={
            'pk': self.message.pk,
        })
        # Non owner
        self.client.login(username='chuck', password='<PASSWORD>')
        response = self.client.get(url)

        self.assertEqual(response.status_code, 404)

        # Owner
        self.client.login(username='alice', password='<PASSWORD>')
        response = self.client.get(url)

        self.assertEqual(response.status_code, 200)

        response = self.client.post(url)

        self.assertEqual(response.status_code, 302)
        self.assertEqual(Message.objects.count(), 0)


# TODO: should be improved
class ShoutboxTagsTest(BaseTestCase):
    def test_urlize2(self):
        text = 'ftp://wedwd'
        open_tag = '<a href="%s">' % text
        close_tag = '</a>'
        urlized = urlize2(text)
        self.assertTrue(open_tag in urlized)
        self.assertTrue(close_tag in urlized)

        text = 'http://wedwd'
        open_tag = '<a href="%s">' % text
        close_tag = '</a>'
        urlized = urlize2(text)
        self.assertTrue(open_tag in urlized)
        self.assertTrue(close_tag in urlized)

        text = 'https://wedwd'
        open_tag = '<a href="%s">' % text
        close_tag = '</a>'
        urlized = urlize2(text)
        self.assertTrue(open_tag in urlized)
        self.assertTrue(close_tag in urlized)<commit_msg>moves urlize2 test to paiji2_utils<commit_after>import django
from django.test import TestCase  # , Client
from htmlvalidator.client import ValidatingClient
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse

from paiji2_shoutbox.models import (
    Note as Message,
)
# from paiji2_shoutbox.views import (
#     NoteListView as MessageListView,
#     NoteCreateView as MessageCreateView,
#     NoteEditView as MessageEditView,
#     NoteDeleteView as MessageDeleteView,
# )


def reload_object(obj):
    if django.VERSION[0] >= 1 and django.VERSION[1] >= 8:
        obj.refresh_from_db()
        return obj
    else:
        return obj._meta.model.objects.get(pk=obj.pk)


User = get_user_model()


class BaseTestCase(TestCase):
    def setUp(self):
        self.alice = User.objects.create_user(
            'alice',
            password='<PASSWORD>',
        )
        self.chuck = User.objects.create_user(
            'chuck',
            password='<PASSWORD>',
        )
        self.message = Message.objects.create(
            author=self.alice,
            message='test',
        )
        self.client = ValidatingClient()


class PagesTestCase(BaseTestCase):
    def test_list(self):
        url = reverse('message-list')

        # Unauthenticated user
        response = self.client.get(url)
        self.assertEqual(response.status_code, 302)

        # Authenticated user
        self.client.login(username='alice', password='<PASSWORD>')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_create(self):
        url = reverse('message-add')
        # Unauthenticated user
        response = self.client.get(url)
        self.assertEqual(response.status_code, 302)

        # Authenticated user
        self.client.login(username='alice', password='<PASSWORD>')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

        response = self.client.post(url, {
            'message': 'test',
        })
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Message.objects.count(), 2)

    def test_edit(self):
        url = reverse('message-edit', kwargs={
            'pk': self.message.pk,
        })

        # Non owner
        self.client.login(username='chuck', password='<PASSWORD>')
        response = self.client.get(url)

        self.assertEqual(response.status_code, 404)

        # Owner
        self.client.login(username='alice', password='<PASSWORD>')
        response = self.client.get(url)

        self.assertEqual(response.status_code, 200)

        response = self.client.post(url, {
            'message': 'retest',
            'next': reverse('message-list'),
        })
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Message.objects.count(), 1)
        self.message = reload_object(self.message)
        self.assertEqual(self.message.message, 'retest')

    def test_delete(self):
        url = reverse('message-delete', kwargs={
            'pk': self.message.pk,
        })
        # Non owner
        self.client.login(username='chuck', password='<PASSWORD>')
        response = self.client.get(url)

        self.assertEqual(response.status_code, 404)

        # Owner
        self.client.login(username='alice', password='<PASSWORD>')
        response = self.client.get(url)

        self.assertEqual(response.status_code, 200)

        response = self.client.post(url)

        self.assertEqual(response.status_code, 302)
        self.assertEqual(Message.objects.count(), 0)
{'fraction_non_alphanumeric': 0.11958814042922715, 'fraction_numerical': 0.011537030145143283, 'mean_word_length': 2.4991319444444446, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 28, 'https://': 1, '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': '779650', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2336, 'n_words': 434}