From c5910adbe9bb2091c1678b152518a701f2495ddc Mon Sep 17 00:00:00 2001 From: jclausen Date: Mon, 15 Apr 2024 14:42:48 -0400 Subject: [PATCH] implements the BIF SessionInvalidate --- .../bifs/global/system/SessionInvalidate.java | 52 ++++++++++++ .../global/system/SessionInvalidateTest.java | 80 +++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/main/java/ortus/boxlang/runtime/bifs/global/system/SessionInvalidate.java create mode 100644 src/test/java/ortus/boxlang/runtime/bifs/global/system/SessionInvalidateTest.java diff --git a/src/main/java/ortus/boxlang/runtime/bifs/global/system/SessionInvalidate.java b/src/main/java/ortus/boxlang/runtime/bifs/global/system/SessionInvalidate.java new file mode 100644 index 000000000..0e35da500 --- /dev/null +++ b/src/main/java/ortus/boxlang/runtime/bifs/global/system/SessionInvalidate.java @@ -0,0 +1,52 @@ + +/** + * [BoxLang] + * + * Copyright [2023] [Ortus Solutions, Corp] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ortus.boxlang.runtime.bifs.global.system; + +import ortus.boxlang.runtime.bifs.BIF; +import ortus.boxlang.runtime.bifs.BoxBIF; +import ortus.boxlang.runtime.context.IBoxContext; +import ortus.boxlang.runtime.context.RequestBoxContext; +import ortus.boxlang.runtime.scopes.ArgumentsScope; + +@BoxBIF + +public class SessionInvalidate extends BIF { + + /** + * Constructor + */ + public SessionInvalidate() { + super(); + } + + /** + * Describe what the invocation of your bif function does + * + * @param context The context in which the BIF is being invoked. + * @param arguments Argument scope for the BIF. + * + * @argument.foo Describe any expected arguments + */ + public Object _invoke( IBoxContext context, ArgumentsScope arguments ) { + context.getParentOfType( RequestBoxContext.class ).resetSession(); + return null; + } + +} diff --git a/src/test/java/ortus/boxlang/runtime/bifs/global/system/SessionInvalidateTest.java b/src/test/java/ortus/boxlang/runtime/bifs/global/system/SessionInvalidateTest.java new file mode 100644 index 000000000..d5f8f9362 --- /dev/null +++ b/src/test/java/ortus/boxlang/runtime/bifs/global/system/SessionInvalidateTest.java @@ -0,0 +1,80 @@ + +/** + * [BoxLang] + * + * Copyright [2023] [Ortus Solutions, Corp] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ortus.boxlang.runtime.bifs.global.system; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import ortus.boxlang.runtime.BoxRuntime; +import ortus.boxlang.runtime.context.IBoxContext; +import ortus.boxlang.runtime.context.ScriptingRequestBoxContext; +import ortus.boxlang.runtime.scopes.IScope; +import ortus.boxlang.runtime.scopes.Key; +import ortus.boxlang.runtime.scopes.VariablesScope; +import ortus.boxlang.runtime.types.IStruct; + +public class SessionInvalidateTest { + + static BoxRuntime instance; + IBoxContext context; + IScope variables; + static Key result = new Key( "result" ); + + @BeforeAll + public static void setUp() { + instance = BoxRuntime.getInstance( true ); + } + + @AfterAll + public static void teardown() { + } + + @BeforeEach + public void setupEach() { + context = new ScriptingRequestBoxContext( instance.getRuntimeContext() ); + variables = context.getScopeNearby( VariablesScope.name ); + } + + @DisplayName( "It tests the BIF SessionInvalidate" ) + @Test + public void testBif() { + instance.executeSource( + """ + application name="unit-test-sm" sessionmanagement="true"; + session.foo = "bar"; + initialSession = duplicate( session ); + SessionInvalidate(); + result = session; + """, + context ); + IStruct initialSession = variables.getAsStruct( Key.of( "initialSession" ) ); + assertFalse( variables.getAsStruct( result ).containsKey( Key.of( "foo" ) ) ); + assertNotEquals( initialSession.getAsString( Key.of( "cfid" ) ), variables.getAsStruct( result ).getAsString( Key.of( "cfid" ) ) ); + assertNotEquals( initialSession.getAsDateTime( Key.of( "timeCreated" ) ), variables.getAsStruct( result ).getAsDateTime( Key.of( "timeCreated" ) ) ); + assertNotEquals( initialSession.getAsString( Key.of( "sessionid" ) ), variables.getAsStruct( result ).getAsString( Key.of( "sessionid" ) ) ); + } + +}