Merge branch 'aureplop/cof-tests_misc' into 'master'
cof -- Add tests for some views See merge request cof-geek/gestioCOF!292
This commit is contained in:
commit
9986cb1dc7
6 changed files with 143 additions and 8 deletions
|
@ -86,13 +86,15 @@ urlpatterns = [
|
||||||
url(r'^utile_bda$', gestioncof_views.utile_bda,
|
url(r'^utile_bda$', gestioncof_views.utile_bda,
|
||||||
name='utile_bda'),
|
name='utile_bda'),
|
||||||
url(r'^utile_bda/bda_diff$', gestioncof_views.liste_bdadiff),
|
url(r'^utile_bda/bda_diff$', gestioncof_views.liste_bdadiff),
|
||||||
url(r'^utile_cof/diff_cof$', gestioncof_views.liste_diffcof),
|
url(r'^utile_cof/diff_cof$', gestioncof_views.liste_diffcof,
|
||||||
|
name='ml_diffcof'),
|
||||||
url(r'^utile_bda/bda_revente$', gestioncof_views.liste_bdarevente),
|
url(r'^utile_bda/bda_revente$', gestioncof_views.liste_bdarevente),
|
||||||
url(r'^k-fet/', include('kfet.urls')),
|
url(r'^k-fet/', include('kfet.urls')),
|
||||||
url(r'^cms/', include(wagtailadmin_urls)),
|
url(r'^cms/', include(wagtailadmin_urls)),
|
||||||
url(r'^documents/', include(wagtaildocs_urls)),
|
url(r'^documents/', include(wagtaildocs_urls)),
|
||||||
# djconfig
|
# djconfig
|
||||||
url(r"^config", gestioncof_views.ConfigUpdate.as_view()),
|
url(r"^config", gestioncof_views.ConfigUpdate.as_view(),
|
||||||
|
name='config.edit'),
|
||||||
]
|
]
|
||||||
|
|
||||||
if 'debug_toolbar' in settings.INSTALLED_APPS:
|
if 'debug_toolbar' in settings.INSTALLED_APPS:
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
{% block realcontent %}
|
{% block realcontent %}
|
||||||
<h2>Modifier mon profil</h2>
|
<h2>Modifier mon profil</h2>
|
||||||
<form id="profile form-horizontal" method="post" action="{% url 'gestioncof.views.profile' %}">
|
<form id="profile form-horizontal" method="post" action="{% url 'profile' %}">
|
||||||
<div class="row" style="margin: 0 15%;">
|
<div class="row" style="margin: 0 15%;">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<fieldset"center-block">
|
<fieldset"center-block">
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
<h3>COF</h3>
|
<h3>COF</h3>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="{% url 'cof.membres_export' %}">Export des membres du COF</a></li>
|
<li><a href="{% url 'cof.membres_export' %}">Export des membres du COF</a></li>
|
||||||
<li><a href="{% url 'gestioncof.views.liste_diffcof' %}">Diffusion COF</a></li>
|
<li><a href="{% url 'ml_diffcof' %}">Diffusion COF</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<h3>Mega</h3>
|
<h3>Mega</h3>
|
||||||
|
|
|
@ -14,7 +14,129 @@ from gestioncof.models import (
|
||||||
)
|
)
|
||||||
from gestioncof.tests.testcases import ViewTestCaseMixin
|
from gestioncof.tests.testcases import ViewTestCaseMixin
|
||||||
|
|
||||||
from .utils import create_user
|
from .utils import create_member, create_root, create_user
|
||||||
|
|
||||||
|
|
||||||
|
class HomeViewTests(ViewTestCaseMixin, TestCase):
|
||||||
|
url_name = 'home'
|
||||||
|
url_expected = '/'
|
||||||
|
|
||||||
|
auth_user = 'user'
|
||||||
|
auth_forbidden = [None]
|
||||||
|
|
||||||
|
def test(self):
|
||||||
|
r = self.client.get(self.url)
|
||||||
|
self.assertEqual(r.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
|
class ProfileViewTests(ViewTestCaseMixin, TestCase):
|
||||||
|
url_name = 'profile'
|
||||||
|
url_expected = '/profile'
|
||||||
|
|
||||||
|
http_methods = ['GET', 'POST']
|
||||||
|
|
||||||
|
auth_user = 'member'
|
||||||
|
auth_forbidden = [None, 'user']
|
||||||
|
|
||||||
|
def test_get(self):
|
||||||
|
r = self.client.get(self.url)
|
||||||
|
self.assertEqual(r.status_code, 200)
|
||||||
|
|
||||||
|
def test_post(self):
|
||||||
|
u = self.users['member']
|
||||||
|
|
||||||
|
r = self.client.post(self.url, {
|
||||||
|
'first_name': 'First',
|
||||||
|
'last_name': 'Last',
|
||||||
|
'phone': '',
|
||||||
|
# 'mailing_cof': '1',
|
||||||
|
# 'mailing_bda': '1',
|
||||||
|
# 'mailing_bda_revente': '1',
|
||||||
|
})
|
||||||
|
|
||||||
|
self.assertEqual(r.status_code, 200)
|
||||||
|
expected_message = Message(messages.SUCCESS, (
|
||||||
|
"Votre profil a été mis à jour avec succès !"
|
||||||
|
))
|
||||||
|
self.assertIn(expected_message, get_messages(r.wsgi_request))
|
||||||
|
u.refresh_from_db()
|
||||||
|
self.assertEqual(u.first_name, 'First')
|
||||||
|
self.assertEqual(u.last_name, 'Last')
|
||||||
|
self.assertFalse(u.profile.mailing_cof)
|
||||||
|
self.assertFalse(u.profile.mailing_bda)
|
||||||
|
self.assertFalse(u.profile.mailing_bda_revente)
|
||||||
|
|
||||||
|
|
||||||
|
class UtilsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
|
url_name = 'utile_cof'
|
||||||
|
url_expected = '/utile_cof'
|
||||||
|
|
||||||
|
auth_user = 'staff'
|
||||||
|
auth_forbidden = [None, 'user', 'member']
|
||||||
|
|
||||||
|
def test(self):
|
||||||
|
r = self.client.get(self.url)
|
||||||
|
self.assertEqual(r.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
|
class MailingListDiffCof(ViewTestCaseMixin, TestCase):
|
||||||
|
url_name = 'ml_diffcof'
|
||||||
|
url_expected = '/utile_cof/diff_cof'
|
||||||
|
|
||||||
|
auth_user = 'staff'
|
||||||
|
auth_forbidden = [None, 'user', 'member']
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
|
||||||
|
self.u1 = create_member('u1', attrs={'mailing_cof': True})
|
||||||
|
self.u2 = create_member('u2', attrs={'mailing_cof': False})
|
||||||
|
self.u3 = create_user('u3', attrs={'mailing_cof': True})
|
||||||
|
|
||||||
|
def test(self):
|
||||||
|
r = self.client.get(self.url)
|
||||||
|
|
||||||
|
self.assertEqual(r.status_code, 200)
|
||||||
|
self.assertEqual(r.context['personnes'].get(), self.u1.profile)
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigUpdateViewTests(ViewTestCaseMixin, TestCase):
|
||||||
|
url_name = 'config.edit'
|
||||||
|
url_expected = '/config'
|
||||||
|
|
||||||
|
http_methods = ['GET', 'POST']
|
||||||
|
|
||||||
|
auth_user = 'root'
|
||||||
|
auth_forbidden = [None, 'user', 'member', 'staff']
|
||||||
|
|
||||||
|
def get_users_extra(self):
|
||||||
|
return {
|
||||||
|
'root': create_root('root'),
|
||||||
|
}
|
||||||
|
|
||||||
|
def test_get(self):
|
||||||
|
r = self.client.get(self.url)
|
||||||
|
self.assertEqual(r.status_code, 200)
|
||||||
|
|
||||||
|
def test_post(self):
|
||||||
|
r = self.client.post(self.url, {
|
||||||
|
'gestion_banner': 'Announcement !',
|
||||||
|
})
|
||||||
|
|
||||||
|
self.assertRedirects(r, reverse('home'))
|
||||||
|
|
||||||
|
|
||||||
|
class UserAutocompleteViewTests(ViewTestCaseMixin, TestCase):
|
||||||
|
url_name = 'cof-user-autocomplete'
|
||||||
|
url_expected = '/user/autocomplete'
|
||||||
|
|
||||||
|
auth_user = 'staff'
|
||||||
|
auth_forbidden = [None, 'user', 'member']
|
||||||
|
|
||||||
|
def test(self):
|
||||||
|
r = self.client.get(self.url, {'q': 'user'})
|
||||||
|
|
||||||
|
self.assertEqual(r.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
class ExportMembersViewTests(ViewTestCaseMixin, TestCase):
|
class ExportMembersViewTests(ViewTestCaseMixin, TestCase):
|
||||||
|
|
|
@ -9,7 +9,9 @@ def _create_user(username, is_cof=False, is_staff=False, attrs=None):
|
||||||
|
|
||||||
password = attrs.pop('password', username)
|
password = attrs.pop('password', username)
|
||||||
|
|
||||||
user_keys = ['first_name', 'last_name', 'email', 'is_staff']
|
user_keys = [
|
||||||
|
'first_name', 'last_name', 'email', 'is_staff', 'is_superuser',
|
||||||
|
]
|
||||||
user_attrs = {k: v for k, v in attrs.items() if k in user_keys}
|
user_attrs = {k: v for k, v in attrs.items() if k in user_keys}
|
||||||
|
|
||||||
profile_keys = [
|
profile_keys = [
|
||||||
|
@ -49,3 +51,11 @@ def create_member(username, attrs=None):
|
||||||
|
|
||||||
def create_staff(username, attrs=None):
|
def create_staff(username, attrs=None):
|
||||||
return _create_user(username, is_cof=True, is_staff=True, attrs=attrs)
|
return _create_user(username, is_cof=True, is_staff=True, attrs=attrs)
|
||||||
|
|
||||||
|
|
||||||
|
def create_root(username, attrs=None):
|
||||||
|
if attrs is None:
|
||||||
|
attrs = {}
|
||||||
|
attrs.setdefault('is_staff', True)
|
||||||
|
attrs.setdefault('is_superuser', True)
|
||||||
|
return _create_user(username, attrs=attrs)
|
||||||
|
|
|
@ -9,6 +9,7 @@ from django.http import Http404, HttpResponse, HttpResponseForbidden
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.contrib.auth.views import (
|
from django.contrib.auth.views import (
|
||||||
login as django_login_view, logout as django_logout_view,
|
login as django_login_view, logout as django_logout_view,
|
||||||
|
redirect_to_login,
|
||||||
)
|
)
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.contrib.sites.models import Site
|
from django.contrib.sites.models import Site
|
||||||
|
@ -338,7 +339,7 @@ def profile(request):
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save()
|
form.save()
|
||||||
messages.success(request,
|
messages.success(request,
|
||||||
"Votre profil a été mis à jour avec succès !")
|
"Votre profil a été mis à jour avec succès !")
|
||||||
else:
|
else:
|
||||||
form = UserProfileForm(instance=request.user.profile)
|
form = UserProfileForm(instance=request.user.profile)
|
||||||
return render(request, "gestioncof/profile.html", {"form": form})
|
return render(request, "gestioncof/profile.html", {"form": form})
|
||||||
|
@ -782,7 +783,7 @@ class ConfigUpdate(FormView):
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
if request.user is None or not request.user.is_superuser:
|
if request.user is None or not request.user.is_superuser:
|
||||||
raise Http404
|
return redirect_to_login(request.get_full_path())
|
||||||
return super().dispatch(request, *args, **kwargs)
|
return super().dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
|
|
Loading…
Reference in a new issue