From 85e60a7212c47ad9b39e902f65ab4bf967048f97 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Thu, 1 Jun 2023 13:42:03 +0000
Subject: [PATCH] Bug 33879: Add unit test to show problem
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Test plan:
Run t/db_dependent/Auth.t without second patch.
Should fail:
    #          got: 'opac'
    #     expected: 'intranet'

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Björn Nylén <bjorn.nylen@ub.lu.se>
Signed-off-by: David Nind <david@davidnind.com>
---
 t/db_dependent/Auth.t | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/t/db_dependent/Auth.t b/t/db_dependent/Auth.t
index 5c2aad72d4..4f4ad6a78c 100755
--- a/t/db_dependent/Auth.t
+++ b/t/db_dependent/Auth.t
@@ -1,7 +1,4 @@
 #!/usr/bin/perl
-#
-# This Koha test module is a stub!  
-# Add more tests here!!!
 
 use Modern::Perl;
 
@@ -10,7 +7,7 @@ use CGI qw ( -utf8 );
 use Test::MockObject;
 use Test::MockModule;
 use List::MoreUtils qw/all any none/;
-use Test::More tests => 17;
+use Test::More tests => 18;
 use Test::Warn;
 use t::lib::Mocks;
 use t::lib::TestBuilder;
@@ -981,4 +978,28 @@ subtest 'create_basic_session tests' => sub {
     is( $session->param('interface'), 'intranet', 'Staff interface gets converted to intranet' );
 };
 
+subtest 'check_cookie_auth overwriting interface already set' => sub {
+    plan tests => 2;
+
+    t::lib::Mocks::mock_preference( 'SessionRestrictionByIP', 0 );
+
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+    my $session = C4::Auth::get_session();
+    $session->param( 'number',       $patron->id );
+    $session->param( 'id',           $patron->userid );
+    $session->param( 'ip',           '1.2.3.4' );
+    $session->param( 'lasttime',     time() );
+    $session->param( 'interface',    'opac' );
+    $session->flush;
+
+    C4::Context->interface('intranet');
+    C4::Auth::check_cookie_auth( $session->id );
+    is( C4::Context->interface, 'intranet', 'check_cookie_auth did not overwrite' );
+    delete $C4::Context::context->{interface}; # clear context interface
+    C4::Auth::check_cookie_auth( $session->id );
+    is( C4::Context->interface, 'opac', 'check_cookie_auth used interface from session when context interface was empty' );
+
+    t::lib::Mocks::mock_preference( 'SessionRestrictionByIP', 1 );
+};
+
 $schema->storage->txn_rollback;
-- 
2.30.2