class TestSecretManager(fake_filesystem_unittest.TestCase):
    def setUp(self):
        self.setUpPyfakefs()
        os.makedirs(TELESERVER_DIR)
        self.manager = SecretManager()

    def test_empty_secret_file(self):
        self.manager.save_secrets()
        secrets = configparser.ConfigParser()
        secrets.read(f'{TELESERVER_DIR}/IoT_secret.ini')
        self.assertIn('THERMAL_CAMERA', secrets)
        self.assertIn('KEY', secrets)
        self.assertIn('key', secrets['KEY'])
        self.assertNotEqual(secrets['KEY']['key'], '')
        self.assertIsInstance(secrets['KEY']['key'], str)
        self.assertEqual(secrets['THERMAL_CAMERA'], {})

    def test_get_secret_key(self):
        self.assertNotEqual(self.manager.get_secret_key(), '')
        self.assertIsInstance(self.manager.get_secret_key(), str)

    def test_create_secrets_for_thermal_camera(self):
        self.manager.create_secrets_for_thermal_camera('test', 'secret',
                                                       '127.0.0.1', '1')
        secrets = configparser.ConfigParser()
        secrets.read(f'{TELESERVER_DIR}/IoT_secret.ini')
        self.assertEqual('test', secrets['THERMAL_CAMERA']['login'])
        self.assertNotEqual('secret', secrets['THERMAL_CAMERA']['password'])
        self.assertEqual('127.0.0.1', secrets['THERMAL_CAMERA']['ip_address'])
        self.assertEqual('1', secrets['THERMAL_CAMERA']['channel'])
        decrypted_password = self.manager.decrypt(
            secrets['KEY']['key'], secrets['THERMAL_CAMERA']['password'])
        self.assertEqual('secret', decrypted_password)

    def test_thermal_camera_credentials(self):
        self.manager.create_secrets_for_thermal_camera('test', 'secret',
                                                       '127.0.0.1', '1')
        out = self.manager.thermal_camera_credentials()
        self.assertIn('login', out)
        self.assertIn('password', out)
        self.assertIn('ip_address', out)
        self.assertIn('channel', out)
        self.assertEqual(out['login'], 'test')
        self.assertEqual(out['password'], 'secret')
        self.assertEqual(out['ip_address'], '127.0.0.1')
        self.assertEqual(out['channel'], '1')
Esempio n. 2
0
class TestSecretManager(fake_filesystem_unittest.TestCase):
    def setUp(self):
        self.setUpPyfakefs()
        os.makedirs(TELESERVER_DIR)
        self.manager = SecretManager()

    def test_empty_secret_file(self):
        self.manager.save_secrets()
        secrets = configparser.ConfigParser()
        secrets.read(f'{TELESERVER_DIR}/secret.ini')
        self.assertIn('PASS', secrets)
        self.assertIn('SERVICE_PRINCIPAL', secrets)
        self.assertIn('TOKEN_COOKIES', secrets)
        self.assertIn('KEY', secrets)
        self.assertIn('key', secrets['KEY'])
        self.assertNotEqual(secrets['KEY']['key'], '')
        self.assertIsInstance(secrets['KEY']['key'], str)
        self.assertEqual(secrets['PASS'], {})
        self.assertEqual(secrets['SERVICE_PRINCIPAL'], {})
        self.assertEqual(secrets['TOKEN_COOKIES'], {})

    def test_get_secret_key(self):
        self.assertNotEqual(self.manager.get_secret_key(), '')
        self.assertIsInstance(self.manager.get_secret_key(), str)

    def test_set_gui_credentials_for_one_user(self):
        self.manager.set_gui_credentials(user='******', password='******')
        secrets = configparser.ConfigParser()
        secrets.read(f'{TELESERVER_DIR}/secret.ini')
        self.assertIn('test', secrets['PASS'])
        decrypted_password = self.manager.decrypt(secrets['KEY']['key'],
                                                  secrets['PASS']['test'])
        self.assertEqual('secret', decrypted_password)

    def test_set_gui_credentials_for_multiple_user(self):
        self.manager.set_gui_credentials(user='******', password='******')
        self.manager.set_gui_credentials(user='******', password='******')
        secrets = configparser.ConfigParser()
        secrets.read(f'{TELESERVER_DIR}/secret.ini')
        self.assertIn('test', secrets['PASS'])
        decrypted_password = self.manager.decrypt(secrets['KEY']['key'],
                                                  secrets['PASS']['test'])
        self.assertEqual('secret', decrypted_password)
        self.assertIn('test2', secrets['PASS'])
        decrypted_password = self.manager.decrypt(secrets['KEY']['key'],
                                                  secrets['PASS']['test2'])
        self.assertEqual('secret2', decrypted_password)

    def test_verify_credentials(self):
        self.manager.set_gui_credentials(user='******', password='******')
        self.manager.set_gui_credentials(user='******', password='******')
        self.assertTrue(
            self.manager.verify_credentials(user='******', password='******'))
        self.assertTrue(
            self.manager.verify_credentials(user='******', password='******'))
        self.assertFalse(
            self.manager.verify_credentials(user='******', password='******'))
        self.assertFalse(
            self.manager.verify_credentials(user='******', password='******'))

    def test_delete_credentials_for_user(self):
        self.manager.set_gui_credentials(user='******', password='******')
        self.assertTrue(
            self.manager.verify_credentials(user='******', password='******'))
        self.manager.delete_credentails_for_user(user='******')
        self.assertFalse(
            self.manager.verify_credentials(user='******', password='******'))

    def test_get_credentials_fot_GUI(self):
        self.manager.set_gui_credentials(user='******', password='******')
        self.manager.set_gui_credentials(user='******', password='******')
        output = {'test': 'secret', 'test2': 'secret2'}
        self.assertDictEqual(self.manager.get_credentials_for_GUI(), output)

    def test_create_service_principal(self):
        token = self.manager.create_service_principal(name='test')
        self.assertNotEqual(token, '')
        self.assertIsInstance(token, str)
        secrets = configparser.ConfigParser()
        secrets.read(f'{TELESERVER_DIR}/secret.ini')
        self.assertIn('test', secrets['SERVICE_PRINCIPAL'])
        self.assertNotEqual(secrets['SERVICE_PRINCIPAL']['test'], '')
        self.assertIsInstance(secrets['SERVICE_PRINCIPAL']['test'], str)

    def test_create_time_token_empty_data(self):
        self.manager.set_gui_credentials(user='******', password='******')
        test_data = {}
        out = self.manager.create_time_token(test_data)
        self.assertEqual(out['rc'], 1)

    def test_create_time_token_empty_password(self):
        self.manager.set_gui_credentials(user='******', password='******')
        test_data = {'user': '******'}
        out = self.manager.create_time_token(test_data)
        self.assertEqual(out['rc'], 1)

    def test_create_time_token_empty_lease(self):
        self.manager.set_gui_credentials(user='******', password='******')
        test_data = {'user': '******', 'password': '******'}
        out = self.manager.create_time_token(test_data)
        self.assertEqual(out['rc'], 1)

    def test_create_time_token_partial_empty_lease(self):
        self.manager.set_gui_credentials(user='******', password='******')
        test_data = {
            'user': '******',
            'password': '******',
            'lease_days': '3',
            'lease_hours': '0'
        }
        out = self.manager.create_time_token(test_data)
        self.assertEqual(out['rc'], 1)

    def test_create_time_token_correct_data(self):
        self.manager.set_gui_credentials(user='******', password='******')
        test_data = {
            'user': '******',
            'password': '******',
            'lease_days': '3',
            'lease_hours': '0',
            'lease_minutes': '0',
            'lease_seconds': '0'
        }
        out = self.manager.create_time_token(test_data)
        self.assertEqual(out['rc'], 0)
        secrets = configparser.ConfigParser()
        secrets.read(f'{TELESERVER_DIR}/secret.ini')
        self.assertEqual(len(secrets['TOKEN_COOKIES']), 1)
        test_data = {
            'user': '******',
            'password': '******',
            'lease_days': '0',
            'lease_hours': '1',
            'lease_minutes': '30',
            'lease_seconds': '0'
        }
        out = self.manager.create_time_token(test_data)
        self.assertEqual(out['rc'], 0)
        secrets = configparser.ConfigParser()
        secrets.read(f'{TELESERVER_DIR}/secret.ini')
        self.assertEqual(len(secrets['TOKEN_COOKIES']), 2)

    def test_delete_time_token(self):
        self.manager.set_gui_credentials(user='******', password='******')
        test_data = {
            'user': '******',
            'password': '******',
            'lease_days': '3',
            'lease_hours': '0'
        }
        self.manager.create_time_token(test_data)
        out = self.manager.delete_time_token({'token_name': 'testuser'})
        self.assertEqual(out['rc'], 0)