From 0225557c7859e520a29be66f03413777bc4b3698 Mon Sep 17 00:00:00 2001 From: jamesstottmoj Date: Tue, 9 Apr 2024 12:05:19 +0100 Subject: [PATCH] test to see if this resets commits --- .gitignore | 1 + Dockerfile | 34 ++++---- doc/running.md | 4 +- package-lock.json | 50 ++++++------ requirements.dev.txt | 20 ++--- requirements.txt | 39 +++++----- tests/api/cluster/test_access_to_s3buckets.py | 10 +-- tests/api/cluster/test_bucket.py | 4 +- tests/api/cluster/test_group.py | 4 +- tests/api/cluster/test_user.py | 23 +++--- tests/api/filters/test_app_filter.py | 14 ++-- tests/api/filters/test_apps3bucket_filter.py | 4 +- tests/api/filters/test_s3bucket_filter.py | 8 +- tests/api/filters/test_users3bucket_filter.py | 24 +++--- tests/api/fixtures/aws.py | 19 +++-- tests/api/models/test_app.py | 20 ++--- tests/api/models/test_apps3bucket.py | 8 +- tests/api/models/test_ip_allowlist.py | 33 +------- tests/api/models/test_s3bucket.py | 6 +- tests/api/models/test_tool.py | 4 +- tests/api/models/test_user.py | 28 ------- tests/api/models/test_users3bucket.py | 6 +- tests/api/permissions/test_app_permissions.py | 13 ++-- .../test_apps3bucket_permissions.py | 22 +++--- .../permissions/test_s3bucket_permissions.py | 4 +- .../test_users3bucket_permissions.py | 10 +-- .../tasks/test_create_app_auth_settings.py | 6 +- tests/api/tasks/test_create_app_aws_role.py | 4 +- tests/api/tasks/test_s3.py | 20 ++--- tests/api/test_auth0.py | 19 ++--- tests/api/test_aws.py | 78 ++----------------- tests/api/test_pagination.py | 4 +- tests/api/views/test_app.py | 10 +-- tests/api/views/test_apps3bucket.py | 10 +-- tests/api/views/test_customer.py | 4 +- tests/api/views/test_s3bucket.py | 12 +-- tests/api/views/test_user.py | 6 +- tests/api/views/test_userapp.py | 6 +- tests/conftest.py | 8 +- tests/frontend/test_forms.py | 3 +- tests/frontend/views/test_app.py | 24 +++--- tests/frontend/views/test_app_variables.py | 36 +++++---- tests/frontend/views/test_auth.py | 2 +- tests/frontend/views/test_datasource.py | 28 +++---- tests/frontend/views/test_index.py | 2 +- tests/frontend/views/test_ip_allowlist.py | 4 +- tests/frontend/views/test_secrets.py | 29 +++---- tests/frontend/views/test_user.py | 1 - tests/kubeapi/test_permissions.py | 6 +- 49 files changed, 312 insertions(+), 422 deletions(-) diff --git a/.gitignore b/.gitignore index 984133cbe..41b77b685 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ __pycache__ /static /tags /venv +/venv_311 /.python-version /coverage/ .idea/ diff --git a/Dockerfile b/Dockerfile index 9b124600d..341f4deb2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM 593291632749.dkr.ecr.eu-west-1.amazonaws.com/node:18.12.1-slim AS jsdep +FROM public.ecr.aws/docker/library/node:20.11.1 AS build-node COPY package.json package-lock.json ./ COPY jest.config.js controlpanel/frontend/static /src/ @@ -9,7 +9,7 @@ RUN ./node_modules/.bin/sass --load-path=node_modules/ --style=compressed src/ap WORKDIR /src RUN /node_modules/.bin/jest -FROM 593291632749.dkr.ecr.eu-west-1.amazonaws.com/python:3.9-slim-buster AS base +FROM public.ecr.aws/docker/library/python:3.12-alpine3.18 AS base ARG HELM_VERSION=3.14.1 ARG HELM_TARBALL=helm-v${HELM_VERSION}-linux-amd64.tar.gz @@ -22,18 +22,20 @@ ENV DJANGO_SETTINGS_MODULE="controlpanel.settings" \ HELM_DATA_HOME=/tmp/helm/data # create a user to run as -RUN addgroup -gid 1000 controlpanel && \ - adduser -uid 1000 --gid 1000 controlpanel +RUN addgroup -g 1000 controlpanel \ + && adduser -G controlpanel -u 1000 controlpanel -D -RUN apt-get update \ - && apt-get install -y --no-install-recommends \ +RUN apk update \ + && apk add --no-cache \ postgresql-client \ wget \ gcc \ - libcurl4-gnutls-dev \ + curl-dev \ python3-dev \ - libgnutls28-dev \ - libssl-dev \ + gnutls-dev \ + openssl-dev \ + libffi-dev \ + musl-dev \ && rm -rf /var/lib/apt/lists/* WORKDIR /home/controlpanel @@ -65,13 +67,13 @@ COPY docker docker COPY tests tests # install javascript dependencies -COPY --from=jsdep dist/app.css dist/app.js static/ -COPY --from=jsdep node_modules/accessible-autocomplete/dist/ static/accessible-autocomplete -COPY --from=jsdep node_modules/govuk-frontend static/govuk-frontend -COPY --from=jsdep node_modules/@ministryofjustice/frontend/moj static/ministryofjustice-frontend -COPY --from=jsdep node_modules/html5shiv/dist static/html5-shiv -COPY --from=jsdep node_modules/jquery/dist static/jquery -COPY --from=jsdep node_modules/jquery-ui/dist/ static/jquery-ui +COPY --from=build-node dist/app.css dist/app.js static/ +COPY --from=build-node node_modules/accessible-autocomplete/dist/ static/accessible-autocomplete +COPY --from=build-node node_modules/govuk-frontend static/govuk-frontend +COPY --from=build-node node_modules/@ministryofjustice/frontend/moj static/ministryofjustice-frontend +COPY --from=build-node node_modules/html5shiv/dist static/html5-shiv +COPY --from=build-node node_modules/jquery/dist static/jquery +COPY --from=build-node node_modules/jquery-ui/dist/ static/jquery-ui # empty .env file to prevent warning messages RUN touch .env diff --git a/doc/running.md b/doc/running.md index 3837fc3bb..31533d307 100644 --- a/doc/running.md +++ b/doc/running.md @@ -27,8 +27,8 @@ versions of the services. ## 1. Required Dependencies -The Control Panel app requires Python 3.8+ It has been confirmed to work -with Python 3.8.12. +The Control Panel app requires Python 3.12. It has been confirmed to work +with Python 3.12.2. Install python dependencies with the following command: ```sh diff --git a/package-lock.json b/package-lock.json index 2e17ac954..12cfd0bea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,27 +9,27 @@ "version": "0.1.0", "license": "MIT", "dependencies": { - "@ministryofjustice/frontend": "^0.0.17-alpha", - "accessible-autocomplete": "^2.0.4", - "core-js": "^3.26.1", - "govuk-frontend": "^3.0.0", - "html5shiv": "^3.7.3", - "jquery": "^3.6.1", - "jquery-ui": "^1.13.2", - "sass": "^1.56.1" + "@ministryofjustice/frontend": "0.0.17-alpha", + "accessible-autocomplete": "2.0.4", + "core-js": "3.26.1", + "govuk-frontend": "3.0.0", + "html5shiv": "3.7.3", + "jquery": "3.6.1", + "jquery-ui": "1.13.2", + "sass": "1.56.1" }, "devDependencies": { - "@babel/cli": "^7.19.3", - "@babel/core": "^7.20.5", - "@babel/plugin-transform-regenerator": "^7.20.5", - "@babel/plugin-transform-runtime": "^7.19.6", - "@babel/preset-env": "^7.20.2", - "@testing-library/jest-dom": "^5.16.5", - "babel-jest": "^29.3.1", - "jest": "^29.3.1", - "jest-environment-jsdom": "^29.3.1", - "jsdom-simulant": "^1.1.2", - "npm-run-all": "^4.1.5" + "@babel/cli": "7.19.3", + "@babel/core": "7.20.5", + "@babel/plugin-transform-regenerator": "7.20.5", + "@babel/plugin-transform-runtime": "7.19.6", + "@babel/preset-env": "7.20.2", + "@testing-library/jest-dom": "5.16.5", + "babel-jest": "29.3.1", + "jest": "29.3.1", + "jest-environment-jsdom": "29.3.1", + "jsdom-simulant": "1.1.2", + "npm-run-all": "4.1.5" } }, "node_modules/@adobe/css-tools": { @@ -4165,9 +4165,9 @@ } }, "node_modules/govuk-frontend": { - "version": "3.14.0", - "resolved": "https://registry.npmjs.org/govuk-frontend/-/govuk-frontend-3.14.0.tgz", - "integrity": "sha512-y7FTuihCSA8Hty+e9h0uPhCoNanCAN+CLioNFlPmlbeHXpbi09VMyxTcH+XfnMPY4Cp++7096v0rLwwdapTXnA==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/govuk-frontend/-/govuk-frontend-3.0.0.tgz", + "integrity": "sha512-GCrEeaQZEnsthNtfmOUFlgsieNjHOeoamSWMdD4Gdq0RPxCA9uzfrT2i3jVnlBORekKjOL0C8eFZQBSNnjtz2A==", "engines": { "node": ">= 4.2.0" } @@ -11423,9 +11423,9 @@ } }, "govuk-frontend": { - "version": "3.14.0", - "resolved": "https://registry.npmjs.org/govuk-frontend/-/govuk-frontend-3.14.0.tgz", - "integrity": "sha512-y7FTuihCSA8Hty+e9h0uPhCoNanCAN+CLioNFlPmlbeHXpbi09VMyxTcH+XfnMPY4Cp++7096v0rLwwdapTXnA==" + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/govuk-frontend/-/govuk-frontend-3.0.0.tgz", + "integrity": "sha512-GCrEeaQZEnsthNtfmOUFlgsieNjHOeoamSWMdD4Gdq0RPxCA9uzfrT2i3jVnlBORekKjOL0C8eFZQBSNnjtz2A==" }, "graceful-fs": { "version": "4.2.10", diff --git a/requirements.dev.txt b/requirements.dev.txt index 236dc0fc8..6211e258b 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -1,12 +1,12 @@ -black==24.3.0 -django-debug-toolbar==3.2.4 +black==24.2.0 +django-debug-toolbar==4.3.0 django-debug-toolbar-requests==1.0.5 -django-elasticsearch-debug-toolbar==2.0.0 -flake8==6.0.0 +django-elasticsearch-debug-toolbar==3.0.2 +flake8==7.0.0 ipdb==0.13.13 -ipython==8.13.2 -isort==5.12.0 -pre-commit==2.20.0 -pylint==2.17.4 -pylint-django==2.4.4 -pandas==1.5.2 \ No newline at end of file +ipython==8.21.0 +isort==5.13.2 +pandas==2.2.0 +pre-commit==3.6.1 +pylint==3.0.3 +pylint-django==2.5.5 diff --git a/requirements.txt b/requirements.txt index 063e7cd83..d09ee36ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,39 +1,38 @@ -asgiref==3.7.2 -auth0-python==4.5.0 -authlib==1.3.0 -beautifulsoup4==4.12.2 -boto3==1.34.64 -celery[sqs]==5.3.1 +asgiref==3.8.1 +auth0-python==4.7.0 +beautifulsoup4==4.12.3 +boto3==1.34.41 +celery[sqs]==5.3.6 channels==4.0.0 channels-redis==4.2.0 -daphne==4.0.0 -Django==4.2.11 +daphne==4.1.0 +Django==5.0.4 django-crequest==2018.5.11 django-extensions==3.2.3 -django-filter==24.1 +django-filter==23.5 django-prometheus==2.3.1 django-redis==5.4.0 -django-simple-history==3.3.0 -django-structlog==2.2.0 +django-simple-history==3.4.0 +django-structlog==7.1.0 djangorestframework==3.14.0 djproxy==2.3.6 elasticsearch-dsl==7.4.1 -gunicorn==20.1.0 +gunicorn==21.2.0 Jinja2==3.1.3 kubernetes==25.3.0 -MarkupSafe==2.1.3 -model-mommy==2.0.0 -moto==1.3.14 +MarkupSafe==2.1.5 +model-bakery==1.17.0 +moto==5.0.1 mozilla-django-oidc==4.0.0 psycopg2-binary==2.9.9 PyNaCl==1.5.0 -pytest==7.3.1 -pytest-django==4.5.2 +pytest==8.0.0 +pytest-django==4.8.0 python-dotenv==1.0.1 python-jose==3.3.0 pyyaml==6.0.1 rules==3.3 -sentry-sdk==1.40.5 +sentry-sdk==1.40.4 slackclient==2.9.4 -urllib3==1.26.18 -uvicorn[standard]==0.28.0 +urllib3==2.0.7 +uvicorn[standard]==0.27.1 diff --git a/tests/api/cluster/test_access_to_s3buckets.py b/tests/api/cluster/test_access_to_s3buckets.py index a49cb51ce..0655d1382 100644 --- a/tests/api/cluster/test_access_to_s3buckets.py +++ b/tests/api/cluster/test_access_to_s3buckets.py @@ -3,7 +3,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api.cluster import App, RoleGroup, User @@ -32,7 +32,7 @@ def grant_policy_bucket_access(): ) as grant_policy_bucket_access_action: yield grant_policy_bucket_access_action -@pytest.yield_fixture +@pytest.fixture def grant_policy_folder_access(): with patch( "controlpanel.api.cluster.AWSPolicy.grant_folder_access" @@ -47,14 +47,14 @@ def enable_db_for_all_tests(db): @pytest.fixture def bucket(): - return mommy.prepare("api.S3Bucket") + return baker.prepare("api.S3Bucket") @pytest.fixture def entities(bucket, users): return { - "app": App(mommy.prepare("api.App")), - "group": RoleGroup(mommy.prepare("api.IAMManagedPolicy")), + "app": App(baker.prepare("api.App")), + "group": RoleGroup(baker.prepare("api.IAMManagedPolicy")), "user": User(users["normal_user"]), } diff --git a/tests/api/cluster/test_bucket.py b/tests/api/cluster/test_bucket.py index 07f118409..423dcedfb 100644 --- a/tests/api/cluster/test_bucket.py +++ b/tests/api/cluster/test_bucket.py @@ -4,7 +4,7 @@ # Third-party import pytest from django.conf import settings -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api import cluster @@ -18,7 +18,7 @@ def enable_db_for_all_tests(db): @pytest.fixture def bucket(): - return mommy.prepare("api.S3Bucket", name="test-bucket") + return baker.prepare("api.S3Bucket", name="test-bucket") def test_arn(bucket): diff --git a/tests/api/cluster/test_group.py b/tests/api/cluster/test_group.py index 6160679fd..a86006e46 100644 --- a/tests/api/cluster/test_group.py +++ b/tests/api/cluster/test_group.py @@ -3,7 +3,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api import cluster @@ -16,7 +16,7 @@ def enable_db_for_all_tests(db): @pytest.fixture def iam_managed_policy(): - return mommy.make("api.IAMManagedPolicy", name="test") + return baker.make("api.IAMManagedPolicy", name="test") def test_arn(settings, iam_managed_policy): diff --git a/tests/api/cluster/test_user.py b/tests/api/cluster/test_user.py index 3c42619bf..6abed4c02 100644 --- a/tests/api/cluster/test_user.py +++ b/tests/api/cluster/test_user.py @@ -1,5 +1,5 @@ # Standard library -from unittest.mock import MagicMock, call, patch +from unittest.mock import call, patch # Third-party import pytest @@ -26,18 +26,19 @@ def test_create(helm, settings, users): call( f"bootstrap-user-{user.slug}", "mojanalytics/bootstrap-user", - f"--namespace=user-{user.slug}", - f"--set=Username={user.slug},", - f"Efsvolume={settings.EFS_VOLUME}", + f"--namespace=cpanel", + f"--set=Username={user.slug}", ), call( - f"config-user-{user.slug}", - "mojanalytics/config-user", + f"provision-user-{user.slug}", + "mojanalytics/provision-user", f"--namespace=user-{user.slug}", - f"--set=Username={user.slug}", + (f"--set=Username={user.slug},Efsvolume={settings.EFS_VOLUME}," + "OidcDomain=oidc.idp.example.com,Email=,Fullname="), ), ] - helm.upgrade_release.has_calls(expected_calls) + + helm.upgrade_release.assert_has_calls(expected_calls) def test_reset_home(helm, users): @@ -52,6 +53,7 @@ def test_reset_home(helm, users): f"--set=Username={user.slug}", ), ] + helm.upgrade_release.assert_has_calls(expected_calls) @@ -74,10 +76,9 @@ def test_delete(aws_delete_role, helm, users): cluster.User(user).delete() aws_delete_role.assert_called_with(user.iam_role_name) expected_calls = [ - call(f"user-{user.slug}", "chart-release"), - call("cpanel", "chart-release"), + call('user-bob', 'chart-release', dry_run=False), ] - helm.delete.has_calls(expected_calls) + helm.delete.assert_has_calls(expected_calls) def test_delete_eks_with_no_releases(aws_delete_role, helm, users): diff --git a/tests/api/filters/test_app_filter.py b/tests/api/filters/test_app_filter.py index 114cbdc91..fb7a3dd17 100644 --- a/tests/api/filters/test_app_filter.py +++ b/tests/api/filters/test_app_filter.py @@ -1,17 +1,17 @@ # Third-party -from model_mommy import mommy +from model_bakery import baker from rest_framework.reverse import reverse from rest_framework.test import APITestCase class AppFilterTest(APITestCase): def setUp(self): - self.superuser = mommy.make("api.User", is_superuser=True) - self.app_admin = mommy.make("api.User", is_superuser=False) - self.app_1 = mommy.make("api.App", name="App 1") - self.app_2 = mommy.make("api.App", name="App 2") - mommy.make("api.UserApp", user=self.app_admin, app=self.app_1, is_admin=True) - mommy.make("api.UserApp", user=self.app_admin, app=self.app_2, is_admin=True) + self.superuser = baker.make("api.User", is_superuser=True) + self.app_admin = baker.make("api.User", is_superuser=False) + self.app_1 = baker.make("api.App", name="App 1") + self.app_2 = baker.make("api.App", name="App 2") + baker.make("api.UserApp", user=self.app_admin, app=self.app_1, is_admin=True) + baker.make("api.UserApp", user=self.app_admin, app=self.app_2, is_admin=True) def test_everyone_see_everything(self): for user in [self.superuser, self.app_admin]: diff --git a/tests/api/filters/test_apps3bucket_filter.py b/tests/api/filters/test_apps3bucket_filter.py index b779d396a..2daa715c0 100644 --- a/tests/api/filters/test_apps3bucket_filter.py +++ b/tests/api/filters/test_apps3bucket_filter.py @@ -3,7 +3,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker from rest_framework.reverse import reverse NUM_APPS3BUCKETS = 2 @@ -13,7 +13,7 @@ def apps3buckets(s3, sqs): with patch("controlpanel.api.aws.AWSBucket.create"),\ patch("controlpanel.api.aws.AWSRole.grant_bucket_access"): - mommy.make("api.AppS3Bucket", NUM_APPS3BUCKETS) + baker.make("api.AppS3Bucket", NUM_APPS3BUCKETS) def list(client, *args): diff --git a/tests/api/filters/test_s3bucket_filter.py b/tests/api/filters/test_s3bucket_filter.py index 54095db48..34e31f976 100644 --- a/tests/api/filters/test_s3bucket_filter.py +++ b/tests/api/filters/test_s3bucket_filter.py @@ -1,6 +1,6 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.reverse import reverse @@ -13,14 +13,14 @@ def enable_db_for_all_tests(db): @pytest.fixture(autouse=True) def buckets(db): return { - 1: mommy.make("api.S3Bucket", name="test-bucket-1"), - 2: mommy.make("api.S3Bucket", name="test-bucket-2"), + 1: baker.make("api.S3Bucket", name="test-bucket-1"), + 2: baker.make("api.S3Bucket", name="test-bucket-2"), } @pytest.fixture(autouse=True) def users3bucket(db, buckets, users): - return mommy.make( + return baker.make( "api.UserS3Bucket", user=users["normal_user"], s3bucket=buckets[1], diff --git a/tests/api/filters/test_users3bucket_filter.py b/tests/api/filters/test_users3bucket_filter.py index 2071d92cd..efafb2089 100644 --- a/tests/api/filters/test_users3bucket_filter.py +++ b/tests/api/filters/test_users3bucket_filter.py @@ -1,5 +1,5 @@ # Third-party -from model_mommy import mommy +from model_bakery import baker from rest_framework.reverse import reverse from rest_framework.status import HTTP_200_OK from rest_framework.test import APITestCase @@ -10,39 +10,39 @@ class UserS3BucketFilterTest(APITestCase): def setUp(self): - self.superuser = mommy.make("api.User", is_superuser=True) - self.normal_user = mommy.make("api.User", is_superuser=False) - self.other_user = mommy.make("api.User", is_superuser=False) + self.superuser = baker.make("api.User", is_superuser=True) + self.normal_user = baker.make("api.User", is_superuser=False) + self.other_user = baker.make("api.User", is_superuser=False) - self.s3bucket_1 = mommy.make("api.S3Bucket", name="test-bucket-1") - self.s3bucket_2 = mommy.make("api.S3Bucket", name="test-bucket-2") - self.s3bucket_3 = mommy.make("api.S3Bucket", name="test-bucket-3") + self.s3bucket_1 = baker.make("api.S3Bucket", name="test-bucket-1") + self.s3bucket_2 = baker.make("api.S3Bucket", name="test-bucket-2") + self.s3bucket_3 = baker.make("api.S3Bucket", name="test-bucket-3") - self.s3bucket_1_normal_user_access = mommy.make( + self.s3bucket_1_normal_user_access = baker.make( "api.UserS3Bucket", user=self.normal_user, s3bucket=self.s3bucket_1, access_level=UserS3Bucket.READONLY, ) - self.s3bucket_1_other_user_access = mommy.make( + self.s3bucket_1_other_user_access = baker.make( "api.UserS3Bucket", user=self.other_user, s3bucket=self.s3bucket_1, access_level=UserS3Bucket.READONLY, ) - self.s3bucket_2_normal_user_access = mommy.make( + self.s3bucket_2_normal_user_access = baker.make( "api.UserS3Bucket", user=self.normal_user, s3bucket=self.s3bucket_2, access_level=UserS3Bucket.READONLY, ) - self.s3bucket_2_superuser_access = mommy.make( + self.s3bucket_2_superuser_access = baker.make( "api.UserS3Bucket", user=self.superuser, s3bucket=self.s3bucket_2, access_level=UserS3Bucket.READWRITE, ) - self.s3bucket_3_superuser_access = mommy.make( + self.s3bucket_3_superuser_access = baker.make( "api.UserS3Bucket", user=self.superuser, s3bucket=self.s3bucket_3, diff --git a/tests/api/fixtures/aws.py b/tests/api/fixtures/aws.py index d3d3e3e59..c52495e23 100644 --- a/tests/api/fixtures/aws.py +++ b/tests/api/fixtures/aws.py @@ -19,31 +19,31 @@ def aws_creds(): @pytest.fixture(autouse=True) def iam(aws_creds): - with moto.mock_iam(): + with moto.mock_aws(): yield boto3.Session().resource("iam") @pytest.fixture(autouse=True) def s3(aws_creds): - with moto.mock_s3(): + with moto.mock_aws(): yield boto3.resource("s3") @pytest.fixture(autouse=True) def sts(aws_creds): - with moto.mock_sts(): + with moto.mock_aws(): yield boto3.client("sts") @pytest.fixture(autouse=True) def ssm(aws_creds): - with moto.mock_ssm(): + with moto.mock_aws(): yield boto3.client("ssm", region_name="eu-west-1") @pytest.fixture(autouse=True) def sqs(aws_creds): - with moto.mock_sqs(): + with moto.mock_aws(): sqs = boto3.resource("sqs") sqs.create_queue(QueueName=settings.DEFAULT_QUEUE) sqs.create_queue(QueueName=settings.IAM_QUEUE_NAME) @@ -53,7 +53,7 @@ def sqs(aws_creds): @pytest.fixture(autouse=True) def secretsmanager(aws_creds): - with moto.mock_secretsmanager(): + with moto.mock_aws(): yield boto3.client("secretsmanager", region_name="eu-west-1") @@ -207,4 +207,9 @@ def logs_bucket(s3): @pytest.fixture() def root_folder_bucket(s3): - yield s3.create_bucket(Bucket=settings.S3_FOLDER_BUCKET_NAME) + yield s3.create_bucket( + Bucket=settings.S3_FOLDER_BUCKET_NAME, + CreateBucketConfiguration={ + "LocationConstraint": settings.BUCKET_REGION, # noqa: F405 + }, + ) diff --git a/tests/api/models/test_app.py b/tests/api/models/test_app.py index 58d5e8d8c..11f6c1cca 100644 --- a/tests/api/models/test_app.py +++ b/tests/api/models/test_app.py @@ -4,7 +4,7 @@ # Third-party import pytest from django.conf import settings -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api.auth0 import Auth0Error @@ -28,7 +28,7 @@ def update_aws_secrets_manager(): @pytest.fixture def app(): - app = mommy.make("api.App") + app = baker.make("api.App") app.repo_url = "https://github.com/example.com/repo_name" auth_settings = dict( client_id="testing_client_id", @@ -129,7 +129,7 @@ def test_delete_customers(auth0, app): ], ) def test_repo_name(url, expected_name): - app = mommy.prepare("api.App") + app = baker.prepare("api.App") app.repo_url = url assert app._repo_name == expected_name @@ -144,7 +144,7 @@ def test_repo_name(url, expected_name): def test_delete_customer_by_email_error_getting_user(auth0, side_effect, error_message): authz = auth0.ExtendedAuth0.return_value authz.users.get_users_email_search.side_effect = side_effect - app = mommy.prepare("api.App") + app = baker.prepare("api.App") with pytest.raises(app.DeleteCustomerError, match=error_message): app.delete_customer_by_email("foo@email.com", group_id="123") @@ -157,7 +157,7 @@ def test_delete_customer_by_email_user_missing_group(auth0): authz.users.get_users_email_search.return_value = [user] authz.users.get_user_groups.return_value = [user_groups] - app = mommy.prepare("api.App") + app = baker.prepare("api.App") with pytest.raises( app.DeleteCustomerError, match="User foo@email.com cannot be found in this application group" @@ -173,7 +173,7 @@ def test_delete_customer_by_email_success(auth0): authz.users.get_users_email_search.return_value = [user] authz.users.get_user_groups.return_value = [user_groups] - app = mommy.prepare("api.App") + app = baker.prepare("api.App") with patch.object(app, "delete_customers") as delete_customers: app.delete_customer_by_email("foo@email.com", group_id="123") @@ -186,12 +186,12 @@ def test_delete_customer_by_email_success(auth0): @pytest.mark.django_db def test_app_allowed_ip_ranges(): ip_allow_lists = [ - mommy.make("api.IPAllowlist", allowed_ip_ranges="127.0.0.1, 128.10.10.100"), - mommy.make("api.IPAllowlist", allowed_ip_ranges=" 123.0.0.122,152.0.0.1"), + baker.make("api.IPAllowlist", allowed_ip_ranges="127.0.0.1, 128.10.10.100"), + baker.make("api.IPAllowlist", allowed_ip_ranges=" 123.0.0.122,152.0.0.1"), ] - app = mommy.make("api.App") # noqa:F841 + app = baker.make("api.App") # noqa:F841 for item in ip_allow_lists: - mommy.make( + baker.make( "api.AppIPAllowList", app_id=app.id, ip_allowlist_id=item.id, diff --git a/tests/api/models/test_apps3bucket.py b/tests/api/models/test_apps3bucket.py index 2d27bf3e4..f9dca4dab 100644 --- a/tests/api/models/test_apps3bucket.py +++ b/tests/api/models/test_apps3bucket.py @@ -5,7 +5,7 @@ import pytest from django.conf import settings from django.db.utils import IntegrityError -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api.models import AppS3Bucket @@ -13,12 +13,12 @@ @pytest.fixture def app(): - return mommy.make("api.App", name="app_1") + return baker.make("api.App", name="app_1") @pytest.fixture def bucket(): - return mommy.make("api.S3Bucket", name="test-bucket-1") + return baker.make("api.S3Bucket", name="test-bucket-1") @pytest.mark.django_db @@ -57,7 +57,7 @@ def test_delete_revoke_permissions(app, bucket): with patch( "controlpanel.api.tasks.S3BucketRevokeAppAccess" ) as revoke_bucket_access_task: - apps3bucket = mommy.make( + apps3bucket = baker.make( "api.AppS3Bucket", app=app, s3bucket=bucket, diff --git a/tests/api/models/test_ip_allowlist.py b/tests/api/models/test_ip_allowlist.py index 78555b76d..56d6a98fb 100644 --- a/tests/api/models/test_ip_allowlist.py +++ b/tests/api/models/test_ip_allowlist.py @@ -3,7 +3,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker @pytest.fixture(autouse=True) @@ -19,47 +19,20 @@ def update_aws_secrets_manager(): yield update_aws_secrets_manager -# def test_ip_allowlist_save_updates_aws_secrets_manager(update_aws_secrets_manager): -# ip_allowlist = mommy.make("api.IPAllowlist", allowed_ip_ranges="123") -# app = mommy.make("api.App", ip_allowlists=[ip_allowlist]) # noqa:F841 -# -# ip_allowlist.save() -# -# update_aws_secrets_manager.assert_has_calls( -# [ -# call({"allowed_ip_ranges": "123"}), -# ] -# ) -# - def test_ip_allowlist_without_app_save_does_not_update_aws_secrets_manager( update_aws_secrets_manager, ): - ip_allowlist = mommy.make("api.IPAllowlist") + ip_allowlist = baker.make("api.IPAllowlist") ip_allowlist.save() update_aws_secrets_manager.assert_not_called() -# def test_ip_allowlist_delete_updates_aws_secrets_manager(update_aws_secrets_manager): -# ip_allowlists = [ -# mommy.make("api.IPAllowlist", allowed_ip_ranges="xyz"), -# mommy.make("api.IPAllowlist", allowed_ip_ranges="123"), -# ] -# apps = mommy.make("api.App", ip_allowlists=ip_allowlists, _quantity=2) # noqa:F841 -# -# ip_allowlists[0].delete() -# -# update_aws_secrets_manager.assert_has_calls( -# [call({"allowed_ip_ranges": "123"}), call({"allowed_ip_ranges": "123"})] -# ) -# - def test_ip_allowlist_without_app_delete_does_not_update_aws_secrets_manager( update_aws_secrets_manager, ): - ip_allowlist = mommy.make("api.IPAllowlist") + ip_allowlist = baker.make("api.IPAllowlist") ip_allowlist.delete() diff --git a/tests/api/models/test_s3bucket.py b/tests/api/models/test_s3bucket.py index 63c999cbb..c97d32427 100644 --- a/tests/api/models/test_s3bucket.py +++ b/tests/api/models/test_s3bucket.py @@ -5,7 +5,7 @@ import pytest from botocore.exceptions import ClientError from django.conf import settings -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api import cluster @@ -26,8 +26,8 @@ def test_delete_revokes_permissions(bucket): with patch("controlpanel.api.models.AppS3Bucket.revoke_bucket_access") as app_revoke_bucket_access, \ patch("controlpanel.api.models.UserS3Bucket.revoke_bucket_access") as user_revoke_user_bucket_access: # link the bucket with an UserS3Bucket and AppS3Bucket - mommy.make("api.UserS3Bucket", s3bucket=bucket) - mommy.make("api.AppS3Bucket", s3bucket=bucket) + baker.make("api.UserS3Bucket", s3bucket=bucket) + baker.make("api.AppS3Bucket", s3bucket=bucket) # delete the source S3Bucket bucket.delete() # check that related objects revoke access methods were called diff --git a/tests/api/models/test_tool.py b/tests/api/models/test_tool.py index c31631602..bff8b481a 100644 --- a/tests/api/models/test_tool.py +++ b/tests/api/models/test_tool.py @@ -4,7 +4,7 @@ # Third-party import pytest from django.conf import settings -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api.models import HomeDirectory, Tool, ToolDeployment, User @@ -12,7 +12,7 @@ @pytest.fixture def tool(db): - return mommy.make("api.Tool") + return baker.make("api.Tool") def test_deploy_for_generic(helm, tool, users): diff --git a/tests/api/models/test_user.py b/tests/api/models/test_user.py index aa173cbc0..2a629fe5f 100644 --- a/tests/api/models/test_user.py +++ b/tests/api/models/test_user.py @@ -3,8 +3,6 @@ # Third-party import pytest -from django.conf import settings -from model_mommy import mommy # First-party/Local from controlpanel.api import cluster @@ -22,32 +20,6 @@ def auth0(): yield auth0 -def test_helm_create_user(helm): - user = mommy.prepare("api.User") - - expected_calls = [ - call( - f"bootstrap-user-{user.slug}", - "mojanalytics/bootstrap-user", - f"--set=Username={user.slug}", - ), - call( - f"provision-user-{user.slug}", - "mojanalytics/provision-user", - f"--namespace=user-{user.slug}", - f"--set=Username={user.slug},", - f"Efsvolume={settings.EFS_VOLUME}", - ), - call( - f"config-user-{user.slug}", - "mojanalytics/config-user", - f"--namespace=user-{user.slug}", - f"--set=Username={user.slug}", - ), - ] - helm.upgrade_release.has_calls(expected_calls) - - def test_helm_delete_user(helm, auth0): user = User.objects.create(username="bob", auth0_id="github|user_2") authz = auth0.ExtendedAuth0.return_value diff --git a/tests/api/models/test_users3bucket.py b/tests/api/models/test_users3bucket.py index c67b80ea0..afec60512 100644 --- a/tests/api/models/test_users3bucket.py +++ b/tests/api/models/test_users3bucket.py @@ -5,7 +5,7 @@ import pytest from django.conf import settings from django.db.utils import IntegrityError -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api.models import UserS3Bucket @@ -14,12 +14,12 @@ @pytest.fixture def user(): - return mommy.make("api.User", auth0_id="github|user_1", username="user_1") + return baker.make("api.User", auth0_id="github|user_1", username="user_1") @pytest.fixture def bucket(): - return mommy.make("api.S3Bucket", name="test-bucket-1") + return baker.make("api.S3Bucket", name="test-bucket-1") @pytest.fixture diff --git a/tests/api/permissions/test_app_permissions.py b/tests/api/permissions/test_app_permissions.py index b7e53725b..0a169a1de 100644 --- a/tests/api/permissions/test_app_permissions.py +++ b/tests/api/permissions/test_app_permissions.py @@ -6,9 +6,8 @@ # Third-party import pytest -from django.contrib import auth from django.contrib.auth import get_user -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.reverse import reverse from rules import perm_exists @@ -17,12 +16,12 @@ @pytest.fixture def users(users): users.update({ - "app_admin": mommy.make( + "app_admin": baker.make( "api.User", username="dave", auth0_id="github|user_4", ), - "app_user": mommy.make( + "app_user": baker.make( "api.User", username="testing", auth0_id="github|user_5", @@ -33,12 +32,12 @@ def users(users): @pytest.fixture(autouse=True) def app(users): - app = mommy.make("api.App", name="Test App 1") + app = baker.make("api.App", name="Test App 1") user = users["app_admin"] - mommy.make("api.UserApp", user=user, app=app, is_admin=True) + baker.make("api.UserApp", user=user, app=app, is_admin=True) user = users["app_user"] - mommy.make("api.UserApp", user=user, app=app, is_admin=False) + baker.make("api.UserApp", user=user, app=app, is_admin=False) return app diff --git a/tests/api/permissions/test_apps3bucket_permissions.py b/tests/api/permissions/test_apps3bucket_permissions.py index c4fb865d9..0c72d0e2f 100644 --- a/tests/api/permissions/test_apps3bucket_permissions.py +++ b/tests/api/permissions/test_apps3bucket_permissions.py @@ -4,7 +4,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.reverse import reverse @@ -16,17 +16,17 @@ def users(users): users.update( { - "app_admin": mommy.make( + "app_admin": baker.make( "api.User", username="dave", auth0_id="github|user_4", ), - "bucket_admin": mommy.make( + "bucket_admin": baker.make( "api.User", username="ethel", auth0_id="github|user_5", ), - "app_bucket_admin": mommy.make( + "app_bucket_admin": baker.make( "api.User", username="fred", auth0_id="github|user_6", @@ -38,14 +38,14 @@ def users(users): @pytest.fixture def app(users): - app = mommy.make("api.App", name="App 1") - mommy.make( + app = baker.make("api.App", name="App 1") + baker.make( "api.UserApp", app=app, user=users["app_admin"], is_admin=True, ) - mommy.make( + baker.make( "api.UserApp", app=app, user=users["app_bucket_admin"], @@ -58,16 +58,16 @@ def app(users): def buckets(users): with patch("controlpanel.api.aws.AWSBucket.create"): buckets = { - "first": mommy.make("api.S3Bucket", is_data_warehouse=False), - "other": mommy.make("api.S3Bucket", is_data_warehouse=False), + "first": baker.make("api.S3Bucket", is_data_warehouse=False), + "other": baker.make("api.S3Bucket", is_data_warehouse=False), } - mommy.make( + baker.make( "api.UserS3Bucket", user=users["bucket_admin"], s3bucket=buckets["first"], is_admin=True, ) - mommy.make( + baker.make( "api.UserS3Bucket", user=users["app_bucket_admin"], s3bucket=buckets["first"], diff --git a/tests/api/permissions/test_s3bucket_permissions.py b/tests/api/permissions/test_s3bucket_permissions.py index ab3571b22..ce6a8fa06 100644 --- a/tests/api/permissions/test_s3bucket_permissions.py +++ b/tests/api/permissions/test_s3bucket_permissions.py @@ -5,7 +5,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.reverse import reverse @@ -19,7 +19,7 @@ def users(users): for i, role in enumerate(("viewer", "editor", "admin")): users.update( { - f"bucket_{role}": mommy.make( + f"bucket_{role}": baker.make( "api.User", username=role, auth0_id=f"github|user_{5 + i}", diff --git a/tests/api/permissions/test_users3bucket_permissions.py b/tests/api/permissions/test_users3bucket_permissions.py index 617e3ded8..1a06f9c38 100644 --- a/tests/api/permissions/test_users3bucket_permissions.py +++ b/tests/api/permissions/test_users3bucket_permissions.py @@ -4,7 +4,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.reverse import reverse @@ -16,7 +16,7 @@ def users(users): users.update( { - "bucket_admin": mommy.make( + "bucket_admin": baker.make( "api.User", username="bucket_admin", auth0_id="github|user_4", @@ -29,19 +29,19 @@ def users(users): @pytest.fixture def bucket(): with patch("controlpanel.api.aws.AWSBucket.create"): - return mommy.make("api.S3Bucket") + return baker.make("api.S3Bucket") @pytest.fixture def users3bucket(bucket, users): - mommy.make( + baker.make( "api.UserS3Bucket", user=users["bucket_admin"], s3bucket=bucket, access_level=UserS3Bucket.READONLY, is_admin=True, ) - return mommy.make( + return baker.make( "api.UserS3Bucket", user=users["normal_user"], s3bucket=bucket, diff --git a/tests/api/tasks/test_create_app_auth_settings.py b/tests/api/tasks/test_create_app_auth_settings.py index f2f5acfce..609dbf66a 100644 --- a/tests/api/tasks/test_create_app_auth_settings.py +++ b/tests/api/tasks/test_create_app_auth_settings.py @@ -3,7 +3,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api.models import App @@ -26,7 +26,7 @@ def test_cluster_not_called_without_valid_app(cluster, complete, users): @patch("controlpanel.api.tasks.handlers.app.cluster") @patch("controlpanel.api.models.user.User.github_api_token", new=PropertyMock(return_value=None)) # noqa def test_cluster_not_called_without_github_api_token(cluster, complete, users): - app = mommy.make("api.App") + app = baker.make("api.App") user = users["superuser"] create_app_auth_settings( @@ -44,7 +44,7 @@ def test_cluster_not_called_without_github_api_token(cluster, complete, users): @patch("controlpanel.api.tasks.handlers.app.cluster") @patch("controlpanel.api.models.user.User.github_api_token", new=PropertyMock(return_value="dummy-token")) # noqa def test_valid_app_and_user(cluster, complete, users): - app = mommy.make("api.App") + app = baker.make("api.App") create_app_auth_settings( app.pk, diff --git a/tests/api/tasks/test_create_app_aws_role.py b/tests/api/tasks/test_create_app_aws_role.py index b377323fa..985bed078 100644 --- a/tests/api/tasks/test_create_app_aws_role.py +++ b/tests/api/tasks/test_create_app_aws_role.py @@ -3,7 +3,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api.models import App @@ -29,7 +29,7 @@ def test_cluster_not_called_without_valid_app(cluster, complete, users): @patch("controlpanel.api.tasks.handlers.base.BaseModelTaskHandler.complete") @patch("controlpanel.api.tasks.handlers.app.cluster") def test_valid_app_and_user(cluster, complete, users): - app = mommy.make("api.App") + app = baker.make("api.App") create_app_aws_role(app.pk, users["superuser"].pk) diff --git a/tests/api/tasks/test_s3.py b/tests/api/tasks/test_s3.py index b8134dd01..7a001e00e 100644 --- a/tests/api/tasks/test_s3.py +++ b/tests/api/tasks/test_s3.py @@ -3,7 +3,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api.models import AppS3Bucket, S3Bucket, UserS3Bucket @@ -37,7 +37,7 @@ def test_exception_raised_when_called_without_valid_app( @patch("controlpanel.api.tasks.handlers.base.BaseTaskHandler.complete") @patch("controlpanel.api.models.s3bucket.cluster") def test_bucket_created(cluster, complete, users): - s3bucket = mommy.make("api.S3Bucket", bucket_owner="APP") + s3bucket = baker.make("api.S3Bucket", bucket_owner="APP") create_s3bucket( s3bucket.pk, users["superuser"].pk, bucket_owner=s3bucket.bucket_owner @@ -60,7 +60,7 @@ def test_bucket_created(cluster, complete, users): def test_access_granted( cluster, complete, users, task_method, model_class, cluster_class ): - obj = mommy.make(model_class) + obj = baker.make(model_class) task_method(obj.pk, users["superuser"].pk) @@ -78,7 +78,7 @@ def test_access_granted( @patch("controlpanel.api.tasks.handlers.base.BaseTaskHandler.complete") @patch("controlpanel.api.tasks.handlers.s3.cluster") def test_revoke_user_access(cluster, complete, bucket_name, is_folder): - user_bucket_access = mommy.make("api.UserS3Bucket", s3bucket__name=bucket_name) + user_bucket_access = baker.make("api.UserS3Bucket", s3bucket__name=bucket_name) s3bucket = user_bucket_access.s3bucket bucket_identifier = s3bucket.name if is_folder else s3bucket.arn revoke_user_s3bucket_access( @@ -104,7 +104,7 @@ def test_revoke_user_access(cluster, complete, bucket_name, is_folder): @patch("controlpanel.api.tasks.handlers.base.BaseTaskHandler.complete") @patch("controlpanel.api.tasks.handlers.s3.cluster") def test_revoke_app_access(cluster, complete): - app_bucket_access = mommy.make("api.AppS3Bucket") + app_bucket_access = baker.make("api.AppS3Bucket") revoke_app_s3bucket_access( bucket_arn=app_bucket_access.s3bucket.arn, app_pk=app_bucket_access.app.pk, @@ -123,11 +123,11 @@ def test_revoke_app_access(cluster, complete): @patch("controlpanel.api.models.PolicyS3Bucket.revoke_bucket_access", new=MagicMock()) @patch("controlpanel.api.tasks.handlers.base.BaseTaskHandler.complete") def test_revoke_all_access(complete, users): - bucket = mommy.make("api.S3Bucket") - user_access = mommy.make("api.UserS3Bucket", s3bucket=bucket) - app_access = mommy.make("api.AppS3Bucket", s3bucket=bucket) - policy_access = mommy.make("api.PolicyS3Bucket", s3bucket=bucket) - task = mommy.make("api.Task", user_id=users["superuser"].pk) + bucket = baker.make("api.S3Bucket") + user_access = baker.make("api.UserS3Bucket", s3bucket=bucket) + app_access = baker.make("api.AppS3Bucket", s3bucket=bucket) + policy_access = baker.make("api.PolicyS3Bucket", s3bucket=bucket) + task = baker.make("api.Task", user_id=users["superuser"].pk) revoke_all_access_s3bucket(bucket.pk, task.user_id) diff --git a/tests/api/test_auth0.py b/tests/api/test_auth0.py index a1788dd04..a33fac265 100644 --- a/tests/api/test_auth0.py +++ b/tests/api/test_auth0.py @@ -1,9 +1,8 @@ # Standard library import json -from unittest.mock import call, patch +from unittest.mock import call, patch, ANY # Third-party -import mock import pytest from django.conf import settings from auth0 import exceptions @@ -511,9 +510,7 @@ def fixture_group_members_200(ExtendedAuth0): yield -def test_group_member_more_than_100( - ExtendedAuth0, fixture_group_members_200 -): +def test_group_member_more_than_100(ExtendedAuth0, fixture_group_members_200): members = ExtendedAuth0.groups.get_group_members(group_id="foo-id-1") assert len(members) == 200 @@ -585,7 +582,7 @@ def _clean_string(content): "client_secret": "WNXFkM3FCTXJhUWs0Q1NwcKFu", }, ) - fixture_connection_create.assert_called_once_with(mock.ANY) + fixture_connection_create.assert_called_once_with(ANY) with open("./tests/api/fixtures/nomis_body.json") as body_file: expected = json.loads(body_file.read()) @@ -606,7 +603,8 @@ def _clean_string(content): def test_create_custom_connection_with_allowed_error(ExtendedAuth0): with patch.object(ExtendedAuth0.connections, "create") as connection_create: connection_create.side_effect = exceptions.Auth0Error( - 409, 409, 'The connection name existed already') + 409, 409, "The connection name existed already" + ) ExtendedAuth0.connections.create_custom_connection( "auth0_nomis", input_values={ @@ -615,13 +613,12 @@ def test_create_custom_connection_with_allowed_error(ExtendedAuth0): "client_secret": "WNXFkM3FCTXJhUWs0Q1NwcKFu", }, ) - connection_create.assert_called_once_with(mock.ANY) + connection_create.assert_called_once_with(ANY) def test_create_custom_connection_with_notallowed_error(ExtendedAuth0): with patch.object(ExtendedAuth0.connections, "create") as connection_create: - connection_create.side_effect = exceptions.Auth0Error( - 400, 400, 'Error') + connection_create.side_effect = exceptions.Auth0Error(400, 400, "Error") with pytest.raises(auth0.Auth0Error, match="400: Error"): ExtendedAuth0.connections.create_custom_connection( "auth0_nomis", @@ -631,4 +628,4 @@ def test_create_custom_connection_with_notallowed_error(ExtendedAuth0): "client_secret": "WNXFkM3FCTXJhUWs0Q1NwcKFu", }, ) - connection_create.assert_called_once_with(mock.ANY) + connection_create.assert_called_once_with(ANY) diff --git a/tests/api/test_aws.py b/tests/api/test_aws.py index 3421c5ba4..15344fd4b 100644 --- a/tests/api/test_aws.py +++ b/tests/api/test_aws.py @@ -274,7 +274,11 @@ def test_create_bucket(logs_bucket, s3): def test_tag_bucket(s3): bucket_name = f"bucket-{id(MagicMock())}" bucket = s3.Bucket(bucket_name) - bucket.create() + bucket.create( + CreateBucketConfiguration={ + "LocationConstraint": settings.BUCKET_REGION, # noqa: F405 + } + ) aws.AWSBucket().tag_bucket(bucket_name, {"env": "test", "test-update": "old-value"}) aws.AWSBucket().tag_bucket( @@ -557,8 +561,8 @@ def test_create_policy(iam, settings): def assert_group_members(policy, role_names): attached_roles = list(policy.attached_roles.all()) assert len(attached_roles) == len(role_names) - for role, role_name in zip(attached_roles, role_names): - assert role.role_name == role_name + for role in attached_roles: + assert role.role_name in role_names @pytest.fixture @@ -1111,71 +1115,3 @@ def test_revoke_access_doesnt_remove_prefixes(s3_access_policy): assert s3_access_policy.statements[f"listSubFolders{bucket_hash}"].get("Resource", None) is not None assert s3_access_policy.statements[f"listFolder{bucket_hash}"]["Condition"]["StringEquals"]["s3:prefix"] != [""] # noqa assert s3_access_policy.statements[f"listSubFolders{bucket_hash}"]["Condition"]["StringLike"]["s3:prefix"] != [] # noqa - - -def test_attach_policy(iam, managed_policy, airflow_dev_policy, airflow_prod_policy, test_policy): - """ - Ensure EKS settngs are in the policy document when running on that - infrastructure. - """ - user = { - "auth0_id": "normal_user", - "user_name": "testing-bob", - "iam_role_name": "testing-bob", - } - - aws.AWSRole().create_role( - user["iam_role_name"], - User.aws_user_policy(user["auth0_id"], user["user_name"]), - User.ATTACH_POLICIES, - ) - - aws.AWSRole().attach_policy( - user["iam_role_name"], - ["a-test-policy"] - ) - role = iam.Role(user["iam_role_name"]) - - - attached_policies = list(role.attached_policies.all()) - assert len(attached_policies) == 4 - arns = [policy.arn for policy in attached_policies] - assert managed_policy["Arn"] in arns - assert airflow_dev_policy["Arn"] in arns - assert airflow_prod_policy["Arn"] in arns - assert test_policy["Arn"] in arns - - -def test_remove_policy(iam, managed_policy, airflow_dev_policy, airflow_prod_policy, test_policy): - """ - Ensure EKS settngs are in the policy document when running on that - infrastructure. - """ - user = { - "auth0_id": "normal_user", - "user_name": "testing-bob", - "iam_role_name": "testing-bob", - } - - policies = User.ATTACH_POLICIES + ["a-test-policy"] - - aws.AWSRole().create_role( - user["iam_role_name"], - User.aws_user_policy(user["auth0_id"], user["user_name"]), - policies, - ) - - aws.AWSRole().remove_policy( - user["iam_role_name"], - ["a-test-policy"] - ) - role = iam.Role(user["iam_role_name"]) - - - attached_policies = list(role.attached_policies.all()) - assert len(attached_policies) == 3 - arns = [policy.arn for policy in attached_policies] - assert managed_policy["Arn"] in arns - assert airflow_dev_policy["Arn"] in arns - assert airflow_prod_policy["Arn"] in arns - assert test_policy["Arn"] not in arns diff --git a/tests/api/test_pagination.py b/tests/api/test_pagination.py index 4bfd4721f..67d24813c 100644 --- a/tests/api/test_pagination.py +++ b/tests/api/test_pagination.py @@ -1,6 +1,6 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker from rest_framework.reverse import reverse DEFAULT_PAGE_SIZE = 100 @@ -23,7 +23,7 @@ def login_superuser(client, superuser): ) @pytest.mark.django_db def test_pagination(client, urlparams, page_size, next, prev, sqs): - mommy.make("api.App", DEFAULT_PAGE_SIZE + 20) + baker.make("api.App", DEFAULT_PAGE_SIZE + 20) app_list_url = reverse("app-list") response = client.get(app_list_url, urlparams) diff --git a/tests/api/views/test_app.py b/tests/api/views/test_app.py index 7aaa6da7b..008c7031f 100644 --- a/tests/api/views/test_app.py +++ b/tests/api/views/test_app.py @@ -3,7 +3,7 @@ # Third-party from botocore.exceptions import ClientError -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.exceptions import ValidationError from rest_framework.reverse import reverse @@ -16,7 +16,7 @@ @pytest.fixture # noqa: F405 def app(): - return mommy.make( + return baker.make( "api.App", repo_url="https://github.com/ministryofjustice/example.git", ) @@ -26,9 +26,9 @@ def app(): def models(app, users): with patch("controlpanel.api.aws.AWSRole.grant_bucket_access"), \ patch("controlpanel.api.aws.AWSBucket.create"): - mommy.make("api.App") - mommy.make("api.AppS3Bucket", app=app) - mommy.make("api.UserApp", app=app, user=users["superuser"]) + baker.make("api.App") + baker.make("api.AppS3Bucket", app=app) + baker.make("api.UserApp", app=app, user=users["superuser"]) def test_list(client): diff --git a/tests/api/views/test_apps3bucket.py b/tests/api/views/test_apps3bucket.py index fd52cd79a..90c89f5f9 100644 --- a/tests/api/views/test_apps3bucket.py +++ b/tests/api/views/test_apps3bucket.py @@ -5,7 +5,7 @@ # Third-party import pytest from django.conf import settings -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.reverse import reverse @@ -16,8 +16,8 @@ @pytest.fixture def apps(): return { - 1: mommy.make("api.App", name="app_1"), - 2: mommy.make("api.App", name="app_2"), + 1: baker.make("api.App", name="app_1"), + 2: baker.make("api.App", name="app_2"), } @@ -133,7 +133,7 @@ def test_update_bad_requests(client, apps, apps3buckets, buckets): def test_create_with_s3_data_warehouse_not_allowed(client, apps): with patch("controlpanel.api.aws.AWSRole.grant_bucket_access"), \ patch("controlpanel.api.aws.AWSBucket.create"): - s3_bucket_app = mommy.make( + s3_bucket_app = baker.make( "api.S3Bucket", is_data_warehouse=False, ) @@ -146,7 +146,7 @@ def test_create_with_s3_data_warehouse_not_allowed(client, apps): response = client.post(reverse("apps3bucket-list"), data) assert response.status_code == status.HTTP_201_CREATED - s3_bucket = mommy.make("api.S3Bucket", is_data_warehouse=True) + s3_bucket = baker.make("api.S3Bucket", is_data_warehouse=True) data = { "app": apps[1].id, diff --git a/tests/api/views/test_customer.py b/tests/api/views/test_customer.py index 044c025c1..2cbdf461a 100644 --- a/tests/api/views/test_customer.py +++ b/tests/api/views/test_customer.py @@ -6,7 +6,7 @@ import pytest from auth0.rest import Auth0Error from bs4 import BeautifulSoup -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.reverse import reverse @@ -16,7 +16,7 @@ @pytest.fixture def app(): - app = mommy.make("api.App") + app = baker.make("api.App") dev_auth_settings = dict( client_id="dev_client_id", group_id=str(uuid.uuid4()) diff --git a/tests/api/views/test_s3bucket.py b/tests/api/views/test_s3bucket.py index 00e3baa85..da9c27347 100644 --- a/tests/api/views/test_s3bucket.py +++ b/tests/api/views/test_s3bucket.py @@ -6,7 +6,7 @@ import pytest from botocore.exceptions import ClientError from django.conf import settings -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.reverse import reverse @@ -18,17 +18,17 @@ @pytest.fixture def bucket(): with patch("controlpanel.api.aws.AWSBucket.create"): - return mommy.make("api.S3Bucket", name="test-bucket-1") + return baker.make("api.S3Bucket", name="test-bucket-1") @pytest.fixture(autouse=True) def models(bucket): with patch("controlpanel.api.aws.AWSRole.grant_bucket_access"), \ patch("controlpanel.api.aws.AWSBucket.create"): - mommy.make("api.S3Bucket") - mommy.make("api.S3Bucket", is_data_warehouse=True) - mommy.make("api.AppS3Bucket", s3bucket=bucket) - mommy.make("api.UserS3Bucket", s3bucket=bucket) + baker.make("api.S3Bucket") + baker.make("api.S3Bucket", is_data_warehouse=True) + baker.make("api.AppS3Bucket", s3bucket=bucket) + baker.make("api.UserS3Bucket", s3bucket=bucket) def test_list(client): diff --git a/tests/api/views/test_user.py b/tests/api/views/test_user.py index 20f3d6760..7f66f2864 100644 --- a/tests/api/views/test_user.py +++ b/tests/api/views/test_user.py @@ -6,7 +6,7 @@ # Third-party import pytest from botocore.exceptions import ClientError -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.reverse import reverse @@ -17,8 +17,8 @@ @pytest.fixture(autouse=True) def models(users): with patch("controlpanel.api.aws.AWSBucket.create"): - mommy.make("api.UserS3Bucket", user=users["normal_user"]) - mommy.make("api.UserApp", user=users["normal_user"]) + baker.make("api.UserS3Bucket", user=users["normal_user"]) + baker.make("api.UserApp", user=users["normal_user"]) @pytest.fixture diff --git a/tests/api/views/test_userapp.py b/tests/api/views/test_userapp.py index 41ac9feda..5b9f5c97c 100644 --- a/tests/api/views/test_userapp.py +++ b/tests/api/views/test_userapp.py @@ -3,7 +3,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.reverse import reverse @@ -14,8 +14,8 @@ @pytest.fixture def apps(): return { - 1: mommy.make("api.App", name="app_1"), - 2: mommy.make("api.App", name="app_2"), + 1: baker.make("api.App", name="app_1"), + 2: baker.make("api.App", name="app_2"), } diff --git a/tests/conftest.py b/tests/conftest.py index bc3e1e5b7..15a3b88b4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,7 +5,7 @@ # Third-party import pytest import yaml -from model_mommy import mommy +from model_bakery import baker # First-party/Local from controlpanel.api import auth0 @@ -92,7 +92,7 @@ def slack_WebClient(): def superuser( db, slack_WebClient, iam, managed_policy, airflow_dev_policy, airflow_prod_policy ): - return mommy.make( + return baker.make( "api.User", auth0_id="github|user_1", is_superuser=True, @@ -104,13 +104,13 @@ def superuser( def users(db, superuser, iam, managed_policy, airflow_dev_policy, airflow_prod_policy): return { "superuser": superuser, - "normal_user": mommy.make( + "normal_user": baker.make( "api.User", auth0_id="github|user_2", username="bob", is_superuser=False, ), - "other_user": mommy.make( + "other_user": baker.make( "api.User", username="carol", auth0_id="github|user_3", diff --git a/tests/frontend/test_forms.py b/tests/frontend/test_forms.py index 8807b023c..cd8310237 100644 --- a/tests/frontend/test_forms.py +++ b/tests/frontend/test_forms.py @@ -5,7 +5,6 @@ import pytest from django.core.exceptions import ValidationError from django.urls import reverse -from mock import MagicMock # First-party/Local from controlpanel.api import aws @@ -469,7 +468,7 @@ def test_ip_allowlist_form_missing_name(): @pytest.mark.parametrize("env", ["dev", "prod"]) @mock.patch( "controlpanel.frontend.forms.CreateAppForm.get_datasource_queryset", - new=MagicMock, + new=mock.MagicMock, ) def test_clean_namespace(env): form = forms.CreateAppForm() diff --git a/tests/frontend/views/test_app.py b/tests/frontend/views/test_app.py index d25ed94ff..2e7b698c0 100644 --- a/tests/frontend/views/test_app.py +++ b/tests/frontend/views/test_app.py @@ -8,7 +8,7 @@ from bs4 import BeautifulSoup from django.contrib.messages import get_messages from django.urls import reverse -from model_mommy import mommy +from model_bakery import baker from rest_framework import status # First-party/Local @@ -43,7 +43,7 @@ def github_api_token(): def users(users): users.update( { - "app_admin": mommy.make("api.User", username="app_admin"), + "app_admin": baker.make("api.User", username="app_admin"), } ) return users @@ -52,10 +52,10 @@ def users(users): @pytest.fixture def app(users): ip_allowlists = [ - mommy.make("api.IPAllowlist", allowed_ip_ranges="xyz"), + baker.make("api.IPAllowlist", allowed_ip_ranges="xyz"), ] - mommy.make("api.App", NUM_APPS - 1) - app = mommy.make("api.App") + baker.make("api.App", NUM_APPS - 1) + app = baker.make("api.App") app.repo_url = "https://github.com/github_org/testing_repo" dev_auth_settings = dict( client_id="dev_client_id", @@ -74,7 +74,7 @@ def app(users): } app.save() AppIPAllowList.objects.update_records(app, "dev_env", ip_allowlists) - mommy.make("api.UserApp", user=users["app_admin"], app=app, is_admin=True) + baker.make("api.UserApp", user=users["app_admin"], app=app, is_admin=True) return app @@ -180,8 +180,8 @@ def repos_for_redundant_auth(githubapi): def s3buckets(app, users): with patch("controlpanel.api.aws.AWSBucket.create") as _: buckets = { - "not_connected": mommy.make("api.S3Bucket", created_by=users["app_admin"]), - "connected": mommy.make("api.S3Bucket", created_by=users["app_admin"]), + "not_connected": baker.make("api.S3Bucket", created_by=users["app_admin"]), + "connected": baker.make("api.S3Bucket", created_by=users["app_admin"]), } return buckets @@ -189,7 +189,7 @@ def s3buckets(app, users): @pytest.fixture def apps3bucket(app, s3buckets): with patch("controlpanel.api.aws.AWSRole.grant_bucket_access"): - return mommy.make("api.AppS3Bucket", app=app, s3bucket=s3buckets["connected"]) + return baker.make("api.AppS3Bucket", app=app, s3bucket=s3buckets["connected"]) def list_apps(client, *args): @@ -528,7 +528,7 @@ def test_app_detail_display_all_envs(client, app, users, repos): def test_app_detail_with_missing_auth_client(client, users, repos_for_missing_auth): - app = mommy.make("api.App") + app = baker.make("api.App") app.repo_url = "https://github.com/github_org/testing_repo_with_auth" app.save() @@ -549,7 +549,7 @@ def test_app_detail_with_auth_client_redundant(client, users, app, repos_for_red @pytest.fixture def app_being_migrated(users): - app = mommy.make("api.App") + app = baker.make("api.App") app.repo_url = "https://github.com/new_github_org/testing_repo" app_old_repo_url = "https://github.com/github_org/testing_repo" migration_json = dict( @@ -563,7 +563,7 @@ def app_being_migrated(users): ) app.description = json.dumps(app_info) app.save() - mommy.make("api.UserApp", user=users["app_admin"], app=app, is_admin=True) + baker.make("api.UserApp", user=users["app_admin"], app=app, is_admin=True) return app diff --git a/tests/frontend/views/test_app_variables.py b/tests/frontend/views/test_app_variables.py index b1f7ac339..cff459549 100644 --- a/tests/frontend/views/test_app_variables.py +++ b/tests/frontend/views/test_app_variables.py @@ -4,7 +4,7 @@ # Third-party import pytest from django.urls import reverse -from model_mommy import mommy +from model_bakery import baker from rest_framework import status NUM_APPS = 3 @@ -14,7 +14,7 @@ def users(users): users.update( { - "owner": mommy.make("api.User", username="owner"), + "owner": baker.make("api.User", username="owner"), } ) return users @@ -22,9 +22,9 @@ def users(users): @pytest.fixture(autouse=True) def app(users): - mommy.make("api.App", NUM_APPS - 1) - app = mommy.make("api.App") - mommy.make("api.UserApp", user=users["owner"], app=app, is_admin=True) + baker.make("api.App", NUM_APPS - 1) + app = baker.make("api.App") + baker.make("api.UserApp", user=users["owner"], app=app, is_admin=True) return app @@ -70,19 +70,22 @@ def fixture_delete_var(): @pytest.mark.parametrize( - "view,user,expected_status,data_input", + "view,user,expected_status,data_input,create_call_count,delete_call_count,expected_data", [ ( create, "superuser", status.HTTP_302_FOUND, dict(key="environment", value="prod", env_name="test_env"), + 1, + 0, + dict(env_name="test_env", key_name="XXX_environment", key_value="prod"), ), - (create, "owner", status.HTTP_200_OK, {}), - (create, "normal_user", status.HTTP_200_OK, {}), - (delete, "superuser", status.HTTP_302_FOUND, dict(key="hello")), - (delete, "owner", status.HTTP_302_FOUND, dict(key="hello")), - (delete, "normal_user", status.HTTP_403_FORBIDDEN, dict(key="hello")), + (create, "owner", status.HTTP_200_OK, {}, 0, 0, None), + (create, "normal_user", status.HTTP_200_OK, {}, 0, 0, None), + (delete, "superuser", status.HTTP_302_FOUND, dict(key="hello"), 0, 1, None), + (delete, "owner", status.HTTP_302_FOUND, dict(key="hello"), 0, 1, None), + (delete, "normal_user", status.HTTP_403_FORBIDDEN, dict(key="hello"), 0, 0, None), ], ) def test_permission( @@ -95,11 +98,14 @@ def test_permission( data_input, fixture_create_update_var, fixture_delete_var, + create_call_count, + delete_call_count, + expected_data ): client.force_login(users[user]) response = view(client, app, data_input) assert response.status_code == expected_status - assert fixture_create_update_var.called_once() - assert fixture_delete_var.called_once() - if data_input: - assert fixture_create_update_var.called_once_with(data_input) + assert fixture_create_update_var.call_count == create_call_count + assert fixture_delete_var.call_count == delete_call_count + if data_input and create_call_count == 1: + fixture_create_update_var.assert_called_once_with(**expected_data) diff --git a/tests/frontend/views/test_auth.py b/tests/frontend/views/test_auth.py index ce7e60cac..4d598d5d8 100644 --- a/tests/frontend/views/test_auth.py +++ b/tests/frontend/views/test_auth.py @@ -2,7 +2,7 @@ import pytest from authlib.integrations.base_client import OAuthError from django.urls import reverse, reverse_lazy -from mock import patch +from unittest.mock import patch from pytest_django.asserts import assertContains diff --git a/tests/frontend/views/test_datasource.py b/tests/frontend/views/test_datasource.py index 2f4a892ac..d1733f70e 100644 --- a/tests/frontend/views/test_datasource.py +++ b/tests/frontend/views/test_datasource.py @@ -5,7 +5,7 @@ import pytest from django.conf import settings from django.urls import reverse, reverse_lazy -from model_mommy import mommy +from model_bakery import baker from rest_framework import status # First-party/Local @@ -23,8 +23,8 @@ def enable_db_for_all_tests(db): def users(users): users.update( { - "bucket_viewer": mommy.make("api.User", username="bucket_viewer"), - "bucket_admin": mommy.make("api.User", username="bucket_admin"), + "bucket_viewer": baker.make("api.User", username="bucket_viewer"), + "bucket_admin": baker.make("api.User", username="bucket_admin"), } ) return users @@ -34,53 +34,53 @@ def users(users): def buckets(db): with patch("controlpanel.api.aws.AWSBucket.create"): return { - "app_data1": mommy.make("api.S3Bucket", is_data_warehouse=False), - "app_data2": mommy.make("api.S3Bucket", is_data_warehouse=False), - "warehouse1": mommy.make("api.S3Bucket", is_data_warehouse=True), - "warehouse2": mommy.make("api.S3Bucket", is_data_warehouse=True), - "other": mommy.make("api.S3Bucket"), + "app_data1": baker.make("api.S3Bucket", is_data_warehouse=False), + "app_data2": baker.make("api.S3Bucket", is_data_warehouse=False), + "warehouse1": baker.make("api.S3Bucket", is_data_warehouse=True), + "warehouse2": baker.make("api.S3Bucket", is_data_warehouse=True), + "other": baker.make("api.S3Bucket"), } @pytest.fixture(autouse=True) def users3buckets(buckets, users): return { - "app_data_admin": mommy.make( + "app_data_admin": baker.make( "api.UserS3Bucket", user=users["bucket_admin"], s3bucket=buckets["app_data1"], access_level=UserS3Bucket.READWRITE, is_admin=True, ), - "app_data_admin2": mommy.make( + "app_data_admin2": baker.make( "api.UserS3Bucket", user=users["bucket_admin"], s3bucket=buckets["app_data2"], access_level=UserS3Bucket.READWRITE, is_admin=True, ), - "app_data_readonly": mommy.make( + "app_data_readonly": baker.make( "api.UserS3Bucket", user=users["bucket_viewer"], s3bucket=buckets["app_data2"], access_level=UserS3Bucket.READONLY, is_admin=False, ), - "warehouse_admin": mommy.make( + "warehouse_admin": baker.make( "api.UserS3Bucket", s3bucket=buckets["warehouse1"], user=users["bucket_admin"], access_level=UserS3Bucket.READWRITE, is_admin=True, ), - "warehouse_admin2": mommy.make( + "warehouse_admin2": baker.make( "api.UserS3Bucket", s3bucket=buckets["warehouse2"], user=users["bucket_admin"], access_level=UserS3Bucket.READWRITE, is_admin=True, ), - "warehouse_readonly": mommy.make( + "warehouse_readonly": baker.make( "api.UserS3Bucket", s3bucket=buckets["warehouse1"], user=users["bucket_viewer"], diff --git a/tests/frontend/views/test_index.py b/tests/frontend/views/test_index.py index eae802835..382c4be0b 100644 --- a/tests/frontend/views/test_index.py +++ b/tests/frontend/views/test_index.py @@ -2,7 +2,7 @@ import pytest from django.http import HttpResponse from django.urls import reverse -from mock import MagicMock, patch +from unittest.mock import MagicMock, patch class TestAccess: diff --git a/tests/frontend/views/test_ip_allowlist.py b/tests/frontend/views/test_ip_allowlist.py index 3f1f1a57d..ffe957d31 100644 --- a/tests/frontend/views/test_ip_allowlist.py +++ b/tests/frontend/views/test_ip_allowlist.py @@ -1,13 +1,13 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker from rest_framework import status from rest_framework.reverse import reverse @pytest.fixture(autouse=True) def ip_allowlist(): - allowlist = mommy.make( + allowlist = baker.make( "api.IPAllowlist", name="Test IP allowlist 1", allowed_ip_ranges="192.168.0.0/28", diff --git a/tests/frontend/views/test_secrets.py b/tests/frontend/views/test_secrets.py index baa970018..2bffc027a 100644 --- a/tests/frontend/views/test_secrets.py +++ b/tests/frontend/views/test_secrets.py @@ -3,7 +3,7 @@ # Third-party from django.urls import reverse -from model_mommy import mommy +from model_bakery import baker # First-party/Local from tests.api.fixtures.aws import * @@ -34,7 +34,7 @@ def github_api_token(): def users(users): users.update( { - "app_admin": mommy.make("api.User", username="app_admin"), + "app_admin": baker.make("api.User", username="app_admin"), } ) return users @@ -42,9 +42,9 @@ def users(users): @pytest.fixture(autouse=True) # noqa: F405 def app(users): - mommy.make("api.App", NUM_APPS - 1) - app = mommy.make("api.App") - mommy.make("api.UserApp", user=users["app_admin"], app=app, is_admin=True) + baker.make("api.App", NUM_APPS - 1) + app = baker.make("api.App") + baker.make("api.UserApp", user=users["app_admin"], app=app, is_admin=True) return app @@ -73,15 +73,15 @@ def repos(githubapi): def s3buckets(app): with patch("controlpanel.api.aws.AWSBucket.create"): buckets = { - "not_connected": mommy.make("api.S3Bucket"), - "connected": mommy.make("api.S3Bucket"), + "not_connected": baker.make("api.S3Bucket"), + "connected": baker.make("api.S3Bucket"), } return buckets @pytest.fixture # noqa: F405 def apps3bucket(app, s3buckets): - return mommy.make("api.AppS3Bucket", app=app, s3bucket=s3buckets["connected"]) + return baker.make("api.AppS3Bucket", app=app, s3bucket=s3buckets["connected"]) @pytest.fixture @@ -128,18 +128,19 @@ def test_add_secret_permissions( @pytest.mark.parametrize( # noqa: F405 - "user, key, data, expected_status", - [["superuser", "testing", {"env_name": "testing"}, 302], - ["app_admin", "testing", {}, 302], - ["normal_user", "testing", {}, 403]], + "user, key, data, expected_status, expected_calls", + [["superuser", "testing", {"env_name": "testing"}, 302, 1], + ["app_admin", "testing", {}, 302, 1], + ["normal_user", "testing", {}, 403, 0]], ) def test_delete_secret( - client, app, users, fixture_delete_secret, user, key, data, expected_status + client, app, users, fixture_delete_secret, user, key, data, expected_status, + expected_calls ): client.force_login(users[user]) response = delete_secret_post(client, app, key, data) assert response.status_code == expected_status - assert fixture_delete_secret.called_once() + assert fixture_delete_secret.call_count == expected_calls def test_add_secret(fixture_create_update_secret, diff --git a/tests/frontend/views/test_user.py b/tests/frontend/views/test_user.py index d7b4252e5..6042f7c6a 100644 --- a/tests/frontend/views/test_user.py +++ b/tests/frontend/views/test_user.py @@ -4,7 +4,6 @@ # Third-party import pytest from django.urls import reverse -from model_mommy import mommy from rest_framework import status diff --git a/tests/kubeapi/test_permissions.py b/tests/kubeapi/test_permissions.py index 0a7188f9a..6f8cdedb1 100644 --- a/tests/kubeapi/test_permissions.py +++ b/tests/kubeapi/test_permissions.py @@ -3,7 +3,7 @@ # Third-party import pytest -from model_mommy import mommy +from model_bakery import baker from rest_framework import status @@ -32,13 +32,13 @@ def k8s_api(): @pytest.fixture def users(): return { - "superuser": mommy.make( + "superuser": baker.make( "api.User", auth0_id="github|0", is_superuser=True, username="alice", ), - "normal_user": mommy.make( + "normal_user": baker.make( "api.User", username="bob", auth0_id="github|1",