Skip to content

Commit

Permalink
Merge pull request #48 from WebFiori/dev
Browse files Browse the repository at this point in the history
Added Support for Level 2  Maturity Model
  • Loading branch information
usernane authored Dec 3, 2023
2 parents 8669955 + 5d6a045 commit a3aadca
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 39 deletions.
1 change: 1 addition & 0 deletions tests/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@
require_once $rootDir.'tests'.$DS.'webfiori'.$DS.'tests'.$DS.'http'.$DS.'testServices'.$DS.'NotImplService.php';
require_once $rootDir.'tests'.$DS.'webfiori'.$DS.'tests'.$DS.'http'.$DS.'testServices'.$DS.'TestUserObj.php';
require_once $rootDir.'tests'.$DS.'webfiori'.$DS.'tests'.$DS.'http'.$DS.'testServices'.$DS.'CreateUserProfileService.php';
require_once $rootDir.'tests'.$DS.'webfiori'.$DS.'tests'.$DS.'http'.$DS.'testServices'.$DS.'MulNubmersService.php';
fwrite($stderr,"Classes Loaded.\n");
31 changes: 30 additions & 1 deletion tests/webfiori/tests/http/WebServicesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function testConstructor00() {
$this->assertEquals('1.0.1',$api->getVersion());
$this->assertEquals('NO DESCRIPTION',$api->getDescription());
$api->setDescription('Test API.');
$this->assertEquals(4,count($api->getServices()));
$this->assertEquals(5,count($api->getServices()));
$this->assertEquals('Test API.',$api->getDescription());
$this->assertTrue($api->getServiceByName('sum-array') instanceof AbstractWebService);
$this->assertNull($api->getServiceByName('request-info'));
Expand Down Expand Up @@ -536,6 +536,35 @@ public function testSumTwoIntegers07() {
$api->process();
$this->assertEquals('{"message":"Method Not Allowed.","type":"error","http-code":405}', $api->readOutputStream());
}
/**
* @test
*/
public function testMulTwoIntegers00() {
$this->clrearVars();
putenv('REQUEST_METHOD=GET');
$_GET['first-number'] = '100';
$_GET['second-number'] = '300';
$_GET['action'] = 'mul-two-integers';
$api = new SampleServicesManager();
$api->setOutputStream($this->outputStreamName);
$api->process();
$this->assertEquals('{"message":"The multiplication of 100 and 300 is 30000.","http-code":200}', $api->readOutputStream());
}
/**
* @test
*/
public function testMulTwoIntegers01() {
$this->clrearVars();
putenv('REQUEST_METHOD=GET');
$_GET['first-number'] = '-100';
$_GET['second-number'] = '300';
$_GET['action'] = 'mul-two-integers';
$api = new SampleServicesManager();
$api->setOutputStream($this->outputStreamName);
ResponseMessage::set(401, 'First number must be positive!');
$api->process();
$this->assertEquals('{"message":"First number must be positive!","type":"error","http-code":401}', $api->readOutputStream());
}
/**
* @test
*/
Expand Down
1 change: 1 addition & 0 deletions tests/webfiori/tests/http/outputStream.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"Not Authorized.","type":"error","http-code":401}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function __construct() {
parent::__construct('add-two-integers');
$this->setDescription('Returns a JSON string that has the sum of two integers.');
$this->addRequestMethod('get');

$this->addParameter(new RequestParameter('first-number', 'integer'));
$this->addParameter(new RequestParameter('second-number', 'integer'));
}
Expand Down
37 changes: 37 additions & 0 deletions tests/webfiori/tests/http/testServices/MulNubmersService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace webfiori\tests\http\testServices;

use webfiori\http\AbstractWebService;
use webfiori\http\RequestParameter;
/**
*
* @author Ibrahim
*/
class MulNubmersService extends AbstractWebService {
public function __construct() {
parent::__construct('mul-two-integers');
$this->setDescription('Returns a JSON string that has the multiplication of two integers.');
$this->addRequestMethod('get');

$this->addParameter(new RequestParameter('first-number', 'integer'));
$this->addParameter(new RequestParameter('second-number', 'integer'));
}

public function isAuthorizedGET() {
if ($this->getParamVal('first-number') < 0) {
return false;
}
}

public function processGet() {
$firstNum = $this->getParamVal('first-number');
$secondNumber = $this->getParamVal('second-number');
$sum = $firstNum*$secondNumber;
$this->sendResponse('The multiplication of '.$firstNum.' and '.$secondNumber.' is '.$sum.'.');
}

public function processRequest() {

}
}
37 changes: 2 additions & 35 deletions tests/webfiori/tests/http/testServices/SampleServicesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,7 @@ public function __construct() {
$this->addService(new SumNumbersService());
$this->addService(new GetUserProfileService());
$this->addService(new CreateUserProfileService());
$this->addService(new MulNubmersService());
}
/**
*
* @return boolean True if $_GET['pass'] or $_POST['pass'] is equal to 123
*/
public function isAuthorized() {
$pass = isset($_GET['pass']) ? $_GET['pass'] : null;

if ($pass == null) {
$pass = isset($_POST['pass']) ? $_POST['pass'] : null;
}

if ($pass == '123') {
return true;
}

return false;
}

public function processRequest() {
$action = $this->getAction();
$i = $this->getInputs();

if ($action == 'add-two-integers') {
$sum = $i['first-number'] + $i['second-number'];
$this->sendResponse('The sum of '.$i['first-number'].' and '.$i['second-number'].' is '.$sum.'.');
} else {
if ($action == 'sum-array') {

} else if ($action == 'get-user-profile') {

} else {
$this->serviceNotImplemented();
}
}
}

}
25 changes: 22 additions & 3 deletions webfiori/http/WebServicesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,14 +816,33 @@ public function toJSON() : Json {

return $json;
}
private function isAuth(AbstractWebService $service) {
if ($service->isAuthRequired()) {
$isAuthCheck = 'isAuthorized'.Request::getMethod();
if (!method_exists($service, $isAuthCheck)) {
return $service->isAuthorized() === null || $service->isAuthorized();
}
return $service->$isAuthCheck() === null || $service->$isAuthCheck();
}
return true;
}
private function processService(AbstractWebService $service) {
$processMethod = 'process'.Request::getMethod();

if (!method_exists($service, $processMethod)) {
$service->processRequest();
} else {
$service->$processMethod();
}
}
private function _AfterParamsCheck($processReq) {
if ($processReq) {

$service = $this->getServiceByName($this->getCalledServiceName());
$isAuth = !$service->isAuthRequired() || $service->isAuthorized() === null || $service->isAuthorized();


if ($isAuth) {
$service->processRequest();
if ($this->isAuth($service)) {
$this->processService($service);
} else {
$this->notAuth();
}
Expand Down

0 comments on commit a3aadca

Please sign in to comment.