Skip to content
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

Add init management cmds #64

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Django CI

on:
push:
branches: [ main, add-pr-github-actions ]
branches: [ main ]
pull_request:
branches: [ main, add-pr-github-actions ]
branches: [ main ]

jobs:
build:
Expand Down Expand Up @@ -61,3 +61,8 @@ jobs:
with:
build: poetry run ./manage.py migrate --noinput
start: poetry run ./manage.py runserver
- name: Run Unit Tests
env:
DATABASE_URL: postgresql://govuk_domain:govuk_domain@localhost:5432/govuk_domain
run: |
SECRET_KEY=unneeded poetry run python manage.py test -v 2
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ migrate-devserver:

clear-db:
docker compose down && docker container prune -f && docker volume rm domains-register-a-govuk-domain_postgres-data

test:
docker compose run --rm --service-ports --entrypoint "python manage.py test -v 2" web
3 changes: 2 additions & 1 deletion local-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

python manage.py collectstatic --noinput
python manage.py migrate --noinput
python manage.py loaddata ./seed/reviewer_group.json ./seed/users.json ./seed/request.json
python manage.py create_reviewer_group
python manage.py loaddata ./seed/users.json ./seed/request.json
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.core.management.base import BaseCommand
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from request_a_govuk_domain.request import models

VIEWABLE_MODELS = [
models.Application,
models.CentralGovernmentAttributes,
models.Person,
models.Registrant,
models.Registrar,
]
EDITABLE_MODELS = [models.Review]


class Command(BaseCommand):
help = "Create the reviewer user group to manage permissions for non-superusers"

def handle(self, *args, **options):
group, created = Group.objects.get_or_create(name="reviewer")

view_content_types = [
ContentType.objects.get_for_model(model) for model in VIEWABLE_MODELS
]

edit_content_types = [
ContentType.objects.get_for_model(model) for model in EDITABLE_MODELS
]

view_permissions = [
Permission.objects.get(codename=f"view_{ct.model}", content_type=ct)
for ct in view_content_types
]
edit_permissions = [
Permission.objects.get(codename=f"change_{ct.model}", content_type=ct)
for ct in edit_content_types
]

group.permissions.add(*view_permissions, *edit_permissions)

if not created:
print("Reviewer group already exists")
30 changes: 0 additions & 30 deletions seed/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,6 @@
"phone_number": ""
}
},
{
"model": "request.person",
"pk": 4,
"fields": {
"name": "Bob Roberts",
"email_address": "",
"role": "",
"phone_number": ""
}
},
{
"model": "request.person",
"pk": 5,
"fields": {
"name": "Terry Terrence",
"email_address": "",
"role": "",
"phone_number": ""
}
},
{
"model": "request.person",
"pk": 6,
"fields": {
"name": "Peter Peters",
"email_address": "",
"role": "",
"phone_number": ""
}
},
{
"model": "request.registrant",
"pk": 1,
Expand Down
11 changes: 0 additions & 11 deletions seed/reviewer_group.json

This file was deleted.

Empty file added tests/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions tests/test_create_reviewer_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.test import TestCase
from django.contrib.auth.models import Group
from django.core.management import call_command

EXPECTED_PERMISSIONS = [
"view_application",
"view_centralgovernmentattributes",
"view_person",
"view_registrant",
"view_registrar",
"change_review",
]


class CreateReviewerGroupTestCase(TestCase):
def setUp(self):
Group.objects.all().delete()

def test_group_created(self):
call_command("create_reviewer_group")
self.assertTrue(Group.objects.filter(name="reviewer").exists())

def test_group_permissions(self):
call_command("create_reviewer_group")
reviewer_group = Group.objects.get(name="reviewer")
codenames = [
permission["codename"]
for permission in reviewer_group.permissions.all().values()
]
self.assertEqual(sorted(EXPECTED_PERMISSIONS), sorted(codenames))