Commit 64fecf9f049149d90ff306c9cea4bb65e1d994e2
1 parent
ca4dcc0a
Exists in
master
and in
5 other branches
Test remember password #8
Showing
5 changed files
with
88 additions
and
18 deletions
Show diff stats
amadeus/settings.py
@@ -144,6 +144,7 @@ MEDIA_URL = '/uploads/' | @@ -144,6 +144,7 @@ MEDIA_URL = '/uploads/' | ||
144 | LOGIN_REDIRECT_URL = 'app:index' | 144 | LOGIN_REDIRECT_URL = 'app:index' |
145 | LOGIN_URL = 'core:home' | 145 | LOGIN_URL = 'core:home' |
146 | AUTH_USER_MODEL = 'users.User' | 146 | AUTH_USER_MODEL = 'users.User' |
147 | + | ||
147 | AUTHENTICATION_BACKENDS = [ | 148 | AUTHENTICATION_BACKENDS = [ |
148 | 'django.contrib.auth.backends.ModelBackend', | 149 | 'django.contrib.auth.backends.ModelBackend', |
149 | ] | 150 | ] |
@@ -153,7 +154,12 @@ LOGS_URL = 'logs/' | @@ -153,7 +154,12 @@ LOGS_URL = 'logs/' | ||
153 | #https://github.com/squ1b3r/Djaneiro | 154 | #https://github.com/squ1b3r/Djaneiro |
154 | 155 | ||
155 | 156 | ||
157 | |||
158 | +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' | ||
159 | +DEFAULT_FROM_EMAIL = 'admin@admin.com' | ||
160 | + | ||
161 | + | ||
156 | try: | 162 | try: |
157 | from .local_settings import * | 163 | from .local_settings import * |
158 | except ImportError: | 164 | except ImportError: |
159 | - pass | ||
160 | \ No newline at end of file | 165 | \ No newline at end of file |
166 | + pass |
core/templates/index.html
@@ -17,7 +17,7 @@ | @@ -17,7 +17,7 @@ | ||
17 | <div class="col-lg-9 col-lg-offset-2"> | 17 | <div class="col-lg-9 col-lg-offset-2"> |
18 | <div class="card"> | 18 | <div class="card"> |
19 | {% if message %} | 19 | {% if message %} |
20 | - <div class="alert alert-danger"> | 20 | + <div class="alert alert-danger fade in"> |
21 | {{message}} | 21 | {{message}} |
22 | </div> | 22 | </div> |
23 | 23 |
core/templates/remember_password.html
@@ -10,20 +10,29 @@ | @@ -10,20 +10,29 @@ | ||
10 | <div class="row "> | 10 | <div class="row "> |
11 | <div class="col-lg-9 col-lg-offset-2"> | 11 | <div class="col-lg-9 col-lg-offset-2"> |
12 | <div class="card"> | 12 | <div class="card"> |
13 | + {% if success %} | ||
14 | + <div class="alert alert-success fade in"> | ||
15 | + {{success}} | ||
16 | + </div> | ||
17 | + {% elif danger %} | ||
18 | + <div class="alert alert-danger fade in"> | ||
19 | + {{danger}} | ||
20 | + </div> | ||
21 | + {% endif %} | ||
13 | <div class="card-content"> | 22 | <div class="card-content"> |
14 | - | ||
15 | <div class="card-body"> | 23 | <div class="card-body"> |
16 | - <form class="form-group "> | 24 | + <form class="form-group " method="post" action=""> |
25 | + {% csrf_token %} | ||
17 | <div class="form-group"> | 26 | <div class="form-group"> |
18 | <label for="inputEmail" class="control-label label-static"> {% trans 'E-mail' %}</label> | 27 | <label for="inputEmail" class="control-label label-static"> {% trans 'E-mail' %}</label> |
19 | - <input type="email" class="form-control" id="inputEmail" placeholder="Email"> | 28 | + <input name="email" type="email" class="form-control" id="inputEmail" placeholder="Email" {% if email %}value="{{email}}"{% endif %}> |
20 | </div> | 29 | </div> |
21 | <div class="form-group"> | 30 | <div class="form-group"> |
22 | <label for="inputRegistration" class=" control-label"> {% trans 'Registration' %} </label> | 31 | <label for="inputRegistration" class=" control-label"> {% trans 'Registration' %} </label> |
23 | - <input type="text" class="form-control" id="inputRegistration" placeholder="Registration"> | 32 | + <input name="registration" type="text" class="form-control" id="inputRegistration" placeholder="Registration" {% if registration %}value="{{registration}}"{% endif %}> |
24 | </div> | 33 | </div> |
25 | <div class="col-lg-offset-4 col-lg-4"> | 34 | <div class="col-lg-offset-4 col-lg-4"> |
26 | - <button class="btn btn-raised btn-primary btn-lg btn-block">{% trans 'Send' %}</button> | 35 | + <button type="submite" class="btn btn-raised btn-primary btn-lg btn-block">{% trans 'Send' %}</button> |
27 | 36 | ||
28 | </div> | 37 | </div> |
29 | 38 |
core/tests.py
@@ -2,7 +2,7 @@ from django.test import TestCase, Client | @@ -2,7 +2,7 @@ from django.test import TestCase, Client | ||
2 | from django.core.urlresolvers import reverse | 2 | from django.core.urlresolvers import reverse |
3 | from rolepermissions.shortcuts import assign_role | 3 | from rolepermissions.shortcuts import assign_role |
4 | from users.models import User | 4 | from users.models import User |
5 | -# from django.core import mail | 5 | +from django.core import mail |
6 | 6 | ||
7 | class LoginTestCase(TestCase): | 7 | class LoginTestCase(TestCase): |
8 | 8 | ||
@@ -10,10 +10,10 @@ class LoginTestCase(TestCase): | @@ -10,10 +10,10 @@ class LoginTestCase(TestCase): | ||
10 | self.client = Client() | 10 | self.client = Client() |
11 | 11 | ||
12 | self.user = User.objects.create_user( | 12 | self.user = User.objects.create_user( |
13 | - username = 'test', | ||
14 | - email = 'testing@amadeus.com', | ||
15 | - is_staff = True, | ||
16 | - is_active = True, | 13 | + username = 'test', |
14 | + email = 'testing@amadeus.com', | ||
15 | + is_staff = True, | ||
16 | + is_active = True, | ||
17 | password = 'testing' | 17 | password = 'testing' |
18 | ) | 18 | ) |
19 | assign_role(self.user, 'system_admin') | 19 | assign_role(self.user, 'system_admin') |
@@ -38,7 +38,7 @@ class LoginTestCase(TestCase): | @@ -38,7 +38,7 @@ class LoginTestCase(TestCase): | ||
38 | # self.assertEquals(response.context['message'], "Email ou senha incorretos!") | 38 | # self.assertEquals(response.context['message'], "Email ou senha incorretos!") |
39 | 39 | ||
40 | class RegisterUserTestCase(TestCase): | 40 | class RegisterUserTestCase(TestCase): |
41 | - | 41 | + |
42 | def setUp(self): | 42 | def setUp(self): |
43 | self.client = Client() | 43 | self.client = Client() |
44 | self.url = reverse('core:register') | 44 | self.url = reverse('core:register') |
@@ -59,3 +59,35 @@ class RegisterUserTestCase(TestCase): | @@ -59,3 +59,35 @@ class RegisterUserTestCase(TestCase): | ||
59 | self.assertRedirects(response, 'http://localhost%s' % reverse('core:home')) | 59 | self.assertRedirects(response, 'http://localhost%s' % reverse('core:home')) |
60 | self.assertEqual(response.status_code, 302) | 60 | self.assertEqual(response.status_code, 302) |
61 | self.assertEqual(User.objects.count(), 1) | 61 | self.assertEqual(User.objects.count(), 1) |
62 | + | ||
63 | +class RememberPasswordTestCase(TestCase): | ||
64 | + | ||
65 | + def setUp(self): | ||
66 | + self.client = Client() | ||
67 | + self.url = reverse('core:remember_password') | ||
68 | + | ||
69 | + def test_remember_password_ok(self): | ||
70 | + response = self.client.get(self.url) | ||
71 | + self.assertEquals(response.status_code, 200) | ||
72 | + self.assertTemplateUsed(response, 'remember_password.html') | ||
73 | + data = {'email': 'fulano@fulano.com', 'registration': '0124578964226'} | ||
74 | + response = self.client.post(self.url, data) | ||
75 | + self.assertEquals(response.status_code, 200) | ||
76 | + self.assertEquals(len(mail.outbox), 1) | ||
77 | + self.assertTrue('success' in response.context) | ||
78 | + self.assertTrue('danger' not in response.context) | ||
79 | + | ||
80 | + def test_remember_password_error(self): | ||
81 | + data = {'email': 'fulano@fulano.com','registration':''} | ||
82 | + response = self.client.post(self.url, data) | ||
83 | + self.assertEquals(response.status_code, 200) | ||
84 | + self.assertEquals(len(mail.outbox), 0) | ||
85 | + self.assertTrue('success' not in response.context) | ||
86 | + self.assertTrue('danger' in response.context) | ||
87 | + | ||
88 | + data = {'email': '', 'registration': '0124578964226'} | ||
89 | + response = self.client.post(self.url, data) | ||
90 | + self.assertEquals(response.status_code, 200) | ||
91 | + self.assertEquals(len(mail.outbox), 0) | ||
92 | + self.assertTrue('success' not in response.context) | ||
93 | + self.assertTrue('danger' in response.context) |
core/views.py
1 | -from rolepermissions.shortcuts import assign_role | ||
2 | from django.utils.translation import ugettext_lazy as _ | 1 | from django.utils.translation import ugettext_lazy as _ |
3 | from django.core.urlresolvers import reverse_lazy, reverse | 2 | from django.core.urlresolvers import reverse_lazy, reverse |
4 | from django.contrib.auth import authenticate, login as login_user | 3 | from django.contrib.auth import authenticate, login as login_user |
5 | -from .decorators import log_decorator | ||
6 | from django.contrib import messages | 4 | from django.contrib import messages |
7 | from django.shortcuts import render, redirect | 5 | from django.shortcuts import render, redirect |
8 | from django.views.generic import CreateView | 6 | from django.views.generic import CreateView |
9 | from django.http import HttpResponse | 7 | from django.http import HttpResponse |
10 | -from .forms import RegisterUserForm | 8 | +from django.core.mail import send_mail,BadHeaderError |
9 | +from django.conf import settings | ||
10 | + | ||
11 | +from rolepermissions.shortcuts import assign_role | ||
12 | + | ||
13 | +from .forms import RegisterUserForm | ||
14 | +from .decorators import log_decorator | ||
15 | + | ||
11 | from users.models import User | 16 | from users.models import User |
12 | 17 | ||
13 | def index(request): | 18 | def index(request): |
@@ -28,7 +33,7 @@ class RegisterUser(CreateView): | @@ -28,7 +33,7 @@ class RegisterUser(CreateView): | ||
28 | assign_role(form.instance, 'student') | 33 | assign_role(form.instance, 'student') |
29 | 34 | ||
30 | messages.success(self.request, _('User successfully registered!')) | 35 | messages.success(self.request, _('User successfully registered!')) |
31 | - | 36 | + |
32 | return super(RegisterUser, self).form_valid(form) | 37 | return super(RegisterUser, self).form_valid(form) |
33 | 38 | ||
34 | def create_account(request): | 39 | def create_account(request): |
@@ -36,7 +41,25 @@ def create_account(request): | @@ -36,7 +41,25 @@ def create_account(request): | ||
36 | 41 | ||
37 | 42 | ||
38 | def remember_password(request): | 43 | def remember_password(request): |
39 | - return render(request, "remember_password.html") | 44 | + context = {} |
45 | + if request.POST: | ||
46 | + email = request.POST['email'] | ||
47 | + registration = request.POST['registration'] | ||
48 | + if email and registration: | ||
49 | + subject = _('Recover your password') | ||
50 | + message = _('Hello %s, \nRecover your password to use your account.\nNumber of registration: %s\nLink for recuver password.\n\nRespectfully,\nTeam Amadeus.' % (request.user,registration)) | ||
51 | + try: | ||
52 | + send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [email],fail_silently=False) | ||
53 | + context['success'] = 'Email successfully sent' | ||
54 | + except BadHeaderError: | ||
55 | + context['email'] = email | ||
56 | + context['registration'] = registration | ||
57 | + context['danger'] = 'E-mail does not send' | ||
58 | + else: | ||
59 | + context['email'] = email | ||
60 | + context['registration'] = registration | ||
61 | + context['danger'] = 'E-mail does not send' | ||
62 | + return render(request, "remember_password.html",context) | ||
40 | 63 | ||
41 | @log_decorator('Entrou no sistema') | 64 | @log_decorator('Entrou no sistema') |
42 | def login(request): | 65 | def login(request): |