无法为我的测试注册活跃用户

时间:2019-07-14 05:32:55

标签: django django-rest-framework django-allauth django-rest-auth

我正在尝试使用django-rest-auth和以下代码编写一些测试:

this.forma.valueChanges.subscribe(value => {
 const sum = calculateSum(value);
 this.forma.get('precioFinal').setValue(sum);
});

只要我不使用def create_user(username='john', email='johndoe@test.com', password='doe'): user = get_user_model().objects.create( username=username, email=email, is_active=False) if password: user.set_password(password) else: user.set_unusable_password() user.save() return user def test_jwt_auth(): username = 'user' email = 'user@foo.com' password = 'pass' create_user(username=username, email=email, password=password) resp = client.post(REST_LOGIN_URL, {'email': email, 'password': password}) assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) 更改/创建用户,client.post就可以正常工作。当我这样做时,出现以下错误:

is_active=True

我不知道如何绕过此错误。我需要我的用户使用经过验证的电子邮件保持活动状态才能继续测试。任何想法?谢谢。

1 个答案:

答案 0 :(得分:0)

create_user在我看来是BaseUserManager,所以也许您应该将其更改为这样

def create_user(self, username=None, email=None, password=None):
    if not username:
        raise ValueError('Users must have an Username')
    if not email:
        raise ValueError('Users must have an email address')
    try:
        user = self.get(email=self.normalize_email(email))
    except self.model.DoesNotExist:
        user = self.model(username=username, email=self.normalize_email(email))
        password_validation.validate_password(password)
        user.set_password(password)
    return user

现在您可以通过用户参数并进行测试了。