我可以使用 Django 自动化测试检查用户电子邮件验证是否成功吗?
Can I check if user email verification was successful using Django automated testing?
我的 AccountActivationView 期待对路径的 GET 请求('email/confirm/',如果密钥存在,AccountActivationView 调用激活函数并在用户配置文件中切换 is_active。
我正在尝试使用 Django TestCase 对此功能进行测试,但它没有产生我期望的结果。视图 class 将客户端重定向到正确的位置,但用户帐户的 is_active 状态没有改变。
谁能给我指明正确的方向,好吗?
class TestUserAccounts(TestCase):
def setUp(self):
self.client = Client()
# Initial user data
self.username = 'TestTest'
self.email = 'test@test.com'
self.password = 'test123test'
# Creating user
User.objects.create_user(
email=self.email, username=self.username, password=self.password)
def test_activating_user(self):
'''Activating user account using link in the email'''
user_email_activation_status = EmailActivation.objects.get(
email=self.email).activated
user = User.objects.get(email=self.email).is_active
activation_key = EmailActivation.objects.get(
email=self.email).key
# The initial state of account and email should be inactive
self.assertEqual(user_email_activation_status, False)
self.assertEqual(user, False)
# Activating the account with get request to email/confirm/<key>
activation = self.client.get(
reverse('accounts:email-activate', kwargs={'key': activation_key}))
print(activation)
# Checking if activation was successful
self.assertEqual(user_email_activation_status, True)
self.assertEqual(user, True)
您只需在点击 URL 激活用户后重新运行查询以检查用户状态
class TestUserAccounts(TestCase):
def setUp(self):
self.client = Client()
# Initial user data
self.username = 'TestTest'
self.email = 'test@test.com'
self.password = 'test123test'
# Creating user
User.objects.create_user(
email=self.email, username=self.username, password=self.password)
def test_activating_user(self):
'''Activating user account using link in the email'''
activation_key = EmailActivation.objects.get(
email=self.email).key
user_email_activation_status = EmailActivation.objects.get(
email=self.email).activated
user = User.objects.get(email=self.email).is_active
# The initial state of account and email should be inactive
self.assertEqual(user_email_activation_status, False)
self.assertEqual(user, False)
# Activating the account with get request to email/confirm/<key>
activation = self.client.get(
reverse('accounts:email-activate', kwargs={'key': activation_key}))
print(activation)
# Checking if activation was successful
# get the value again after calling the route to activate the user
user_email_activation_status = EmailActivation.objects.get(
email=self.email).activated
user = User.objects.get(email=self.email).is_active
self.assertEqual(user_email_activation_status, True)
self.assertEqual(user, True)
我的 AccountActivationView 期待对路径的 GET 请求('email/confirm/',如果密钥存在,AccountActivationView 调用激活函数并在用户配置文件中切换 is_active。 我正在尝试使用 Django TestCase 对此功能进行测试,但它没有产生我期望的结果。视图 class 将客户端重定向到正确的位置,但用户帐户的 is_active 状态没有改变。 谁能给我指明正确的方向,好吗?
class TestUserAccounts(TestCase):
def setUp(self):
self.client = Client()
# Initial user data
self.username = 'TestTest'
self.email = 'test@test.com'
self.password = 'test123test'
# Creating user
User.objects.create_user(
email=self.email, username=self.username, password=self.password)
def test_activating_user(self):
'''Activating user account using link in the email'''
user_email_activation_status = EmailActivation.objects.get(
email=self.email).activated
user = User.objects.get(email=self.email).is_active
activation_key = EmailActivation.objects.get(
email=self.email).key
# The initial state of account and email should be inactive
self.assertEqual(user_email_activation_status, False)
self.assertEqual(user, False)
# Activating the account with get request to email/confirm/<key>
activation = self.client.get(
reverse('accounts:email-activate', kwargs={'key': activation_key}))
print(activation)
# Checking if activation was successful
self.assertEqual(user_email_activation_status, True)
self.assertEqual(user, True)
您只需在点击 URL 激活用户后重新运行查询以检查用户状态
class TestUserAccounts(TestCase):
def setUp(self):
self.client = Client()
# Initial user data
self.username = 'TestTest'
self.email = 'test@test.com'
self.password = 'test123test'
# Creating user
User.objects.create_user(
email=self.email, username=self.username, password=self.password)
def test_activating_user(self):
'''Activating user account using link in the email'''
activation_key = EmailActivation.objects.get(
email=self.email).key
user_email_activation_status = EmailActivation.objects.get(
email=self.email).activated
user = User.objects.get(email=self.email).is_active
# The initial state of account and email should be inactive
self.assertEqual(user_email_activation_status, False)
self.assertEqual(user, False)
# Activating the account with get request to email/confirm/<key>
activation = self.client.get(
reverse('accounts:email-activate', kwargs={'key': activation_key}))
print(activation)
# Checking if activation was successful
# get the value again after calling the route to activate the user
user_email_activation_status = EmailActivation.objects.get(
email=self.email).activated
user = User.objects.get(email=self.email).is_active
self.assertEqual(user_email_activation_status, True)
self.assertEqual(user, True)