|
| 1 | +<?php namespace Tests; |
| 2 | +/** |
| 3 | + * Copyright 2026 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | + |
| 15 | +use Auth\Exceptions\AuthenticationException; |
| 16 | +use Auth\Repositories\IUserRepository; |
| 17 | +use Auth\User; |
| 18 | +use Illuminate\Support\Facades\Auth; |
| 19 | +use LaravelDoctrine\ORM\Facades\EntityManager; |
| 20 | +use Utils\Services\IAuthService; |
| 21 | +use Utils\Services\UtilsServiceCatalog; |
| 22 | + |
| 23 | +/** |
| 24 | + * Class AuthServiceValidateCredentialsIntegrationTest |
| 25 | + * Exercises AuthService::validateCredentials() against the real database and |
| 26 | + * security-checkpoint stack to verify that failed attempts increment the |
| 27 | + * user's login_failed_attempt counter (via LockUserCounterMeasure) and that |
| 28 | + * no session is established on either success or failure. |
| 29 | + */ |
| 30 | +final class AuthServiceValidateCredentialsIntegrationTest extends OpenStackIDBaseTestCase |
| 31 | +{ |
| 32 | + // CustomAuthProvider looks up users via IUserRepository::getByEmailOrName(), |
| 33 | + // which currently matches only on the email column — so login uses the email |
| 34 | + // as the "username". |
| 35 | + private const SEEDED_USERNAME = 'sebastian@tipit.net'; |
| 36 | + private const SEEDED_PASSWORD = '1Qaz2wsx!'; |
| 37 | + |
| 38 | + private IAuthService $auth_service; |
| 39 | + |
| 40 | + protected function prepareForTests(): void |
| 41 | + { |
| 42 | + parent::prepareForTests(); |
| 43 | + $this->auth_service = $this->app[UtilsServiceCatalog::AuthenticationService]; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * A failed validateCredentials() call must: |
| 48 | + * - throw AuthenticationException, |
| 49 | + * - NOT establish a session (Auth::check() stays false), |
| 50 | + * - trigger LockUserCounterMeasure so the user's login_failed_attempt counter increments. |
| 51 | + */ |
| 52 | + public function testFailedAttempt_incrementsLoginFailedAttemptCounter(): void |
| 53 | + { |
| 54 | + $initial_attempts = $this->getLoginFailedAttempt(self::SEEDED_USERNAME); |
| 55 | + $this->assertFalse(Auth::check(), 'precondition: no authenticated user'); |
| 56 | + |
| 57 | + $threw = false; |
| 58 | + try { |
| 59 | + $this->auth_service->validateCredentials(self::SEEDED_USERNAME, 'wrong-password'); |
| 60 | + } catch (AuthenticationException $ex) { |
| 61 | + $threw = true; |
| 62 | + } |
| 63 | + |
| 64 | + $this->assertTrue($threw, 'Expected AuthenticationException on wrong password'); |
| 65 | + $this->assertFalse(Auth::check(), 'No session should be established after a failed attempt'); |
| 66 | + |
| 67 | + $new_attempts = $this->getLoginFailedAttempt(self::SEEDED_USERNAME); |
| 68 | + $this->assertSame( |
| 69 | + $initial_attempts + 1, |
| 70 | + $new_attempts, |
| 71 | + 'login_failed_attempt counter must increment via LockUserCounterMeasure' |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * A successful validateCredentials() call must return the user without |
| 77 | + * establishing a session — Auth::check() must remain false afterwards. |
| 78 | + */ |
| 79 | + public function testSuccessfulValidation_doesNotEstablishSession(): void |
| 80 | + { |
| 81 | + $this->assertFalse(Auth::check(), 'precondition: no authenticated user'); |
| 82 | + |
| 83 | + $user = $this->auth_service->validateCredentials( |
| 84 | + self::SEEDED_USERNAME, |
| 85 | + self::SEEDED_PASSWORD |
| 86 | + ); |
| 87 | + |
| 88 | + $this->assertInstanceOf(User::class, $user); |
| 89 | + $this->assertFalse( |
| 90 | + Auth::check(), |
| 91 | + 'validateCredentials() must NOT call Auth::login() on success' |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + private function getLoginFailedAttempt(string $username): int |
| 96 | + { |
| 97 | + // Clear Doctrine's identity map so we read fresh state from the DB, |
| 98 | + // not a cached in-memory entity from a prior transaction. |
| 99 | + EntityManager::clear(); |
| 100 | + $repo = EntityManager::getRepository(User::class); |
| 101 | + /** @var IUserRepository $repo */ |
| 102 | + $user = $repo->getByEmailOrName($username); |
| 103 | + $this->assertInstanceOf(User::class, $user, "Seeded user {$username} not found"); |
| 104 | + return $user->getLoginFailedAttempt(); |
| 105 | + } |
| 106 | +} |
0 commit comments