-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from CrumpetDev/setup/environments
Setup/environments
- Loading branch information
Showing
20 changed files
with
290 additions
and
28 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 |
---|---|---|
@@ -1,4 +1,14 @@ | ||
{ | ||
"python.envFile": "${workspaceFolder}/backend/.env", | ||
"prettier.configPath": "./frontend/.prettierrc.js", | ||
"prettier.bracketSpacing": false | ||
"prettier.bracketSpacing": false, | ||
"python.testing.unittestArgs": [ | ||
"-v", | ||
"-s", | ||
"./backend", | ||
"-p", | ||
"test_*.py" | ||
], | ||
"python.testing.pytestEnabled": false, | ||
"python.testing.unittestEnabled": true | ||
} |
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,11 @@ | ||
DEBUG=False | ||
DEVELOPMENT_MODE=False | ||
DJANGO_ALLOWED_HOSTS='localhost,0.0.0.0,127.0.0.1,192.168.1.220' | ||
DJANGO_SETTINGS_MODULE=backend.settings | ||
|
||
DB_NAME = crumpet_db | ||
DB_USERNAME = head_baker | ||
DB_PASSWORD = Crumpet2023 | ||
DB_HOST = db | ||
DB_PORT = 5432 | ||
DB_SSL_MODE = require |
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 @@ | ||
default_app_config = 'app.apps.AppConfig' |
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,42 @@ | ||
# Generated by Django 4.1.5 on 2023-10-29 21:24 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("app", "0006_alter_project_api_key"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Environment", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("name", models.CharField(max_length=100)), | ||
("identifier", models.CharField(default=uuid.uuid4, max_length=255)), | ||
("is_default", models.BooleanField(default=False)), | ||
( | ||
"project", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="environments", | ||
to="app.project", | ||
), | ||
), | ||
], | ||
options={ | ||
"unique_together": {("identifier", "project")}, | ||
}, | ||
), | ||
] |
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,24 @@ | ||
from django.db import models | ||
|
||
import uuid | ||
|
||
|
||
class Environment(models.Model): | ||
name = models.CharField(max_length=100, blank=False, null=False) | ||
identifier = models.CharField(max_length=255, default=uuid.uuid4, blank=False, null=False) | ||
project = models.ForeignKey( | ||
"Project", on_delete=models.CASCADE, related_name="environments" | ||
) | ||
is_default = models.BooleanField(default=False, blank=False, null=False) | ||
|
||
class Meta: | ||
unique_together = ["identifier", "project"] | ||
|
||
def save(self, *args, **kwargs): | ||
if not self.identifier: | ||
# If identifier is not provided, assign a GUID. | ||
self.identifier = str(uuid.uuid4()) | ||
super(Environment, self).save(*args, **kwargs) | ||
|
||
def __str__(self): | ||
return self.name |
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,9 @@ | ||
from app.models import Environment | ||
from rest_framework import serializers | ||
|
||
|
||
class EnvironmentSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Environment | ||
fields = ["id", "name", "identifier", "is_default"] | ||
extra_kwargs = {'id': {'required': True}, 'name': {'required': True}, 'identifier': {'required': True}, 'is_default': {'required': True} } |
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,11 @@ | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from .models.environment import Environment | ||
from .models.project import Project | ||
|
||
|
||
@receiver(post_save, sender=Project) | ||
def create_default_environments(sender, instance, created, **kwargs): | ||
if created: | ||
Environment.objects.create(name="Development", identifier="development", project=instance, is_default=True) | ||
Environment.objects.create(name="Production", identifier="production", project=instance, is_default=True) |
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,4 +1,8 @@ | ||
import django | ||
django.setup() | ||
|
||
from .test_openapi import * | ||
from .test_user_auth import * | ||
from .test_views import * | ||
from .test_projects import * | ||
from .test_environments import * |
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,68 @@ | ||
import uuid | ||
from django.test import TestCase | ||
from django.contrib.auth import get_user_model | ||
from app.models import Environment, Project, ProjectMembership | ||
|
||
User = get_user_model() | ||
|
||
|
||
class EnvironmentModelTestCase(TestCase): | ||
def setUp(self): | ||
self.project = Project.objects.create(name="Test Project") | ||
self.environment = Environment.objects.create( | ||
name="Staging", project=self.project | ||
) | ||
|
||
def test_environment_creation(self): | ||
""" | ||
Test if the environment is created with provided name and project. | ||
""" | ||
self.assertEqual(self.environment.name, "Staging") | ||
self.assertEqual(self.environment.project, self.project) | ||
|
||
def test_environment_identifier(self): | ||
""" | ||
Test if the environment gets a UUID identifier if not provided. | ||
""" | ||
self.assertIsNotNone(self.environment.identifier) | ||
self.assertTrue(isinstance(self.environment.identifier, uuid.UUID)) | ||
|
||
def test_unique_together_constraint(self): | ||
""" | ||
Test the unique together constraint for identifier and project. | ||
""" | ||
with self.assertRaises(Exception): | ||
Environment.objects.create( | ||
name="Duplicate", | ||
identifier=self.environment.identifier, | ||
project=self.project, | ||
) | ||
|
||
|
||
class ProjectEnvironmentCreationTestCase(TestCase): | ||
def setUp(self): | ||
self.project = Project.objects.create(name="Another Test Project") | ||
|
||
def test_default_environments_created(self): | ||
""" | ||
Test if Development and Production environments are created for new projects. | ||
""" | ||
dev_env = Environment.objects.filter( | ||
name="Development", project=self.project | ||
).first() | ||
prod_env = Environment.objects.filter( | ||
name="Production", project=self.project | ||
).first() | ||
|
||
self.assertIsNotNone(dev_env, "Development environment was not created.") | ||
self.assertIsNotNone(prod_env, "Production environment was not created.") | ||
|
||
def test_default_environments_identifiers(self): | ||
""" | ||
Test if Development and Production environments have correct identifiers. | ||
""" | ||
dev_env = Environment.objects.get(name="Development", project=self.project) | ||
prod_env = Environment.objects.get(name="Production", project=self.project) | ||
|
||
self.assertEqual(dev_env.identifier, "development") | ||
self.assertEqual(prod_env.identifier, "production") |
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
Oops, something went wrong.