Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/DBC22-1520
Browse files Browse the repository at this point in the history
  • Loading branch information
fatbird authored Feb 21, 2024
2 parents 498aa8d + 8e6ef50 commit 0cc897a
Show file tree
Hide file tree
Showing 86 changed files with 2,087 additions and 604 deletions.
11 changes: 11 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ DJANGO_SUPERUSER_PASSWORD=<password>
DJANGO_CSRF_COOKIE_SECURE=true
DJANGO_SECURE_SSL_REDIRECT=true
DJANGO_SESSION_COOKIE_SECURE=true
DJANGO_RECAPTCHA_SECRET_KEY=<reCAPTCHA secret key>
DJANGO_EMAIL_BACKEND=<django.core.mail.backends.console.EmailBackend/django.core.mail.backends.smtp.EmailBackend>
DJANGO_EMAIL_HOST=<smtp host>
DJANGO_EMAIL_PORT=<smtp port>
DJANGO_FEEDBACK_EMAIL_DEFAULT=<default feedback recipient email>

# Django image database connection details
DB_NAME=<database name>
Expand Down Expand Up @@ -47,6 +52,7 @@ REACT_APP_ROUTE_PLANNER=<route planner url>
REACT_APP_ROUTE_PLANNER_KEY=<route planner auth key>
REACT_APP_GEOCODER_HOST=<geocoder url>
REACT_APP_GEOCODER_API_AUTH_KEY=<api auth key>
REACT_APP_RECAPTCHA_SITE_KEY=<reCAPTCHA site key>

# API
DRIVEBC_INLAND_FERRY_API_BASE_URL=<inland ferry api url>
Expand All @@ -57,3 +63,8 @@ DRIVEBC_OPEN_511_API_BASE_URL=<open511 api url>
DRIVEBC_DIT_API_BASE_URL=<CARS api url>
DRIVEBC_ROUTE_PLANNER_API_BASE_URL=<route planner url>
DRIVEBC_ROUTE_PLANNER_API_AUTH_KEY=<route planner auth key>
DRIVEBC_WEATHER_API_BASE_URL=<weather api base url>
DRIVEBC_WEATHER_API_TOKEN_URL=<weather api token url>
DRIVEBC_WEATHER_AREAS_API_BASE_URL=<weather areas api base url>
WEATHER_CLIENT_ID=<weather client id>
WEATHER_CLIENT_SECRET=<weather client secret>
92 changes: 92 additions & 0 deletions .github/workflows/codetest-sonarcloud.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: Unit tests & Sonar Scan

on:
push:
branches: [ "main" ]
# pull_request:
# types: [opened, synchronize, reopened]
workflow_dispatch:

jobs:
run-tests:
runs-on: ubuntu-latest
name: Run Tests
timeout-minutes: 10
services:
redis:
image: redis:7
ports:
- 6379:6379
postgres:
image: postgis/postgis:15-3.4
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: testpw
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
# Disabling shallow clone is recommended for improving relevancy of reporting for SonarCloud
fetch-depth: 0
- name: Install GDAL
run: sudo apt-get install -y gdal-bin
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r ./src/backend/requirements/development.txt
- name: Run Tests
env:
DB_NAME: postgres
DB_USER: postgres
DB_PASSWORD: testpw
DB_HOST: localhost
DB_PORT: 5432
REDIS_HOST: localhost
REDIS_PORT: 6379
DEBUG: false
DJANGO_URL: http://localhost:8000
DJANGO_ALLOWED_HOSTS: localhost
DJANGO_CORS_ORIGIN_WHITELIST: http://localhost:8000,http://localhost:3000
DJANGO_SUPERUSER_USERNAME: testvar
DJANGO_SUPERUSER_EMAIL: testvar@testvar.com
DJANGO_SUPERUSER_PASSWORD: testvar
DJANGO_CSRF_COOKIE_SECURE: false
DJANGO_SECURE_SSL_REDIRECT: false
DJANGO_SESSION_COOKIE_SECURE: false
DJANGO_EMAIL_BACKEND: django.core.mail.backends.console.EmailBackend
DJANGO_EMAIL_HOST: testvar
DJANGO_EMAIL_PORT: 1
DJANGO_RECAPTCHA_SECRET_KEY: testvar
DJANGO_FEEDBACK_EMAIL_DEFAULT: testvar@testvar.com
DRIVEBC_INLAND_FERRY_API_BASE_URL: testvar
DRIVEBC_IMAGE_API_BASE_URL: testvar
DRIVEBC_IMAGE_PROXY_URL: testvar
DRIVEBC_OPEN_511_API_BASE_URL: testvar
DRIVEBC_WEBCAM_API_BASE_URL: testvar
DRIVEBC_ROUTE_PLANNER_API_BASE_URL: testvar
DRIVEBC_ROUTE_PLANNER_API_AUTH_KEY: testvar
DRIVEBC_DIT_API_BASE_URL: testvar
DRIVEBC_WEATHER_API_BASE_URL: testvar
DRIVEBC_WEATHER_API_TOKEN_URL: testvar
WEATHER_CLIENT_ID: testvar
WEATHER_CLIENT_SECRET: testvar
DRIVEBC_WEATHER_AREAS_API_BASE_URL: testvar
SECRET_KEY: testvar
run: |
coverage run ./src/backend/manage.py test --noinput && coverage report && coverage xml
- name: Override Coverage Source Path for Sonar
run: |
sed -i "s/<source>\/home\/runner\/work\/DriveBC.ca\/DriveBC.ca<\/source>/<source>\/github\/workspace<\/source>/g" /home/runner/work/DriveBC.ca/DriveBC.ca/coverage.xml
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
8 changes: 4 additions & 4 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: Generate SonarQube report
name: Generate SonarCloud Report

on:
workflow_dispatch:
push:
branches:
- main
# push:
# branches:
# - main
# pull_request:
# types: [opened, synchronize, reopened]

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# file based env values
.env
.env.backup

# Build files
*.pyc
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
- flake8-django==1.1.2
- flake8-pytest-style==1.6.0
- flake8-typing-imports==1.12.0
args: [ --max-line-length=88 ]
args: [ --max-line-length=128 ]
stages: [commit]
exclude: '^backend/\w+/migrations'
- repo: https://github.com/PyCQA/bandit
Expand Down
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
SHELL := /bin/bash
.PHONY: help down
ARCH := $(shell uname -m)

help: ## This help.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help

up: ## Bring up containers
@echo 'Starting containers...'
@if [ "$(ARCH)" = "arm64" ]; then \
docker-compose -f docker-compose.yml -f docker-compose.m1.yml up -d; \
else \
docker-compose up -d; \
fi

down: ## Stop and remove containers and volumes
@echo 'Removing containers and volumes...'
docker-compose down -v
9 changes: 1 addition & 8 deletions compose/frontend/StaticBuild
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,7 @@ RUN touch /run/nginx.pid
COPY ./compose/frontend/default.conf /etc/nginx/conf.d
# COPY nginx.conf /etc/nginx
COPY --from=buildnode /app/build /usr/share/nginx/html
RUN chmod -R 777 /run /var/log/nginx /var/cache/nginx /usr/share/nginx/html/static/js

#FOUR LINES BELOW ARE TEMPORARY FOR THE PRIVATE BETA
RUN mkdir -p /etc/apache2
RUN touch /etc/apache2/.htpasswd
RUN chmod -R 777 /etc/apache2/.htpasswd
RUN chmod -R 777 /etc/nginx/conf.d

RUN chmod -R 777 /run /var/log/nginx /var/cache/nginx /usr/share/nginx/html/static/js /etc/nginx/conf.d

ARG DEBUG_BUILD=false

Expand Down
2 changes: 0 additions & 2 deletions compose/frontend/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ server {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
auth_basic "Beta Site";
auth_basic_user_file /etc/apache2/.htpasswd;
}

proxy_cache cache;
Expand Down
12 changes: 1 addition & 11 deletions compose/frontend/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,4 @@ echo -e "window.ROUTE_PLANNER_KEY='${REACT_APP_ROUTE_PLANNER_KEY}';" >> $MAIN
echo -e "window.REPLAY_THE_DAY='${REACT_APP_REPLAY_THE_DAY}';" >> $MAIN

# Set the environment to be used for caching django content
sed -i "s~{ENVIRONMENT}~$ENVIRONMENT~g" /etc/nginx/conf.d/default.conf

#FOLLOWING SECTION IS TEMPORARY FOR THE PRIVATE BETA
# Check if $ENVIRONMENT is not set to "prod". If it is prod we want to keep these lines
if [ "$ENVIRONMENT" != "prod" ]; then
# Remove the specified lines from default.conf
sed -i '/auth_basic "Beta Site";/d' /etc/nginx/conf.d/default.conf
sed -i '/auth_basic_user_file \/etc\/apache2\/.htpasswd;/d' /etc/nginx/conf.d/default.conf
fi
#Copy the content of the secret into the .htpasswd file.
echo "$TEMP_AUTH_SECRET" >> /etc/apache2/.htpasswd
sed -i "s~{ENVIRONMENT}~$ENVIRONMENT~g" /etc/nginx/conf.d/default.conf
5 changes: 5 additions & 0 deletions docker-compose.m1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
db:
image: tobi312/rpi-postgresql-postgis:15-3.4-alpine-arm
django:
platform: linux/amd64
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ services:
- ./src/frontend:/app
# One-way volume to use node_modules from inside image
- /app/node_modules
depends_on:
- django
# depends_on:
# - django
command: npm run start-frontend

db:
Expand Down
6 changes: 5 additions & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ sonar.sources=src
#sonar.sourceEncoding=UTF-8

# enabling python3 only to take advantage of sonarqube's stricter analysis
sonar.python.version=3
sonar.python.version=3

sonar.coverage.exclusions=**/frontend/**/*.*

sonar.python.coverage.reportPaths=coverage.xml
19 changes: 19 additions & 0 deletions src/backend/apps/cms/migrations/0015_alter_advisory_geometry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.3 on 2024-02-14 14:45

import django.contrib.gis.db.models.fields
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('cms', '0014_bulletin_image_alt_text'),
]

operations = [
migrations.AlterField(
model_name='advisory',
name='geometry',
field=django.contrib.gis.db.models.fields.PolygonField(srid=4326),
),
]
2 changes: 1 addition & 1 deletion src/backend/apps/cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def rendered_body(self):
]

# Geo fields
geometry = models.GeometryField()
geometry = models.PolygonField()

def save(self, *args, **kwargs):
super().save(*args, **kwargs)
Expand Down
17 changes: 13 additions & 4 deletions src/backend/apps/cms/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from pathlib import Path

import environ
from apps.cms.models import Advisory, Bulletin, Ferry
from rest_framework import serializers
from wagtail.templatetags import wagtailcore_tags

# Base dir and env
BASE_DIR = Path(__file__).resolve().parents[4]
SRC_DIR = Path(__file__).resolve().parents[3]
env = environ.Env()
environ.Env.read_env(BASE_DIR / ".env", overwrite=True)

CMS_FIELDS = [
"live",
"has_unpublished_changes",
Expand Down Expand Up @@ -61,8 +70,8 @@ class BulletinSerializer(BulletinTestSerializer):
image_url = serializers.SerializerMethodField('get_image_url')

def get_image_url(self, obj):
request = self.context.get("request")
return request.build_absolute_uri(obj.image.file.url) if obj.image else ''
host = env('DJANGO_URL') if 'DJANGO_URL' in env else ''
return host + obj.image.file.url if obj.image else ''

def get_body(self, obj):
return self.get_richtext(obj.body)
Expand All @@ -79,8 +88,8 @@ class Meta:
fields = "__all__"

def get_image_url(self, obj):
request = self.context.get("request")
return request.build_absolute_uri(obj.image.file.url) if obj.image else ''
host = env('DJANGO_URL') if 'DJANGO_URL' in env else ''
return host + obj.image.file.url if obj.image else ''

def get_description(self, obj):
return self.get_richtext(obj.description)
Expand Down
11 changes: 8 additions & 3 deletions src/backend/apps/cms/tests/test_advisory_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from apps.shared.enums import CacheKey
from apps.shared.tests import BaseTest
from django.contrib.contenttypes.models import ContentType
from django.contrib.gis.geos import LineString
from django.contrib.gis.geos import Polygon
from django.core.cache import cache
from rest_framework.test import APITestCase

Expand All @@ -14,7 +14,7 @@ def setUp(self):
advisory = Advisory.objects.create(
title="Advisory title",
body="Advisory body",
geometry=LineString([(-119, 35), (-118, 32)]),
geometry=Polygon([(-119, 35), (-118, 32), (-117, 31), (-119, 35)]),
path="000100010001",
depth=3,
content_type=ContentType.objects.get(app_label='cms',
Expand All @@ -25,7 +25,12 @@ def setUp(self):
advisory_2 = Advisory.objects.create(
title="Advisory title 2",
body="Advisory body 2",
geometry=LineString([(-119, 35), (-118, 32)]),
geometry=Polygon([
(-119, 35),
(-118, 32),
(-117, 31),
(-119, 35)
]),
path="000100010002",
depth=3,
content_type=ContentType.objects.get(app_label='cms',
Expand Down
19 changes: 14 additions & 5 deletions src/backend/apps/cms/tests/test_advisory_serializer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from apps.cms.models import Advisory
from apps.cms.serializers import AdvisorySerializer, AdvisoryTestSerializer
from apps.shared.tests import BaseTest
from django.contrib.gis.geos import LineString
from django.contrib.gis.geos import Polygon


class TestAdvisorySerializer(BaseTest):
Expand All @@ -11,8 +11,13 @@ def setUp(self):
self.advisory = Advisory(
title="Advisory title 1",
body="Advisory body 1",
geometry=LineString([(-123.569743, 48.561231),
(-123.569743, 48.561231)]),
geometry=Polygon([
(-123.569743, 48.561231),
(-123.569743, 48.561231),
(-123.569743, 48.561231),
(-123.569743, 48.561231)
]
),
path="000100010001",
depth=3,
)
Expand Down Expand Up @@ -41,8 +46,12 @@ def test_serializer_save(self):
'id': 3,
'title': 'Advisory title 1',
'body': 'Advisory body 1',
'geometry': LineString([(-123.569743, 48.561231),
(-123.569743, 48.561231)]),
'geometry': Polygon([
(-123.569743, 48.561231),
(-123.569743, 48.561231),
(-123.569743, 48.561231),
(-123.569743, 48.561231)
]),
'content_type': 55,
'depth': 1,
'path': '000100010005',
Expand Down
Loading

0 comments on commit 0cc897a

Please sign in to comment.