Ad FakeMember

This commit is contained in:
philipp lang 2021-11-17 22:33:25 +01:00
parent a39c8513c9
commit 53dcda68fc
4 changed files with 231 additions and 40 deletions

View File

@ -29,29 +29,39 @@ class Api {
return Backend::init($this->cookie);
}
public function findNr($nr) {
return $this->search(['mitgliedsNummber' => $nr]);
public function findNr(int $nr): Member
{
return $this->find(['mitgliedsNummber' => $nr]);
}
public function search($payload) {
$url = self::$url.'/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode($payload)).'&page=1&start=0&limit=10';
/**
* @param array<string, mixed> $payload
*/
public function find(array $payload): ?Member
{
return $this->search($payload)->first();
}
/**
* @param array<string, mixed> $payload
* @return Collection<int, Member>
*/
public function search(array $payload): Collection
{
$url = self::$url.'/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode($payload) ?: '[]').'&page=1&start=0&limit=10';
$response = $this->http()->get($url);
if ($response->json()['success'] === true) {
if (!count($response->json()['data'])) {
return null;
}
$data = collect($response->json()['data'][0])->mapWithKeys(function($value, $key) {
return [ str_replace('entries_', '', $key) => $value ];
});
return Member::fromNami($data);
}
if ($response->json()['success'] !== true) {
$this->exception('Search failed', ['url' => $url], $response->json());
}
return collect($response->json()['data'])->map(function($member) {
return Member::fromNami(collect($member)->mapWithKeys(function($value, $key) {
return [ str_replace('entries_', '', (string) $key) => $value ];
}));
});
}
protected function loggedInAlready(): bool {
return $this->loggedIn !== null;
}

View File

@ -69,9 +69,13 @@ class NamiGuard {
{
try {
$api = Nami::login($credentials['mglnr'], $credentials['password']);
$user = $api->findNr($credentials['mglnr']);
$payload = [
'credentials' => $credentials
'credentials' => $credentials,
'firstname' => $user->firstname,
'lastname' => $user->lastname,
'group_id' => $user->group_id,
];
$this->setUser(NamiUser::fromPayload($payload));

View File

@ -2,6 +2,7 @@
namespace Zoomyboy\LaravelNami\Backend;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Cache;
@ -148,30 +149,185 @@ class FakeBackend {
]);
}
private function notAuthorizedResponse() {
return $this->response([
'success' => true,
'data' => null,
'responseType' => 'ERROR',
'message' => 'Session expired',
'title' => 'Exception',
]);
}
public function response($data) {
return new Response(new GuzzleResponse(200, [], json_encode($data)));
}
/**
* @param string $mglnr
* @param array<int, array> $groups
*/
public function fakeLogin(string $mglnr, array $groups): void
public function fakeLogin(string $mglnr): self
{
app(LoginFake::class)->succeeds($mglnr);
foreach ($groups as $group) {
GroupFake::addGroup($group);
return $this;
}
/**
* @param int $mitgliedsNr
* @param array <string, mixed> $data
*/
public function addSearch(int $mitgliedsNr, array $data): self
{
Http::fake(function($request) use ($data, $mitgliedsNr) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode(['mitgliedsNummber' => $mitgliedsNr]) ?: '{}').'&page=1&start=0&limit=10') {
$content = [
'success' => true,
'data' => [$data],
'responseType' => 'OK',
'totalEntries' => 1,
];
return Http::response(json_encode($content) ?: '{}', 200);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeNationalities(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/baseadmin/staatsangehoerigkeit') {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<string, string> $data
*/
public function fakeMember(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode('[]').'&page=1&start=0&limit=10') {
return Http::response(json_encode([
'success' => true,
'data' => [[
'entries_id' => $data['id'],
'entries_gruppierungId' => $data['gruppierungId'],
]]
]) ?: '{}', 200);
}
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$data['gruppierungId']}/{$data['id']}") {
$content = [
'success' => true,
'data' => $data,
];
return Http::response(json_encode($content) ?: '{}', 200);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeCountries(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/baseadmin/land') {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeGenders(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/baseadmin/geschlecht') {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeRegions(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/baseadmin/region') {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param int $groupId
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeActivities(int $groupId, array $data): self
{
Http::fake(function($request) use ($data, $groupId) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/taetigkeitaufgruppierung/filtered/gruppierung/gruppierung/{$groupId}") {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param int $groupId
* @param array<int, array<int, array{name: string, id: int}>> $data
*/
public function fakeSubactivities($matches): self
{
Http::fake(function($request) use ($matches) {
foreach ($matches as $activityId => $data) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/untergliederungauftaetigkeit/filtered/untergliederung/taetigkeit/{$activityId}") {
return $this->dataResponse($data);
}
}
});
return $this;
}
/**
* @param int $groupId
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeFees(int $groupId, array $data): self
{
Http::fake(function($request) use ($data, $groupId) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/namiBeitrag/beitragsartmgl/gruppierung/{$groupId}") {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeConfessions(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/baseadmin/konfession") {
return $this->dataResponse($data);
}
});
return $this;
}
public function fakeFailedLogin(string $mglnr): void
@ -179,7 +335,8 @@ class FakeBackend {
app(LoginFake::class)->fails($mglnr);
}
public function asForm() {
public function asForm(): self
{
return $this;
}
@ -187,4 +344,19 @@ class FakeBackend {
throw new \Exception('no handler found for URL '.$url);
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
private function dataResponse(array $data): FulfilledPromise
{
$content = [
'success' => true,
'data' => collect($data)->map(fn ($item) => ['descriptor' => $item['name'], 'id' => $item['id'], 'name' => ''])->toArray(),
'responseType' => 'OK',
'totalEntries' => count ($data),
];
return Http::response(json_encode($content) ?: '{}', 200);
}
}

View File

@ -2,24 +2,33 @@
namespace Zoomyboy\LaravelNami;
use Cache;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Cache;
class NamiUser implements Authenticatable {
public $mglnr;
public $password;
public string $firstname;
public string $lastname;
public int $group_id;
public function __construct($attributes) {
$this->mglnr = $attributes['mglnr'];
$this->password = $attributes['password'];
$this->firstname = $attributes['firstname'];
$this->lastname = $attributes['lastname'];
$this->group_id = $attributes['group_id'];
}
public static function fromPayload($payload) {
$user = new static([
'mglnr' => data_get($payload, 'credentials.mglnr'),
'password' => data_get($payload, 'credentials.password'),
'firstname' => data_get($payload, 'firstname'),
'lastname' => data_get($payload, 'lastname'),
'group_id' => data_get($payload, 'group_id'),
]);
return $user;
@ -30,7 +39,7 @@ class NamiUser implements Authenticatable {
}
public function getNamiGroupId() {
return $this->api()->findNr($this->mglnr)->group_id;
return $this->group_id;
}
public function getAuthIdentifierName() {
@ -42,15 +51,11 @@ class NamiUser implements Authenticatable {
}
public function getFirstname() {
return Cache::remember('member-'.$this->mglnr.'-firstname', 3600, function() {
return $this->api()->findNr($this->mglnr)->firstname;
});
return $this->firstname;
}
public function getLastname() {
return Cache::remember('member-'.$this->mglnr.'-lastname', 3600, function() {
return $this->api()->findNr($this->mglnr)->lastname;
});
return $this->firstname;
}
public function getAuthIdentifier() {