Skip to content

Commit 81d7c14

Browse files
committed
Add EntityidController
1 parent a6d4883 commit 81d7c14

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Ldap\CesnetOrganization;
6+
use App\Ldap\EduidczOrganization;
7+
use Illuminate\Http\Request;
8+
9+
class EntityidController extends Controller
10+
{
11+
/**
12+
* Handle the incoming request.
13+
*/
14+
public function __invoke(Request $request)
15+
{
16+
abort_unless($request->ico, 400, 'Missing company ID.');
17+
18+
$organization = CesnetOrganization::findBy('ico', $request->ico);
19+
abort_unless($organization, 404, 'No organization found.');
20+
21+
$idp = EduidczOrganization::findBy('opointer', $organization->getDn());
22+
abort_unless($idp, 404, 'No entityID.');
23+
24+
return response()->json([
25+
'entityID' => $idp->getFirstAttribute('entityIDofIdP'),
26+
])->setEncodingOptions(JSON_UNESCAPED_SLASHES);
27+
}
28+
}

routes/api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\Http\Controllers\EntityidController;
34
use App\Http\Controllers\StatisticController;
45
use Illuminate\Support\Facades\Route;
56

@@ -14,4 +15,5 @@
1415
|
1516
*/
1617

18+
Route::get('entityid', EntityidController::class)->name('api:entityid');
1719
Route::get('statistics', [StatisticController::class, 'index'])->name('api:statistics');
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use App\Ldap\CesnetOrganization;
6+
use App\Ldap\EduidczOrganization;
7+
use LdapRecord\Laravel\Testing\DirectoryEmulator;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use Tests\TestCase;
10+
11+
class EntityidControllerTest extends TestCase
12+
{
13+
#[Test]
14+
public function missing_company_id_returns_bad_request_response(): void
15+
{
16+
$this
17+
->get(route('api:entityid'))
18+
->assertBadRequest();
19+
}
20+
21+
#[Test]
22+
public function organization_not_found_returns_not_found_response(): void
23+
{
24+
DirectoryEmulator::setup();
25+
26+
$this
27+
->get(route('api:entityid', ['ico' => '123']))
28+
->assertNotFound();
29+
30+
DirectoryEmulator::tearDown();
31+
}
32+
33+
#[Test]
34+
public function organization_without_an_idp_returns_not_found_response(): void
35+
{
36+
$ico = '123456789';
37+
38+
DirectoryEmulator::setup();
39+
DirectoryEmulator::setup('eduidczorganizations');
40+
41+
CesnetOrganization::create([
42+
'dc' => 'Example',
43+
'ico' => $ico,
44+
]);
45+
46+
$this->assertCount(1, CesnetOrganization::all());
47+
48+
$this
49+
->get(route('api:entityid', ['ico' => $ico]))
50+
->assertNotFound();
51+
52+
DirectoryEmulator::tearDown();
53+
}
54+
55+
#[Test]
56+
public function organization_with_an_idp_returns_its_entityid(): void
57+
{
58+
$ico = '1234567890';
59+
$dc = 'Example';
60+
$entityid = 'https://idp.example.org/idp/shibboleth';
61+
$scope = 'example.org';
62+
63+
DirectoryEmulator::setup();
64+
DirectoryEmulator::setup('eduidczorganizations');
65+
66+
$o = CesnetOrganization::create([
67+
'ico' => $ico,
68+
'dc' => $dc,
69+
]);
70+
71+
$this->assertCount(1, CesnetOrganization::all());
72+
73+
EduidczOrganization::create([
74+
'dc' => now()->timestamp,
75+
'oPointer' => $o->getDn(),
76+
'entityIDofIdP' => $entityid,
77+
'eduIDczScope' => $scope,
78+
]);
79+
80+
$this->assertCount(1, EduidczOrganization::all());
81+
82+
$this
83+
->get(route('api:entityid', ['ico' => $ico]))
84+
->assertOk()
85+
->assertJson([
86+
'entityID' => $entityid,
87+
]);
88+
89+
DirectoryEmulator::tearDown();
90+
}
91+
}

0 commit comments

Comments
 (0)