Add remote search

This commit is contained in:
philipp lang 2024-02-22 00:40:19 +01:00
parent 60b0052622
commit 458c643615
5 changed files with 80 additions and 26 deletions

View File

@ -18,9 +18,9 @@ class NamiSearchAction
*
* @return LengthAwarePaginator<MemberEntry>
*/
public function handle(Api $api, int $page, array $params): LengthAwarePaginator
public function handle(Api $api, int $page, array $params, int $perPage = 10): LengthAwarePaginator
{
return $api->pageSearch($params, $page, 10);
return $api->pageSearch($params, $page, $perPage);
}
/**

View File

@ -0,0 +1,26 @@
<?php
namespace App\Remote\Actions;
use App\Initialize\Actions\NamiSearchAction;
use Illuminate\Support\Facades\Crypt;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Nami;
class SearchAction
{
use AsAction;
public function handle(ActionRequest $request)
{
$token = str($request->header('Authorization'))->replace('Bearer ', '')->toString();
$credentials = json_decode(Crypt::decryptString($token));
$api = Nami::login($credentials->mglnr, $credentials->password);
$results = NamiSearchAction::run($api, $request->input('page', 1), [], 50);
return $results->map(fn ($member) => ['id' => $member->memberId, 'name' => $member->firstname . ' ' . $member->lastname]);
}
}

View File

@ -1,6 +1,7 @@
<?php
use App\Contribution\Actions\GenerateApiAction as ContributionGenerateApiAction;
use App\Remote\Actions\LoginAction;
use App\Remote\Actions\SearchAction;
Route::post('/nami/token', LoginAction::class)->name('remote.nami.token');
Route::post('/nami/search', SearchAction::class)->name('remote.nami.search');

View File

@ -1,23 +0,0 @@
<?php
namespace Tests\Feature\Nami;
use Illuminate\Support\Facades\Crypt;
use Tests\EndToEndTestCase;
use Zoomyboy\LaravelNami\Authentication\Auth;
class LoginTest extends EndToEndTestCase
{
public function testItCanLoginRemotelyWithUser(): void
{
Auth::success(90100, 'secret');
$response = $this->postJson(route('remote.nami.token'), [
'mglnr' => 90100,
'password' => 'secret',
]);
$response->assertOk();
$accessTokenPayload = Crypt::decryptString($response->json('access_token'));
$this->assertEquals(['mglnr' => 90100, 'password' => 'secret'], json_decode($accessTokenPayload, true));
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace Tests\Feature\Nami;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Testing\TestResponse;
use Tests\EndToEndTestCase;
use Tests\TestCase;
use Zoomyboy\LaravelNami\Authentication\Auth;
use Zoomyboy\LaravelNami\Data\MemberEntry;
use Zoomyboy\LaravelNami\Fakes\SearchFake;
class RemoteSearchTest extends TestCase
{
use DatabaseTransactions;
public function testItCanLoginRemotelyWithUser(): void
{
Auth::success(90100, 'secret');
$response = $this->loginRemotely(90100, 'secret');
$response->assertOk();
$accessTokenPayload = Crypt::decryptString($response->json('access_token'));
$this->assertEquals(['mglnr' => 90100, 'password' => 'secret'], json_decode($accessTokenPayload, true));
}
public function testItCanSearchForOwnGroupMembers(): void
{
Auth::success(90100, 'secret');
app(SearchFake::class)->fetches(1, 0, 50, [
MemberEntry::factory()->toMember(['groupId' => 100, 'id' => 20, 'memberId' => 56, 'firstname' => 'Max', 'lastname' => 'Muster']),
]);
$accessToken = $this->loginRemotely()->json('access_token');
$this->postJson(route('remote.nami.search'), [], ['Authorization' => 'Bearer ' . $accessToken])
->assertOk()
->assertJsonPath('0.id', 56)
->assertJsonPath('0.name', 'Max Muster');
}
protected function loginRemotely(?int $mglnr = 90100, ?string $password = 'secret'): TestResponse
{
return $this->postJson(route('remote.nami.token'), [
'mglnr' => $mglnr,
'password' => $password,
]);
}
}