diff --git a/core/forms.py b/core/forms.py index 1ac601e2..15032abd 100644 --- a/core/forms.py +++ b/core/forms.py @@ -27,7 +27,7 @@ class LoginForm(AuthenticationForm): if u is not None: if u.is_active: login(self.request, u) - logging.debug("Logging in "+u) + logging.debug("Logging in "+str(u)) else: raise forms.ValidationError( self.error_messages['invalid_login'], diff --git a/core/templates/core/base.html b/core/templates/core/base.html index e9573f29..f0213010 100644 --- a/core/templates/core/base.html +++ b/core/templates/core/base.html @@ -18,6 +18,9 @@
+ {% if error %} + {{ error }} + {% endif %} {% block content %}{% endblock %}
@@ -26,5 +29,10 @@ Site réalisé par des gens biens {% endblock %} + diff --git a/core/templates/core/register.html b/core/templates/core/register.html index ad53b450..2d051d12 100644 --- a/core/templates/core/register.html +++ b/core/templates/core/register.html @@ -10,7 +10,6 @@ Welcome {{ user_registered.get_display_name }}! You successfully registred and you will soon receive a confirmation mail. Your username is {{ user_registered.username }}. - {% endif %}
diff --git a/core/tests.py b/core/tests.py index 9163af77..a6d95a79 100644 --- a/core/tests.py +++ b/core/tests.py @@ -31,7 +31,7 @@ class UserRegistrationTest(SimpleTestCase): 'password2': 'plop2', }) 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): """ @@ -45,7 +45,7 @@ class UserRegistrationTest(SimpleTestCase): 'password2': 'plop', }) 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): """ @@ -59,7 +59,7 @@ class UserRegistrationTest(SimpleTestCase): 'password2': 'plop', }) 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): """ @@ -73,7 +73,7 @@ class UserRegistrationTest(SimpleTestCase): 'password2': 'plop', }) 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): """ @@ -93,5 +93,34 @@ class UserRegistrationTest(SimpleTestCase): 'password2': 'plop', }) 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)) diff --git a/core/views.py b/core/views.py index 6f91edb8..efca786f 100644 --- a/core/views.py +++ b/core/views.py @@ -9,8 +9,17 @@ import logging logging.basicConfig(level=logging.DEBUG) -def index(request): - return render(request, "core/index.html", {'title': 'Bienvenue!'}) +# This is a global default context that can be used everywhere and provide default basic values +# 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): 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']) u = form.save() context['user_registered'] = u + context['tests'] = 'TEST_REGISTER_USER_FORM_OK' form = RegisteringForm() + else: + context['error'] = 'Erreur' + context['tests'] = 'TEST_REGISTER_USER_FORM_FAIL' else: form = RegisteringForm() context['form'] = form.as_p() return render(request, "core/register.html", context) def login(request): + context = {'title': 'Login'} if request.method == 'POST': try: form = LoginForm(request) form.login() - # TODO redirect to profile when done - return redirect('index') + context['tests'] = 'LOGIN_OK' + return render(request, 'core/index.html', context) except Exception as e: logging.debug(e) + context['error'] = "Login failed" + context['tests'] = 'LOGIN_FAIL' else: 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): auth_logout(request)