Skip to content

Commit 132d678

Browse files
committed
Add EntityidController
1 parent 211f269 commit 132d678

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
// response
14+
15+
#[Test]
16+
public function missing_company_id_returns_bad_request_response(): void
17+
{
18+
$this
19+
->get(route('api:entityid'))
20+
->assertBadRequest();
21+
}
22+
23+
#[Test]
24+
public function organization_not_found_returns_not_found_response(): void
25+
{
26+
$this
27+
->get(route('api:entityid', ['ico' => '123']))
28+
->assertNotFound();
29+
}
30+
31+
#[Test]
32+
public function organization_without_an_idp_returns_not_found_response(): void
33+
{
34+
$ico = '123456789';
35+
36+
DirectoryEmulator::setup();
37+
38+
CesnetOrganization::create([
39+
'ico' => $ico,
40+
]);
41+
42+
$this->assertCount(1, CesnetOrganization::all());
43+
44+
$this
45+
->get(route('api:entityid', ['ico' => $ico]))
46+
->assertNotFound();
47+
48+
DirectoryEmulator::tearDown();
49+
}
50+
51+
#[Test]
52+
public function organization_with_an_idp_returns_its_entityid(): void
53+
{
54+
$ico = '1234567890';
55+
$dc = 'Example';
56+
$entityid = 'https://idp.example.org/idp/shibboleth';
57+
$scope = 'example.org';
58+
59+
DirectoryEmulator::setup();
60+
DirectoryEmulator::setup('eduidczorganizations');
61+
62+
$o = CesnetOrganization::create([
63+
'ico' => $ico,
64+
'dc' => $dc,
65+
]);
66+
67+
$this->assertCount(1, CesnetOrganization::all());
68+
69+
EduidczOrganization::create([
70+
'dc' => now()->timestamp,
71+
'oPointer' => $o->getDn(),
72+
'entityIDofIdP' => $entityid,
73+
'eduIDczScope' => $scope,
74+
]);
75+
76+
$this->assertCount(1, EduidczOrganization::all());
77+
78+
$this
79+
->get(route('api:entityid', ['ico' => $ico]))
80+
->assertOk()
81+
->assertJson([
82+
'entityID' => $entityid,
83+
]);
84+
85+
DirectoryEmulator::tearDown();
86+
}
87+
}

0 commit comments

Comments
 (0)