如何测试电子邮件确认? [英] How to test email confirmation?

查看:96
本文介绍了如何测试电子邮件确认?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用django-rest-auth对电子邮件确认视图进行测试.这是我所拥有的:

I'm trying to make a test for the email confirmation view with django-rest-auth. Here is what I have:

def test_verify_email(self):
    # Verify email address
    username = 'userTest'
    payload = {
        'email': 'test@example.com',
        'password1': 'TestpassUltra1',
        'password2': 'TestpassUltra1',
        'username': username,
    }
    res = self.client.post(REGISTER_USER_URL, payload)

    self.assertEqual(res.status_code, status.HTTP_201_CREATED)
    user = get_user_model().objects.get(email='test@example.com')
    # TODO retrieve the confirmation key from the user
    resp = self.client.post(VERIFY_USER_URL, {'key': ''})
    self.assertEqual(resp.status_code, status.HTTP_200_OK)

self.client.post(REGISTER_USER_URL,有效负载)将发送一封包含确认代码的电子邮件,尽管我知道我可以使用 django.core.mail.outbox检索该代码在代码中,我宁愿不这样做,因为我必须解析电子邮件内容以查找确认代码(如果电子邮件发生更改,这可能会杀死我的测试).我找不到此代码存储在数据库中的任何位置,它似乎确实只存在于所发送电子邮件的正文中.

self.client.post(REGISTER_USER_URL, payload) will send an email with the confirmation code in it and while I know I could retrieve that code with django.core.mail.outbox in the code I rather not do that as I will have to parse the email content to look for the confirmation code (which can kill my test if the email changes). I couldn't find this code stored anywhere in the DB, it seems to really exist only inside the body of the email sent.

我的问题是:是否可以在不解析电子邮件的情况下在我的测试中恢复此验证码?我只想检索它以启动我的 self.client.post(VERIFY_USER_URL,{'key':``}}).

My question is: Is it possible to recover this verification code in my test without parsing the email? I just want to retrieve it to launch my self.client.post(VERIFY_USER_URL, {'key': ''}) with it.

以下是电子邮件内容的示例:

Here is the example of the content of an email:

Hello from example.com!

You're receiving this e-mail from NomadSpeed because user detro1 has given yours as an e-mail address to connect their account.

To confirm this is correct, go to http://127.0.0.1:8000/registration/account-confirm-email/Mg:1hmqdS:J-2jGV028nd4qZQ3lPmFgXGFhsM/

Thank you from example.com!
example.com

我需要的是 Mg:1hmqdS:J-2jGV028nd4qZQ3lPmFgXGFhsM .谢谢.

推荐答案

我认为解决此问题的最佳方法是使用正则表达式来匹配预期的链接,并从中解析出所需的位.可以担心的是,对电子邮件所做的更改可能会破坏测试,这很公平,但是在这种情况下,您正在测试电子邮件中的 link 是否有效,并且如果此更改有所改变,则可能会破坏您的测试.如果您更改了其他一些文本,如问候语,介绍等,则不会影响链接和令牌的正则表达式.

I think the best way to approach this is to use a regex to match the expected link, and parse the bits you want out of that. It's fair to worry that changes to the email might break the tests, but in this case you are testing that the link in the email is valid, and if this changes in some way, perhaps it should break your tests. If you change some of the other text, like the greeting, introduction, etc. it won't affect the regex for your link and token.

无论如何,这就是我将如何构造该测试的方法:

Anyway here is how I would structure that test:

import re

def test_verify_email(self):
    # Verify email address
    username = 'userTest'
    payload = {
        'email': 'test@example.com',
        'password1': 'TestpassUltra1',
        'password2': 'TestpassUltra1',
        'username': username,
    }
    response = self.client.post(REGISTER_USER_URL, payload)
    self.assertEqual(response.status_code, status.HTTP_201_CREATED)
    user = get_user_model().objects.get(email='test@example.com')

    # Get token from email
    token_regex = r"registration\/account-confirm-email\/([A-Za-z0-9:\-]+)\/"
    email_content = django.core.mail.outbox[0].body
    match = re.search(token_regex, email_content)
    assert match.groups(), "Could not find the token in the email" # You might want to use some other way to raise an error for this
    token = match.group(1)

    # Verify 
    response = self.client.post(VERIFY_USER_URL, {'key': token})
    self.assertEqual(response.status_code, status.HTTP_200_OK)

您甚至可以断言该链接路径的正确性,因此,如果有人更改了该链接,您将无法通过测试以表明某些内容可能会损坏.

You could perhaps even assert the correctness of the path of that link, so if someone changes that link you'll have a failing test to indicate that something may break.

这篇关于如何测试电子邮件确认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆