summaryrefslogtreecommitdiffstats
path: root/core/TESTS/test_logins.py
diff options
context:
space:
mode:
Diffstat (limited to 'core/TESTS/test_logins.py')
-rw-r--r--core/TESTS/test_logins.py171
1 files changed, 78 insertions, 93 deletions
diff --git a/core/TESTS/test_logins.py b/core/TESTS/test_logins.py
index 8596205..3954dba 100644
--- a/core/TESTS/test_logins.py
+++ b/core/TESTS/test_logins.py
@@ -14,56 +14,53 @@ from django.test import Client, TestCase
import troggle.settings as settings
from troggle.core.models.wallets import Wallet
from troggle.core.models.troggle import Expedition
+from django.contrib.auth.models import User
+from troggle.core.utils import current_expo
+
+current_year = current_expo()
+
+def create_user(name=None, last_name="Caver", is_superuser=False):
+ u = User()
+ u.username = name
+ u.email = f"philip.sargent+{name}@gmail.com"
+ u.first_name, u.last_name = name, last_name
+ u.set_password("secretword") # all test users have same password
+ u.save()
+ return u
+
class DataTests(TestCase):
- """These check that the NULL and NON-UNIQUE constraints are working in the database"""
+ """These check that the NULL and NON-UNIQUE constraints are working in the database
+
+ no tests here... !"""
@classmethod
def setUpTestData(cls):
pass
def setUp(self):
- from django.contrib.auth.models import User
-
- u = User()
- u.pk = 9000
- u.user_id = 8000
- u.username, u.password = "stinker", "secretword"
- u.email = "philip.sargent+SP@gmail.com"
- u.first_name, u.last_name = "Stinker", "Pinker"
- u.save()
- self.user = u
-
+ create_user(name="expo")
+
def tearDown(self):
- # self.member.delete() # must delete member before user
- # self.user.delete() # horrible crash, why?
- pass
+ Users.objects.all().delete()
-class FixturePageTests(TestCase):
- # The fixtures have a password hash which is compatible with plain-text password 'secretword'
- fixtures = ["auth_users"]
+class LoginTests(TestCase):
def setUp(self):
- from django.contrib.auth.models import User
-
- self.user = User.objects.get(username="expotest")
+ create_user(name="expo")
+ create_user(name="expotest")
+ create_user(name="expotestadmin", is_superuser = True)
def tearDown(self):
- pass
+ User.objects.all().delete()
def test_fix_admin_login_fail(self):
c = self.client
- from django.contrib.auth.models import User
-
u = User.objects.get(username="expotest")
- self.assertTrue(u.is_active, "User '" + u.username + "' is INACTIVE")
-
- logged_in = c.login(username=u.username, password="secretword") # fails to work if password=u.password !
- self.assertTrue(logged_in, "FAILED to login as '" + u.username + "'")
-
+
response = c.get("/admin/")
content = response.content.decode()
# with open('admin-op.html', 'w') as f:
@@ -75,17 +72,16 @@ class FixturePageTests(TestCase):
class PostTests(TestCase):
"""Tests walletedit form"""
- fixtures = ["auth_users"]
-
@classmethod
def setUpTestData(cls):
pass
def setUp(self):
- from django.contrib.auth.models import User
-
- self.user = User.objects.get(username="expotest")
- self.client = Client()
+ create_user(name="expo")
+ create_user(name="expotestadmin", is_superuser = True)
+ self.user = create_user(name="expotest")
+
+ c = self.client
testyear = "2022"
wname = f"{testyear}:00"
@@ -102,14 +98,17 @@ class PostTests(TestCase):
e.save()
self.expedition = e
+ def tearDown(self):
+ User.objects.all().delete()
+ Wallet.objects.all().delete()
+ Expedition.objects.all().delete()
+
def test_file_permissions(self):
"""Expect to be allowed to write to SCANS_ROOT, DRAWINGS_DATA, SURVEX_DATA, EXPOWEB
Need to login first.
"""
c = self.client
- from django.contrib.auth.models import User
-
- u = User.objects.get(username="expotest")
+ u = self.user
testyear = self.testyear
self.assertTrue(u.is_active, "User '" + u.username + "' is INACTIVE")
@@ -173,14 +172,12 @@ class PostTests(TestCase):
def test_photo_upload(self):
"""Expect photo upload to work on any file (contrary to msg on screen)
- Upload into current default year. settings.PHOTOS_YEAR
+ Upload into current default year.
Deletes file afterwards
Need to login first.
"""
c = self.client
- from django.contrib.auth.models import User
-
- u = User.objects.get(username="expotest")
+ u = self.user
self.assertTrue(u.is_active, "User '" + u.username + "' is INACTIVE")
c.login(username=u.username, password="secretword")
@@ -192,11 +189,11 @@ class PostTests(TestCase):
content = response.content.decode()
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(response.status_code, HTTPStatus.OK)
- # with open('_test_response.html', 'w') as f:
- # f.write(content)
+ with open('_test_response.html', 'w') as f:
+ f.write(content)
for ph in [
r"test_upload_",
- r"Upload photos into /photos/" + str(settings.PHOTOS_YEAR),
+ r"Upload photos into /photos/" + str(current_year),
r" you can create a new folder in your name",
r"Create new Photographer folder",
r"only photo image files are accepted",
@@ -206,19 +203,17 @@ class PostTests(TestCase):
# Does not use the filename Django actually uses, assumes it is unchanged. Bug: accumulates one file with random name
# added each time it is run. The name of the uploaded file is only available within the code where it happens
- remove_file = pathlib.Path(settings.PHOTOS_ROOT, settings.PHOTOS_YEAR) / "test_upload_file.txt"
+ remove_file = pathlib.Path(settings.PHOTOS_ROOT, current_year) / "test_upload_file.txt"
remove_file.unlink()
def test_photo_upload_rename(self):
"""Expect photo upload to work on any file (contrary to msg on screen)
- Upload into current default year. settings.PHOTOS_YEAR
+ Upload into current default year.
Deletes file afterwards
Need to login first.
"""
c = self.client
- from django.contrib.auth.models import User
-
- u = User.objects.get(username="expotest")
+ u = self.user
self.assertTrue(u.is_active, "User '" + u.username + "' is INACTIVE")
c.login(username=u.username, password="secretword")
@@ -239,19 +234,20 @@ class PostTests(TestCase):
# Does not use the filename Django actually uses, assumes it is unchanged. Bug: accumulates one file with random name
# added each time it is run. The name of the uploaded file is only available within the code where it happens
- remove_file = pathlib.Path(settings.PHOTOS_ROOT, settings.PHOTOS_YEAR) / rename
- remove_file.unlink()
+ remove_file = pathlib.Path(settings.PHOTOS_ROOT, current_year) / rename
+ if remove_file.is_file():
+ remove_file.unlink()
+
+
def test_photo_folder_create(self):
"""Create folder for new user
- Create in current default year. settings.PHOTOS_YEAR
+ Create in current year.
Deletes folder afterwards
Need to login first.
"""
c = self.client
- from django.contrib.auth.models import User
-
- u = User.objects.get(username="expotest")
+ u = self.user
self.assertTrue(u.is_active, "User '" + u.username + "' is INACTIVE")
c.login(username=u.username, password="secretword")
@@ -268,17 +264,17 @@ class PostTests(TestCase):
# Does not use the filename Django actually uses, assumes it is unchanged. Bug: accumulates one file with random name
# added each time it is run. The name of the uploaded file is only available within the code where it happens
- remove_dir = pathlib.Path(settings.PHOTOS_ROOT, settings.PHOTOS_YEAR) / "GussieFinkNottle"
- remove_dir.rmdir()
+ remove_dir = pathlib.Path(settings.PHOTOS_ROOT, current_year) / "GussieFinkNottle"
+ if remove_dir.is_dir():
+ remove_dir.rmdir()
def test_dwg_upload_txt(self):
"""Expect .pdf file to be refused upload
Need to login first.
"""
c = self.client
- from django.contrib.auth.models import User
+ u = self.user
- u = User.objects.get(username="expotest")
self.assertTrue(u.is_active, "User '" + u.username + "' is INACTIVE")
c.login(username=u.username, password="secretword")
@@ -298,9 +294,8 @@ class PostTests(TestCase):
Need to login first.
"""
c = self.client
- from django.contrib.auth.models import User
+ u = self.user
- u = User.objects.get(username="expotest")
self.assertTrue(u.is_active, "User '" + u.username + "' is INACTIVE")
c.login(username=u.username, password="secretword")
@@ -327,7 +322,8 @@ class PostTests(TestCase):
# added each time it is run. The name of the uploaded file is only available within the code where it happens
# UploadedFile.name see https://docs.djangoproject.com/en/4.1/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile
remove_file = pathlib.Path(settings.DRAWINGS_DATA) / "uploads" / "test_upload_nosuffix"
- remove_file.unlink()
+ if remove_file.is_file():
+ remove_file.unlink()
class ComplexLoginTests(TestCase):
@@ -335,27 +331,14 @@ class ComplexLoginTests(TestCase):
def setUp(self):
"""setUp runs once for each test in this class"""
- from django.contrib.auth.models import User
-
- u = User()
- u.pk = 9000
- u.user_id = 8000
- u.username, u.password = "expotest", "secretword"
- u.email = "philip.sargent+ET@gmail.com"
- u.first_name, u.last_name = "ExpoTest", "Caver"
- u.is_staff = True
- u.is_superuser = True
-
- u.set_password(u.password) # This creates a new salt and thus a new key for EACH test
- u.save() # vital that we save all this before attempting login
- # print ('\n',u.password)
- self.user = u
+ create_user(name="expo")
+ create_user(name="expotest")
+ create_user(name="expotestadmin", is_superuser = True)
def tearDown(self):
self.client.logout() # not needed as each test creates a new self.client
- # self.member.delete()
- ##self.user.delete() # id attribute set to None !
- pass
+ User.objects.all().delete()
+
# def test_login_redirect_for_non_logged_on_user(self): # need to fix this in real system
# c = self.client
@@ -365,11 +348,11 @@ class ComplexLoginTests(TestCase):
def test_ordinary_login(self):
c = self.client
- u = self.user
+ u = User.objects.get(username="expotest")
self.assertTrue(u.is_active, "User '" + u.username + "' is INACTIVE")
- logged_in = c.login(username=u.username, password="secretword") # fails to work if password=u.password !
+ logged_in = c.login(username=u.username, password="secretword")
self.assertTrue(logged_in, "FAILED to login as '" + u.username + "'")
response = c.get("/accounts/login/") # defined by auth system
@@ -379,14 +362,14 @@ class ComplexLoginTests(TestCase):
def test_authentication_login(self):
c = self.client
- u = self.user
+ u = User.objects.get(username="expotest")
self.assertTrue(u.is_active, "User '" + u.username + "' is INACTIVE")
# This is weird. I thought that the user had to login before she was in the authenticated state
self.assertTrue(u.is_authenticated, "User '" + u.username + "' is NOT AUTHENTICATED before login")
- logged_in = c.login(username=u.username, password="secretword") # fails to work if password=u.password !
+ logged_in = c.login(username=u.username, password="secretword")
self.assertTrue(logged_in, "FAILED to login as '" + u.username + "'")
self.assertTrue(u.is_authenticated, "User '" + u.username + "' is NOT AUTHENTICATED after login")
@@ -396,24 +379,26 @@ class ComplexLoginTests(TestCase):
def test_admin_login(self):
c = self.client
- u = self.user
+ u = User.objects.get(username="expotestadmin")
- logged_in = c.login(username=u.username, password="secretword") # fails to work if password=u.password !
+ logged_in = c.login(username=u.username, password="secretword")
self.assertTrue(logged_in, "FAILED to login as '" + u.username + "'")
- response = c.get("/admin/")
+ response = c.get("/admin/login/")
content = response.content.decode()
- # with open('admin-op.html', 'w') as f:
- # f.write(content)
+ # fn='admin-op.html'
+ # print(f"Writing {fn}")
+ # with open(fn, 'w') as f:
+ # f.write(content)
t = re.search(r"Troggle database administration", content)
self.assertIsNotNone(t, "Logged in as '" + u.username + "' but failed to get the Troggle Admin page")
def test_noinfo_login(self):
c = self.client # inherited from TestCase
- u = self.user
+ u = User.objects.get(username="expotest")
- logged_in = c.login(username=u.username, password="secretword") # fails if password=u.password !
+ logged_in = c.login(username=u.username, password="secretword")
self.assertTrue(logged_in, "FAILED to login as '" + u.username + "'")
response = c.get("/stats") # a page with the Troggle menus
content = response.content.decode()
@@ -428,7 +413,7 @@ class ComplexLoginTests(TestCase):
def test_user_force(self):
c = self.client
- u = self.user
+ u = User.objects.get(username="expotest")
try:
c.force_login(u)