Skip to content

Commit

Permalink
test: multitenancy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Muchogoc committed Jan 5, 2023
1 parent eca1607 commit c323cf2
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 68 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ jobs:

steps:
- name: Checkout Code Repository
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Set up Python 3.10
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.10.0
python-version: '3.10'

# Run all pre-commit hooks on all the files.
# Getting only staged files can be tricky in case a new PR is opened
# since the action is run on a branch in detached head state
- name: Install and Run Pre-commit
uses: pre-commit/action@v2.0.3
uses: pre-commit/action@v3.0.0

- name: Install requirements
run: |
Expand Down Expand Up @@ -87,7 +87,12 @@ jobs:

steps:
- name: Checkout Code Repository
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Decrypt GCS service account
run: ./.github/scripts/decrypt_secret.sh
Expand Down
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ fail_fast: true

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml

- repo: https://github.com/psf/black
rev: 22.3.0
rev: 22.12.0
hooks:
- id: black
args: ["-l", "99"]

- repo: https://github.com/timothycrosley/isort
rev: 5.9.1
rev: 5.11.4
hooks:
- id: isort
args: ["--profile", "black"]

- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
args: ["--config=setup.cfg"]
Expand Down
4 changes: 2 additions & 2 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@

# ADMIN
# ------------------------------------------------------------------------------
ADMIN_URL = "admin/"
ADMIN_URL = env("DJANGO_ADMIN_URL", default="admin/")
ADMINS = [
(
"Savannah Informatics Global Health Institute",
Expand Down Expand Up @@ -401,7 +401,7 @@
TAGGIT_CASE_INSENSITIVE = True
WAGTAILMEDIA = {
"MEDIA_MODEL": "content.CustomMedia",
"MEDIA_FORM_BASE": "mycarehub.content.forms.CustomBaseMediaForm",
"MEDIA_FORM_BASE": "mycarehub.content.filters.CustomBaseMediaForm",
"AUDIO_EXTENSIONS": ["aac", "wav"],
"VIDEO_EXTENSIONS": ["mp4"],
}
Expand Down
4 changes: 0 additions & 4 deletions config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@
)
]

# ADMIN
# ------------------------------------------------------------------------------
# Django Admin URL regex.
ADMIN_URL = env("DJANGO_ADMIN_URL", default="admin/")

# Anymail
# ------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions config/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def gen_func():
WHITENOISE_MANIFEST_STRICT = False
GCS_BUCKET_ALLOWED_ORIGINS = env.list("GCS_BUCKET_ALLOWED_ORIGINS")

# Admin
ADMIN_URL = "test-admin/"


def require_env(name: str) -> str:
value = env(name)
Expand Down
9 changes: 8 additions & 1 deletion mycarehub/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def request_with_user(rf, django_user_model, user_with_all_permissions):


@pytest.fixture
def content_item_with_tag_and_category(request_with_user):
def content_item_index(request_with_user):
# get the root page
site = Site.find_for_request(request_with_user)
assert site is not None
Expand All @@ -90,6 +90,13 @@ def content_item_with_tag_and_category(request_with_user):
home.add_child(instance=content_item_index)
home.save_revision().publish()

content_item_index.save_revision().publish()

return content_item_index


@pytest.fixture
def content_item_with_tag_and_category(content_item_index):
# get a hero image
hero = baker.make("wagtailimages.Image", _create_files=True)

Expand Down
22 changes: 22 additions & 0 deletions mycarehub/content/filters.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import django_filters
from django.core.files.storage import get_storage_class
from django.db.models import Count, Q
from rest_framework.filters import BaseFilterBackend
from wagtail.admin.filters import WagtailFilterSet
from wagtailmedia.forms import BaseMediaForm

from mycarehub.common.filters.base_filters import CommonFieldsFilterset
from mycarehub.content.models import ContentItem
from mycarehub.utils.signed_url import generate_media_name

from .models import (
Author,
Expand Down Expand Up @@ -99,6 +102,7 @@ class Meta:
class ContentItemCategoryFilterSet(WagtailFilterSet):
def filter_queryset(self, *args, **kwargs):
return self.queryset.filter(organisation=self.request.user.organisation)

class Meta:
model = ContentItemCategory
fields = ["name"]
Expand All @@ -107,6 +111,24 @@ class Meta:
class AuthorFilterSet(WagtailFilterSet):
def filter_queryset(self, *args, **kwargs):
return self.queryset.filter(organisation=self.request.user.organisation)

class Meta:
model = Author
fields = ["name"]


class CustomBaseMediaForm(BaseMediaForm):
def save(self, commit=True): # pragma: no cover
instance = super().save(commit=False)

# Checks the storage class being used
# Google Cloud Storage should save only the file name
# because the upload is already done using a signed url
if get_storage_class().__name__ == "MediaRootGoogleCloudStorage": # pragma: no cover
temp_file = instance.file
instance.file = generate_media_name(temp_file.name)

if commit:
instance.save()

return instance
21 changes: 0 additions & 21 deletions mycarehub/content/forms.py

This file was deleted.

34 changes: 18 additions & 16 deletions mycarehub/content/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
ContentItemGalleryImage,
ContentItemIndexPage,
ContentItemMediaLink,
ContentItemPageForm,
ContentItemQuestionnaire,
ContentItemTag,
ContentItemTagIndexPage,
Expand All @@ -14,20 +15,21 @@
from .snippets import Author, ContentItemCategory

__all__ = [
MediaSerializedField,
Author,
ContentItemCategory,
ContentItemTag,
ContentItemTagIndexPage,
ContentItemIndexPage,
ContentItem,
ContentItemDocumentLink,
ContentItemMediaLink,
ContentItemGalleryImage,
ContentLike,
ContentBookmark,
ContentShare,
ContentView,
ContentItemQuestionnaire,
CustomMedia,
"MediaSerializedField",
"Author",
"ContentItemCategory",
"ContentItemTag",
"ContentItemTagIndexPage",
"ContentItemIndexPage",
"ContentItem",
"ContentItemDocumentLink",
"ContentItemMediaLink",
"ContentItemGalleryImage",
"ContentLike",
"ContentBookmark",
"ContentShare",
"ContentView",
"ContentItemQuestionnaire",
"CustomMedia",
"ContentItemPageForm",
]
2 changes: 1 addition & 1 deletion mycarehub/content/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def get_context(self, request):
class ContentItemPageForm(WagtailAdminPageForm):
def __init__(
self, data=None, files=None, parent_page=None, subscription=None, *args, **kwargs
):
): # pragma: no cover
super().__init__(data, files, parent_page, subscription, *args, **kwargs)

self.fields["categories"].queryset = self.fields["categories"].queryset.filter(
Expand Down
2 changes: 1 addition & 1 deletion mycarehub/content/models/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ContentItemCategory(index.Indexed, models.Model):
FieldPanel("icon"),
]

search_fields = [index.SearchField("name"), index.FilterField('organisation_id')]
search_fields = [index.SearchField("name"), index.FilterField("organisation_id")]

def __str__(self):
return self.name
Expand Down
68 changes: 67 additions & 1 deletion mycarehub/content/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import pytest
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import override_settings
from django.urls import reverse
from model_bakery import baker
from rest_framework import status

from ..models import ContentBookmark, ContentLike, ContentShare, ContentView
from ..models import (
Author,
ContentBookmark,
ContentItemCategory,
ContentLike,
ContentShare,
ContentView,
)

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -252,3 +260,61 @@ def test_content_bookmark_create_view(
response_data = response.json()
print(response_data)
assert response_data["id"] != ""


def test_author_snippet_list(user_with_all_permissions, client):
client.force_login(user_with_all_permissions)
url = reverse("wagtailsnippets_content_author:list")

baker.make(Author, organisation=user_with_all_permissions.organisation)

response = client.get(url)

print(response.content.decode())
assert response.status_code == status.HTTP_200_OK


def test_content_item_category_snippet_list(user_with_all_permissions, client):
client.force_login(user_with_all_permissions)
url = reverse("wagtailsnippets_content_contentitemcategory:list")

baker.make(ContentItemCategory, organisation=user_with_all_permissions.organisation)

response = client.get(url)

print(response.content)
assert response.status_code == status.HTTP_200_OK


def test_author_chooser_list(user_with_all_permissions, client):
client.force_login(user_with_all_permissions)
url = reverse("wagtailadmin_home")
url = url + "author_chooser/"

baker.make(Author, organisation=user_with_all_permissions.organisation)

response = client.get(url)

print(response.content)
assert response.status_code == status.HTTP_200_OK


def test_add_media(user_with_all_permissions, client):
client.force_login(user_with_all_permissions)
url = reverse("wagtailmedia:add", kwargs={"media_type": "video"})

video = SimpleUploadedFile("file.mp4", b"file_content", content_type="video/mp4")

response = client.post(
url,
data={
"title": "Test Video",
},
files={"file": video},
content_type="application/json",
accept="application/json",
follow=True,
)

print(response.content)
assert response.status_code == status.HTTP_200_OK

0 comments on commit c323cf2

Please sign in to comment.