mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-22 06:03:20 +00:00
Update login system and add tests
This commit is contained in:
parent
5ee2baedba
commit
d3896ad676
@ -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'],
|
||||
|
@ -18,6 +18,9 @@
|
||||
</header>
|
||||
|
||||
<div id="content">
|
||||
{% if error %}
|
||||
{{ error }}
|
||||
{% endif %}
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
|
||||
@ -26,5 +29,10 @@
|
||||
Site réalisé par des gens biens
|
||||
{% endblock %}
|
||||
</footer>
|
||||
<!--
|
||||
{% block tests %}
|
||||
{{ tests }}
|
||||
{% endblock %}
|
||||
-->
|
||||
</body>
|
||||
</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 }}.
|
||||
<!-- TEST_REGISTER_USER_FORM_OK -->
|
||||
{% endif %}
|
||||
|
||||
<form action="{% url 'core:register' %}" method="post">
|
||||
|
@ -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))
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user