-
Notifications
You must be signed in to change notification settings - Fork 23
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
Improve views query #29
Open
nnsnodnb
wants to merge
4
commits into
mkalioby:main
Choose a base branch
from
nnsnodnb:improve-views-query
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
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 | ||
if not getattr(self, "assertEquals", None): | ||
self.assertEquals = self.assertEqual | ||
|
||
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,28 @@ | ||
from django.contrib.auth.decorators import login_required | ||
from django.http import HttpResponse | ||
from django.shortcuts import render | ||
from django.http import HttpResponse, HttpResponseForbidden | ||
from django.shortcuts import get_object_or_404, render | ||
|
||
from .models import UserPasskey | ||
|
||
|
||
@login_required | ||
def index(request,enroll=False): # noqa | ||
keys = UserPasskey.objects.filter(user=request.user) # pragma: no cover | ||
return render(request,'PassKeys.html',{"keys":keys,"enroll":enroll}) # pragma: no cover | ||
def index(request, enroll=False): # noqa | ||
keys = UserPasskey.objects.filter(user=request.user) # pragma: no cover | ||
return render(request, "PassKeys.html", {"keys": keys, "enroll": enroll}) # pragma: no cover | ||
|
||
|
||
@login_required | ||
def delKey(request): | ||
key=UserPasskey.objects.get(id=request.GET["id"]) | ||
if key.user.pk == request.user.pk: | ||
key.delete() | ||
return HttpResponse("Deleted Successfully") | ||
return HttpResponse("Error: You own this token so you can't delete it", status=403) | ||
key = get_object_or_404(UserPasskey, id=request.GET["id"], user=request.user) | ||
key.delete() | ||
return HttpResponse("Deleted Successfully") | ||
|
||
|
||
@login_required | ||
def toggleKey(request): | ||
id=request.GET["id"] | ||
q=UserPasskey.objects.filter(user=request.user, id=id) | ||
if q.count()==1: | ||
key=q[0] | ||
key.enabled=not key.enabled | ||
key.save() | ||
key = UserPasskey.objects.filter(id=request.GET["id"], user=request.user).first() | ||
if key is not None: | ||
key.enabled = not key.enabled | ||
key.save(update_fields=["enabled"]) | ||
return HttpResponse("OK") | ||
return HttpResponse("Error: You own this token so you can't toggle it", status=403) | ||
return HttpResponseForbidden("Error: You own this token so you can't toggle it") | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be "You don't own [...]".