# -*- coding: utf-8 -*-
from django.test import TestCase, override_settings

from corehq.apps.hqwebapp.models import HashedPasswordLoginAttempt
from corehq.apps.hqwebapp.utils import decode_password, extract_password

HASHED_PASSWORD_MAPPING = {
    "sha256$1e2d5bc2hhMjU2JDFlMmQ1Yk1USXpORFUyZjc5MTI3PQ==f79127=": "123456",
    "sha256$8f5008c2hhMjU2JDhmNTAwOFlXSmpNVEl6TFE9PTRhNjBhOT0=4a60a9=": "abc123-",
    "sha256$4bf7cdc2hhMjU2JDRiZjdjZE1USXpRQ01rSlRFeTEzMGM4ZD0=130c8d=": "123@#$%12",
    "sha256$29df66c2hhMjU2JDI5ZGY2NklDRkFJeVFsWGlZcUtDbGZLeTFjYTQwN2VkPQ==a407ed=": " !@#$%^&*()_+-\\",
    "sha256$ad5e3ac2hhMjU2JGFkNWUzYTRLU0o0S1NxNEtTVjRLU3c0S1NqTVRJejQyNDgyOT0=424829=": u"उपकरण123"
}


@override_settings(ENABLE_PASSWORD_HASHING=True)
class TestDecodePassword(TestCase):
    def test_decoder(self):
        for password_hash, password in HASHED_PASSWORD_MAPPING.items():
            self.assertFalse(HashedPasswordLoginAttempt.objects.filter(
                username="username",
                password_hash=password_hash
            ).exists())
            self.assertEqual(decode_password(password_hash, "username"), password)
            self.assertTrue(HashedPasswordLoginAttempt.objects.filter(
                username="username",
                password_hash=password_hash
            ).exists())

    def test_replay_attack(self):
        password_hash = "sha256$1e2d5bc2hhMjU2JDFlMmQ1Yk1USXpORFUyZjc5MTI3PQ==f79127="
        username = "james@007.com"
        HashedPasswordLoginAttempt.objects.create(
            username=username,
            password_hash=password_hash
        )
        self.assertEqual(decode_password(password_hash, username), '')


class TestExtractPassword(TestCase):
    def test_invalid_regex_format(self):
        password_hash = "sha255$1e2d5bc2hhMjU2JDFlMmQ1Yk1USXpORFUyZjc5MTI3PQ==f79127="
        self.assertEqual(extract_password(password_hash), password_hash)

        password_hash = "sha255$1e2d5bc2hhMjU2JDFlMmQ1Yk1USXpORFUyZjc5MTI3PQ=="
        self.assertEqual(extract_password(password_hash), password_hash)

    def test_invalid_padding(self):
        password_hash = "sha256$1e456bc2hhMjU2JDFlMmQ1Yk1USXpORFUyZjc5MTI3PQ==f79127="
        self.assertEqual(extract_password(password_hash), '')

@@ -68,16 +68,67 @@
 settings
+%0Afrom django.contrib.auth.hashers import get_hasher
 %0A%0Afrom c
@@ -849,32 +849,62 @@
 _decoder(self):%0A
+        hasher = get_hasher()%0A
         for pass
@@ -975,25 +975,8 @@
     
-self.assertFalse(
 Hash
@@ -1010,119 +1010,21 @@
 cts.
-filter(%0A                username=%22username%22,%0A                password_hash=password_hash%0A            ).exists()
+all().delete(
 )%0A  
@@ -1132,16 +1132,103 @@
 ertTrue(
+%0A                hasher.verify(%0A                    password_hash,%0A                    
 HashedPa
@@ -1261,33 +1261,16 @@
 .filter(
-%0A                
 username
@@ -1284,53 +1284,50 @@
 ame%22
-,%0A                password_hash=password_hash
+).all()%5B0%5D.password_hash%0A                )
 %0A   
@@ -1340,18 +1340,8 @@
    )
-.exists())
 %0A%0A  
@@ -1413,33 +1413,33 @@
 e2d5bc2hhMjU2JDF
-l
+L
 MmQ1Yk1USXpORFUy
@@ -1451,32 +1451,62 @@
 TI3PQ==f79127=%22%0A
+        hasher = get_hasher()%0A
         username
@@ -1632,29 +1632,59 @@
 rd_hash=
-password_hash
+hasher.encode(password_hash, hasher.salt())
 %0A       

update test cases for hashing of encoded password saved