<commit_before>from io import BytesIO
from django.test import TestCase
from django.test import RequestFactory
from django.core.urlresolvers import reverse
from django.core.files import File
from customuser.models import User
from custompayment.constants import SCIENTIST_ACCOUNT
from customuser.views import UserDetailView


class TestUserDetailView(TestCase):

    @classmethod
    def setUpClass(cls):
        super(TestUserDetailView, cls).setUpClass()
        user = User.objects.create_user(username="testuser", password="<PASSWORD>", email="<EMAIL>")
        user.save()
        request = RequestFactory().get('/fake-path/')
        view = UserDetailView.as_view()
        cls.response = view(request, pk=user.pk)

    def test_response_code(self):
        self.assertEqual(self.response.status_code, 200)

    def test_get_context_data(self):
        self.assertEqual(self.response.context_data['account_status'], 'Skeptic')
        self.assertEqual(self.response.context_data['order'], SCIENTIST_ACCOUNT)


class TestUserUpdateView(TestCase):

    @classmethod
    def setUpClass(cls):
        super(TestUserUpdateView, cls).setUpClass()
        cls.user = User.objects.create_user(username="testuser", password="<PASSWORD>", email="<EMAIL>")
        cls.user.save()
        cls.url = reverse("edit_profile")

    def test_show_page_response_code(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 200)

    def test_object(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        resp = self.client.get(self.url)
        self.assertEqual(resp.context_data['object'], self.user)

    def test_send_data_response_code(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        # nimp is not process (normal behaviour don't need test
        resp = self.client.post(self.url, {'first_name': 'john', 'nimp': 'lol', 'email': "<EMAIL>"}, follow=True)
        self.assertEqual(resp.template_name, ["customuser/detail_user.html"])
        self.assertEqual(resp.status_code, 200)

    def test_send_wrong_data_response_code(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        resp = self.client.post(self.url, {'first_name': 'john'})
        self.assertEqual(resp.template_name, ["customuser/update_user.html"])
        self.assertEqual(resp.status_code, 200)

    def test_not_connected(self):
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 302)


class TestGetPHDImage(TestCase):

    @classmethod
    def setUpClass(cls):
        super(TestGetPHDImage, cls).setUpClass()
        cls.user = User.objects.create_superuser(username="testuser", password="<PASSWORD>", email="<EMAIL>")
        cls.user.save()

    def test_get_phd_image_empty(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        url = reverse("phd_image", kwargs={'pk':self.user.pk})
        with self.assertRaises(ValueError):
            self.client.get(url)

    def test_get_phd_image_filled(self):
        self.user.phd_image = File(BytesIO(), name='lol')
        self.user.save()
        assert self.client.login(username="testuser", password="<PASSWORD>")
        url = reverse("phd_image", kwargs={'pk':self.user.pk})
        resp = self.client.get(url)
        self.assertEqual(resp.status_code, 200)

    def test_not_connected(self):
        url = reverse("phd_image", kwargs={'pk':self.user.pk})
        resp = self.client.get(url)
        self.assertEqual(resp.status_code, 302)

    def test_not_super_user(self):
        user = User.objects.create_user(username="testuser2", password="<PASSWORD>", email="<EMAIL>")
        user.save()
        assert self.client.login(username="testuser2", password="<PASSWORD>")
        url = reverse("phd_image", kwargs={'pk':self.user.pk})
        resp = self.client.get(url)
        self.assertEqual(resp.status_code, 302)<commit_msg>more test for customuser view<commit_after>from io import BytesIO
from django.utils import timezone
from django.test import TestCase
from django.test import RequestFactory
from django.core.urlresolvers import reverse
from django.core.files import File
from customuser.models import User
from custompayment.constants import SCIENTIST_ACCOUNT
from customuser.views import UserDetailView, UserPHDTableView


class TestUserDetailView(TestCase):

    @classmethod
    def setUpClass(cls):
        super(TestUserDetailView, cls).setUpClass()
        user = User.objects.create_user(username="testuser", password="<PASSWORD>", email="<EMAIL>")
        user.save()
        request = RequestFactory().get('/fake-path/')
        view = UserDetailView.as_view()
        cls.response = view(request, pk=user.pk)

    def test_response_code(self):
        self.assertEqual(self.response.status_code, 200)

    def test_get_context_data(self):
        self.assertEqual(self.response.context_data['account_status'], 'Skeptic')
        self.assertEqual(self.response.context_data['order'], SCIENTIST_ACCOUNT)


class TestUserUpdateView(TestCase):

    @classmethod
    def setUpClass(cls):
        super(TestUserUpdateView, cls).setUpClass()
        cls.user = User.objects.create_user(username="testuser", password="<PASSWORD>", email="<EMAIL>")
        cls.user.save()
        cls.url = reverse("edit_profile")

    def test_show_page_response_code(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 200)

    def test_object(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        resp = self.client.get(self.url)
        self.assertEqual(resp.context_data['object'], self.user)

    def test_send_data_response_code(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        # nimp is not process (normal behaviour don't need test
        resp = self.client.post(self.url, {'first_name': 'john', 'nimp': 'lol', 'email': "<EMAIL>"}, follow=True)
        self.assertEqual(resp.template_name, ["customuser/detail_user.html"])
        self.assertEqual(resp.status_code, 200)

    def test_send_wrong_data_response_code(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        resp = self.client.post(self.url, {'first_name': 'john'})
        self.assertEqual(resp.template_name, ["customuser/update_user.html"])
        self.assertEqual(resp.status_code, 200)

    def test_not_connected(self):
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 302)


class TestGetPHDImage(TestCase):

    def setUp(self):
        self.user = User.objects.create_superuser(username="testuser", password="<PASSWORD>", email="<EMAIL>")
        self.user.save()

    def test_get_phd_image_empty(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        url = reverse("phd_image", kwargs={'pk':self.user.pk})
        with self.assertRaises(ValueError):
            self.client.get(url)

    def test_get_phd_image_filled(self):
        self.user.phd_image = File(BytesIO(), name='lol')
        self.user.save()
        assert self.client.login(username="testuser", password="<PASSWORD>")
        url = reverse("phd_image", kwargs={'pk':self.user.pk})
        resp = self.client.get(url)
        self.assertEqual(resp.status_code, 200)

    def test_not_connected(self):
        url = reverse("phd_image", kwargs={'pk':self.user.pk})
        resp = self.client.get(url)
        self.assertEqual(resp.status_code, 302)

    def test_not_super_user(self):
        user = User.objects.create_user(username="testuser2", password="<PASSWORD>", email="<EMAIL>")
        user.save()
        assert self.client.login(username="testuser2", password="<PASSWORD>")
        url = reverse("phd_image", kwargs={'pk':self.user.pk})
        resp = self.client.get(url)
        self.assertEqual(resp.status_code, 302)


class TestUserPHDTableView(TestCase):

    @classmethod
    def setUpClass(cls):
        super(TestUserPHDTableView, cls).setUpClass()
        cls.user = User.objects.create_superuser(username="testuser", password="<PASSWORD>", email="<EMAIL>")
        # all of this to get one anwers in the query
        cls.user.phd_image = File(BytesIO(), name='lol')
        cls.user.phd_rate_date = None
        cls.user.phd_update_date = timezone.now()
        cls.user.save()
        cls.user2 = User.objects.create_user(username="testuser2", password="<PASSWORD>", email="<EMAIL>")
        cls.user2.save()

    def test_connection_status_code_superuser(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        url = reverse("phd_ask_list")
        resp = self.client.get(url)
        self.assertEqual(resp.status_code, 200)

    def test_connection_status_code_simpleuser(self):
        assert self.client.login(username="testuser2", password="<PASSWORD>")
        url = reverse("phd_ask_list")
        resp = self.client.get(url)
        self.assertEqual(resp.status_code, 302)

    def test_connection_status_code_unconnected(self):
        url = reverse("phd_ask_list")
        resp = self.client.get(url)
        self.assertEqual(resp.status_code, 302)

    def test_get_queryset(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        url = reverse("phd_ask_list")
        resp = self.client.get(url)
        self.assertEqual(list(resp.context_data['object_list'])[0], self.user)

    # def test_get_table(self):
    #     # should be done

    def test_context_filter_name(self):
        assert self.client.login(username="testuser", password="<PASSWORD>")
        url = reverse("phd_ask_list")
        resp = self.client.get(url)
        self.assertEqual(resp.context_data['filter'].data, {'phd_to_rate': True})



{'fraction_non_alphanumeric': 0.13528116870122073, 'fraction_numerical': 0.006603962377426456, 'mean_word_length': 3.679307116104869, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '7245462', 'n_tokens_mistral': 3115, 'n_tokens_neox': 3051, 'n_words': 505}