Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nnsnodnb committed Jan 6, 2024
1 parent dd6fd51 commit 90d433b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 56 deletions.
8 changes: 5 additions & 3 deletions example/test_app/tests/test_current_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ class TestCurrentPlatform(TestCase):
def setUp(self) -> None:
self.request_factory = RequestFactory()

def check_platform(self,user_agent, platform):
request = self.request_factory.get('/', HTTP_USER_AGENT=user_agent)
self.assertEquals(get_current_platform(request), platform)
def check_platform(self, user_agent, platform):
request = self.request_factory.get("/", HTTP_USER_AGENT=user_agent)
self.assertEqual(get_current_platform(request), platform)

def test_mac(self):
self.check_platform("Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15","Apple")

def test_ios(self):
self.check_platform("Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15","Apple")

def test_ipad(self):
self.check_platform("Mozilla/5.0 (iPad; CPU OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1","Apple")

Expand Down
59 changes: 29 additions & 30 deletions example/test_app/tests/test_fido.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from base64 import urlsafe_b64encode
from importlib import import_module

from django.http import HttpRequest
from django.test import RequestFactory,TransactionTestCase, Client
from django.test import RequestFactory, TransactionTestCase, Client
from django.urls import reverse

from django.conf import settings
Expand All @@ -15,15 +14,17 @@
def get_server_id(request):
return request.META["SERVER_NAME"] + "1"


def get_server_name(request):
return "MySite"

class test_fido(TransactionTestCase):

class TestFIDO(TransactionTestCase):
def setUp(self) -> None:
from django.contrib.auth import get_user_model
self.user_model = get_user_model()
if self.user_model.objects.filter(username="test").count()==0:
self.user = self.user_model.objects.create_user(username="test",password="test")
if not self.user_model.objects.filter(username="test").exists():
self.user = self.user_model.objects.create_user(username="test", password="test")
else:
self.user = self.user_model.objects.get(username="test")
self.client = Client()
Expand All @@ -38,32 +39,30 @@ def setUp(self) -> None:
self.client.post("/auth/login", {"username": "test", "password": "test", 'passkeys': ''})
self.factory = RequestFactory()


def test_key_reg(self):
self.client.post('auth/login',{"usernaame":"test","password":"test","passkeys":""})
self.client.post('auth/login', {"usernaame": "test", "password": "test", "passkeys": ""})
r = self.client.get(reverse('passkeys:reg_begin'))
self.assertEquals(r.status_code, 200)
self.assertEqual(r.status_code, 200)
j = json.loads(r.content)
j['publicKey']['challenge'] = j['publicKey']['challenge'].encode("ascii")
s = SoftWebauthnDevice()
res = s.create(j, "https://" + j["publicKey"]["rp"]["id"])
res["key_name"]="testKey"
res["key_name"] = "testKey"
u = reverse('passkeys:reg_complete')
r = self.client.post(u, data=json.dumps(res),headers={"USER_AGENT":""}, HTTP_USER_AGENT="", content_type="application/json")
r = self.client.post(u, data=json.dumps(res), headers={"USER_AGENT": ""}, HTTP_USER_AGENT="", content_type="application/json")
try:
j = json.loads(r.content)
except Exception:
raise AssertionError("Failed to get the required JSON after reg_completed")
self.assertTrue("status" in j)

self.assertEquals(j["status"], "OK")
self.assertEquals(UserPasskey.objects.latest('id').name, "testKey")
self.assertEqual(j["status"], "OK")
self.assertEqual(UserPasskey.objects.latest('id').name, "testKey")
return s


def test_auto_key_name(self):
r = self.client.get(reverse('passkeys:reg_begin'))
self.assertEquals(r.status_code, 200)
self.assertEqual(r.status_code, 200)
j = json.loads(r.content)
j['publicKey']['challenge'] = j['publicKey']['challenge'].encode("ascii")
s = SoftWebauthnDevice()
Expand All @@ -75,8 +74,8 @@ def test_auto_key_name(self):
except Exception:
raise AssertionError("Failed to get the required JSON after reg_completed")
self.assertTrue("status" in j)
self.assertEquals(j["status"], "OK")
self.assertEquals(UserPasskey.objects.latest('id').name,"Apple")
self.assertEqual(j["status"], "OK")
self.assertEqual(UserPasskey.objects.latest('id').name, "Apple")
return s

def test_error_when_no_session(self):
Expand All @@ -90,35 +89,35 @@ def test_error_when_no_session(self):
except Exception:
raise AssertionError("Failed to get the required JSON after reg_completed")
self.assertTrue("status" in j)
self.assertEquals(j["status"], "ERR")
self.assertEquals(j["message"], "FIDO Status can't be found, please try again")
self.assertEqual(j["status"], "ERR")
self.assertEqual(j["message"], "FIDO Status can't be found, please try again")

def test_passkey_login(self):
authenticator = self.test_key_reg()
self.client.get('/auth/logout')
r = self.client.get(reverse('passkeys:auth_begin'))
self.assertEquals(r.status_code, 200)
self.assertEqual(r.status_code, 200)
j = json.loads(r.content)
j['publicKey']['challenge'] = j['publicKey']['challenge'].encode("ascii")

res = authenticator.get(j, "https://" + j["publicKey"]["rpId"])
u = reverse('login')
self.client.post(u, {'passkeys': json.dumps(res), "username": "", "password": ""},headers={"USER_AGENT":""}, HTTP_USER_AGENT="")
self.assertTrue(self.client.session.get('_auth_user_id',False))
self.assertTrue(self.client.session.get("passkey",{}).get("passkey",False))
self.assertEquals(self.client.session.get("passkey",{}).get("name"),"testKey")
self.client.post(u, {'passkeys': json.dumps(res), "username": "", "password": ""}, headers={"USER_AGENT": ""}, HTTP_USER_AGENT="")
self.assertTrue(self.client.session.get('_auth_user_id', False))
self.assertTrue(self.client.session.get("passkey", {}).get("passkey", False))
self.assertEqual(self.client.session.get("passkey", {}).get("name"), "testKey")

def test_base_username(self):
authenticator = self.test_key_reg()
self.client.get('/auth/logout')
session = self.session
session["base_username"]= "test"
session["base_username"] = "test"
session.save(must_create=True)
self.client.cookies["sessionid"] = session.session_key
r = self.client.get(reverse('passkeys:auth_begin'))
self.assertEquals(r.status_code, 200)
self.assertEqual(r.status_code, 200)
j = json.loads(r.content)
self.assertEquals(j['publicKey']['allowCredentials'][0]['id'],urlsafe_b64encode(authenticator.credential_id).decode("utf8").strip('='))
self.assertEqual(j['publicKey']['allowCredentials'][0]['id'], urlsafe_b64encode(authenticator.credential_id).decode("utf8").strip('='))

def test_passkey_login_no_session(self):
pass
Expand All @@ -128,14 +127,14 @@ def test_server_id_callable(self):
from test_app.tests.test_fido import get_server_id
settings.FIDO_SERVER_ID = get_server_id
r = self.client.get(reverse('passkeys:auth_begin'))
self.assertEquals(r.status_code, 200)
self.assertEqual(r.status_code, 200)
j = json.loads(r.content)
self.assertEquals(j['publicKey']['rpId'],'testserver1')
self.assertEqual(j['publicKey']['rpId'],'testserver1')

def test_server_name_callable(self):
from test_app.tests.test_fido import get_server_name
settings.FIDO_SERVER_NAME = get_server_name
r = self.client.get(reverse('passkeys:reg_begin'))
self.assertEquals(r.status_code, 200)
self.assertEqual(r.status_code, 200)
j = json.loads(r.content)
self.assertEquals(j['publicKey']['rp']["name"],'MySite')
self.assertEqual(j['publicKey']['rp']["name"], 'MySite')
29 changes: 15 additions & 14 deletions example/test_app/tests/test_passkeys.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
from django.test import RequestFactory,TransactionTestCase, Client
from django.test import RequestFactory, TransactionTestCase, Client

class test_passkeys(TransactionTestCase):

class TestPasskeys(TransactionTestCase):
def setUp(self) -> None:
from django.contrib.auth import get_user_model
self.user_model = get_user_model()
self.user = self.user_model.objects.create_user(username="test",password="test")
self.user = self.user_model.objects.create_user(username="test", password="test")
self.client = Client()
self.factory = RequestFactory()

def test_raiseException(self):
from django.contrib.auth import authenticate
try:
authenticate(request=None,username="test",password="test")
authenticate(request=None, username="test", password="test")
self.assertFalse(True)
except Exception as e:
self.assertEquals(str(e),"request is required for passkeys.backend.PasskeyModelBackend")
self.assertEqual(str(e), "request is required for passkeys.backend.PasskeyModelBackend")

def test_not_add_passkeys_field(self):
request = self.factory.post("/auth/login",{"username":"","password":""})
request = self.factory.post("/auth/login", {"username": "", "password": ""})
from django.contrib.auth import authenticate
try:
user = authenticate(request=request,username="",password="")
user = authenticate(request=request, username="", password="")
self.assertFalse(True)
except Exception as e:
self.assertEquals(str(e),"Can't find 'passkeys' key in request.POST, did you add the hidden input?")
self.assertEqual(str(e), "Can't find 'passkeys' key in request.POST, did you add the hidden input?")

def test_username_password_failed_login(self):
self.client.post("/auth/login",{"username":"test","password":"test123",'passkeys':''})
self.assertFalse(self.client.session.get('_auth_user_id',False))
self.client.post("/auth/login", {"username": "test", "password": "test123", "passkeys": ""})
self.assertFalse(self.client.session.get('_auth_user_id', False))

def test_username_password_login(self):
self.client.post("/auth/login",{"username":"test","password":"test",'passkeys':''})
self.assertTrue(self.client.session.get('_auth_user_id',False))
self.client.post("/auth/login", {"username": "test", "password": "test", "passkeys": ""})
self.assertTrue(self.client.session.get('_auth_user_id', False))
self.assertFalse(self.client.session.get('passkey', {}).get('passkey', False))

def test_no_data(self):
self.client.post("/auth/login",{"username":"","password":"",'passkeys':''})
self.assertFalse(self.client.session.get('_auth_user_id',False))
self.client.post("/auth/login", {"username": "", "password": "", "passkeys": ""})
self.assertFalse(self.client.session.get('_auth_user_id', False))
18 changes: 9 additions & 9 deletions example/test_app/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from django.urls import reverse

from passkeys.models import UserPasskey
from .test_fido import test_fido
from .test_fido import TestFIDO as test_fido

class test_views(TransactionTestCase):

class TestViews(TransactionTestCase):

def setUp(self) -> None:
from django.contrib.auth import get_user_model
Expand All @@ -15,11 +16,11 @@ def setUp(self) -> None:
test = test_fido()
test.setUp()
self.authenticator = test.test_key_reg()
self.client.post("/auth/login", {"username": "test", "password": "test", 'passkeys': ''})
self.client.post("/auth/login", {"username": "test", "password": "test", "passkeys": ""})
self.user = self.user_model.objects.get(username="test")

def test_disabling_key(self):
key =UserPasskey.objects.filter(user=self.user).latest('id')
key = UserPasskey.objects.filter(user=self.user).latest('id')
self.client.get(reverse('passkeys:toggle') + "?id=" + str(key.id))
self.assertFalse(UserPasskey.objects.get(id=key.id).enabled)

Expand All @@ -29,7 +30,7 @@ def test_disabling_key(self):
def test_deleting_key(self):
key = UserPasskey.objects.filter(user=self.user).latest('id')
self.client.get(reverse('passkeys:delKey') + "?id=" + str(key.id))
self.assertEquals(UserPasskey.objects.filter(id=key.id).count(), 0)
self.assertFalse(UserPasskey.objects.filter(id=key.id).exists())

def test_wrong_ownership(self):
test = test_fido()
Expand All @@ -39,8 +40,7 @@ def test_wrong_ownership(self):
self.user = self.user_model.objects.create_user(username="test2", password="test2")
self.client.post("/auth/login", {"username": "test2", "password": "test2", 'passkeys': ''})
r = self.client.get(reverse('passkeys:delKey') + "?id="+str(key.id))
self.assertEquals(r.status_code, 403)
self.assertEquals(r.content,b"Error: You own this token so you can't delete it")
self.assertEqual(r.status_code, 404)
r = self.client.get(reverse('passkeys:toggle') + "?id=" + str(key.id))
self.assertEquals(r.status_code, 403)
self.assertEquals(r.content, b"Error: You own this token so you can't toggle it")
self.assertEqual(r.status_code, 403)
self.assertEqual(r.content, b"Error: You own this token so you can't toggle it")
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ envlist=
py39-django{22,32,40,41,42},
py310-django{22,32,40,41,42},
py311-django{22,32,40,41,42,50},
py312-django{32,40,41,42,50},


[testenv]
Expand Down

0 comments on commit 90d433b

Please sign in to comment.