<commit_before>from django.test import TestCase
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from django_stormpath.backends import StormpathBackend
from django_stormpath.forms import UserCreateForm
try:
    from unittest import mock
except ImportError:
    import mock


class BackendTest(TestCase):
    def allow_account(self, key_id, key_secret, href, username, password):
        account = mock.MagicMock()

        account.username = "gangelos"
        account.given_name = "Gabriel"
        account.surname = "Angelos"
        account.email = "<EMAIL>"
        account.password = "<PASSWORD>"
        account.href = "https://api.stormpath.com/va/accounts/123"
        account.is_enabled = mock.MagicMock(return_value=True)

        return account

    def deny_account(self, key_id, key_secret, href, username, password):
        return None

    def setUp(self):
        self.username = "gangelos"
        self.given_name = "Gabriel"
        self.surname = "Angelos"
        self.email = "<EMAIL>"
        self.password = "<PASSWORD>"
        self.href = settings.STORMPATH_APPLICATION
        self.key_id = settings.STORMPATH_ID
        self.key_secret = settings.STORMPATH_SECRET
        self.user_model = get_user_model()

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account',
        allow_account)
    def test_user_creation(self):

        user = StormpathBackend().authenticate(self.username, self.password)
        self.assertTrue(self.user_model.objects.filter(id=user.id).exists())

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account',
        allow_account)
    def test_user_update(self):
        user = self.user_model(
            username=self.username,
            first_name=self.given_name,
            last_name="FAKE",
            email=self.email,
            password=<PASSWORD>,
            url=self.href)
        user.save()

        user = StormpathBackend().authenticate(self.username, self.password)
        dbuser = self.user_model.objects.get(id=user.id)
        self.assertEqual(dbuser.last_name, self.surname)

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account',
        allow_account)
    def test_db_wasnt_updated(self):
        user = self.user_model(
            username=self.username,
            first_name=self.given_name,
            last_name=self.surname,
            email=self.email,
            password="<PASSWORD>")
        user.save()

        # The query should only execute when fetching the Django user
        # If Stormpath users haven't changed, the database shouldn't be queried
        with self.assertNumQueries(1):
            dbuser = StormpathBackend().authenticate(
                self.username,
                self.password)
            self.assertEqual(user.id, dbuser.id)

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account',
        deny_account)
    def test_invalid_credentials(self):

        StormpathBackend().authenticate(self.username, self.password)

        with self.assertRaises(ObjectDoesNotExist):
            self.user_model.objects.get(username=self.username)

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account',
        deny_account)
    def test_nonexisting_account(self):
        user = self.user_model(
            username=self.username,
            first_name=self.given_name,
            last_name=self.surname,
            email=self.email,
            password=self.password)
        user.save()

        dbuser = StormpathBackend().authenticate(self.username, self.password)
        self.assertIsNone(dbuser)
        self.assertTrue(self.user_model.objects.filter(id=user.id).exists())


class UserCreateTest(TestCase):

    def setUp(self):
        self.username = "gangelos"
        self.given_name = "Gabriel"
        self.surname = "Angelos"
        self.email = "<EMAIL>"
        self.password = "<PASSWORD>"
        self.user_model = get_user_model()

    @mock.patch('django_stormpath.forms.get_application')
    def test_user_create(self, get_application):
        form = UserCreateForm({
            'last_name': self.surname, 'username': self.username,
            'password2': 'password', 'password': 'password',
            'email': self.email, 'first_name': self.given_name})

        form.is_valid()
        search = get_application.return_value.accounts.search
        search.assert_any_call({'username': self.username})
        search.assert_any_call({'email': self.email})

        form.save()
        stormpath_data = {
            'given_name': self.given_name, 'email': self.email,
            'password': 'password',
            'username': self.username, 'surname': self.surname}
        accounts = get_application.return_value.accounts
        accounts.create.assert_called_once_with(stormpath_data)

        try:
            user = self.user_model.objects.get(**{'last_name': self.surname,
                'username': self.username, 'email': self.email,
                'first_name': self.given_name})
        except ObjectDoesNotExist as err:
            self.fail(str(err))

        self.assertEqual(user.password, "<PASSWORD>")

    @mock.patch('django_stormpath.forms.Client')
    def test_matching_passwords(self, client):
        form = UserCreateForm({
            'last_name': self.surname, 'username': self.username,
            'password2': 'password', 'password': '<PASSWORD>',
            'email': self.email, 'first_name': self.given_name})

        form.is_valid()
        with self.assertRaises(ValueError):
            form.save()

    @mock.patch('django_stormpath.forms.Client')
    def test_user_exists(self, client):
        form = UserCreateForm({
            'last_name': self.surname, 'username': self.username,
            'password2': 'password', 'password': 'password',
            'email': self.email, 'first_name': self.given_name})

        form.is_valid()
        accounts = client.return_value.applications.get.return_value.accounts
        accounts.search.return_value.__len__.return_value = 1
        form.save()

    def tearDown(self):
        self.user_model.objects.all().delete()<commit_msg>assertation for existing user<commit_after>from django.test import TestCase
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from django_stormpath.backends import StormpathBackend
from django_stormpath.forms import UserCreateForm
try:
    from unittest import mock
except ImportError:
    import mock


class BackendTest(TestCase):
    def allow_account(self, key_id, key_secret, href, username, password):
        account = mock.MagicMock()

        account.username = "gangelos"
        account.given_name = "Gabriel"
        account.surname = "Angelos"
        account.email = "<EMAIL>"
        account.password = "<PASSWORD>"
        account.href = "https://api.stormpath.com/va/accounts/123"
        account.is_enabled = mock.MagicMock(return_value=True)

        return account

    def deny_account(self, key_id, key_secret, href, username, password):
        return None

    def setUp(self):
        self.username = "gangelos"
        self.given_name = "Gabriel"
        self.surname = "Angelos"
        self.email = "<EMAIL>"
        self.password = "<PASSWORD>"
        self.href = settings.STORMPATH_APPLICATION
        self.key_id = settings.STORMPATH_ID
        self.key_secret = settings.STORMPATH_SECRET
        self.user_model = get_user_model()

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account',
        allow_account)
    def test_user_creation(self):

        user = StormpathBackend().authenticate(self.username, self.password)
        self.assertTrue(self.user_model.objects.filter(id=user.id).exists())

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account',
        allow_account)
    def test_user_update(self):
        user = self.user_model(
            username=self.username,
            first_name=self.given_name,
            last_name="FAKE",
            email=self.email,
            password=<PASSWORD>,
            url=self.href)
        user.save()

        user = StormpathBackend().authenticate(self.username, self.password)
        dbuser = self.user_model.objects.get(id=user.id)
        self.assertEqual(dbuser.last_name, self.surname)

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account',
        allow_account)
    def test_db_wasnt_updated(self):
        user = self.user_model(
            username=self.username,
            first_name=self.given_name,
            last_name=self.surname,
            email=self.email,
            password="<PASSWORD>")
        user.save()

        # The query should only execute when fetching the Django user
        # If Stormpath users haven't changed, the database shouldn't be queried
        with self.assertNumQueries(1):
            dbuser = StormpathBackend().authenticate(
                self.username,
                self.password)
            self.assertEqual(user.id, dbuser.id)

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account',
        deny_account)
    def test_invalid_credentials(self):

        StormpathBackend().authenticate(self.username, self.password)

        with self.assertRaises(ObjectDoesNotExist):
            self.user_model.objects.get(username=self.username)

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account',
        deny_account)
    def test_nonexisting_account(self):
        user = self.user_model(
            username=self.username,
            first_name=self.given_name,
            last_name=self.surname,
            email=self.email,
            password=self.password)
        user.save()

        dbuser = StormpathBackend().authenticate(self.username, self.password)
        self.assertIsNone(dbuser)
        self.assertTrue(self.user_model.objects.filter(id=user.id).exists())


class UserCreateTest(TestCase):

    def setUp(self):
        self.username = "gangelos"
        self.given_name = "Gabriel"
        self.surname = "Angelos"
        self.email = "<EMAIL>"
        self.password = "<PASSWORD>"
        self.user_model = get_user_model()

    @mock.patch('django_stormpath.forms.get_application')
    def test_user_create(self, get_application):
        form = UserCreateForm({
            'last_name': self.surname, 'username': self.username,
            'password2': 'password', 'password': 'password',
            'email': self.email, 'first_name': self.given_name})

        self.assertTrue(form.is_valid())
        search = get_application.return_value.accounts.search
        search.assert_any_call({'username': self.username})
        search.assert_any_call({'email': self.email})

        form.save()
        stormpath_data = {
            'given_name': self.given_name, 'email': self.email,
            'password': 'password',
            'username': self.username, 'surname': self.surname}
        accounts = get_application.return_value.accounts
        accounts.create.assert_called_once_with(stormpath_data)

        try:
            user = self.user_model.objects.get(**{'last_name': self.surname,
                'username': self.username, 'email': self.email,
                'first_name': self.given_name})
        except ObjectDoesNotExist as err:
            self.fail(str(err))

        self.assertEqual(user.password, "<PASSWORD>")

    @mock.patch('django_stormpath.forms.Client')
    def test_matching_passwords(self, client):
        form = UserCreateForm({
            'last_name': self.surname, 'username': self.username,
            'password2': 'password', 'password': '<PASSWORD>',
            'email': self.email, 'first_name': self.given_name})

        self.assertFalse(form.is_valid())
        with self.assertRaises(ValueError):
            form.save()

    @mock.patch('django_stormpath.forms.get_application')
    def test_user_exists(self, get_application):
        form = UserCreateForm({
            'last_name': self.surname, 'username': self.username,
            'password2': 'password', 'password': 'password',
            'email': self.email, 'first_name': self.given_name})

        accounts = get_application.return_value.accounts.search.return_value
        accounts.__len__.return_value = 1
        self.assertFalse(form.is_valid())

    def tearDown(self):
        self.user_model.objects.all().delete()
{'fraction_non_alphanumeric': 0.110606784519828, 'fraction_numerical': 0.0012740882306099697, 'mean_word_length': 2.7178804026050916, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 2, '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': '492375', 'n_tokens_mistral': 3744, 'n_tokens_neox': 3632, 'n_words': 611}