From 458c6436158b3e97d46d758095ff4e7417629874 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 22 Feb 2024 00:40:19 +0100 Subject: [PATCH] Add remote search --- app/Initialize/Actions/NamiSearchAction.php | 4 +- app/Remote/Actions/SearchAction.php | 26 +++++++++++ routes/remote.php | 3 +- tests/Feature/Remote/LoginTest.php | 23 ---------- tests/Feature/Remote/RemoteSearchTest.php | 50 +++++++++++++++++++++ 5 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 app/Remote/Actions/SearchAction.php delete mode 100644 tests/Feature/Remote/LoginTest.php create mode 100644 tests/Feature/Remote/RemoteSearchTest.php diff --git a/app/Initialize/Actions/NamiSearchAction.php b/app/Initialize/Actions/NamiSearchAction.php index 998ac0bc..260a81c0 100644 --- a/app/Initialize/Actions/NamiSearchAction.php +++ b/app/Initialize/Actions/NamiSearchAction.php @@ -18,9 +18,9 @@ class NamiSearchAction * * @return LengthAwarePaginator */ - 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); } /** diff --git a/app/Remote/Actions/SearchAction.php b/app/Remote/Actions/SearchAction.php new file mode 100644 index 00000000..eeaa681e --- /dev/null +++ b/app/Remote/Actions/SearchAction.php @@ -0,0 +1,26 @@ +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]); + } +} diff --git a/routes/remote.php b/routes/remote.php index 0ab5ce20..ca3388a5 100644 --- a/routes/remote.php +++ b/routes/remote.php @@ -1,6 +1,7 @@ name('remote.nami.token'); +Route::post('/nami/search', SearchAction::class)->name('remote.nami.search'); diff --git a/tests/Feature/Remote/LoginTest.php b/tests/Feature/Remote/LoginTest.php deleted file mode 100644 index 11722044..00000000 --- a/tests/Feature/Remote/LoginTest.php +++ /dev/null @@ -1,23 +0,0 @@ -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)); - } -} diff --git a/tests/Feature/Remote/RemoteSearchTest.php b/tests/Feature/Remote/RemoteSearchTest.php new file mode 100644 index 00000000..76f8a850 --- /dev/null +++ b/tests/Feature/Remote/RemoteSearchTest.php @@ -0,0 +1,50 @@ +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, + ]); + } +}