Example #1
0
def test_add_remove_user(mExists, mWrite):
    mExists.return_value = False
    passwordManager = password_manager.PasswordManager()
    mWrite.reset_mock()
    passwordManager.add_user('hello', 'paradrop')
    mWrite.assert_called_once()
    assert len(passwordManager.records) == 2
    mWrite.reset_mock()
    passwordManager.add_user('helloworld', 'paradrop')
    mWrite.assert_called_once()
    assert len(passwordManager.records) == 3
    # Can not add a duplicated user name
    mWrite.reset_mock()
    passwordManager.add_user('hello', 'haha')
    mWrite.assert_not_called()
    assert len(passwordManager.records) == 3

    mWrite.reset_mock()
    passwordManager.remove_user(settings.DEFAULT_PANEL_USERNAME)
    mWrite.assert_called_once()
    assert len(passwordManager.records) == 2

    mWrite.reset_mock()
    passwordManager.remove_user('notexists')
    mWrite.assert_not_called()
    assert len(passwordManager.records) == 2
Example #2
0
def test_hash_password(mExists, mWrite):
    mExists.return_value = False
    passwordManager = password_manager.PasswordManager()
    password_hash = passwordManager._hash_password('hello')
    assert password_hash.startswith('$6$')
    # 3+1+16+86
    assert len(password_hash) == 106
Example #3
0
def test_reset(mExists, mWrite):
    mExists.return_value = False
    passwordManager = password_manager.PasswordManager()
    mWrite.assert_called_once()
    mWrite.reset_mock()
    passwordManager.reset()
    mWrite.assert_called_once()
Example #4
0
def test_change_password(mExists, mWrite):
    mExists.return_value = False
    passwordManager = password_manager.PasswordManager()

    assert passwordManager.verify_password(settings.DEFAULT_PANEL_USERNAME,
                                           settings.DEFAULT_PANEL_PASSWORD)

    assert passwordManager.add_user('hello', 'password!!')
    assert passwordManager.verify_password('hello', 'password!!')

    assert passwordManager.change_password(settings.DEFAULT_PANEL_USERNAME,
                                           'haha')
    assert not passwordManager.verify_password(settings.DEFAULT_PANEL_USERNAME,
                                               settings.DEFAULT_PANEL_PASSWORD)
    assert passwordManager.verify_password(settings.DEFAULT_PANEL_USERNAME,
                                           'haha')

    assert passwordManager.change_password('hello', 'heihei')
    assert passwordManager.verify_password('hello', 'heihei')

    assert not passwordManager.change_password('notexists', 'heihei')

    passwordManager.reset()
    assert passwordManager.verify_password(settings.DEFAULT_PANEL_USERNAME,
                                           settings.DEFAULT_PANEL_PASSWORD)
Example #5
0
def test_init_with_password_file(mReadFile, mExists, mWrite):
    mExists.return_value = True
    mReadFile.return_value = [
        'paradrop:$6$zwPQnb0CjDMPQ6HH$o564TOpYc9F10Dqmpp6hhGwXaCDKJcixkKxi6BrfrA.KrBE6QPmKaLPkUay9yM.C9cl2gwRYMqJhxiUuMoYgg/'
    ]
    passwordManager = password_manager.PasswordManager()
    assert not mWrite.called, 'method should not have been called'
    assert len(passwordManager.records) == 1
    assert passwordManager.records[0]['user_name'] == 'paradrop'
    assert passwordManager.records[0][
        'password_hash'] == '$6$zwPQnb0CjDMPQ6HH$o564TOpYc9F10Dqmpp6hhGwXaCDKJcixkKxi6BrfrA.KrBE6QPmKaLPkUay9yM.C9cl2gwRYMqJhxiUuMoYgg/'
Example #6
0
def test_verify_password(mExists, mWrite):
    mExists.return_value = False
    passwordManager = password_manager.PasswordManager()

    passwordManager.verify_password(settings.DEFAULT_PANEL_USERNAME,
                                    settings.DEFAULT_PANEL_PASSWORD)

    passwordManager.add_user('hello', 'password!!')
    passwordManager.verify_password('hello', 'password!!')

    passwordManager.add_user('helloworld', 'password!!!')
    passwordManager.verify_password('helloworld', 'password!!!')
Example #7
0
def test_init_without_password_file(mExists, mWrite):
    mExists.return_value = False
    passwordManager = password_manager.PasswordManager()
    assert passwordManager.password_file == settings.CONFIG_HOME_DIR + 'password'
    mWrite.assert_called_once()
    assert len(passwordManager.records) == 1
Example #8
0
def test_generate_salt():
    passwordManager = password_manager.PasswordManager()
    salt = passwordManager._generate_salt()
    assert len(salt) == 19
    assert salt.startswith('$6$')