# Path: users/models.py
# class RecoveryPassword(models.Model):
#     usuario = models.OneToOneField(User, primary_key=True,blank=True)
#     token_hash = models.TextField(max_length = 60,blank=True)
#     date_expired = models.DateField(auto_now=True)
#     token_used = models.BooleanField(default=False)
# 
# 
#     def search_email_user(self, email):
#         self.usuario = User.objects.get(email=email)
# 
# 
#     def generate_hash(self):
# 
#         plain_text = str(self.usuario.email) + str(self.usuario.password +str(self.date_expired))
#         self.token_hash = hashlib.sha256(plain_text.encode('utf-8')).hexdigest()
# 
# 
#     def make_url(self):
#         return 'localhost:8000/users/recuperar_senha/' + str(self.token_hash)
# 
# 
#     def send_email_url(self, email):
#         self.search_email_user(email)
#         self.generate_hash()
#         self.search_token_user()
#         self.make_url()
#         send_mail(
#                'Troca de senha',
#                'Entre nesse link para mudar sua senha ' + self.make_url(),
#                 EMAIL_HOST_USER,
#                 [self.usuario.email],
#                 fail_silently=False,
#         )
# 
# 
#     def search_token_user(self):
#         try:
#             recovery_password = RecoveryPassword.objects.get(usuario=self.usuario)
#         except ObjectDoesNotExist:
#             recovery_password = None
# 
#         if recovery_password is None:
#             super(RecoveryPassword,self).save()
#         else:
#             recovery_password.token_hash = self.token_hash
#             recovery_password.token_used = False
#             recovery_password.save()


import hashlib
import pytest
from django.contrib.auth.models import User
from django.core import mail
from factories import UserFactory
from users.models import RecoveryPassword
                follow=True)
        assert response.status_code == 200

    # Test wrong email.
    def test_solicitation_reset_password_post(self,client):
        response = client.post('/users/solicitar_senha/',
                {'email': ''},
                follow=True)
        assert response.status_code == 200

    # Test content of email.
    def test_email(self,client):
        response = client.post('/users/solicitar_senha/',
                               {'email': self.user1.email},
        follow=True)
            
        recovery_password = RecoveryPassword.objects.get(usuario=self.user1) 
        assert mail.outbox[0].subject == 'Troca de senha'
        assert mail.outbox[0].body == 'Entre nesse link para mudar sua senha localhost:8000/users/recuperar_senha/' + recovery_password.token_hash

    #Test with the wring token
    def test_url_recovery_wrong_token(self, client):
        response = client.get('/users/recuperar_senha/testtest123123', follow=True)
        assert response.status_code == 200

    #Test with the correct token
    def test_url_recovery_correct_token(self, client):
        client.post('/users/solicitar_senha/',
                               {'email': self.user1.email},
        follow=True)

# Path: users/models.py
# class RecoveryPassword(models.Model):
#     usuario = models.OneToOneField(User, primary_key=True,blank=True)
#     token_hash = models.TextField(max_length = 60,blank=True)
#     date_expired = models.DateField(auto_now=True)
#     token_used = models.BooleanField(default=False)
# 
# 
#     def search_email_user(self, email):
#         self.usuario = User.objects.get(email=email)
# 
# 
#     def generate_hash(self):
# 
#         plain_text = str(self.usuario.email) + str(self.usuario.password +str(self.date_expired))
#         self.token_hash = hashlib.sha256(plain_text.encode('utf-8')).hexdigest()
# 
# 
#     def make_url(self):
#         return 'localhost:8000/users/recuperar_senha/' + str(self.token_hash)
# 
# 
#     def send_email_url(self, email):
#         self.search_email_user(email)
#         self.generate_hash()
#         self.search_token_user()
#         self.make_url()
#         send_mail(
#                'Troca de senha',
#                'Entre nesse link para mudar sua senha ' + self.make_url(),
#                 EMAIL_HOST_USER,
#                 [self.usuario.email],
#                 fail_silently=False,
#         )
# 
# 
#     def search_token_user(self):
#         try:
#             recovery_password = RecoveryPassword.objects.get(usuario=self.usuario)
#         except ObjectDoesNotExist:
#             recovery_password = None
# 
#         if recovery_password is None:
#             super(RecoveryPassword,self).save()
#         else:
#             recovery_password.token_hash = self.token_hash
#             recovery_password.token_used = False
#             recovery_password.save()


# Path: users/tests/test_recovery_password.py
import hashlib
import pytest
from django.contrib.auth.models import User
from django.core import mail
from factories import UserFactory
from users.models import RecoveryPassword

                follow=True)
        assert response.status_code == 200

    # Test wrong email.
    def test_solicitation_reset_password_post(self,client):
        response = client.post('/users/solicitar_senha/',
                {'email': ''},
                follow=True)
        assert response.status_code == 200

    # Test content of email.
    def test_email(self,client):
        response = client.post('/users/solicitar_senha/',
                               {'email': self.user1.email},
        follow=True)
            
        recovery_password = RecoveryPassword.objects.get(usuario=self.user1) 
        assert mail.outbox[0].subject == 'Troca de senha'
        assert mail.outbox[0].body == 'Entre nesse link para mudar sua senha localhost:8000/users/recuperar_senha/' + recovery_password.token_hash

    #Test with the wring token
    def test_url_recovery_wrong_token(self, client):
        response = client.get('/users/recuperar_senha/testtest123123', follow=True)
        assert response.status_code == 200

    #Test with the correct token
    def test_url_recovery_correct_token(self, client):
        client.post('/users/solicitar_senha/',
                               {'email': self.user1.email},
        follow=True)

        recovery_password = RecoveryPassword.objects.get(usuario=self.user1)