<commit_before>import json
import time
import unittest

from django.contrib.auth.models import User
from django.core.management import call_command
from django.test import LiveServerTestCase, TestCase
from django.utils.six import StringIO
from wagtail.tests.utils import WagtailPageTests, WagtailTestUtils
from wagtail.wagtailimages.tests.utils import Image, get_test_image_file

from accounts.utils import create_user
from api.serializers import UserSerializer


class UserAPI(LiveServerTestCase, WagtailPageTests):
    serialized_rollback = True

    def setUp(self):
        super(LiveServerTestCase, self).setUp()
        super(WagtailPageTests, self).setUp()
        [user.delete() for user in User.objects.all()]

    @unittest.skip("SF password expired")
    def test_anonymous_user_fields(self):
        response = self.client.get('/api/user/?format=json')
        user_list = json.loads(response.content.decode(response.charset))
        self.assertEqual(len(user_list), 1)
        user_dict = user_list[0]
        returned_set = set(user_dict.keys())
        expected_set = set(
            ['username', 'accounts_id', 'is_superuser', 'groups', 'is_staff', 'pending_verification'])
        self.assertSetEqual(expected_set, returned_set)

    @unittest.skip("SF password expired")
    def test_logged_in_user_fields(self):
        test_user = {'last_name': 'last_name',
                     'username': 'username',
                     'full_name': None,
                     'first_name': 'first_name',
                     'uid': '0',
                     'pending_verification': False}

        new_user = create_user(**test_user)

        self.client.force_login(new_user)
        response = self.client.get('/api/user/?format=json')
        user_list = json.loads(response.content.decode(response.charset))
        self.assertEqual(len(user_list), 1)
        user_dict = user_list[0]
        returned_set = set(user_dict.keys())
        expected_set = set(UserSerializer.Meta.fields)
        self.assertSetEqual(expected_set, returned_set)
        self.assertEqual(user_dict['accounts_id'], test_user['uid'])
        self.assertEqual(user_dict['username'], test_user['username'])
        self.assertEqual(user_dict['first_name'], test_user['first_name'])
        self.assertEqual(user_dict['last_name'], test_user['last_name'])

    @unittest.skip("faculty group failing")
    def test_user_faculty_group(self):
        user = User.objects.create_user('john',
                                        '<EMAIL>',
                                        'johnpassword')
        user.save()
        self.client.force_login(user)
        response = self.client.get('/api/user/')
        self.assertEqual(response.status_code, 200)
        response_list = json.loads(response.content.decode(response.charset))
        expected_user_info = {'is_superuser': False,
                              'username': 'john',
                              'first_name': '',
                              'groups': [],
                              'last_name': '',
                              'is_staff': False,
                              'accounts_id': None,
                              'pending_verification': False}
        returned_user_info = response_list[0]
        self.assertDictEqual(expected_user_info, returned_user_info)

        test_user = {'last_name': 'last_name',
                     'username': 'username',
                     'full_name': None,
                     'first_name': 'first_name',
                     'uid': '0'}

        new_user = create_user(**test_user)

        self.client.force_login(new_user)
        self.client.get('/api/user/')
        time.sleep(1)
        response = self.client.get('/api/user/')
        self.assertEqual(response.status_code, 200)
        response_list = json.loads(response.content.decode(response.charset))
        expected_user_info = {'is_superuser': False,
                              'username': 'username',
                              'first_name': 'first_name',
                              'groups': ['Faculty'],
                              'last_name': 'last_name',
                              'is_staff': False,
                              'accounts_id': '0',
                              'pending_verification': False}
        returned_user_info = response_list[0]
        self.assertDictEqual(expected_user_info, returned_user_info)

    @unittest.skip("SF password expired")
    def test_adopters(self):
        # Test No adopters
        response = self.client.get('/api/adopters/')
        self.assertEqual(response.status_code, 200)
        response_list = eval(response.content.decode(response.charset))
        self.assertIsInstance(response_list, list)
        self.assertEqual(response_list, [])

        # Test with adopters
        out = StringIO()
        call_command('update_adopters', stdout=out)
        response = self.client.get('/api/adopters/')
        self.assertEqual(response.status_code, 200)
        response_list = json.loads(response.content.decode(response.charset))
        self.assertIsInstance(response_list, list)
        self.assertGreater(len(response_list), 1)
        response_item = response_list[0]
        self.assertIsInstance(response_item, dict)
        expected_set = {'description', 'website', 'name'}
        returned_set = set(response_item.keys())
        self.assertSetEqual(expected_set, returned_set)
        names = [adopter['name'] for adopter in response_list]
        self.assertIn('Rice University', names)

    def tearDown(self):
        super(WagtailPageTests, self).setUp()
        super(LiveServerTestCase, self).setUp()


class ImageAPI(TestCase, WagtailTestUtils):

    def setUp(self):
        self.login()

    def test_api_v0_no_images(self):
        response = self.client.get('/api/v0/images/')
        self.assertEqual(response.status_code, 200)
        response_list = eval(response.content.decode(response.charset))
        self.assertIsInstance(response_list, list)
        self.assertEqual(response_list, [])

    def test_api_v1_no_images(self):
        response = self.client.get('/api/v1/images/')
        self.assertEqual(response.status_code, 200)
        response_dict = eval(response.content.decode(response.charset))
        self.assertIsInstance(response_dict, dict)
        self.assertEqual(response_dict['meta']['total_count'], 0)
        self.assertEqual(response_dict['images'], [])

    def test_api_v0_single_image(self):
        response = self.client.get('/api/v0/images/')
        self.assertEqual(response.status_code, 200)<commit_msg>update image tests<commit_after>import json
import time
import unittest

from django.contrib.auth.models import User
from django.core.management import call_command
from django.test import LiveServerTestCase, TestCase
from django.utils.six import StringIO
from wagtail.tests.utils import WagtailPageTests, WagtailTestUtils
from wagtail.wagtailimages.tests.utils import Image, get_test_image_file

from accounts.utils import create_user
from api.serializers import UserSerializer


class UserAPI(LiveServerTestCase, WagtailPageTests):
    serialized_rollback = True

    def setUp(self):
        super(LiveServerTestCase, self).setUp()
        super(WagtailPageTests, self).setUp()
        [user.delete() for user in User.objects.all()]

    @unittest.skip("SF password expired")
    def test_anonymous_user_fields(self):
        response = self.client.get('/api/user/?format=json')
        user_list = json.loads(response.content.decode(response.charset))
        self.assertEqual(len(user_list), 1)
        user_dict = user_list[0]
        returned_set = set(user_dict.keys())
        expected_set = set(
            ['username', 'accounts_id', 'is_superuser', 'groups', 'is_staff', 'pending_verification'])
        self.assertSetEqual(expected_set, returned_set)

    @unittest.skip("SF password expired")
    def test_logged_in_user_fields(self):
        test_user = {'last_name': 'last_name',
                     'username': 'username',
                     'full_name': None,
                     'first_name': 'first_name',
                     'uid': '0',
                     'pending_verification': False}

        new_user = create_user(**test_user)

        self.client.force_login(new_user)
        response = self.client.get('/api/user/?format=json')
        user_list = json.loads(response.content.decode(response.charset))
        self.assertEqual(len(user_list), 1)
        user_dict = user_list[0]
        returned_set = set(user_dict.keys())
        expected_set = set(UserSerializer.Meta.fields)
        self.assertSetEqual(expected_set, returned_set)
        self.assertEqual(user_dict['accounts_id'], test_user['uid'])
        self.assertEqual(user_dict['username'], test_user['username'])
        self.assertEqual(user_dict['first_name'], test_user['first_name'])
        self.assertEqual(user_dict['last_name'], test_user['last_name'])

    @unittest.skip("faculty group failing")
    def test_user_faculty_group(self):
        user = User.objects.create_user('john',
                                        '<EMAIL>',
                                        'johnpassword')
        user.save()
        self.client.force_login(user)
        response = self.client.get('/api/user/')
        self.assertEqual(response.status_code, 200)
        response_list = json.loads(response.content.decode(response.charset))
        expected_user_info = {'is_superuser': False,
                              'username': 'john',
                              'first_name': '',
                              'groups': [],
                              'last_name': '',
                              'is_staff': False,
                              'accounts_id': None,
                              'pending_verification': False}
        returned_user_info = response_list[0]
        self.assertDictEqual(expected_user_info, returned_user_info)

        test_user = {'last_name': 'last_name',
                     'username': 'username',
                     'full_name': None,
                     'first_name': 'first_name',
                     'uid': '0'}

        new_user = create_user(**test_user)

        self.client.force_login(new_user)
        self.client.get('/api/user/')
        time.sleep(1)
        response = self.client.get('/api/user/')
        self.assertEqual(response.status_code, 200)
        response_list = json.loads(response.content.decode(response.charset))
        expected_user_info = {'is_superuser': False,
                              'username': 'username',
                              'first_name': 'first_name',
                              'groups': ['Faculty'],
                              'last_name': 'last_name',
                              'is_staff': False,
                              'accounts_id': '0',
                              'pending_verification': False}
        returned_user_info = response_list[0]
        self.assertDictEqual(expected_user_info, returned_user_info)

    @unittest.skip("SF password expired")
    def test_adopters(self):
        # Test No adopters
        response = self.client.get('/api/adopters/')
        self.assertEqual(response.status_code, 200)
        response_list = eval(response.content.decode(response.charset))
        self.assertIsInstance(response_list, list)
        self.assertEqual(response_list, [])

        # Test with adopters
        out = StringIO()
        call_command('update_adopters', stdout=out)
        response = self.client.get('/api/adopters/')
        self.assertEqual(response.status_code, 200)
        response_list = json.loads(response.content.decode(response.charset))
        self.assertIsInstance(response_list, list)
        self.assertGreater(len(response_list), 1)
        response_item = response_list[0]
        self.assertIsInstance(response_item, dict)
        expected_set = {'description', 'website', 'name'}
        returned_set = set(response_item.keys())
        self.assertSetEqual(expected_set, returned_set)
        names = [adopter['name'] for adopter in response_list]
        self.assertIn('Rice University', names)

    def tearDown(self):
        super(WagtailPageTests, self).setUp()
        super(LiveServerTestCase, self).setUp()


class ImageAPI(TestCase, WagtailTestUtils):

    def setUp(self):
        self.login()

    def test_api_v0_no_images(self):
        response = self.client.get('/api/images/')
        self.assertEqual(response.status_code, 200)
        response_list = eval(response.content.decode(response.charset))
        self.assertIsInstance(response_list, list)
        self.assertEqual(response_list, [])

    def test_api_v1_no_images(self):
        response = self.client.get('/api/v1/images/')
        self.assertEqual(response.status_code, 200)
        response_dict = eval(response.content.decode(response.charset))
        self.assertIsInstance(response_dict, dict)
        self.assertEqual(response_dict['meta']['total_count'], 0)
        self.assertEqual(response_dict['images'], [])

    def test_api_v0_single_image(self):
        response = self.client.get('/api/images/')
        self.assertEqual(response.status_code, 200)
{'fraction_non_alphanumeric': 0.11466122818944029, 'fraction_numerical': 0.005891683661907999, 'mean_word_length': 2.418538600568035, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7503500', 'n_tokens_mistral': 3783, 'n_tokens_neox': 3653, 'n_words': 601}