Skip to content

Commit

Permalink
updated to model-bakery
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesstottmoj committed Apr 5, 2024
1 parent a3a6f5a commit d404b0b
Show file tree
Hide file tree
Showing 37 changed files with 177 additions and 179 deletions.
1 change: 1 addition & 0 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ flake8==7.0.0
ipdb==0.13.13
ipython==8.21.0
isort==5.13.2
model-bakery==1.17.0
pandas==2.2.0
pre-commit==3.6.1
pylint==3.0.3
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ gunicorn==21.2.0
Jinja2==3.1.3
kubernetes==25.3.0
MarkupSafe==2.1.5
model-mommy==2.0.0
moto==5.0.1
mozilla-django-oidc==4.0.0
psycopg2-binary==2.9.9
Expand Down
8 changes: 4 additions & 4 deletions tests/api/cluster/test_access_to_s3buckets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]),
}

Expand Down
4 changes: 2 additions & 2 deletions tests/api/cluster/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions tests/api/cluster/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
14 changes: 7 additions & 7 deletions tests/api/filters/test_app_filter.py
Original file line number Diff line number Diff line change
@@ -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]:
Expand Down
4 changes: 2 additions & 2 deletions tests/api/filters/test_apps3bucket_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions tests/api/filters/test_s3bucket_filter.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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],
Expand Down
24 changes: 12 additions & 12 deletions tests/api/filters/test_users3bucket_filter.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions tests/api/models/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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

Expand All @@ -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("[email protected]", group_id="123")

Expand All @@ -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 [email protected] cannot be found in this application group"
Expand All @@ -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("[email protected]", group_id="123")
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tests/api/models/test_apps3bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
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


@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
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions tests/api/models/test_ip_allowlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Third-party
import pytest
from model_mommy import mommy
from model_bakery import baker


@pytest.fixture(autouse=True)
Expand All @@ -20,8 +20,8 @@ def 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 = baker.make("api.IPAllowlist", allowed_ip_ranges="123")
# app = baker.make("api.App", ip_allowlists=[ip_allowlist]) # noqa:F841
#
# ip_allowlist.save()
#
Expand All @@ -35,7 +35,7 @@ def update_aws_secrets_manager():
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()

Expand All @@ -44,10 +44,10 @@ def test_ip_allowlist_without_app_save_does_not_update_aws_secrets_manager(

# 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"),
# baker.make("api.IPAllowlist", allowed_ip_ranges="xyz"),
# baker.make("api.IPAllowlist", allowed_ip_ranges="123"),
# ]
# apps = mommy.make("api.App", ip_allowlists=ip_allowlists, _quantity=2) # noqa:F841
# apps = baker.make("api.App", ip_allowlists=ip_allowlists, _quantity=2) # noqa:F841
#
# ip_allowlists[0].delete()
#
Expand All @@ -59,7 +59,7 @@ def test_ip_allowlist_without_app_save_does_not_update_aws_secrets_manager(
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()

Expand Down
6 changes: 3 additions & 3 deletions tests/api/models/test_s3bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/api/models/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
# 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


@pytest.fixture
def tool(db):
return mommy.make("api.Tool")
return baker.make("api.Tool")


def test_deploy_for_generic(helm, tool, users):
Expand Down
Loading

0 comments on commit d404b0b

Please sign in to comment.