Domanda

Sto tentando di accedere al client di test utilizzando la sua funzione di accesso integrata. Sto provando a test di prova unitaria e devo accedere per testare alcune di esse. Ho cercato di farlo troppo a lungo e ho bisogno di aiuto. Alcune note:

Create_user () crea un utente valido, è stato utilizzato in altre posizioni.

Da quello che ho visto su client.login () restituisce un booleano, quando ho eseguito i miei test il fallimento era "false non è vero", quindi questo sembra essere corretto.

L'unico modo in cui ho effettuato l'accesso correttamente è chiamando client.post ("/my/login/url", {nome utente e password in dict.}) Tuttavia, per qualche motivo non è stato effettuato l'accesso per tutti i miei casi di test che trovo molto strano.

def setUp(self):
    """
    Initializes the test client and logs it in.
    """
    self.user = create_user()
    self.logged_in = self.client.login(username=self.user.username, password=self.user.password)

def test_valid(self):
    self.assertTrue(self.logged_in)

L'ho cambiato in quanto segue:

def setUp(self):
    """
    Initializes the test client and logs it in.
    """
    self.password = "password"
    self.user = create_user(password=self.password)
    self.logged_in = self.client.login(username=self.user.username, password=self.password)

Non riesce ancora ad accedere.

Creare l'utente è in classe "statico" e ha un User_Count inizializzato come 0, la funzione è la seguente:

def create_user(username=None, password=None, email=None, is_superuser=False):
    if username is None:
        username = "user%d" % Static.user_count
        while User.objects.filter(username=username).count() != 0:
            Static.user_count += 1
            username = "user%d" % Static.user_count
    if password is None:
        password = "password"
    if email is None:
        email="user%d@test.com" % Static.user_count

    Static.user_count += 1
    user = User.objects.create(username=username, password=password,   is_superuser=is_superuser)
È stato utile?

Soluzione

Non è possibile accedere direttamente alla password. Il password L'attributo è crittografato. (Vedere Gestione della password in Django.)

Ad esempio, ecco output di esempio di password.

>>> user = User.objects.create_user(username='asdf', email='asdf@example.com', password='xxxx')
>>> user.password
'sha1$166e7$4028738f0c0df0e7ec3cec06843c35d2b5a1aae8'

Come potete vedere, user.password non è xxxx Ho dato.

Modificherei create_user Per accettare il parametro della password opzionale. E passare una password entrambi a create_user, e client.login come segue:

def setUp(self):
    """
    Initializes the test client and logs it in.
    """
    password = 'secret'
    self.user = create_user(password=password)
    self.logged_in = self.client.login(username=self.user.username, password=password)

AGGIORNARE

create_user dovrebbe usare User.objects.create_user invece di User.objects.create. E l'oggetto utente creato deve essere restituito:

def create_user(username=None, password=None, email=None, is_superuser=False):
    if username is None:
        username = "user%d" % Static.user_count
        while User.objects.filter(username=username).count() != 0:
            Static.user_count += 1
            username = "user%d" % Static.user_count
    if password is None:
        password = "password"
    if email is None:
        email="user%d@test.com" % Static.user_count

    Static.user_count += 1
    user = User.objects.create_user(username=username, password=password)
    #                   ^^^^^^^^^^^
    user.is_superuser = is_superuser
    user.save()
    return user # <---
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top