Repository navigation
FOUR-34196 Review the issue Unauthorized #9117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devmiguelangel
wants to merge
5
commits into
develop
Choose a base branch
from
FOUR-34196
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5e154f9
test: add tests for EnsureAccountAllowsAccess middleware
devmiguelangel f412a1c
fix: enhance EnsureAccountAllowsAccess middleware to accept dynamic g…
devmiguelangel b284103
feat: update ProcessMakerAuthenticate middleware to pass guards to En…
devmiguelangel 29ba1da
test: enhance k6 ETag tests with configuration validation and dynamic…
devmiguelangel b9a7090
fix: avoid logout on stateless API guard
devmiguelangel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
tests/Feature/Http/Middleware/EnsureAccountAllowsAccessTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Feature\Http\Middleware; | ||
|
|
||
| use Illuminate\Contracts\Auth\Guard; | ||
| use Illuminate\Contracts\Auth\StatefulGuard; | ||
| use Illuminate\Http\Request; | ||
| use Illuminate\Support\Facades\Auth; | ||
| use Mockery; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use ProcessMaker\Http\Middleware\EnsureAccountAllowsAccess; | ||
| use ProcessMaker\Http\Middleware\ProcessMakerAuthenticate; | ||
| use ProcessMaker\Models\User; | ||
| use Tests\TestCase; | ||
|
|
||
| class EnsureAccountAllowsAccessTest extends TestCase | ||
| { | ||
| public function test_web_middleware_does_not_resolve_the_api_guard_for_a_malformed_bearer_token(): void | ||
| { | ||
| $user = new User(['status' => 'ACTIVE']); | ||
| $webGuard = Mockery::mock(StatefulGuard::class); | ||
| $webGuard->shouldReceive('check')->once()->andReturn(true); | ||
| $webGuard->shouldReceive('user')->once()->andReturn($user); | ||
|
|
||
| Auth::shouldReceive('guard')->with('web')->andReturn($webGuard); | ||
| Auth::shouldReceive('guard')->never()->with('api'); | ||
|
|
||
| $request = Request::create('/', 'GET', [], [], [], [ | ||
| 'HTTP_AUTHORIZATION' => 'Bearer undefined', | ||
| ]); | ||
|
|
||
| $response = (new EnsureAccountAllowsAccess())->handle($request, fn () => response('allowed')); | ||
|
|
||
| $this->assertSame(200, $response->getStatusCode()); | ||
| $this->assertSame('allowed', $response->getContent()); | ||
| } | ||
|
|
||
| #[DataProvider('deniedWebStatuses')] | ||
| public function test_web_middleware_denies_users_with_a_disallowed_status(string $status): void | ||
| { | ||
| $user = new User(['status' => $status]); | ||
| $webGuard = Mockery::mock(StatefulGuard::class); | ||
| $webGuard->shouldReceive('check')->once()->andReturn(true); | ||
| $webGuard->shouldReceive('user')->once()->andReturn($user); | ||
| $webGuard->shouldReceive('logout')->once(); | ||
|
|
||
| Auth::shouldReceive('guard')->with('web')->andReturn($webGuard); | ||
| Auth::shouldReceive('guard')->never()->with('api'); | ||
|
|
||
| $response = (new EnsureAccountAllowsAccess())->handle(Request::create('/', 'GET'), fn () => response('allowed')); | ||
|
|
||
| $this->assertSame(302, $response->getStatusCode()); | ||
| $this->assertStringEndsWith('/login', $response->headers->get('Location')); | ||
| } | ||
|
|
||
| public static function deniedWebStatuses(): array | ||
| { | ||
| return [ | ||
| 'blocked' => ['BLOCKED'], | ||
| 'inactive' => ['INACTIVE'], | ||
| ]; | ||
| } | ||
|
|
||
| public function test_api_account_check_does_not_resolve_the_web_guard_for_an_active_user(): void | ||
| { | ||
| $user = new User(['status' => 'ACTIVE']); | ||
| $apiGuard = Mockery::mock(Guard::class); | ||
| $apiGuard->shouldReceive('check')->once()->andReturn(true); | ||
| $apiGuard->shouldReceive('user')->once()->andReturn($user); | ||
|
|
||
| Auth::shouldReceive('guard')->never()->with('web'); | ||
| Auth::shouldReceive('guard')->with('api')->andReturn($apiGuard); | ||
|
|
||
| $response = EnsureAccountAllowsAccess::blockingResponseForRequest( | ||
| Request::create('/api/test', 'GET'), | ||
| ['api'] | ||
| ); | ||
|
|
||
| $this->assertNull($response); | ||
| } | ||
|
|
||
| #[DataProvider('deniedApiStatuses')] | ||
| public function test_api_account_check_denies_users_with_a_disallowed_status(string $status, string $message): void | ||
| { | ||
| $user = new User(['status' => $status]); | ||
| $apiGuard = Mockery::mock(Guard::class); | ||
| $apiGuard->shouldReceive('check')->once()->andReturn(true); | ||
| $apiGuard->shouldReceive('user')->once()->andReturn($user); | ||
|
|
||
| Auth::shouldReceive('guard')->never()->with('web'); | ||
| Auth::shouldReceive('guard')->with('api')->andReturn($apiGuard); | ||
|
|
||
| $response = EnsureAccountAllowsAccess::blockingResponseForRequest( | ||
| Request::create('/api/test', 'GET', [], [], [], ['HTTP_ACCEPT' => 'application/json']), | ||
| ['api'] | ||
| ); | ||
|
|
||
| $this->assertSame(401, $response->getStatusCode()); | ||
| $this->assertSame(['message' => $message], json_decode($response->getContent(), true)); | ||
| } | ||
|
|
||
| public static function deniedApiStatuses(): array | ||
| { | ||
| return [ | ||
| 'blocked' => ['BLOCKED', 'Account locked after too many failed attempts. Contact administrator.'], | ||
| 'inactive' => ['INACTIVE', 'Unauthorized'], | ||
| ]; | ||
| } | ||
|
|
||
| public function test_processmaker_authenticate_checks_only_the_authenticated_api_guard(): void | ||
| { | ||
| $user = new User(['status' => 'INACTIVE']); | ||
| $apiGuard = Mockery::mock(Guard::class); | ||
| $apiGuard->shouldReceive('check')->once()->andReturn(true); | ||
| $apiGuard->shouldReceive('user')->once()->andReturn($user); | ||
|
|
||
| Auth::shouldReceive('guard')->never()->with('web'); | ||
| Auth::shouldReceive('guard')->with('api')->andReturn($apiGuard); | ||
|
|
||
| $middleware = new class(app('auth')) extends ProcessMakerAuthenticate { | ||
| protected function authenticate($request, array $guards) | ||
| { | ||
| // Authentication is outside the scope of this guard-selection test. | ||
| } | ||
| }; | ||
|
|
||
| $response = $middleware->handle( | ||
| Request::create('/api/test', 'GET', [], [], [], ['HTTP_ACCEPT' => 'application/json']), | ||
| fn () => response('allowed'), | ||
| 'api' | ||
| ); | ||
|
|
||
| $this->assertSame(401, $response->getStatusCode()); | ||
| $this->assertSame(['message' => 'Unauthorized'], json_decode($response->getContent(), true)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # ETag k6 tests | ||
|
|
||
| The ETag tests require a valid ProcessMaker OAuth bearer token. They do not run with a missing, `null`, `undefined`, or `FAKE_JWT` token. | ||
|
|
||
| Set the target URL and token through environment variables: | ||
|
|
||
| ```bash | ||
| PM_K6_BASE_URL=https://performance2-qa.processmaker.net \ | ||
| PM_K6_TOKEN='<access-token>' \ | ||
| k6 run tests/k6/etag/performance-test.js | ||
| ``` | ||
|
|
||
| Use the same variables for the other tests: | ||
|
|
||
| ```bash | ||
| k6 run tests/k6/etag/long-duration-test.js | ||
| k6 run tests/k6/etag/detect-dynamic-endpoints.js | ||
| ``` | ||
|
|
||
| Do not set an `Authorization` header outside these scripts when the token is unavailable. Test setup must fail instead of sending `Bearer null` or `Bearer undefined`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,28 @@ | ||
| const baseUrl = (__ENV.PM_K6_BASE_URL || '').replace(/\/$/, ''); | ||
|
|
||
| export const config = { | ||
| token: 'FAKE_JWT', // Replace with a valid token. | ||
| token: __ENV.PM_K6_TOKEN || '', | ||
| endpoints: { | ||
| startProcesses: 'https://processmaker.test/api/1.0/start_processes?page=1&per_page=15&filter=&order_by=category.name%2Cname&order_direction=asc%2Casc&include=events%2Ccategories&without_event_definitions=true', | ||
| startProcesses: `${baseUrl}/api/1.0/start_processes?page=1&per_page=15&filter=&order_by=category.name%2Cname&order_direction=asc%2Casc&include=events%2Ccategories&without_event_definitions=true`, | ||
| }, | ||
| }; | ||
|
|
||
| export const headers = { | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${config.token}`, | ||
| }; | ||
| export function validateConfiguration() { | ||
| if (!baseUrl) { | ||
| throw new Error('PM_K6_BASE_URL is required.'); | ||
| } | ||
|
|
||
| const token = config.token.trim(); | ||
| if (!token || ['null', 'undefined', 'fake_jwt'].includes(token.toLowerCase())) { | ||
| throw new Error('PM_K6_TOKEN must contain a valid bearer token.'); | ||
| } | ||
| } | ||
|
|
||
| export function getHeaders() { | ||
| validateConfiguration(); | ||
|
|
||
| return { | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${config.token}`, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.