|
Pierre-Yves Chibon |
2e4286 |
# -*- coding: utf-8 -*-
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
"""
|
|
Pierre-Yves Chibon |
2e4286 |
(c) 2016 - Copyright Red Hat Inc
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
Authors:
|
|
Pierre-Yves Chibon |
2e4286 |
Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
|
|
farhaanbukhsh |
96d198 |
Farhaan Bukhsh <farhaan.bukhsh@gmail.com></farhaan.bukhsh@gmail.com>
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
"""
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
__requires__ = ['SQLAlchemy >= 0.8']
|
|
Pierre-Yves Chibon |
2e4286 |
import pkg_resources
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
import unittest
|
|
Pierre-Yves Chibon |
2e4286 |
import shutil
|
|
Pierre-Yves Chibon |
2e4286 |
import sys
|
|
Pierre-Yves Chibon |
2e4286 |
import os
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
from mock import patch
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
sys.path.insert(0, os.path.join(os.path.dirname(
|
|
Pierre-Yves Chibon |
2e4286 |
os.path.abspath(__file__)), '..'))
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
import pagure.lib
|
|
farhaanbukhsh |
355681 |
from pagure.exceptions import PagureException
|
|
Pierre-Yves Chibon |
2e4286 |
import pagure.lib.login
|
|
farhaanbukhsh |
0a64a5 |
import hashlib
|
|
farhaanbukhsh |
0a64a5 |
from pagure import APP
|
|
Pierre-Yves Chibon |
2e4286 |
import tests
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
class PagureLibLogintests(tests.Modeltests):
|
|
Pierre-Yves Chibon |
2e4286 |
""" Tests for pagure.lib.login """
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
def test_id_generator(self):
|
|
Pierre-Yves Chibon |
2e4286 |
''' Test pagure.lib.login.id_generator. '''
|
|
Pierre-Yves Chibon |
2e4286 |
self.assertEqual(
|
|
Pierre-Yves Chibon |
2e4286 |
pagure.lib.login.id_generator(size=3, chars=['a']),
|
|
Pierre-Yves Chibon |
2e4286 |
'aaa'
|
|
Pierre-Yves Chibon |
2e4286 |
)
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
def test_get_users_by_group(self):
|
|
Pierre-Yves Chibon |
2e4286 |
''' Test pagure.lib.login.get_users_by_group. '''
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
users = pagure.lib.login.get_users_by_group(self.session, 'foo')
|
|
Pierre-Yves Chibon |
2e4286 |
self.assertEqual(users, [])
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
def test_get_session_by_visitkey(self):
|
|
Pierre-Yves Chibon |
2e4286 |
''' Test pagure.lib.login.get_session_by_visitkey. '''
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
session = pagure.lib.login.get_session_by_visitkey(self.session, 'foo')
|
|
Pierre-Yves Chibon |
2e4286 |
self.assertEqual(session, None)
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
3e58b0 |
def test_generate_hashed_value(self):
|
|
Pierre-Yves Chibon |
3e58b0 |
''' Test pagure.lib.login.generate_hashed_value. '''
|
|
Pierre-Yves Chibon |
3e58b0 |
password = pagure.lib.login.generate_hashed_value('foo')
|
|
Pierre-Yves Chibon |
3e58b0 |
self.assertTrue(password.startswith('$2$'))
|
|
Pierre-Yves Chibon |
3e58b0 |
self.assertEqual(len(password), 63)
|
|
Pierre-Yves Chibon |
3e58b0 |
|
|
Pierre-Yves Chibon |
3e58b0 |
def test_check_password(self):
|
|
Pierre-Yves Chibon |
3e58b0 |
''' Test pagure.lib.login.check_password. '''
|
|
Pierre-Yves Chibon |
3e58b0 |
|
|
Pierre-Yves Chibon |
a1412f |
# Version 2
|
|
Pierre-Yves Chibon |
3e58b0 |
password = pagure.lib.login.generate_hashed_value('foo')
|
|
Pierre-Yves Chibon |
3e58b0 |
self.assertTrue(
|
|
Pierre-Yves Chibon |
a1412f |
pagure.lib.login.check_password('foo', password))
|
|
Pierre-Yves Chibon |
a1412f |
self.assertFalse(
|
|
Pierre-Yves Chibon |
a1412f |
pagure.lib.login.check_password('bar', password))
|
|
Pierre-Yves Chibon |
a1412f |
|
|
Pierre-Yves Chibon |
a1412f |
# Version 1
|
|
farhaanbukhsh |
0a64a5 |
password = '%s%s' % ('foo', APP.config.get('PASSWORD_SEED', None))
|
|
farhaanbukhsh |
355681 |
password = '$1$' + hashlib.sha512(password).hexdigest()
|
|
Pierre-Yves Chibon |
a1412f |
self.assertTrue(pagure.lib.login.check_password('foo', password))
|
|
Pierre-Yves Chibon |
a1412f |
self.assertFalse(pagure.lib.login.check_password('bar', password))
|
|
Pierre-Yves Chibon |
a1412f |
|
|
Pierre-Yves Chibon |
a1412f |
# Invalid password - No version
|
|
farhaanbukhsh |
355681 |
password = '%s%s' % ('foo', APP.config.get('PASSWORD_SEED', None))
|
|
farhaanbukhsh |
355681 |
password = hashlib.sha512(password).hexdigest()
|
|
Pierre-Yves Chibon |
a1412f |
self.assertRaises(
|
|
Pierre-Yves Chibon |
a1412f |
PagureException,
|
|
Pierre-Yves Chibon |
a1412f |
pagure.lib.login.check_password,
|
|
Pierre-Yves Chibon |
a1412f |
'foo', password
|
|
Pierre-Yves Chibon |
a1412f |
)
|
|
Pierre-Yves Chibon |
a1412f |
|
|
Pierre-Yves Chibon |
a1412f |
# Invalid password - Invalid version
|
|
farhaanbukhsh |
355681 |
password = '$3$' + password
|
|
Pierre-Yves Chibon |
a1412f |
self.assertRaises(
|
|
Pierre-Yves Chibon |
a1412f |
PagureException,
|
|
Pierre-Yves Chibon |
a1412f |
pagure.lib.login.check_password,
|
|
Pierre-Yves Chibon |
a1412f |
'foo',
|
|
Pierre-Yves Chibon |
a1412f |
password
|
|
Pierre-Yves Chibon |
a1412f |
)
|
|
farhaanbukhsh |
0fc6df |
password = '%s%s' % ('foo', APP.config.get('PASSWORD_SEED', None))
|
|
farhaanbukhsh |
0fc6df |
password = hashlib.sha512(password).hexdigest()
|
|
Pierre-Yves Chibon |
375407 |
self.assertRaises(
|
|
Pierre-Yves Chibon |
375407 |
PagureException,
|
|
Pierre-Yves Chibon |
375407 |
pagure.lib.login.check_password,
|
|
Pierre-Yves Chibon |
375407 |
'foo', password
|
|
Pierre-Yves Chibon |
375407 |
)
|
|
Pierre-Yves Chibon |
375407 |
|
|
Pierre-Yves Chibon |
375407 |
# Invalid password - Invalid version
|
|
farhaanbukhsh |
0fc6df |
password = '$3$' + password
|
|
Pierre-Yves Chibon |
375407 |
self.assertRaises(
|
|
Pierre-Yves Chibon |
375407 |
PagureException,
|
|
Pierre-Yves Chibon |
375407 |
pagure.lib.login.check_password,
|
|
Pierre-Yves Chibon |
375407 |
'foo',
|
|
Pierre-Yves Chibon |
375407 |
password
|
|
Pierre-Yves Chibon |
375407 |
)
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
if __name__ == '__main__':
|
|
Pierre-Yves Chibon |
2e4286 |
SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureLibLogintests)
|
|
Pierre-Yves Chibon |
2e4286 |
unittest.TextTestRunner(verbosity=2).run(SUITE)
|