From 4968398d4fd1e0cc900f89ba51c5b640d1d42980 Mon Sep 17 00:00:00 2001 From: Demian Katz Date: Wed, 3 Jan 2024 15:54:49 -0500 Subject: [PATCH] Modernize tests. --- .../responses/check-invalid-token-legacy.json | 25 +++++++ .../folio/responses/check-invalid-token.json | 4 +- .../folio/responses/get-tokens-legacy.json | 16 +++++ .../folio/responses/get-tokens-rtr.json | 16 ----- .../fixtures/folio/responses/get-tokens.json | 4 +- ...essful-patron-login-with-okapi-legacy.json | 19 +++++ .../successful-patron-login-with-okapi.json | 2 +- .../src/VuFindTest/ILS/Driver/FolioTest.php | 72 +++++++++++++++---- 8 files changed, 122 insertions(+), 36 deletions(-) create mode 100644 module/VuFind/tests/fixtures/folio/responses/check-invalid-token-legacy.json create mode 100644 module/VuFind/tests/fixtures/folio/responses/get-tokens-legacy.json delete mode 100644 module/VuFind/tests/fixtures/folio/responses/get-tokens-rtr.json create mode 100644 module/VuFind/tests/fixtures/folio/responses/successful-patron-login-with-okapi-legacy.json diff --git a/module/VuFind/tests/fixtures/folio/responses/check-invalid-token-legacy.json b/module/VuFind/tests/fixtures/folio/responses/check-invalid-token-legacy.json new file mode 100644 index 00000000000..9a7322416a6 --- /dev/null +++ b/module/VuFind/tests/fixtures/folio/responses/check-invalid-token-legacy.json @@ -0,0 +1,25 @@ +[ + { + "comment": "simulate bad token", + "expectedPath": "/users", + "status": 400 + }, + { + "comment": "send new token", + "expectedMethod": "POST", + "expectedPath": "/authn/login", + "expectedParams": "{\"tenant\":\"legacy_tenant\",\"username\":\"config_username\",\"password\":\"config_password\"}", + "headers": { "X-Okapi-Token": "x-okapi-token-after-invalid" } + }, + { + "comment": "confirm that new token is used", + "expectedPath": "/service-points", + "expectedHeaders": { "X-Okapi-Token": "x-okapi-token-after-invalid" }, + "expectedParams": { + "query": "pickupLocation=true", + "offset": 0, + "limit": 1000 + }, + "body": "{ \"servicepoints\": [] }" + } +] diff --git a/module/VuFind/tests/fixtures/folio/responses/check-invalid-token.json b/module/VuFind/tests/fixtures/folio/responses/check-invalid-token.json index 0dc0a76b216..ac97fec04fc 100644 --- a/module/VuFind/tests/fixtures/folio/responses/check-invalid-token.json +++ b/module/VuFind/tests/fixtures/folio/responses/check-invalid-token.json @@ -7,9 +7,9 @@ { "comment": "send new token", "expectedMethod": "POST", - "expectedPath": "/authn/login", + "expectedPath": "/authn/login-with-expiry", "expectedParams": "{\"tenant\":\"config_tenant\",\"username\":\"config_username\",\"password\":\"config_password\"}", - "headers": { "X-Okapi-Token": "x-okapi-token-after-invalid" } + "headers": { "Set-Cookie": "folioAccessToken=x-okapi-token-after-invalid; Max-Age=600; Expires=Fri, 22 Sep 2023 14:30:10 GMT; Path=/; Secure; HTTPOnly; SameSite=None" } }, { "comment": "confirm that new token is used", diff --git a/module/VuFind/tests/fixtures/folio/responses/get-tokens-legacy.json b/module/VuFind/tests/fixtures/folio/responses/get-tokens-legacy.json new file mode 100644 index 00000000000..4f96adb556f --- /dev/null +++ b/module/VuFind/tests/fixtures/folio/responses/get-tokens-legacy.json @@ -0,0 +1,16 @@ +[ + { + "comment": "Initial request for token", + "expectedMethod": "POST", + "expectedParams": "{\"tenant\":\"legacy_tenant\",\"username\":\"config_username\",\"password\":\"config_password\"}", + "expectedPath": "/authn/login", + "expectedHeaders": { "X-Okapi-Tenant": "legacy_tenant" }, + "headers": { "X-Okapi-Token": "x-okapi-token-config-tenant" } + }, + { + "expectedPath": "/users", + "expectedParams": { "query": "id == \"whatever\"" }, + "expectedHeaders": { "X-Okapi-Token": "x-okapi-token-config-tenant" }, + "body": "{ \"users\": [ { \"id\": \"id\" } ] }" + } +] diff --git a/module/VuFind/tests/fixtures/folio/responses/get-tokens-rtr.json b/module/VuFind/tests/fixtures/folio/responses/get-tokens-rtr.json deleted file mode 100644 index 7c3e498c242..00000000000 --- a/module/VuFind/tests/fixtures/folio/responses/get-tokens-rtr.json +++ /dev/null @@ -1,16 +0,0 @@ -[ - { - "comment": "Initial request for token", - "expectedMethod": "POST", - "expectedParams": "{\"tenant\":\"rtr_tenant\",\"username\":\"config_username\",\"password\":\"config_password\"}", - "expectedPath": "/authn/login-with-expiry", - "expectedHeaders": { "X-Okapi-Tenant": "rtr_tenant" }, - "headers": { "Set-Cookie": "folioAccessToken=x-okapi-token-config-tenant; Max-Age=600; Expires=Fri, 22 Sep 2023 14:30:10 GMT; Path=/; Secure; HTTPOnly; SameSite=None" } - }, - { - "expectedPath": "/users", - "expectedParams": { "query": "id == \"whatever\"" }, - "expectedHeaders": { "X-Okapi-Token": "x-okapi-token-config-tenant" }, - "body": "{ \"users\": [ { \"id\": \"id\" } ] }" - } -] diff --git a/module/VuFind/tests/fixtures/folio/responses/get-tokens.json b/module/VuFind/tests/fixtures/folio/responses/get-tokens.json index e969a1892dc..cf9894f29be 100644 --- a/module/VuFind/tests/fixtures/folio/responses/get-tokens.json +++ b/module/VuFind/tests/fixtures/folio/responses/get-tokens.json @@ -3,9 +3,9 @@ "comment": "Initial request for token", "expectedMethod": "POST", "expectedParams": "{\"tenant\":\"config_tenant\",\"username\":\"config_username\",\"password\":\"config_password\"}", - "expectedPath": "/authn/login", + "expectedPath": "/authn/login-with-expiry", "expectedHeaders": { "X-Okapi-Tenant": "config_tenant" }, - "headers": { "X-Okapi-Token": "x-okapi-token-config-tenant" } + "headers": { "Set-Cookie": "folioAccessToken=x-okapi-token-config-tenant; Max-Age=600; Expires=Fri, 22 Sep 2023 14:30:10 GMT; Path=/; Secure; HTTPOnly; SameSite=None" } }, { "expectedPath": "/users", diff --git a/module/VuFind/tests/fixtures/folio/responses/successful-patron-login-with-okapi-legacy.json b/module/VuFind/tests/fixtures/folio/responses/successful-patron-login-with-okapi-legacy.json new file mode 100644 index 00000000000..fade83cc077 --- /dev/null +++ b/module/VuFind/tests/fixtures/folio/responses/successful-patron-login-with-okapi-legacy.json @@ -0,0 +1,19 @@ +[ + { + "comment": "Initial token check" + }, + { + "comment": "login attempt", + "expectedMethod": "POST", + "expectedPath": "/authn/login", + "expectedParams": "{\"tenant\":\"legacy_tenant\",\"username\":\"foo\",\"password\":\"bar\"}" + }, + { + "comment": "profile retrieval", + "expectedPath": "/users", + "expectedParams": { + "query": "username == foo" + }, + "body": "{ \"users\": [ { \"id\": \"fake-id\", \"personal\": { \"firstName\": \"first\", \"lastName\": \"last\", \"email\": \"fake@fake.com\" } } ] }" + } +] diff --git a/module/VuFind/tests/fixtures/folio/responses/successful-patron-login-with-okapi.json b/module/VuFind/tests/fixtures/folio/responses/successful-patron-login-with-okapi.json index 725eae82648..717c7f64540 100644 --- a/module/VuFind/tests/fixtures/folio/responses/successful-patron-login-with-okapi.json +++ b/module/VuFind/tests/fixtures/folio/responses/successful-patron-login-with-okapi.json @@ -5,7 +5,7 @@ { "comment": "login attempt", "expectedMethod": "POST", - "expectedPath": "/authn/login", + "expectedPath": "/authn/login-with-expiry", "expectedParams": "{\"tenant\":\"config_tenant\",\"username\":\"foo\",\"password\":\"bar\"}" }, { diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/ILS/Driver/FolioTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/ILS/Driver/FolioTest.php index 9560bf458ff..0aadfaf5270 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/ILS/Driver/FolioTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/ILS/Driver/FolioTest.php @@ -1,11 +1,11 @@ 'config_tenant', 'username' => 'config_username', 'password' => 'config_password', - 'legacy_authentication' => 'true', ], ]; @@ -191,14 +190,9 @@ protected function createConnector(string $test, array $config = null): void * * @return void */ - public function testTokensWithExpiry(): void + public function testTokens(): void { - // Take default configuration, but use a different tenant (to avoid - // session collision with other tests) and disable legacy authentication: - $config = $this->defaultDriverConfig; - $config['API']['tenant'] = 'rtr_tenant'; - $config['API']['legacy_authentication'] = 0; - $this->createConnector('get-tokens-rtr', $config); // saves to $this->driver + $this->createConnector('get-tokens'); // saves to $this->driver $this->driver->getMyProfile(['id' => 'whatever']); } @@ -207,9 +201,14 @@ public function testTokensWithExpiry(): void * * @return void */ - public function testTokens(): void + public function testTokensWithLegacyAuth(): void { - $this->createConnector('get-tokens'); // saves to $this->driver + // Take default configuration, but use a different tenant (to avoid + // session collision with other tests) and disable legacy authentication: + $config = $this->defaultDriverConfig; + $config['API']['tenant'] = 'legacy_tenant'; + $config['API']['legacy_authentication'] = 1; + $this->createConnector('get-tokens-legacy', $config); // saves to $this->driver $this->driver->getMyProfile(['id' => 'whatever']); } @@ -225,7 +224,7 @@ public function testCheckValidToken(): void } /** - * Check and renew an invalid token retrieved from session cache + * Check and renew an invalid token retrieved from session cache (RTR authentication) * * @return void */ @@ -235,6 +234,22 @@ public function testCheckInvalidToken(): void $this->driver->getPickupLocations(['username' => 'whatever']); } + /** + * Check and renew an invalid token retrieved from session cache (legacy authentication) + * + * @return void + */ + public function testCheckInvalidTokenLegacyAuth(): void + { + // Take default configuration, but use a different tenant (to avoid + // session collision with other tests) and disable legacy authentication: + $config = $this->defaultDriverConfig; + $config['API']['tenant'] = 'legacy_tenant'; + $config['API']['legacy_authentication'] = 1; + $this->createConnector('check-invalid-token-legacy', $config); + $this->driver->getPickupLocations(['username' => 'whatever']); + } + /** * Confirm that cancel holds validates the current patron. * @@ -283,7 +298,7 @@ public function testUnsuccessfulPatronLogin(): void } /** - * Test patron login with Okapi + * Test patron login with Okapi (RTR authentication) * * @return void */ @@ -306,6 +321,33 @@ public function testSuccessfulPatronLoginWithOkapi(): void $this->assertEquals($expected, $result); } + /** + * Test patron login with Okapi (Legacy authentication) + * + * @return void + */ + public function testSuccessfulPatronLoginWithOkapiLegacyAuth(): void + { + $config = $this->defaultDriverConfig; + $config['API']['tenant'] = 'legacy_tenant'; + $config['API']['legacy_authentication'] = 1; + $this->createConnector( + 'successful-patron-login-with-okapi-legacy', + $config + ['User' => ['okapi_login' => true]] + ); + $result = $this->driver->patronLogin('foo', 'bar'); + $expected = [ + 'id' => 'fake-id', + 'username' => 'foo', + 'cat_username' => 'foo', + 'cat_password' => 'bar', + 'firstname' => 'first', + 'lastname' => 'last', + 'email' => 'fake@fake.com', + ]; + $this->assertEquals($expected, $result); + } + /** * Test successful place hold *