Repository: translate/pootle

Issue:
Add view tests for disabled projects
This issue #4439 has been fixed in #4443 but we have to add proper tests there.



Discussion:

Resolving patch:
diff --git a/pytest_pootle/env.py b/pytest_pootle/env.py
--- a/pytest_pootle/env.py
+++ b/pytest_pootle/env.py
@@ -38,8 +38,8 @@ class PootleTestEnv(object):
     methods = (
         "redis", "case_sensitive_schema", "content_type", "site_root",
         "languages", "site_matrix", "system_users", "permissions",
-        "site_permissions", "tps", "vfolders", "subdirs", "submissions",
-        "announcements", "terminology")
+        "site_permissions", "tps", "disabled_project", "vfolders",
+        "subdirs", "submissions", "announcements", "terminology")
 
     def __init__(self, request):
         self.request = request
@@ -300,6 +300,28 @@ def setup_terminology(self):
         for language in Language.objects.all():
             TranslationProjectFactory(project=terminology, language=language)
 
+    def setup_disabled_project(self):
+        from pytest_pootle.factories import (DirectoryFactory,
+                                             ProjectFactory,
+                                             TranslationProjectFactory)
+
+        from pootle_language.models import Language
+
+        source_language = Language.objects.get(code="en")
+        project = ProjectFactory(code="disabled_project0",
+                                 fullname="Disabled Project 0",
+                                 source_language=source_language)
+        project.disabled = True
+        project.save()
+        language = Language.objects.get(code="language0")
+        tp = TranslationProjectFactory(project=project, language=language)
+        tp_dir = tp.directory
+        tp_dir.obsolete = False
+        tp_dir.save()
+        self._add_stores(tp, n=(1, 1))
+        subdir0 = DirectoryFactory(name="subdir0", parent=tp.directory)
+        self._add_stores(tp, n=(1, 1), parent=subdir0)
+
     def setup_subdirs(self):
         from pytest_pootle.factories import DirectoryFactory
 
diff --git a/pytest_pootle/fixtures/views.py b/pytest_pootle/fixtures/views.py
--- a/pytest_pootle/fixtures/views.py
+++ b/pytest_pootle/fixtures/views.py
@@ -214,6 +214,35 @@
       {"dir_path": "subdir0/",
        "filename": "store3.po"})))
 
+DISABLED_PROJECT_URL_PARAMS = OrderedDict(
+    (("project", {
+        "view_name": "pootle-project",
+        "project_code": "disabled_project0",
+        "dir_path": "",
+        "filename": ""}),
+     ("tp", {
+         "view_name": "pootle-tp",
+         "project_code": "disabled_project0",
+         "language_code": "language0",
+         "dir_path": ""}),
+     ("tp_subdir", {
+         "view_name": "pootle-tp",
+         "project_code": "disabled_project0",
+         "language_code": "language0",
+         "dir_path": "subdir0/"}),
+     ("tp_store", {
+         "view_name": "pootle-tp-store",
+         "project_code": "disabled_project0",
+         "language_code": "language0",
+         "dir_path": "",
+         "filename": "store0.po"}),
+     ("tp_subdir_store", {
+         "view_name": "pootle-tp-store",
+         "project_code": "disabled_project0",
+         "language_code": "language0",
+         "dir_path": "subdir0/",
+         "filename": "store1.po"})))
+
 
 @pytest.fixture(params=GET_UNITS_TESTS.keys())
 def get_units_views(request, client, request_users):
@@ -251,7 +280,7 @@ def project_views(request, client):
     from pootle_project.models import Project
 
     test_type = request.param.split("_")[0]
-    project = Project.objects.all()[0]
+    project = Project.objects.get(code="project0")
     kwargs = {"project_code": project.code, "dir_path": "", "filename": ""}
     kwargs.update(PROJECT_VIEW_TESTS[request.param])
     view_name = "pootle-project-%s" % test_type
@@ -345,3 +374,19 @@ def tp_uploads(client, member):
             for unit in store.units])})
 
     return tp, response.wsgi_request, response, kwargs
+
+
+@pytest.fixture(params=("browse", "translate", "export"))
+def view_types(request):
+    """List of possible view types."""
+    return request.param
+
+
+@pytest.fixture(params=DISABLED_PROJECT_URL_PARAMS.keys())
+def dp_view_urls(request, view_types):
+    """List of url params required for disabled project tests."""
+    kwargs = DISABLED_PROJECT_URL_PARAMS[request.param].copy()
+    view_name = kwargs.pop("view_name")
+    view_name = "%s-%s" % (view_name, view_types)
+
+    return reverse(view_name, kwargs=kwargs)


Test patch:
diff --git a/tests/views/disabled_project.py b/tests/views/disabled_project.py
new file mode 100644
--- /dev/null
+++ b/tests/views/disabled_project.py
@@ -0,0 +1,45 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) Pootle contributors.
+#
+# This file is a part of the Pootle project. It is distributed under the GPL3
+# or later license. See the LICENSE file for a copy of the license and the
+# AUTHORS file for copyright and authorship information.
+
+import pytest
+
+from django.core.urlresolvers import reverse
+
+
+@pytest.mark.django_db
+def test_views_disabled_project(client, dp_view_urls, request_users):
+    url = dp_view_urls
+    user = request_users["user"]
+    if user.username != "nobody":
+        client.login(
+            username=user.username,
+            password=request_users["password"])
+
+    response = client.get(url)
+
+    if user.is_superuser:
+        assert response.status_code == 200
+    else:
+        assert response.status_code == 404
+
+
+@pytest.mark.django_db
+def test_disabled_project_in_lang_browse_view(client, request_users):
+    user = request_users["user"]
+    if user.username != "nobody":
+        client.login(
+            username=user.username,
+            password=request_users["password"])
+
+    response = client.get(reverse("pootle-language-browse",
+                                  kwargs={"language_code": "language0"}))
+
+    disabled_project_exists = "language0-disabled_project0" in [
+        item["code"] for item in response.context["table"]["items"]]
+
+    assert (user.is_superuser is disabled_project_exists)