async def test_logout_unauthorized(mock_app_config, plugin_config): # noqa: F811 auth.init(mock_app_config.app_key(), mock_app_config.server.auth) context = _event_context(mock_app_config, plugin_config) context.auth_info['auth_type'] = "UNKNOWN" with pytest.raises(Unauthorized): await execute_flow(context)
def test_token_lifecycle(mock_app_config): # noqa: F811 context = _setup_event_context(mock_app_config) payload = { 'test': 'test_value', 'iat': datetime.now().astimezone(timezone.utc).timestamp(), 'exp': datetime.now().astimezone(timezone.utc) + timedelta(seconds=2) } token = auth.new_token(payload) assert token is not None decoded = auth.validate_token(token, context) assert decoded == payload time.sleep(3) assert auth.validate_token(token, context) is None with pytest.raises(ExpiredSignatureError): auth.decode_token(token) with pytest.raises(DecodeError): auth.decode_token('INVALID_TOKEN!!') token = auth.new_token(payload={ 'test': 'test_value', 'iat': datetime.now().astimezone(timezone.utc), 'exp': datetime.now().astimezone(timezone.utc) + timedelta(seconds=2) }) auth.init(AuthConfig( secrets_location=f"/tmp/{uuid.uuid4()}", auth_passphrase='test', enabled=True, create_keys=True )) assert auth.validate_token(token, context) is None with pytest.raises(InvalidSignatureError): auth.decode_token(token)
def _setup_client_context(app_config: AppConfig, server_app_config: AppConfig, register_client_key: bool = True) -> EventContext: _init_engine_logger(app_config) assert app_config.server assert server_app_config.server app_config.server.auth = AuthConfig( secrets_location=f"/tmp/{uuid.uuid4()}", auth_passphrase='test_passphrase', enabled=True, create_keys=True ) auth.init(app_config.app_key(), app_config.server.auth) if register_client_key: os.rename( pathlib.Path(app_config.server.auth.secrets_location) / 'public' / f'{app_config.app_key()}_pub.pem', pathlib.Path(server_app_config.server.auth.secrets_location) / 'public' / f'{app_config.app_key()}_pub.pem' ) return EventContext( app_config=app_config, plugin_config=app_config, event_name='mock_event', settings=get_event_settings(app_config.effective_settings, 'mock_event'), # type: ignore track_ids={}, auth_info={} )
async def start(self, *, config: ServerConfig): """ Starts a engine instance """ global logger logger = engine_logger().init_server(config) logger.info(__name__, 'Starting engine...') auth.init(config.auth) return self
def test_init(): config = AuthConfig( secrets_location=f"/tmp/{uuid.uuid4()}", auth_passphrase='test_passphrase', enabled=False ) auth.init("test_app", config) assert auth.private_keys == {} assert auth.public_keys == {} config.enabled = True with pytest.raises(FileNotFoundError): auth.init("test_app", config) config.create_keys = True auth.init("test_app", config) assert "test_app" in auth.private_keys assert "test_app" in auth.public_keys assert os.path.exists(pathlib.Path(config.secrets_location) / '.private' / 'test_app.pem') assert os.path.exists(pathlib.Path(config.secrets_location) / 'public' / 'test_app_pub.pem') config.create_keys = False auth.init("test_app", config) assert "test_app" in auth.private_keys assert "test_app" in auth.public_keys
def test_init(): config = AuthConfig( secrets_location=f"/tmp/{uuid.uuid4()}", auth_passphrase='test_passphrase', enabled=False ) auth.init(config) assert auth.private_key is None assert auth.public_key is None config.enabled = True with pytest.raises(FileNotFoundError): auth.init(config) config.create_keys = True auth.init(config) assert auth.private_key is not None assert auth.public_key is not None assert os.path.exists(pathlib.Path(config.secrets_location) / 'key.pem') assert os.path.exists(pathlib.Path(config.secrets_location) / 'key_pub.pem') config.create_keys = False auth.init(config) assert auth.private_key is not None assert auth.public_key is not None
def _setup_event_context(mock_app_config: AppConfig) -> EventContext: # noqa: F811 _init_engine_logger(mock_app_config) assert mock_app_config.server mock_app_config.server.auth = AuthConfig( secrets_location=f"/tmp/{uuid.uuid4()}", auth_passphrase='test_passphrase', enabled=True, create_keys=True ) auth.init(mock_app_config.server.auth) return EventContext( app_config=mock_app_config, plugin_config=mock_app_config, event_name='mock_event', track_ids={}, auth_info={} )
def _setup_server_context(app_config: AppConfig) -> EventContext: _init_engine_logger(app_config) assert app_config.server app_config.server.auth = AuthConfig( secrets_location=f"/tmp/{uuid.uuid4()}", auth_passphrase='test_passphrase', enabled=True, create_keys=True ) auth.init(app_config.app_key(), app_config.server.auth) return EventContext( app_config=app_config, plugin_config=app_config, event_name='mock_event', settings=get_event_settings(app_config.effective_settings, 'mock_event'), # type: ignore track_ids={}, auth_info={} )
async def start(self): """ Starts handlers, services and pools for this application """ self.event_handler = EventHandler( app_config=self.app_config, plugins=self.plugins, effective_events=self.effective_events, settings=self.settings) streams_present = any( True for _, event_info in self.effective_events.items() if (event_info.type == EventType.STREAM) or ( event_info.write_stream is not None)) if streams_present and self.streams_enabled: mgr = StreamManager.create(self.app_config.server.streams) self.stream_manager = await mgr.connect() auth.init(self.app_key, self.app_config.server.auth) await register_app_connections(self.app_config) return self
async def test_logout(mock_app_config, plugin_config): # noqa: F811 auth.init(mock_app_config.app_key(), mock_app_config.server.auth) context = _event_context(mock_app_config, plugin_config) await execute_flow(context)