Skip to content

Commit

Permalink
Merge f1532d5 into 4720ace
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarzkrieger committed Jun 2, 2021
2 parents 4720ace + f1532d5 commit d5b930e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 118 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/testing.yml
Expand Up @@ -35,8 +35,8 @@ jobs:
coverage report -m
bash <(curl -s https://codecov.io/bash)
test_check_unapplied_migrations_middleware:
name: middleware for unapplied migrations
check_for_unapplied_migrations:
name: check for unapplied migrations
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -59,8 +59,8 @@ jobs:
- name: Run test
run: |
export LANG=en-us
export TEST_CHECK_UNAPPLIED_MIGRATIONS_MIDDLEWARE=1
coverage run --source='.' ./manage.py test -v2 --noinput --settings=tcms.settings.test tcms.core.tests.test_middleware.TestCheckUnappliedMigrationsMiddleware
export TEST_DASHBOARD_CHECK_UNAPPLIED_MIGRATIONS=1
coverage run --source='.' ./manage.py test -v2 --noinput --settings=tcms.settings.test tcms.core.tests.test_views.TestDashboardCheckMigrations
- name: Send coverage to codecov.io
run: |
Expand Down
57 changes: 0 additions & 57 deletions tcms/core/middleware.py
@@ -1,16 +1,11 @@
# pylint: disable=no-self-use, too-few-public-methods

from django.conf import settings
from django.contrib import messages
from django.contrib.sites.models import Site
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.migrations.executor import MigrationExecutor
from django.db.utils import OperationalError, ProgrammingError
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils.deprecation import MiddlewareMixin
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _


class CheckDBStructureExistsMiddleware(MiddlewareMixin):
Expand All @@ -23,55 +18,3 @@ def process_request(self, request):
# Redirect to Setup view
return HttpResponseRedirect(reverse("init-db"))
return None


class CheckSettingsMiddleware(MiddlewareMixin):
def process_request(self, request):
doc_url = "https://kiwitcms.readthedocs.io/en/latest/admin.html#configure-kiwi-s-base-url"
if request.path == "/init-db/":
return
site = Site.objects.get(pk=settings.SITE_ID)

if site.domain == "127.0.0.1:8000":
messages.add_message(
request,
messages.ERROR,
mark_safe( # nosec:B308:B703
_(
"Base URL is not configured! "
'See <a href="%(doc_url)s">documentation</a> and '
'<a href="%(admin_url)s">change it</a>'
)
% {
"doc_url": doc_url,
"admin_url": reverse("admin:sites_site_change", args=[site.pk]),
}
),
)


class CheckUnappliedMigrationsMiddleware(MiddlewareMixin):
def process_request(self, request):
if request.path == "/init-db/":
return
doc_url = (
"https://kiwitcms.readthedocs.io/en/latest/"
"installing_docker.html#initial-configuration-of-running-container"
)
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
plan = executor.migration_plan(executor.loader.graph.leaf_nodes())
if plan:
messages.add_message(
request,
messages.ERROR,
mark_safe( # nosec:B308:B703
_(
"You have %(unapplied_migration_count)s unapplied migration(s). "
'See <a href="%(doc_url)s">documentation</a>'
)
% {
"unapplied_migration_count": len(plan),
"doc_url": doc_url,
}
),
)
51 changes: 0 additions & 51 deletions tcms/core/tests/test_middleware.py

This file was deleted.

60 changes: 57 additions & 3 deletions tcms/core/tests/test_views.py
@@ -1,23 +1,42 @@
# -*- coding: utf-8 -*-
# pylint: disable=too-many-ancestors
import os
import unittest
from http import HTTPStatus

from django import test
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.management import call_command
from django.urls import include, path, reverse
from django.utils.translation import gettext_lazy as _

from tcms import urls
from tcms.tests import BaseCaseRun
from tcms.tests.factories import TestExecutionFactory, TestPlanFactory, TestRunFactory
from tcms.tests import LoggedInTestCase
from tcms.tests.factories import (
TestExecutionFactory,
TestPlanFactory,
TestRunFactory,
UserFactory,
)


class TestDashboard(BaseCaseRun):
class TestDashboard(LoggedInTestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()
# used to reproduce Sentry #KIWI-TCMS-38 where rendering fails
# with that particular value
cls.chinese_tp = TestPlanFactory(name="缺货反馈测试需求", author=cls.tester)
doc_url = "https://kiwitcms.readthedocs.io/en/latest/admin.html#configure-kiwi-s-base-url"
cls.base_url_error_message = _(
"Base URL is not configured! "
'See <a href="%(doc_url)s">documentation</a> and '
'<a href="%(admin_url)s">change it</a>'
) % {
"doc_url": doc_url,
"admin_url": reverse("admin:sites_site_change", args=[settings.SITE_ID]),
}

def test_when_not_logged_in_redirects_to_login(self):
self.client.logout()
Expand Down Expand Up @@ -53,6 +72,41 @@ def test_dashboard_shows_testruns_for_execution_assignee(self):
response = self.client.get(reverse("core-views-index"))
self.assertContains(response, execution.run.summary)

def test_check_base_url_not_configured(self):
response = self.client.get("/", follow=True)
self.assertContains(response, self.base_url_error_message)

def test_check_base_url_configured(self):
site = Site.objects.create(domain="example.com", name="example")
with test.override_settings(SITE_ID=site.pk):
response = self.client.get("/", follow=True)
self.assertNotContains(response, self.base_url_error_message)


@unittest.skipUnless(
os.getenv("TEST_DASHBOARD_CHECK_UNAPPLIED_MIGRATIONS"),
"Check for missing migrations testing is not enabled",
)
class TestDashboardCheckMigrations(test.TransactionTestCase):
unapplied_migration_message = _(
"unapplied migration(s). See "
'<a href="https://kiwitcms.readthedocs.io/en/latest/'
"installing_docker.html#initial-configuration-of-running-"
'container">documentation</a>'
)

def test_check_unapplied_migrations(self):
call_command("migrate", "bugs", "zero", verbosity=2, interactive=False)
tester = UserFactory()
tester.set_password("password")
tester.save()
self.client.login( # nosec:B106:hardcoded_password_funcarg
username=tester.username,
password="password",
)
response = self.client.get("/", follow=True)
self.assertContains(response, self.unapplied_migration_message)


def exception_view(request):
raise Exception
Expand Down
51 changes: 50 additions & 1 deletion tcms/core/views.py
Expand Up @@ -5,13 +5,19 @@

from django import http
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import Site
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.migrations.executor import MigrationExecutor
from django.db.models import Count, Q
from django.http import HttpResponseRedirect, StreamingHttpResponse
from django.template import loader
from django.urls import reverse
from django.utils import timezone, translation
from django.utils.decorators import method_decorator
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from django.utils.translation import trans_real
from django.views import i18n
from django.views.decorators.csrf import requires_csrf_token
Expand All @@ -29,7 +35,50 @@ class DashboardView(TemplateView):
template_name = "dashboard.html"

def get_context_data(self, **kwargs):
"""List all recent TestPlans and TestRuns"""
# Check if domain is configured
site = Site.objects.get(pk=settings.SITE_ID)
doc_url = "https://kiwitcms.readthedocs.io/en/latest/admin.html#configure-kiwi-s-base-url"
if site.domain == "127.0.0.1:8000":
messages.add_message(
self.request,
messages.ERROR,
mark_safe( # nosec:B308:B703
_(
"Base URL is not configured! "
'See <a href="%(doc_url)s">documentation</a> and '
'<a href="%(admin_url)s">change it</a>'
)
% {
"doc_url": doc_url,
"admin_url": reverse("admin:sites_site_change", args=[site.pk]),
}
),
)

# Check for missing migrations
doc_url = (
"https://kiwitcms.readthedocs.io/en/latest/"
"installing_docker.html#initial-configuration-of-running-container"
)
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
plan = executor.migration_plan(executor.loader.graph.leaf_nodes())
if plan:
messages.add_message(
self.request,
messages.ERROR,
mark_safe( # nosec:B308:B703
_(
"You have %(unapplied_migration_count)s unapplied migration(s). "
'See <a href="%(doc_url)s">documentation</a>'
)
% {
"unapplied_migration_count": len(plan),
"doc_url": doc_url,
}
),
)

# List all recent TestPlans and TestRuns
test_plans = (
TestPlan.objects.filter(author=self.request.user)
.order_by("-pk")
Expand Down
2 changes: 0 additions & 2 deletions tcms/settings/common.py
Expand Up @@ -141,8 +141,6 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"simple_history.middleware.HistoryRequestMiddleware",
"tcms.core.middleware.CheckSettingsMiddleware",
"tcms.core.middleware.CheckUnappliedMigrationsMiddleware",
]


Expand Down

0 comments on commit d5b930e

Please sign in to comment.