#!/usr/bin/python3 # -*- coding: utf-8 -*- # # create certificate (use for test) # Copyright (C) 2017 AleaJactaEst # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . import unittest import tempfile import os from unittest.mock import patch try: import pymanager.password as Password except ImportError: import sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import pymanager.password as Password class TestPassword(unittest.TestCase): def setUp(self): pass def test_init_without_password_but_send(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() with self.assertRaises(Exception): password = Password.PasswordFile(pwdfile.name, False, False, 'username', 'password') password.verify() def test_init_missing_password(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() with self.assertRaises(Exception): password = Password.PasswordFile(pwdfile.name, False, True, 'username', None) password.verify() def test_init_without_password(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() password = Password.PasswordFile(pwdfile.name, False, False, 'username', None) def test_init_create_file(self): tmpDir = tempfile.mkdtemp(prefix='password-') pwdfile = os.path.join(tmpDir, 'temp') password = Password.PasswordFile(pwdfile , True, True, 'username', 'password') def test_init_create_file_impossible(self): tmpDir = tempfile.mkdtemp(prefix='password-') pwdfile = os.path.join('not', 'exist', 'directory', 'temp') with self.assertRaises(FileNotFoundError): password = Password.PasswordFile(pwdfile , True, True, 'username', 'password') def test_init_create_file_impossible(self): tmpDir = tempfile.mkdtemp(prefix='password-') pwdfile = os.path.join('not', 'exist', 'directory', 'temp') with self.assertRaises(Exception): password = Password.PasswordFile(pwdfile , False, True, 'username', 'password') def test_verify(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() password = Password.PasswordFile(pwdfile.name, False, True, 'username', 'password') password.verify() def test_verify_bad_password(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() with self.assertRaises(Exception): password = Password.PasswordFile(pwdfile.name, False, True, 'username', 'BadPassword') password.verify() def test_save(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() password = Password.PasswordFile(pwdfile.name, False, True, 'MyAccount', 'MyPassword') password.save() def test_update(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() password = Password.PasswordFile(pwdfile.name, False, True, 'username', 'MyPassword2') password.update() password.save() password = Password.PasswordFile(pwdfile.name, False, True, 'username', 'MyPassword2') password.verify() with self.assertRaises(Exception): password = Password.PasswordFile(pwdfile.name, False, True, 'username', 'BadPassword') password.verify() def test_delete(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.write('username2:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() password = Password.PasswordFile(pwdfile.name, False, True, 'username', 'MyPassword') password.delete() def test_run_update(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() Password.root(pwdfile.name, False, True, 'username', 'password', False, False) def test_run_verify(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() Password.root(pwdfile.name, False, True, 'username', 'password', False, True) def test_run_verify_bad_password(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() with self.assertRaises(Exception): Password.root(pwdfile.name, False, True, 'username', 'BadPassword', False, True) def test_run_verify_bad_account(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() with self.assertRaises(KeyError): Password.root(pwdfile.name, False, True, 'BadUsername', 'password', False, True) def test_run_delete(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() Password.root(pwdfile.name, False, True, 'username', 'password', True, False) def test_main(self): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() args = ['-v', '-b', pwdfile.name, 'username', 'password'] print(args) Password.main(args=args) @patch("getpass.getpass") def test_run_verify_stdin_password(self, getpass): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() getpass.return_value = "password" password = Password.PasswordFile(pwdfile.name, False, False, 'username', None) password.verify() @patch("getpass.getpass") def test_run_update_stdin_password(self, getpass): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() getpass.return_value = "password" password = Password.PasswordFile(pwdfile.name, False, False, 'username', None) password.update() @patch("getpass.getpass") def test_run_update_stdin_password2(self, getpass): pwdfile = tempfile.NamedTemporaryFile(suffix="passwordfile.tmp", mode='w+t') pwdfile.write('username:$2a$12$2C97xW0KC/vFp3YyjlOgU.fWXJ3EiGT2Ihb0SWN9Mw0XI4WngiUqS\n\n') pwdfile.flush() getpass.side_effect = ["password", "password2"] password = Password.PasswordFile(pwdfile.name, False, False, 'username', None) with self.assertRaises(Exception): password.update() if __name__ == '__main__': unittest.main()