Update login system and add tests

This commit is contained in:
Skia 2015-11-19 14:44:48 +01:00
parent 5ee2baedba
commit d3896ad676
5 changed files with 65 additions and 12 deletions

View File

@ -27,7 +27,7 @@ class LoginForm(AuthenticationForm):
if u is not None: if u is not None:
if u.is_active: if u.is_active:
login(self.request, u) login(self.request, u)
logging.debug("Logging in "+u) logging.debug("Logging in "+str(u))
else: else:
raise forms.ValidationError( raise forms.ValidationError(
self.error_messages['invalid_login'], self.error_messages['invalid_login'],

View File

@ -18,6 +18,9 @@
</header> </header>
<div id="content"> <div id="content">
{% if error %}
{{ error }}
{% endif %}
{% block content %}{% endblock %} {% block content %}{% endblock %}
</div> </div>
@ -26,5 +29,10 @@
Site réalisé par des gens biens Site réalisé par des gens biens
{% endblock %} {% endblock %}
</footer> </footer>
<!--
{% block tests %}
{{ tests }}
{% endblock %}
-->
</body> </body>
</html> </html>

View File

@ -10,7 +10,6 @@ Welcome {{ user_registered.get_display_name }}!
You successfully registred and you will soon receive a confirmation mail. You successfully registred and you will soon receive a confirmation mail.
Your username is {{ user_registered.username }}. Your username is {{ user_registered.username }}.
<!-- TEST_REGISTER_USER_FORM_OK -->
{% endif %} {% endif %}
<form action="{% url 'core:register' %}" method="post"> <form action="{% url 'core:register' %}" method="post">

View File

@ -31,7 +31,7 @@ class UserRegistrationTest(SimpleTestCase):
'password2': 'plop2', 'password2': 'plop2',
}) })
self.assertTrue(response.status_code == 200) self.assertTrue(response.status_code == 200)
self.assertTrue('TEST_REGISTER_USER_FORM_OK' not in str(response.content)) self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
def test_register_user_form_fail_email(self): def test_register_user_form_fail_email(self):
""" """
@ -45,7 +45,7 @@ class UserRegistrationTest(SimpleTestCase):
'password2': 'plop', 'password2': 'plop',
}) })
self.assertTrue(response.status_code == 200) self.assertTrue(response.status_code == 200)
self.assertTrue('TEST_REGISTER_USER_FORM_OK' not in str(response.content)) self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
def test_register_user_form_fail_missing_name(self): def test_register_user_form_fail_missing_name(self):
""" """
@ -59,7 +59,7 @@ class UserRegistrationTest(SimpleTestCase):
'password2': 'plop', 'password2': 'plop',
}) })
self.assertTrue(response.status_code == 200) self.assertTrue(response.status_code == 200)
self.assertTrue('TEST_REGISTER_USER_FORM_OK' not in str(response.content)) self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
def test_register_user_form_fail_missing_first_name(self): def test_register_user_form_fail_missing_first_name(self):
""" """
@ -73,7 +73,7 @@ class UserRegistrationTest(SimpleTestCase):
'password2': 'plop', 'password2': 'plop',
}) })
self.assertTrue(response.status_code == 200) self.assertTrue(response.status_code == 200)
self.assertTrue('TEST_REGISTER_USER_FORM_OK' not in str(response.content)) self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
def test_register_user_form_fail_already_exists(self): def test_register_user_form_fail_already_exists(self):
""" """
@ -93,5 +93,34 @@ class UserRegistrationTest(SimpleTestCase):
'password2': 'plop', 'password2': 'plop',
}) })
self.assertTrue(response.status_code == 200) self.assertTrue(response.status_code == 200)
self.assertTrue('TEST_REGISTER_USER_FORM_OK' not in str(response.content)) self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
def test_login_success(self):
"""
Should login a user correctly
"""
c = Client()
c.post(reverse('core:register'), {'first_name': 'Guy',
'last_name': 'Carlier',
'email': 'bibou@git.an',
'password1': 'plop',
'password2': 'plop',
})
response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'plop'})
self.assertTrue(response.status_code == 200)
self.assertTrue('LOGIN_OK' in str(response.content))
def test_login_fail(self):
"""
Should not login a user correctly
"""
c = Client()
c.post(reverse('core:register'), {'first_name': 'Guy',
'last_name': 'Carlier',
'email': 'bibou@git.an',
'password1': 'plop',
'password2': 'plop',
})
response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'guy'})
self.assertTrue(response.status_code == 200)
self.assertTrue('LOGIN_FAIL' in str(response.content))

View File

@ -9,8 +9,17 @@ import logging
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
def index(request): # This is a global default context that can be used everywhere and provide default basic values
return render(request, "core/index.html", {'title': 'Bienvenue!'}) # It needs to be completed by every function using templates
#context = {'title': 'Bienvenue!',
# 'tests': '',
# }
def index(request, context=None):
if context == None:
return render(request, "core/index.html", {'title': 'Bienvenue!'})
else:
return render(request, "core/index.html", context)
def register(request): def register(request):
context = {'title': 'Register a user'} context = {'title': 'Register a user'}
@ -20,24 +29,32 @@ def register(request):
logging.debug("Registering "+form.cleaned_data['first_name']+form.cleaned_data['last_name']) logging.debug("Registering "+form.cleaned_data['first_name']+form.cleaned_data['last_name'])
u = form.save() u = form.save()
context['user_registered'] = u context['user_registered'] = u
context['tests'] = 'TEST_REGISTER_USER_FORM_OK'
form = RegisteringForm() form = RegisteringForm()
else:
context['error'] = 'Erreur'
context['tests'] = 'TEST_REGISTER_USER_FORM_FAIL'
else: else:
form = RegisteringForm() form = RegisteringForm()
context['form'] = form.as_p() context['form'] = form.as_p()
return render(request, "core/register.html", context) return render(request, "core/register.html", context)
def login(request): def login(request):
context = {'title': 'Login'}
if request.method == 'POST': if request.method == 'POST':
try: try:
form = LoginForm(request) form = LoginForm(request)
form.login() form.login()
# TODO redirect to profile when done context['tests'] = 'LOGIN_OK'
return redirect('index') return render(request, 'core/index.html', context)
except Exception as e: except Exception as e:
logging.debug(e) logging.debug(e)
context['error'] = "Login failed"
context['tests'] = 'LOGIN_FAIL'
else: else:
form = LoginForm() form = LoginForm()
return render(request, "core/login.html", {'title': 'Login', 'form': form.as_p()}) context['form'] = form.as_p()
return render(request, "core/login.html", context)
def logout(request): def logout(request):
auth_logout(request) auth_logout(request)