Add remote search
This commit is contained in:
parent
60b0052622
commit
458c643615
|
@ -18,9 +18,9 @@ class NamiSearchAction
|
||||||
*
|
*
|
||||||
* @return LengthAwarePaginator<MemberEntry>
|
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Contribution\Actions\GenerateApiAction as ContributionGenerateApiAction;
|
|
||||||
use App\Remote\Actions\LoginAction;
|
use App\Remote\Actions\LoginAction;
|
||||||
|
use App\Remote\Actions\SearchAction;
|
||||||
|
|
||||||
Route::post('/nami/token', LoginAction::class)->name('remote.nami.token');
|
Route::post('/nami/token', LoginAction::class)->name('remote.nami.token');
|
||||||
|
Route::post('/nami/search', SearchAction::class)->name('remote.nami.search');
|
||||||
|
|
|
@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue