<commit_before># Copyright (c) 2011-2014 Berkeley Model United Nations. All rights reserved.
# Use of this source code is governed by a BSD License (see LICENSE).

from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client

from huxley.accounts.models import HuxleyUser
from huxley.utils.test import TestUsers

import json

class UserDetailTestCase(TestCase):
    def setUp(self):
        self.client = Client()

    def get_url(self, user_id):
        return reverse('api:user_detail', args=(user_id,))

    def get_response(self, url):
        return json.loads(self.client.get(url).content)

    def test_anonymous_user(self):
        '''It should reject request from an anonymous user.'''
        user = TestUsers.new_user()
        url = self.get_url(user.id)
        data = self.get_response(url)

        self.assertEqual(len(data.keys()), 1)
        self.assertEqual(data['detail'],
                         u'Authentication credentials were not provided.')

    def test_other_user(self):
        '''It should reject request from another user.'''
        user1 = TestUsers.new_user(username='user1')
        user2 = TestUsers.new_user(username='user2', password='<PASSWORD>')
        url = self.get_url(user1.id)

        self.client.login(username='user2', password='<PASSWORD>')
        data = self.get_response(url)

        self.assertEqual(len(data.keys()), 1)
        self.assertEqual(data['detail'],
                         u'You do not have permission to perform this action.')

    def test_superuser(self):
        '''It should return the correct fields for a superuser.'''
        user1 = TestUsers.new_user(username='user1')
        user2 = TestUsers.new_superuser(username='user2', password='<PASSWORD>')
        url = self.get_url(user1.id)

        self.client.login(username='user2', password='<PASSWORD>')
        data = self.get_response(url)

        self.assertEqual(len(data.keys()), 6)
        self.assertEqual(data['id'], user1.id)
        self.assertEqual(data['first_name'], user1.first_name)
        self.assertEqual(data['last_name'], user1.last_name)
        self.assertEqual(data['user_type'], HuxleyUser.TYPE_ADVISOR)
        self.assertEqual(data['school'], user1.school_id)
        self.assertEqual(data['committee'], user1.committee_id)

    def test_self(self):
        '''It should return the correct fields for a single user.'''
        user = TestUsers.new_user(username='lol', password='<PASSWORD>', school_id=1)
        url = self.get_url(user.id)

        self.client.login(username='lol', password='<PASSWORD>')
        data = self.get_response(url)

        self.assertEqual(len(data.keys()), 6)
        self.assertEqual(data['id'], user.id)
        self.assertEqual(data['first_name'], user.first_name)
        self.assertEqual(data['last_name'], user.last_name)
        self.assertEqual(data['user_type'], HuxleyUser.TYPE_ADVISOR)
        self.assertEqual(data['school'], user.school_id)
        self.assertEqual(data['committee'], user.committee_id)

    def test_chair(self):
        '''It should have the correct fields for chairs.'''
        user = TestUsers.new_user(user_type=HuxleyUser.TYPE_CHAIR,
                                  committee_id=4)
        url = self.get_url(user.id)

        self.client.login(username='testuser', password='<PASSWORD>')
        data = self.get_response(url)

        self.assertEqual(len(data.keys()), 6)
        self.assertEqual(data['id'], user.id)
        self.assertEqual(data['first_name'], user.first_name)
        self.assertEqual(data['last_name'], user.last_name)
        self.assertEqual(data['user_type'], HuxleyUser.TYPE_CHAIR)
        self.assertEqual(data['school'], user.school_id)
        self.assertEqual(data['committee'], user.committee_id)


<commit_msg>Add UserDetailDeleteTestCase.<commit_after># Copyright (c) 2011-2014 Berkeley Model United Nations. All rights reserved.
# Use of this source code is governed by a BSD License (see LICENSE).

from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client

from huxley.accounts.models import HuxleyUser
from huxley.utils.test import TestUsers

import json


class UserDetailGetTestCase(TestCase):
    def setUp(self):
        self.client = Client()

    def get_url(self, user_id):
        return reverse('api:user_detail', args=(user_id,))

    def get_response(self, url):
        return json.loads(self.client.get(url).content)

    def test_anonymous_user(self):
        '''It should reject request from an anonymous user.'''
        user = TestUsers.new_user()
        url = self.get_url(user.id)
        data = self.get_response(url)

        self.assertEqual(len(data.keys()), 1)
        self.assertEqual(data['detail'],
                         u'Authentication credentials were not provided.')

    def test_other_user(self):
        '''It should reject request from another user.'''
        user1 = TestUsers.new_user(username='user1')
        user2 = TestUsers.new_user(username='user2', password='<PASSWORD>')
        url = self.get_url(user1.id)

        self.client.login(username='user2', password='<PASSWORD>')
        data = self.get_response(url)

        self.assertEqual(len(data.keys()), 1)
        self.assertEqual(data['detail'],
                         u'You do not have permission to perform this action.')

    def test_superuser(self):
        '''It should return the correct fields for a superuser.'''
        user1 = TestUsers.new_user(username='user1')
        user2 = TestUsers.new_superuser(username='user2', password='<PASSWORD>')
        url = self.get_url(user1.id)

        self.client.login(username='user2', password='<PASSWORD>')
        data = self.get_response(url)

        self.assertEqual(len(data.keys()), 6)
        self.assertEqual(data['id'], user1.id)
        self.assertEqual(data['first_name'], user1.first_name)
        self.assertEqual(data['last_name'], user1.last_name)
        self.assertEqual(data['user_type'], HuxleyUser.TYPE_ADVISOR)
        self.assertEqual(data['school'], user1.school_id)
        self.assertEqual(data['committee'], user1.committee_id)

    def test_self(self):
        '''It should return the correct fields for a single user.'''
        user = TestUsers.new_user(username='lol', password='<PASSWORD>', school_id=1)
        url = self.get_url(user.id)

        self.client.login(username='lol', password='<PASSWORD>')
        data = self.get_response(url)

        self.assertEqual(len(data.keys()), 6)
        self.assertEqual(data['id'], user.id)
        self.assertEqual(data['first_name'], user.first_name)
        self.assertEqual(data['last_name'], user.last_name)
        self.assertEqual(data['user_type'], HuxleyUser.TYPE_ADVISOR)
        self.assertEqual(data['school'], user.school_id)
        self.assertEqual(data['committee'], user.committee_id)

    def test_chair(self):
        '''It should have the correct fields for chairs.'''
        user = TestUsers.new_user(user_type=HuxleyUser.TYPE_CHAIR,
                                  committee_id=4)
        url = self.get_url(user.id)

        self.client.login(username='testuser', password='<PASSWORD>')
        data = self.get_response(url)

        self.assertEqual(len(data.keys()), 6)
        self.assertEqual(data['id'], user.id)
        self.assertEqual(data['first_name'], user.first_name)
        self.assertEqual(data['last_name'], user.last_name)
        self.assertEqual(data['user_type'], HuxleyUser.TYPE_CHAIR)
        self.assertEqual(data['school'], user.school_id)
        self.assertEqual(data['committee'], user.committee_id)


class UserDetailDeleteTestCase(TestCase):
    def setUp(self):
        self.client = Client()

    def get_url(self, user_id):
        return reverse('api:user_detail', args=(user_id,))

    def get_response(self, url):
        return self.client.delete(url)

    def get_data(self, url):
        return json.loads(self.get_response(url).content)

    def test_anonymous_user(self):
        '''It should reject the request from an anonymous user.'''
        user = TestUsers.new_user()
        url = self.get_url(user.id)
        data = self.get_data(url)

        self.assertEqual(len(data.keys()), 1)
        self.assertEqual(data['detail'],
                         u'Authentication credentials were not provided.')
        self.assertTrue(HuxleyUser.objects.filter(id=user.id).exists())

    def test_other_user(self):
        '''It should reject the request from another user.'''
        username = 'user1'
        user1 = TestUsers.new_user(username=username)
        user2 = TestUsers.new_user(username='user2', password='<PASSWORD>')
        url = self.get_url(user1.id)

        self.client.login(username='user2', password='<PASSWORD>')
        data = self.get_data(url)

        self.assertEqual(len(data.keys()), 1)
        self.assertEqual(data['detail'],
                         u'You do not have permission to perform this action.')
        self.assertTrue(HuxleyUser.objects.filter(id=user1.id).exists())

    def test_self(self):
        '''It should allow a user to delete themself.'''
        user = TestUsers.new_user(username='lol', password='<PASSWORD>', school_id=1)
        url = self.get_url(user.id)

        self.client.login(username='lol', password='<PASSWORD>')
        response = self.get_response(url)

        self.assertEqual(response.status_code, 204)
        self.assertFalse(HuxleyUser.objects.filter(id=user.id).exists())

    def test_superuser(self):
        '''It should allow a superuser to delete a user.'''
        user1 = TestUsers.new_user(username='user1')
        user2 = TestUsers.new_superuser(username='user2', password='<PASSWORD>')
        url = self.get_url(user1.id)

        self.client.login(username='user2', password='<PASSWORD>')
        response = self.get_response(url)

        self.assertEqual(response.status_code, 204)
        self.assertFalse(HuxleyUser.objects.filter(id=user1.id).exists())



{'fraction_non_alphanumeric': 0.13142686329214825, 'fraction_numerical': 0.008668792347548825, 'mean_word_length': 3.1168990976210007, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '322780', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2934, 'n_words': 581}