Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pritchyspritch committed May 16, 2024
1 parent 94df32a commit 05051f8
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 6 deletions.
30 changes: 29 additions & 1 deletion .github/workflows/build-and-push-docker-image.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: Extend, build and push docker image
name: Test, build and push code to docker registry

on:
push:
branches:
- 'main'
- 'tests'
paths:
- 'Dockerfile'
- '.github/workflows/build-and-push-docker-image.yaml'
Expand All @@ -14,9 +15,36 @@ on:
workflow_call:

jobs:

test-python:
runs-on: ubuntu-latest

permissions:
contents: read

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Setup python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Install Dependencies
run: |
python -m pip install --uprade pip
pip install -r requirements.txt
- name: Run Unittest
run: |
python -m unittest discover
python -m unittest -v
build-and-push-image:
runs-on: ubuntu-latest
needs: test-python

permissions:
contents: read
Expand Down
Empty file added __init__.py
Empty file.
9 changes: 9 additions & 0 deletions build_data_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def build_teacher_pii_data_asset() -> tuple:
}
template_file = open("yaml-templates/data_assets_template.yaml")
template_str = template_file.read()
template_file.close()
data_asset_template = Template(template_str)
data_asset_yaml = data_asset_template.render(data_asset_dict)

Expand All @@ -41,6 +42,7 @@ def build_student_pii_data_asset() -> tuple:
}
template_file = open("yaml-templates/data_assets_template.yaml")
template_str = template_file.read()
template_file.close()
data_asset_template = Template(template_str)
data_asset_yaml = data_asset_template.render(data_asset_dict)

Expand Down Expand Up @@ -72,6 +74,7 @@ def build_client_app_data_asset() -> tuple:
}
template_file = open("yaml-templates/data_assets_template.yaml")
template_str = template_file.read()
template_file.close()
data_asset_template = Template(template_str)
data_asset_yaml = data_asset_template.render(data_asset_dict)

Expand All @@ -96,6 +99,7 @@ def build_server_app_data_asset() -> tuple:
}
template_file = open("yaml-templates/data_assets_template.yaml")
template_str = template_file.read()
template_file.close()
data_asset_template = Template(template_str)
data_asset_yaml = data_asset_template.render(data_asset_dict)

Expand Down Expand Up @@ -126,6 +130,7 @@ def build_vulnerable_children_data_asset() -> tuple:
}
template_file = open("yaml-templates/data_assets_template.yaml")
template_str = template_file.read()
template_file.close()
data_asset_template = Template(template_str)
data_asset_yaml = data_asset_template.render(data_asset_dict)

Expand All @@ -150,6 +155,7 @@ def build_job_information_data_asset() -> tuple:
}
template_file = open("yaml-templates/data_assets_template.yaml")
template_str = template_file.read()
template_file.close()
data_asset_template = Template(template_str)
data_asset_yaml = data_asset_template.render(data_asset_dict)

Expand All @@ -174,6 +180,7 @@ def build_school_data_asset() -> tuple:
}
template_file = open("yaml-templates/data_assets_template.yaml")
template_str = template_file.read()
template_file.close()
data_asset_template = Template(template_str)
data_asset_yaml = data_asset_template.render(data_asset_dict)

Expand Down Expand Up @@ -205,6 +212,7 @@ def build_payment_details_asset() -> tuple:
}
template_file = open("yaml-templates/data_assets_template.yaml")
template_str = template_file.read()
template_file.close()
data_asset_template = Template(template_str)
data_asset_yaml = data_asset_template.render(data_asset_dict)

Expand Down Expand Up @@ -235,6 +243,7 @@ def build_secrets_asset() -> tuple:
}
template_file = open("yaml-templates/data_assets_template.yaml")
template_str = template_file.read()
template_file.close()
data_asset_template = Template(template_str)
data_asset_yaml = data_asset_template.render(data_asset_dict)

Expand Down
15 changes: 10 additions & 5 deletions dfe_threagile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def temp_file_read() -> list:
try:
file = open("/app/work/test-data.json", "r")
except FileNotFoundError as e:
print("test-data.json file not found, this file is for testing purposes - automated Azure resource collection feature not yet implemented.")
print(
"test-data.json file not found, this file is for testing purposes - automated Azure resource collection feature not yet implemented."
)
sys.exit(0)
lines = file.readlines()

Expand Down Expand Up @@ -342,7 +344,7 @@ def produce_asset_lists() -> tuple:
print(risks_output)
else:

# Writes initial threat model and produces risks.json
# Writes initial threat model and produces risks.json

yaml_list, data_list, all_tags = produce_asset_lists()

Expand All @@ -362,19 +364,22 @@ def produce_asset_lists() -> tuple:
"threagile -verbose -model /app/yaml-templates/threagile-pre-risks.yaml -output /app/work/output"
)


# Writes final version, with risks from risks.json added to the yaml for automated mitigation tracking

risks = read_risks_json("/app/work/output/risks.json")

final_with_risks = template_inject(yaml_list, data_list, all_tags, risks)

try:
with open("/app/work/yaml-templates/dfe-threagile-final.yaml", "x") as yaml_file:
with open(
"/app/work/yaml-templates/dfe-threagile-final.yaml", "x"
) as yaml_file:
yaml_file.write(final_with_risks)
except FileExistsError:
print("File exists, overwriting...")
with open("/app/work/yaml-templates/dfe-threagile-final.yaml", "w") as yaml_file:
with open(
"/app/work/yaml-templates/dfe-threagile-final.yaml", "w"
) as yaml_file:
yaml_file.write(final_with_risks)

os.system(
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jinja2
Empty file added tests/__init__.py
Empty file.
Loading

0 comments on commit 05051f8

Please sign in to comment.