Commit d3896ad6 authored by Skia's avatar Skia

Update login system and add tests

parent 5ee2baed
...@@ -27,7 +27,7 @@ class LoginForm(AuthenticationForm): ...@@ -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'],
......
...@@ -18,6 +18,9 @@ ...@@ -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 @@ ...@@ -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>
...@@ -10,7 +10,6 @@ Welcome {{ user_registered.get_display_name }}! ...@@ -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">
......
...@@ -31,7 +31,7 @@ class UserRegistrationTest(SimpleTestCase): ...@@ -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): ...@@ -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): ...@@ -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): ...@@ -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): ...@@ -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))
...@@ -9,8 +9,17 @@ import logging ...@@ -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): ...@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment