gvacommon/gvacommon/tests/test_viewmixins.py
Jan Dittberner 8dda40a363 Make gvacommon distributable
This commit performs a lot of changes to make gvacommon properly
distributable:

- add a setup.py
- move code into a module directory
- add a __version__
- add a README.rst and AGPLv3 license file (COPYING)
- specify the licensing in file headers
- move tests into a submodule of gvacommon and split tests for different
  modules into separate test modules
- get rid of django-braces dependency
- adapt setup.cfg to the new directory structure
- ignore build and dist directories
2016-01-29 13:40:12 +01:00

119 lines
3.7 KiB
Python

#
# gvacommon - common parts of gnuviechadmin
# Copyright (C) 2014-2016 Jan Dittberner
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Tests for gvacommon code.
"""
from __future__ import absolute_import, unicode_literals
import unittest
try:
from unittest.Mock import MagicMock
except ImportError:
from mock import MagicMock
import django
from django.db import connection
from django.http import HttpResponseForbidden
from django.conf import settings
from django.views.generic import View
from django.contrib.auth import get_user_model
if not settings.configured:
settings.configure(
DEBUG=True,
ROOT_URLCONF='tests',
TEMPLATE_DIRS=['.'],
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'test',
}
},
INSTALLED_APPS=(
'django.contrib.contenttypes',
'django.contrib.auth',),
)
django.setup()
urlpatterns = []
class StaffOrSelfLoginRequiredMixinTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
connection.creation.create_test_db()
User = get_user_model()
User.objects.create_superuser(
username='admin', password='admin', email='test@example.org')
User.objects.create_user(username='test')
@classmethod
def tearDownClass(cls):
connection.creation.destroy_test_db()
def setUp(self):
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
class SubjectView(StaffOrSelfLoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
return "success"
def get_customer_object(self):
return get_user_model().objects.get(username='test')
self.subject = SubjectView.as_view()
def test_dispatch_anonymous(self):
from django.contrib.auth.models import AnonymousUser
request = MagicMock(method='GET')
request.user = AnonymousUser()
result = self.subject(request)
self.assertIsInstance(result, HttpResponseForbidden)
def test_dispatch_staff_user(self):
request = MagicMock(method='GET')
request.user = get_user_model().objects.get(username='admin')
result = self.subject(request)
self.assertEqual(result, "success")
def test_dispatch_customer_user(self):
request = MagicMock(method='GET')
request.user = get_user_model().objects.get(username='test')
result = self.subject(request)
self.assertEqual(result, "success")
def test_dispatch_other_user(self):
request = MagicMock(method='GET')
request.user = get_user_model().objects.create_user('other')
result = self.subject(request)
self.assertIsInstance(result, HttpResponseForbidden)
def test_get_customer_object_not_implemented(self):
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
class IncompleteView(StaffOrSelfLoginRequiredMixin, View):
pass
view = IncompleteView()
with self.assertRaises(NotImplementedError):
view.get_customer_object()