0
모델에 django.contrib.auth.model.User
이 있고 is_active
속성이 False
인 모델이 있습니다. 이 사용자는 자신을 로그인 할 수 없습니다. 관리자 사용자는 django.contrib.auth.authenticate()
을 사용하여 작성한 사용자 정의 코드를 사용하여 해당 사용자를 대신해야합니다.Django 테스트에서 비활성 사용자로 로그인
,
class ParticipantFactory(factory.django.DjangoModelFactory):
class Meta:
model = Participant
user = factory.SubFactory(InactiveUserFactory)
first_location = factory.SubFactory(LocationFactory)
location = factory.SubFactory(LocationFactory)
study_id = FuzzyText(prefix='7')
class BasicTest(TestCase):
def setUp(self):
self.u = User.objects.create_user(
'test_user', '[email protected]', 'test_pass')
self.u.is_active = False
self.u.save()
self.participant = ParticipantFactory(user=self.u)
# This works but has no effect on the tests
auth = authenticate(username=self.u.username, password='test_pass')
assert(auth is not None)
# This fails because the user is inactive
# login = self.client.login(username=self.u.username,
# password='test_pass')
# assert(login is True)
사람이 어떻게 비활성 사용자를 인증하는 생각이 있습니까?
class BasicTest(TestCase):
def setUp(self):
u = InactiveUserFactory()
u.set_password('test_pass')
u.save()
self.participant = ParticipantFactory(user=u)
self.u = self.participant.user
self.u.is_active = True
self.u.save()
login = self.client.login(username=self.u.username,
password='test_pass')
assert(login is True)
self.u.is_active = False
self.u.save()
: