-
Notifications
You must be signed in to change notification settings - Fork 44
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
Used a plain HTML form for DjangoProject login #171
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dfbe6dd
Used a plain HTML form for DjangoProject login (no more basic auth)
bmispelon 3e2bed6
Update .github/workflows/tests.yml
felixxm 1df08c4
Update DjangoPlugin/tracdjangoplugin/settings_tests.py
felixxm 02130ba
Update DjangoPlugin/tracdjangoplugin/settings.py
felixxm 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from .settings import * | ||
|
||
DATABASES = { | ||
"default": { | ||
"ENGINE": "django.db.backends.sqlite3", | ||
}, | ||
} | ||
|
||
PASSWORD_HASHERS = [ | ||
"django.contrib.auth.hashers.MD5PasswordHasher", | ||
] | ||
|
||
SECRET_KEY = "test" |
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 |
---|---|---|
@@ -0,0 +1,129 @@ | ||
from functools import partial | ||
|
||
from django.contrib.auth.forms import AuthenticationForm | ||
from django.contrib.auth.models import User | ||
from django.test import TestCase | ||
|
||
from trac.test import EnvironmentStub, MockRequest | ||
from trac.web.api import RequestDone | ||
from trac.web.main import RequestDispatcher | ||
|
||
from tracdjangoplugin.plugins import PlainLoginComponent | ||
|
||
|
||
class PlainLoginComponentTestCase(TestCase): | ||
def setUp(self): | ||
self.env = EnvironmentStub() | ||
self.component = PlainLoginComponent(self.env) | ||
self.request_factory = partial(MockRequest, self.env) | ||
|
||
def test_component_matches_correct_url(self): | ||
request = self.request_factory(path_info="/login") | ||
self.assertTrue(self.component.match_request(request)) | ||
|
||
def test_component_doesnt_match_another_url(self): | ||
request = self.request_factory(path_info="/github/login") | ||
self.assertFalse(self.component.match_request(request)) | ||
|
||
def test_component(self): | ||
request = self.request_factory(path_info="/login") | ||
template, context = self.component.process_request(request) | ||
self.assertEqual(template, "plainlogin.html") | ||
self.assertFalse(context["form"].is_bound) | ||
|
||
def assertLoginSucceeds( | ||
self, username, password, check_redirect=None, extra_data=None | ||
): | ||
data = {"username": username, "password": password} | ||
if extra_data is not None: | ||
data.update(extra_data) | ||
request = self.request_factory(method="POST", path_info="/login", args=data) | ||
with self.assertRaises(RequestDone): | ||
self.component.process_request(request) | ||
|
||
self.assertEqual(request.authname, "test") | ||
self.assertEqual(request.status_sent, ["303 See Other"]) | ||
if check_redirect is not None: | ||
redirect_url = request.headers_sent["Location"] | ||
self.assertEqual(redirect_url, check_redirect) | ||
|
||
def test_login_valid_user(self): | ||
User.objects.create_user(username="test", password="test") | ||
self.assertLoginSucceeds(username="test", password="test") | ||
|
||
def test_login_valid_default_redirect(self): | ||
self.env.config.set("trac", "base_url", "") | ||
User.objects.create_user(username="test", password="test") | ||
with self.settings(LOGIN_REDIRECT_URL="/test"): | ||
self.assertLoginSucceeds( | ||
username="test", password="test", check_redirect="/test" | ||
) | ||
|
||
def test_login_valid_with_custom_redirection(self): | ||
self.env.config.set("trac", "base_url", "") | ||
User.objects.create_user(username="test", password="test") | ||
self.assertLoginSucceeds( | ||
username="test", | ||
password="test", | ||
check_redirect="/test", | ||
extra_data={"next": "/test"}, | ||
) | ||
|
||
def test_login_valid_with_custom_redirection_with_hostname(self): | ||
self.env.config.set("trac", "base_url", "http://localhost") | ||
User.objects.create_user(username="test", password="test") | ||
self.assertLoginSucceeds( | ||
username="test", | ||
password="test", | ||
check_redirect="http://localhost/test", | ||
extra_data={"next": "http://localhost/test"}, | ||
) | ||
|
||
def test_login_valid_with_malicious_redirection(self): | ||
self.env.config.set("trac", "base_url", "http://localhost") | ||
User.objects.create_user(username="test", password="test") | ||
with self.settings(LOGIN_REDIRECT_URL="/test"): | ||
self.assertLoginSucceeds( | ||
username="test", | ||
password="test", | ||
check_redirect="http://localhost/test", | ||
extra_data={"next": "http://example.com/evil"}, | ||
) | ||
|
||
def assertLoginFails(self, username, password, error_message=None): | ||
if error_message is None: | ||
error_message = AuthenticationForm.error_messages["invalid_login"] % { | ||
"username": "username" | ||
} | ||
|
||
request = self.request_factory( | ||
method="POST", | ||
path_info="/login", | ||
args={"username": username, "password": password}, | ||
) | ||
template, context = self.component.process_request(request) | ||
self.assertEqual(template, "plainlogin.html") | ||
self.assertEqual(context["form"].errors, {"__all__": [error_message]}) | ||
|
||
def test_login_invalid_no_users(self): | ||
self.assertLoginFails(username="test", password="test") | ||
|
||
def test_login_invalid_incorrect_username(self): | ||
User.objects.create_user(username="test", password="test") | ||
self.assertLoginFails(username="test123", password="test") | ||
|
||
def test_login_invalid_incorrect_password(self): | ||
User.objects.create_user(username="test", password="test") | ||
self.assertLoginFails(username="test", password="test123") | ||
|
||
def test_login_invalid_incorrect_username_and_password(self): | ||
User.objects.create_user(username="test", password="test") | ||
self.assertLoginFails(username="test123", password="test123") | ||
|
||
def test_login_invalid_username_uppercased(self): | ||
User.objects.create_user(username="test", password="test") | ||
self.assertLoginFails(username="TEST", password="test") | ||
|
||
def test_login_invalid_inactive_user(self): | ||
User.objects.create_user(username="test", password="test", is_active=False) | ||
self.assertLoginFails(username="test", password="test") |
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
Oops, something went wrong.
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'd use a non-blank default, e.g.
Also,
str()
seems unnecessary.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.
My idea was to prevent accidentally running a known secret key in production which is why I used an empty default.
Isn't it the case that Django will refuse to start if it detects an empty secret key?
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.
Right, sorry I was thinking we have test settings here 😅
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.
but
str()
is still unnecessaryThere 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.
Most likely yes, unless you use an integer in your secrets.json but then you had it coming 😅