-
Notifications
You must be signed in to change notification settings - Fork 458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to write unit test (APITestCase) include activation email ! #678
Comments
Actually when testing to create account (user sign-up) it does NOT send activation email |
So when does the email is sent ? @saaz181 |
I set up my settings like you explained but it doesn't send any email
|
Perhaps you forget to change the environment variable. Or you can do like this if you want ENVIROMENT=test python manage.py test Make you sure you run on the test enviroment becausethe setting wont be able to load the test setting config. you can check by adding print value on the |
I don't disable Here's my example using pytest: @pytest.fixture
def email_activation_url():
return reverse('user-activation')
@pytest.mark.django_db
def test_email_activation(
api_client: APIClient,
registration_url: str,
email_activation_url: str,
):
expected_user = dict(
email="[email protected]",
username="tester",
password="superTester1sPass.",
)
response: Response = api_client.post(
path=registration_url,
data=expected_user,
)
assert response.status_code == status.HTTP_201_CREATED, response.data
user = UserModel.objects.filter(email=expected_user['email']).first()
assert user.is_active is False
assert len(mail.outbox) == 1
first_email = mail.outbox[0]
# Check that we received activation email
activation_email_re = re.compile(
r'.*Please go to the following page to activate account:.*',
re.I + re.M,
)
activation_re_result = activation_email_re.search(first_email.body)
assert activation_re_result is not None
# regex template is based on string '#/activate/{uid}/{token}'. Check Djoser Settings
activation_url_template = r'/activate/(?P<uid>.*)/(?P<token>.*)[\"\n>]'
activation_url_re = re.search(
activation_url_template,
first_email.body,
re.U + re.I + re.M,
)
assert activation_url_re is not None
uid = activation_url_re.groupdict().get('uid')
assert uid is not None
token = activation_url_re.groupdict().get('token')
assert token is not None
response = api_client.post(
path=email_activation_url,
data={
'uid': uid,
'token': token
}
)
assert response.status_code == status.HTTP_204_NO_CONTENT, response.data
user.refresh_from_db()
assert user.is_active is True |
My registeration follow : register --> get uid & token via email --> use ui & token for activation --> login--> accsess_token.
So if easily if I disable the feature SEND_ACTIVATION_EMAIL, but if I want to write a test with this feature, how to do it? And this is necessary for the test?
The text was updated successfully, but these errors were encountered: