Files
PyRIGS/users/tests/test_users.py
AJ 08171767a0 Port to Django 5.2 (#635)
* Port to Django 5.2

* Pin pluggy to 1.2.0

Any newer and the mystery importlib metadata error appears. Weird! >_>

* Update for premailer changed default

* Update view logic for is_ajax being changed to a template context processor

* Port a few more tests to pytest proper

Having two distinct test flavours is giving me a headache

* Version 1 dockerfile

Makes a VERY big image, I suspect we can optimise this a lot...

* Optimise dockerfile a little lot a bit

* fix(users): change logout link to POST request

* fix(tests): fix some syntax errors in test code

still got lots of failed tests :(

* fix: replace deprecated Django APIs

* ci: update Dockerfile

* ci: update Dockerfile

* fix(dependencies+tests): update EoL/vulnerable dependencies and improve test stability

* Upgrade Python from 3.10 to 3.12
* Update frontend dependencies, replace node-sass with sass

* chore(logging): ignore dangling obj reference warning from pypdf

* ci: update compose.yml for prod deployment

- Add Nginx as a reverse proxy
- Add cert-selfsign.sh for generating self-signed certificates
- Add certbot-issue.sh for Let's Encrypt certificate issuance and renewal
- Add cron-install.sh and cron-uninstall.sh for system cron management
- Add .env.example as an environment variable template

* style: reformat code

* ci: add more sleep trying to pass tests

* ci: ignore browser-based tests during ci testing

* fix: create home for the new user during Dockerfile building

* chore: remove heroku conf file

* fix: RIGS not franken anymore

* fix: restore is_ajax as a boolean and split out the context processor

* test: remove Selenium interaction tests and their CI workarounds

* fix: harden production settings (CSRF origins, env casts, ADMINS) and drop dead debug toolbar code

* deploy: serve static files from nginx, pin PGDATA, and make cert scripts loud on failure

* build: switch .dockerignore to an allowlist

* build: drop unused dependencies, soft-pin the rest and target Python 3.14

* build: move image to Python 3.14 / Node 24 and force DEBUG off

* ci: add dependabot config for uv, npm, docker and actions

* build: narrow Sass deprecation silencing to @import and require Node 24

* build: provide placeholder env for collectstatic now that DEBUG is off in the image

* deploy: add plain-HTTP nginx config for local development and use it in compose

* deploy: remove self-signed cert script

* build: make the image multi-arch with official node and python base images

* ci: lint and test on PRs, build the image on PRs and push it to GHCR on master

* docs: add local running and sample data instructions

* fix: report a form error instead of crashing when big power has no Power MIC

* fix: upgrade pypdf and urllib3 to patched releases

* fix: apply non-breaking npm audit fixes

* fix: strip whitespace in ALLOWED_HOSTS and drop misleading HSTS preload

* deploy: add a pyrigs healthcheck and make nginx wait for it

* build: pin the uv image version

* fix: correct the check-in person picker condition and use BeautifulSoup's string argument

* refactor: replace unique_together with UniqueConstraint

* build: replace pycodestyle with ruff and fix what it found

Removes unused imports and variables, and fixes a few real problems it
surfaced:
- EventCheckIn.active() referenced an undefined name and raised NameError;
  it now returns whether the check-in has no end time
- RIGS.admin defined EventChecklistAdmin twice; the second is now
  PowerTestRecordAdmin
- RIGS/tests/conftest.py used date/timedelta without importing them
- the signal-registering imports in apps.py are kept with noqa

pycodestyle config in setup.cfg is dropped.

* style: format the codebase with ruff

* style: normalise line endings, trailing whitespace and end-of-file newlines

* ci: run ruff and file hygiene through prek, and document it

---------

Co-authored-by: Hang <me@hangxu.me>
Co-authored-by: Joe Banks <joe@jb3.dev>
2026-10-01 23:43:41 +01:00

161 lines
6.6 KiB
Python

import re
import time
from django.core import mail
from django.test import LiveServerTestCase
from django.test.utils import override_settings
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from PyRIGS.tests.base import create_browser
from RIGS import models
# Functional Tests
class UserRegistrationTest(LiveServerTestCase):
def setUp(self):
self.browser = create_browser()
self.browser.implicitly_wait(5) # Set implicit wait session wide
def tearDown(self):
self.browser.quit()
@override_settings(DEBUG=True)
def test_registration(self):
# Navigate to the registration page
self.browser.get(self.live_server_url + "/user/register/")
title_text = self.browser.find_element(By.TAG_NAME, "h3").text
self.assertIn("User Registration", title_text)
# Check the form invites correctly
username = self.browser.find_element(By.ID, "id_username")
self.assertEqual(username.get_attribute("placeholder"), "Username")
email = self.browser.find_element(By.ID, "id_email")
self.assertEqual(email.get_attribute("placeholder"), "E-mail")
# If this is correct we don't need to test it later
self.assertEqual(email.get_attribute("type"), "email")
password1 = self.browser.find_element(By.ID, "id_password1")
self.assertEqual(password1.get_attribute("placeholder"), "Password")
self.assertEqual(password1.get_attribute("type"), "password")
password2 = self.browser.find_element(By.ID, "id_password2")
self.assertEqual(password2.get_attribute("placeholder"), "Password confirmation")
self.assertEqual(password2.get_attribute("type"), "password")
first_name = self.browser.find_element(By.ID, "id_first_name")
self.assertEqual(first_name.get_attribute("placeholder"), "First name")
last_name = self.browser.find_element(By.ID, "id_last_name")
self.assertEqual(last_name.get_attribute("placeholder"), "Last name")
initials = self.browser.find_element(By.ID, "id_initials")
self.assertEqual(initials.get_attribute("placeholder"), "Initials")
# No longer required for new users
# phone = self.browser.find_element(By.ID, 'id_phone')
# self.assertEqual(phone.get_attribute('placeholder'), 'Phone')
# Fill the form out incorrectly
username.send_keys("TestUsername")
email.send_keys("test@example.com")
password1.send_keys("correcthorsebatterystaple")
# deliberate mistake
password2.send_keys("correcthorsebatterystapleerror")
first_name.send_keys("John")
last_name.send_keys("Smith")
initials.send_keys("JS")
# phone.send_keys('0123456789')
time.sleep(1)
self.browser.switch_to.frame(self.browser.find_element(By.TAG_NAME, "iframe"))
self.browser.find_element(By.ID, "anchor").click()
self.browser.switch_to.default_content()
time.sleep(3)
# Submit incorrect form
submit = self.browser.find_element(By.XPATH, "//input[@type='submit']")
submit.click()
time.sleep(2)
# Restablish error fields
password1 = self.browser.find_element(By.ID, "id_password1")
password2 = self.browser.find_element(By.ID, "id_password2")
# Read what the error is
alert = self.browser.find_element(By.CSS_SELECTOR, ".alert-danger").text
# TODO Use regex matching to handle smart/unsmart quotes...
self.assertIn("password fields didn", alert)
# Passwords should be empty
self.assertEqual(password1.get_attribute("value"), "")
self.assertEqual(password2.get_attribute("value"), "")
# Correct error
password1.send_keys("correcthorsebatterystaple")
password2.send_keys("correcthorsebatterystaple")
# Submit again
password2.send_keys(Keys.ENTER)
# Check we have a success message
alert = self.browser.find_element(By.CSS_SELECTOR, ".alert-success").text
self.assertIn("register", alert)
self.assertIn("email", alert)
# Check Email
self.assertEqual(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertIn('John Smith "JS" activation required', email.subject)
urls = re.findall(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", email.body)
self.assertEqual(len(urls), 1)
mail.outbox = [] # empty this for later
# Follow link
self.browser.get(urls[0]) # go to the first link
# Complete registration
title_text = self.browser.find_element(By.TAG_NAME, "h2").text
self.assertIn("Complete", title_text)
# Test login
self.browser.get(self.live_server_url + "/user/login")
username = self.browser.find_element(By.ID, "id_username")
self.assertEqual(username.get_attribute("placeholder"), "Username")
password = self.browser.find_element(By.ID, "id_password")
self.assertEqual(password.get_attribute("placeholder"), "Password")
self.assertEqual(password.get_attribute("type"), "password")
# Expected to fail as not approved
username.send_keys("TestUsername")
password.send_keys("correcthorsebatterystaple")
password.send_keys(Keys.ENTER)
# Test approval
profileObject = models.Profile.objects.all()[0]
self.assertFalse(profileObject.is_approved)
# Read what the error is
alert = self.browser.find_element(By.CSS_SELECTOR, "div.alert-danger").text
self.assertIn("approved", alert)
# Approve the user so we can proceed
profileObject.is_approved = True
profileObject.save()
# Retry login
self.browser.get(self.live_server_url + "/user/login")
username = self.browser.find_element(By.ID, "id_username")
username.send_keys("TestUsername")
password = self.browser.find_element(By.ID, "id_password")
password.send_keys("correcthorsebatterystaple")
password.send_keys(Keys.ENTER)
time.sleep(2)
# Check we are logged in
udd = self.browser.find_element(By.CLASS_NAME, "navbar").text
self.assertIn("Hi John", udd)
# Check all the data actually got saved
self.assertEqual(profileObject.username, "TestUsername")
self.assertEqual(profileObject.first_name, "John")
self.assertEqual(profileObject.last_name, "Smith")
self.assertEqual(profileObject.initials, "JS")
# self.assertEqual(profileObject.phone, '0123456789')
self.assertEqual(profileObject.email, "test@example.com")
# All is well