<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 stormpath.resource.accounts import Account
from stormpath.resource import enabled


try:
    from unittest import mock
except ImportError:
    import mock


def allow_account(self, href, username, password):
    account = Account()

    account.username = "gangelos"
    account.given_name = "Gabriel"
    account.surname = "Angelos"
    account.email = "<EMAIL>"
    account.status = enabled

    return account


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


class BackendTest(TestCase):

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

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

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

        self.assertTrue(isinstance(self.user_model.objects.get(
            username=self.username,
            first_name=self.given_name,
            last_name=self.surname,
            email=self.email,
            password="<PASSWORD>"), self.user_model))

    @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=self.password)
        user.save()

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

        self.assertTrue(isinstance(self.user_model.objects.get(
            username=self.username,
            first_name=self.given_name,
            last_name=self.surname,
            email=self.email,
            password="<PASSWORD>"), self.user_model))

    @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()

        with self.assertNumQueries(1):
            StormpathBackend().authenticate(self.username, self.password)

        self.assertTrue(isinstance(self.user_model.objects.get(
            username=self.username,
            first_name=self.given_name,
            last_name=self.surname,
            email=self.email,
            password="<PASSWORD>"), self.user_model))

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account', deny_account)
    def test_invalid_credentials(self):<commit_msg>test command for setup added<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 stormpath.resource.accounts import Account
from stormpath.resource import enabled


try:
    from unittest import mock
except ImportError:
    import mock


def allow_account(self, href, username, password):
    account = Account()

    account.username = "gangelos"
    account.given_name = "Gabriel"
    account.surname = "Angelos"
    account.email = "<EMAIL>"
    account.status = enabled

    return account


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


class BackendTest(TestCase):

    def setUp(self):
        self.username = "gangelos"
        self.given_name = "Gabriel"
        self.surname = "Angelos"
        self.email = "gab<EMAIL>"
        self.password = "<PASSWORD>"
        self.href = settings.STORMPATH_URL
        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(isinstance(self.user_model.objects.get(
            username=self.username,
            first_name=self.given_name,
            last_name=self.surname,
            email=self.email,
            password="<PASSWORD>"), self.user_model))

    @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=self.password)
        user.save()

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

        self.assertTrue(isinstance(self.user_model.objects.get(
            username=self.username,
            first_name=self.given_name,
            last_name=self.surname,
            email=self.email,
            password="<PASSWORD>"), self.user_model))

    @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):
            StormpathBackend().authenticate(self.username, self.password)

        self.assertTrue(isinstance(self.user_model.objects.get(
            username=self.username,
            first_name=self.given_name,
            last_name=self.surname,
            email=self.email,
            password="<PASSWORD>"), self.user_model))

    @mock.patch('django_stormpath.backends.StormpathBackend.check_account', deny_account)
    def test_invalid_credentials(self):
{'fraction_non_alphanumeric': 0.09666613494975275, 'fraction_numerical': 0.000319030148349019, 'mean_word_length': 2.8632162661737524, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4305607', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1792, 'n_words': 308}