Exemplo n.º 1
0
class SqliteUserManagerTest(SimpleUserManagerTest):
    file_descriptor, _test_auth_db_path = tempfile.mkstemp(suffix=".db")
    os.close(file_descriptor)
    os.unlink(_test_auth_db_path)

    def _create_test_auth_db(self, db_path, username, password):
        if os.path.exists(db_path):
            os.remove(db_path)

        salt = binascii.b2a_hex(os.urandom(8))
        crypto_hash = hashlib.sha256(username + password +
                                     salt).hexdigest() + salt

        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()

        cursor.execute("""CREATE TABLE IF NOT EXISTS auth
                       (user VARCHAR PRIMARY KEY, hash VARCHAR)""")

        cursor.execute("INSERT INTO auth VALUES (?, ?)",
                       (username, crypto_hash))

        conn.commit()
        conn.close()

    def setUp(self):
        self._create_test_auth_db(self._test_auth_db_path, self._good_test_un,
                                  self._good_test_pw)
        self._user_manager = SqliteUserManager(self._test_auth_db_path)

    def tearDown(self):
        if os.path.exists(self._test_auth_db_path):
            os.remove(self._test_auth_db_path)

    def test_authenticate(self):
        self.assertTrue(
            self._user_manager.authenticate(self._good_test_un,
                                            self._good_test_pw))

        self.assertFalse(
            self._user_manager.authenticate(self._bad_test_un,
                                            self._bad_test_pw))

        self.assertFalse(
            self._user_manager.authenticate(self._good_test_un,
                                            self._bad_test_pw))

        self.assertFalse(
            self._user_manager.authenticate(self._bad_test_un,
                                            self._good_test_pw))
Exemplo n.º 2
0
class SqliteUserManagerTest(SimpleUserManagerTest):
    file_descriptor, _test_auth_db_path = tempfile.mkstemp(suffix=".db")
    os.close(file_descriptor)
    os.unlink(_test_auth_db_path)

    def _create_test_auth_db(self, db_path, username, password):
        if os.path.exists(db_path):
            os.remove(db_path)

        salt = binascii.b2a_hex(os.urandom(8))
        crypto_hash = hashlib.sha256(username+password+salt).hexdigest()+salt

        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()

        cursor.execute("""CREATE TABLE IF NOT EXISTS auth
                       (user VARCHAR PRIMARY KEY, hash VARCHAR)""")

        cursor.execute("INSERT INTO auth VALUES (?, ?)", (username, crypto_hash))

        conn.commit()
        conn.close()

    def setUp(self):
        self._create_test_auth_db(self._test_auth_db_path,
                                  self._good_test_un,
                                  self._good_test_pw)
        self._user_manager = SqliteUserManager(self._test_auth_db_path)

    def tearDown(self):
        if os.path.exists(self._test_auth_db_path):
            os.remove(self._test_auth_db_path)

    def test_authenticate(self):
        self.assertTrue(self._user_manager.authenticate(self._good_test_un,
                                                        self._good_test_pw))

        self.assertFalse(self._user_manager.authenticate(self._bad_test_un,
                                                         self._bad_test_pw))

        self.assertFalse(self._user_manager.authenticate(self._good_test_un,
                                                         self._bad_test_pw))

        self.assertFalse(self._user_manager.authenticate(self._bad_test_un,
                                                         self._good_test_pw))
 def setUp(self):
     self._create_test_auth_db(self._test_auth_db_path, self._good_test_un, self._good_test_pw)
     self._user_manager = SqliteUserManager(self._test_auth_db_path)
Exemplo n.º 4
0
 def setUp(self):
     self._create_test_auth_db(self._test_auth_db_path, self._good_test_un,
                               self._good_test_pw)
     self._user_manager = SqliteUserManager(self._test_auth_db_path)