-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add first time setup page * Add first time setup check middleware * Hide admin from normal users nav bar * Fix tests * Update Readme
- Loading branch information
Showing
9 changed files
with
161 additions
and
16 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,13 +58,7 @@ By default, the `docker-compose.yml` will use the latest image from GHCR. Howeve | |
docker compose -f docker/docker-compose.yml up | ||
``` | ||
|
||
5. To create an admin user, run the following command and follow the prompts: | ||
|
||
``` | ||
docker compose -f docker/docker-compose.yml exec web python manage.py createsuperuser | ||
``` | ||
|
||
6. After creating the admin user, you will be able to log into the site using the credentials you entered. | ||
5. Access the site on the configured port. You will be asked to setup an admin user when you first visit the site. | ||
|
||
### Using Postgresql | ||
|
||
|
@@ -80,7 +74,7 @@ These instructions are for setting up the project in development mode which may | |
|
||
1. Download or clone this repository. | ||
2. Make a copy of the `.env EXAMPLE` file and name it `.env.dev`. In your new copy, make sure `DEBUG` is set to 1, and change any values that are set to `CHANGEME` to the appropriate values for your development environment. | ||
3. In the .env.dev file, add values for the following variables: `DJANGO_SUPERUSER_EMAIL` and `DJANGO_SUPERUSER_PASSWORD`. These will be used to create an admin user when the containers are started. For example: | ||
3. (Optional) In the .env.dev file, add values for the following variables: `DJANGO_SUPERUSER_EMAIL` and `DJANGO_SUPERUSER_PASSWORD`. These will be used to create an admin user when the containers are started. For example: | ||
|
||
``` | ||
[email protected] | ||
|
@@ -93,6 +87,6 @@ DJANGO_SUPERUSER_PASSWORD=CHANGEME | |
docker compose -f docker/dev/docker-compose.dev.yml up --build | ||
``` | ||
|
||
4. Once the containers are running, you should be able to access the site in your web browser at `127.0.0.1:8000`. By default, the admin user will automatically be created and you will be able to log in using the credentials you entered in the .env.dev file. | ||
4. Once the containers are running, you should be able to access the site in your web browser at `127.0.0.1:8000`. If you added environment variables for the superuser, you should be able to login with those credentials. Otherwise you will be prompted to create a super user every time to start up the server. | ||
|
||
If you would like contribute to this project, please read the [contributing guidelines](CONTRIBUTING.md) for more information. |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{% extends 'base.html' %} | ||
|
||
{% block title %}<title>First Time Setup | Shifter</title>{% endblock %} | ||
|
||
{% block content %} | ||
<div class="standard-page-width"> | ||
<div class="py-2"> | ||
<h1 class="title">First Time Setup</h1> | ||
</div> | ||
<div class="border-b-2 border-gray-400"> | ||
<p class="text-gray-800 pb-2">Welcome to Shifter!</p> | ||
<p class="text-gray-800 pb-2">First you need to create an admin user. Admins are able to create accounts for other people, and can be used as a regular user.</p> | ||
</div> | ||
<div class="p-2"> | ||
<form class="space-y-1 p2" method="post"> | ||
{% csrf_token %} | ||
{% if form.non_field_errors %}<div class="error-box">{{ form.non_field_errors }}</div>{% endif %} | ||
|
||
<div class="flex"> | ||
<p class="text-gray-800">{{ form.email.label }}:</p> | ||
{% if form.email.errors %}<div class="ml-2 error-box grow">{{ form.email.errors }}</div>{% endif %} | ||
</div> | ||
<input name="{{ form.email.html_name }}" class="w-full input-primary" placeholder="[email protected]"> | ||
|
||
<div class="flex"> | ||
<p class="text-gray-800">{{ form.password.label }}:</p> | ||
{% if form.password.errors %}<div class="ml-2 error-box grow">{{ form.password.errors }}</div>{% endif %} | ||
</div> | ||
<input name="{{ form.password.html_name }}" type="password" class="w-full input-primary" placeholder="••••••••"> | ||
|
||
<div class="flex"> | ||
<p class="text-gray-800">{{ form.confirm_password.label }}:</p> | ||
{% if form.confirm_password.errors %}<div class="ml-2 error-box grow">{{ form.confirm_password.errors }}</div>{% endif %} | ||
</div> | ||
<input name="{{ form.confirm_password.html_name }}" type="password" class="w-full input-primary" placeholder="••••••••"> | ||
|
||
<div class="flex justify-end py-2"> | ||
<input type="submit" value="Register User" class="btn-primary"> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
{% endblock %} |
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,7 +1,12 @@ | ||
from django.contrib.auth import get_user, get_user_model | ||
from django.test import Client, TestCase | ||
from django.test import Client, RequestFactory, TestCase | ||
from django.urls import reverse | ||
|
||
from shifter_auth.middleware import ( | ||
ensure_first_time_setup_completed, | ||
is_first_time_setup_required, | ||
) | ||
|
||
TEST_USER_EMAIL = "[email protected]" | ||
TEST_STAFF_USER_EMAIL = "[email protected]" | ||
TEST_ADDITIONAL_USER_EMAIL = "[email protected]" | ||
|
@@ -221,3 +226,43 @@ def test_new_user_not_staff(self): | |
self.assertEqual( | ||
User.objects.filter(email=TEST_ADDITIONAL_USER_EMAIL).count(), 0 | ||
) | ||
|
||
|
||
class FirstTimeSetupTest(TestCase): | ||
def setUp(self): | ||
self.factory = RequestFactory() | ||
|
||
def test_is_first_time_setup_required_true(self): | ||
User = get_user_model() | ||
User.objects.all().delete() | ||
self.assertTrue(is_first_time_setup_required()) | ||
|
||
def test_is_first_time_setup_required_false(self): | ||
User = get_user_model() | ||
User.objects.create_user(TEST_USER_EMAIL, TEST_USER_PASSWORD) | ||
self.assertFalse(is_first_time_setup_required()) | ||
|
||
def test_ensure_first_time_setup_completed_redirect(self): | ||
User = get_user_model() | ||
User.objects.all().delete() | ||
|
||
middleware = ensure_first_time_setup_completed(lambda request: None) | ||
request = self.factory.get(reverse("shifter_files:index")) | ||
response = middleware(request) | ||
self.assertEqual(response.status_code, 302) | ||
self.assertEqual( | ||
response.url, reverse("shifter_auth:first-time-setup") | ||
) | ||
|
||
def test_ensure_first_time_setup_completed_no_redirect(self): | ||
User = get_user_model() | ||
User.objects.create_user( | ||
TEST_ADDITIONAL_USER_EMAIL, TEST_USER_PASSWORD | ||
) | ||
middleware = ensure_first_time_setup_completed(lambda request: None) | ||
request = self.factory.get(reverse("shifter_files:index")) | ||
request.user = get_user_model().objects.create_user( | ||
TEST_USER_EMAIL, TEST_USER_PASSWORD | ||
) | ||
response = middleware(request) | ||
self.assertIsNone(response) |
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