Add tests for group fetching

This commit is contained in:
philipp lang 2022-02-19 17:08:43 +01:00
parent 6c6212fa29
commit d6167f1b7f
6 changed files with 133 additions and 94 deletions

View File

@ -319,6 +319,7 @@ class Api {
}
public function nationalities(): Collection {
$this->assertLoggedIn();
return collect($this->http()->get($this->url."/ica/rest/baseadmin/staatsangehoerigkeit")['data'])->map(function($gender) {
return Nationality::fromNami($gender);
});

View File

@ -1,37 +0,0 @@
<?php
namespace Zoomyboy\LaravelNami;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
trait AuthenticatesNamiUsers {
use AuthenticatesUsers;
protected function validateLogin(Request $request)
{
$request->validate([
'mglnr' => 'required_if:provider,nami',
'email' => 'required_if:provider,database',
'password' => 'required|string',
]);
}
public function username()
{
return 'mglnr';
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
return $request->only($this->username(), 'email', 'password', 'provider');
}
}

View File

@ -36,6 +36,27 @@ class FakeBackend {
return $this;
}
/**
* @param array $data
*/
public function fakeGroups(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root') {
$data = collect($data)->mapWithKeys(fn ($group, $id) =>
$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
*/

55
src/Fakes/GroupFake.php Normal file
View File

@ -0,0 +1,55 @@
<?php
namespace Zoomyboy\LaravelNami\Fakes;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
class GroupFake extends Fake {
public function get(array $data): void
{
$this->fakeResponse(null, $data);
}
private function fakeResponse(?int $parentId = null, array $data): void
{
$url = 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/'.($parentId ?: 'root');
Http::fake(function($request) use ($data, $url) {
if ($request->url() === $url) {
return Http::response(json_encode([
'success' => true,
'totalEntries' => count($this->mapToResponse($data)),
'data' => $this->mapToResponse($data),
]) ?: '{}', 200);
}
});
foreach ($data as $id => $group) {
$this->fakeResponse($id, data_get($group, 'children', []));
}
}
public function assertRootFetched(): void
{
Http::assertSent(fn ($request) => $request->url() === 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root'
&& $request->method() === 'GET'
);
}
public function assertFetched(int $id): void
{
Http::assertSent(fn ($request) => $request->url() === 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/'.$id
&& $request->method() === 'GET'
);
}
private function mapToResponse(array $data): array
{
return collect($data)->map(fn ($group, $id) => [
'id' => $id,
'descriptor' => $group['name'],
])->values()->toArray();
}
}

View File

@ -1,57 +0,0 @@
<?php
namespace Zoomyboy\LaravelNami;
use Cache;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class NamiUser {
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;
}
public function api() {
return Nami::login($this->mglnr, $this->password);
}
public function getNamiGroupId() {
return $this->group_id;
}
public function getMglnr() {
return $this->mglnr;
}
public function getFirstname() {
return $this->firstname;
}
public function getLastname() {
return $this->lastname;
}
}

56
tests/Unit/GroupsTest.php Normal file
View File

@ -0,0 +1,56 @@
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
use Illuminate\Support\Facades\Http;
use Zoomyboy\LaravelNami\Authentication\Auth;
use Zoomyboy\LaravelNami\Exceptions\NotAuthenticatedException;
use Zoomyboy\LaravelNami\Fakes\CourseFake;
use Zoomyboy\LaravelNami\Fakes\GroupFake;
use Zoomyboy\LaravelNami\LoginException;
use Zoomyboy\LaravelNami\Nami;
use Zoomyboy\LaravelNami\Tests\TestCase;
class GroupsTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
Auth::fake();
}
public function test_get_groups(): void
{
Auth::success(12345, 'secret');
app(GroupFake::class)->get([
1234 => ['name' => 'testgroup']
]);
$group = Nami::login(12345, 'secret')->group(1234);
$this->assertEquals('testgroup', $group->name);
$this->assertEquals(1234, $group->id);
app(GroupFake::class)->assertRootFetched();
}
public function test_get_subgroups(): void
{
Auth::success(12345, 'secret');
app(GroupFake::class)->get([
1234 => ['name' => 'testgroup', 'children' => [
555 => ['name' => 'ABC']
]]
]);
$group = Nami::login(12345, 'secret')->group(1234)->subgroups()->first();
$this->assertEquals('ABC', $group->name);
$this->assertEquals(555, $group->id);
app(GroupFake::class)->assertFetched(1234);
}
}