Using: Django 4.0.4 Python 3.9.13
Running: python ../../manage.py test website.functional_tests.test_loggedOutUser.HomePageTest.test_user_email_registration
Files: settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'ProdComment': 'This is the PROD DB.',
'TEST': {
'NAME': BASE_DIR / 'TEST_.db.sqlite3',
'MIRROR': 'default',
'COMMENT': "This is the TEST DB.",
},
}
}
test_loggedOutUser.py content:
from selenium import webdriver
from django.test import TestCase as TC
from django.db import connections as DBC
from django.db import connection as DBCON
from allauth.utils import get_user_model as getusermodel
import time
from django.core.exceptions import ObjectDoesNotExist
allUsersALLAUTH = getusermodel()
# Method used to check if a test user exists in the DB and then delete this user
# Test if account creation works.
# Returns 1 if user does not exist, 0 if deleted successfully.
def delTestUser():
username = 'TheTester'
allUsersALLAUTH = getusermodel()
print("DELTESTUSER, allUsersALLAUTH.objects.all(): ", allUsersALLAUTH.objects.all())
print("DELTESTUSER, Testing if we can delete the test user.")
try:
userToDelete = allUsersALLAUTH.objects.get(username=username)
print("DELTESTUSER, usertodelete: ", userToDelete)
except ObjectDoesNotExist:
print("DELTESTUSER, The user you tried to find does not exist.")
# Trying to see which DB is used to no avail.
print("DELTESTUSER: ", DBC.databases['default']['NAME'])
return 1
else:
print("DELTESTUSER - ELSE: ", DBC.databases['default']['NAME'])
print("DELTESTUSER, We found the user and can delete him.")
userToDelete.delete()
print("DELTESTUSER, The user has now been deleted.")
# This confirms that the user is effectively gone from whatever DB is used.
print("DELTESTUSER: allauthusers AFTER DELETION:", allUsersALLAUTH.objects.all())
return 0
class HomePageTest(TC):
print("Logged out User: In HP test class.")
def setUp(self):
self.browser = webdriver.Firefox()
print("In setUp, logged out user tests.")
def tearDown(self):
self.browser.quit()
print("Logged out User: Tearing down...")
def test_user_email_registration(self):
# Check if we can do the rego test.
print("REGO METHOD: Calling deltest.")
if delTestUser() == 1:
print("REGO METHOD: The user did not exist, continuing with test.")
else:
print("REGO METHOD: The test user existed, was deleted, and we can proceed with test.")
print("REGO METHOD: waiting after potential del of user!")
time.sleep(2)
# Does not reveal the DB used but only quotes from settings file.
print("REGO METHOD DB USED: ", DBC.databases['default']['NAME'])
# Go to the signup page
self.browser.get('http://localhost:8000/accounts/signup/')
element = self.browser.find_element(by='id', value='id_email')
element.send_keys('emailaddress@example.com')
element = self.browser.find_element(by='id', value='id_email2')
element.send_keys('emailaddress@example.com')
element = self.browser.find_element(by='id', value='id_username')
element.send_keys('TheTester')
element = self.browser.find_element(by='id', value='id_password1')
element.send_keys('HereIsMyPWDForAllToSee:)')
element = self.browser.find_element(by='id', value='id_password2')
element.send_keys('HereIsMyPWDForAllToSee:)')
element = self.browser.find_element(by='id', value='signUpBtn')
element.click()
print("REGO METHOD: waiting 5 secs. after signup click.")
time.sleep(5)
url = self.browser.current_url
print("REGO METHOD: We are now at URL: ", url)
print("We now should have a new user in the DB:")
print("########################################")
print("Current content of DB after adding user:")
# No new user appears. Magic DB switch?
print("REGO METHOD: printing allauth users: ", allUsersALLAUTH.objects.all())
time.sleep(2)
source = self.browser.page_source
email_result_success = 'We have sent an e-mail to you for verification. Follow the link provided to finalize the signup process. If you do not see the verification e-mail in your main inbox, check your spam folder. Please contact us if you do not receive the verification e-mail within a few minutes.'
username_exists_failure = 'A user is already registered with this e-mail address.'
targetURL = r"http://localhost:8000/accounts/confirm-email/"
# We just check for the standard error message in the source code.
if username_exists_failure in source:
print("REGO METHOD: We could not create the test account, since it already exists!")
self.fail("REGO METHOD: We cannot create the test account, since it already exists!")
if (email_result_success in source) and (url == targetURL):
print("REGO METHOD: We are at the right URL and got the correct text.")
print("REGO METHOD: Cleaning up the testing account.")
# calling delTestUser in order to cleanup.
if delTestUser(self) == 0:
print("REGO METHOD: Successfully deleted the test account!")
else:
print("REGO METHOD: We failed to remove the test account!")
else:
print("REGO METHOD: test found in source: ", (email_result_success in source))
print("REGO METHOD: Tgt URL found: ", (url == targetURL))
print(url, targetURL)
print("REGO METHOD: deltestuser:", delTestUser())
time.sleep(2)
self.fail("REGO METHOD: We did not get sent to the signup page, or received incorrect text!")
Test output in terminal
(venv) Mac:functional_tests User$ python ../../manage.py test website.functional_tests.test_loggedOutUser.HomePageTest.test_user_email_registration
Logged out User: In HP test class.
Found 1 test(s).
System check identified no issues (0 silenced).
In setUp, logged out user tests.
REGO METHOD: Calling deltest.
DELTESTUSER, allUsersALLAUTH.objects.all(): <QuerySet [<User: adminuser>, <User: seconduser>, <User: me>, <User: testuser>, <User: TheTester>]>
DELTESTUSER, Testing if we can delete an account.
DELTESTUSER, usertodelete: TheTester
DELTESTUSER - ELSE: [...] db.sqlite3
DBC vendor in else: <django.utils.connection.ConnectionProxy object at 0x10e501400>
DELTESTUSER, We found the user and can delete him.
DELTESTUSER, The user has now been deleted.
DELTESTUSER: allauthusers AFTER DELETION: <QuerySet [<User: adminuser>, <User: seconduser>, <User: me>, <User: testuser>]>
REGO METHOD: The test user existed, was deleted, and we can proceed with test!
REGO METHOD: waiting after potential del of user!
REGO METHOD DB USED: [...]db.sqlite3
REGO METHOD: waiting 5 secs. after signup click.
REGO METHOD: We are now at URL: http://localhost:8000/accounts/signup/
We now should have a new user in the DB:
########################################
Current content of DB after adding user:
REGO METHOD: printing allauth users: <QuerySet [<User: adminuser>, <User: seconduser>, <User: me>, <User: testuser>]>
REGO METHOD: We could not create the test account, since it already exists!
Logged out User: Tearing down...
F
======================================================================
FAIL: test_user_email_registration (website.functional_tests.test_loggedOutUser.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "[...] /website/functional_tests/test_loggedOutUser.py", line 323, in test_user_email_registration
self.fail("REGO METHOD: We cannot create the test account, since it already exists.")
AssertionError: REGO METHOD: We cannot create the test account, since it already exists.
----------------------------------------------------------------------
Ran 1 test in 12.668s
FAILED (failures=1)
Updates
A previous question of mine got "cancelled" because of a supposed lack of source code. There was source code to support the question, but apparently not enough? So, I tried a different approach this time and posted my problem described through code, output and annotations in the output mainly... Anyways, here are some clarifications.
Goals / summary
- Provide a test (.test_loggedOutUser.HomePageTest.test_user_email_registration) that can check if my website's sign-up feature (still) works.
- I check if a certain user exists in allauth, if yes: delete this user, if no: continue with the front-end testing and create the user through clicks. Delete the user again as a final cleanup action.
Observations
- From the output it is clear that the user gets deleted, I just cannot see from WHERE exactly. Test DB? Main DB? There is no way to get feedback as to what channel / connection was used. In other questions people query something like "connection.vendor" or even settings.py, which is also not helpful.
- There is no documentation explaining how to use the DB settings for testing and what happens with different settings.
- time.sleep() breaks are necessary otherwise there are locking errors from the DB. Sadly, here as well, there is no mention of WHERE exactly these issues come from, what the underlying connection was.
Issues As you can see this is a very primitive front-end test. However, the user "TheTester" will get found (see lines around "DELTESTUSER, usertodelete: TheTester"), deleted and then we are unable to create the user though the web interface because it still exists (probably in a different DB?). I tried to figure out which DB is being used, to no avail. No idea what I am doing wrong or what is broken, but 2 different DBs appear to be used during the test.