From f162e58ed52acfb828153c68e118921f5e3f47c3 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 5 Jul 2020 01:35:15 +0200 Subject: [PATCH] Fixed: Handle errors of memberships fetcher --- src/Api.php | 16 +++++++++++++++- tests/Unit/PullMembershipsTest.php | 27 ++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/Api.php b/src/Api.php index ecac7f9..0358ff7 100644 --- a/src/Api.php +++ b/src/Api.php @@ -70,7 +70,21 @@ class Api { } public function membershipsOf($memberId): Collection { - return collect($this->http()->get(self::$url.'/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/'.$memberId.'/flist')->json()['data']); + $url = self::$url.'/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/'.$memberId.'/flist'; + + $r = $this->http()->get($url); + + if ($r->status() == 200) { + Log::debug('Member Request '.$memberId, [ 'url' => $url, 'response' => $r->body(), 'json' => $r->json(), 'memberId' => $memberId ]); + } else { + Log::error('Member Request '.$memberId, [ 'url' => $url, 'response' => $r->body(), 'json' => $r->json(), 'memberId' => $memberId ]); + } + + if (!isset($r->json()['data'])) { + return collect([]); + } + + return collect($r->json()['data']); } public function subactivitiesOf($activityId) { diff --git a/tests/Unit/PullMembershipsTest.php b/tests/Unit/PullMembershipsTest.php index 40c850a..9054023 100644 --- a/tests/Unit/PullMembershipsTest.php +++ b/tests/Unit/PullMembershipsTest.php @@ -10,7 +10,14 @@ use Zoomyboy\LaravelNami\Member; class PullMembershipsTest extends TestCase { public $groupsResponse = '{"success":true,"data":[{"descriptor":"Group","name":"","representedClass":"de.iconcept.nami.entity.org.Gruppierung","id":103}],"responseType":"OK"}'; - public $unauthorizedResponse = '{"success":false,"data":null,"responseType":"EXCEPTION","message":"Access denied - no right for requested operation","title":"Exception"}'; + public $unauthorizedResponse = ''; + + public function errorProvider() { + return [ + 'unauth' => ['{"success":false,"data":null,"responseType":"EXCEPTION","message":"Access denied - no right for requested operation","title":"Exception"}'], + 'noright' => ['{"success":false,"responseType":"EXCEPTION","message":"Sicherheitsverletzung: Zugriff auf Rechte Recht (n:2001002 o:2) fehlgeschlagen"}'] + ]; + } public function dataProvider() { return [ @@ -49,4 +56,22 @@ class PullMembershipsTest extends TestCase Http::assertSentCount(5); } + /** + * @dataProvider errorProvider + */ + public function test_it_gets_no_memberships_with_no_rights($error) { + Http::fake(array_merge($this->login(), [ + 'https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/16/flist' => Http::response($error, 200) + ])); + + $this->setCredentials(); + + Nami::login(); + $member = new Member(['id' => 16]); + + $memberships = $member->memberships(); + $this->assertSame([], $member->memberships()->toArray()); + + Http::assertSentCount(4); + } }