Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Services/OpenId/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public function update(int $id, array $payload): IEntity
$user = $this->tx_service->transaction(function () use ($id, $payload) {

$user = $this->repository->getById($id);

if (is_null($user) || !$user instanceof User)
throw new EntityNotFoundException("user not found");

Expand Down
12 changes: 6 additions & 6 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
$this->call('OpenIdExtensionsSeeder');
$this->call('ServerConfigurationSeeder');
$this->call(OpenIdExtensionsSeeder::class);
$this->call(ServerConfigurationSeeder::class);

DB::table('oauth2_api_endpoint_api_scope')->delete();
DB::table('oauth2_client_api_scope')->delete();
Expand All @@ -35,9 +35,9 @@ public function run()
DB::table('oauth2_api')->delete();
DB::table('oauth2_resource_server')->delete();

$this->call('ResourceServerSeeder');
$this->call('ApiSeeder');
$this->call('ApiScopeSeeder');
$this->call('ApiEndpointSeeder');
$this->call(ResourceServerSeeder::class);
$this->call(ApiSeeder::class);
$this->call(ApiScopeSeeder::class);
$this->call(ApiEndpointSeeder::class);
}
}
1 change: 0 additions & 1 deletion database/seeds/ResourceServerSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class ResourceServerSeeder extends Seeder {

public function run()
{
DB::table('oauth2_resource_server')->delete();

$current_realm = Config::get('app.url');

Expand Down
1,501 changes: 230 additions & 1,271 deletions database/seeds/TestSeeder.php

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ php artisan db:seed --force --env={env}

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

nvm use
nvm use

# Tests

php artisan view:clear
php artisan cache:clear

./vendor/bin/phpunit
84 changes: 78 additions & 6 deletions tests/OAuth2UserRegistrationServiceApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
*/
final class OAuth2UserRegistrationServiceApiTest extends OAuth2ProtectedApiTest
{


public function testRegisterUserRequestCreation()
{

$data = [
'email' => 'test_'. str_random(16).'@test.com',
'first_name' => 'test_'. str_random(16),
Expand Down Expand Up @@ -54,11 +51,86 @@ public function testRegisterUserRequestCreation()
$user_registration_request = json_decode($content);

$this->assertTrue(!empty($user_registration_request->hash));
}

public function testRegistrationRequestUpdate()
{
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];

$data = [
'email' => 'test_'. str_random(16).'@test.com',
'first_name' => 'test_'. str_random(16),
'last_name' => 'test_'. str_random(16),
];

//Create new registration request
$response = $this->action
(
"POST",
"Api\\OAuth2\\OAuth2UserRegistrationRequestApiController@register",
[],
[],
[],
[],
$headers,
json_encode($data)
);

//Fetch created registration request
$params = [
'token' => $user_registration_request->hash,
'redirect_uri' => 'https://www.test.com/oauth2'
];
'filter' => 'email==' . $data['email'],
];

$response = $this->action
(
"GET",
"Api\\OAuth2\\OAuth2UserRegistrationRequestApiController@getAll",
$params,
[],
[],
[],
$headers
);

$content = $response->getContent();
$response = json_decode($content);

$this->assertTrue($response->total == 1);

//Update fetched registration request
$params = [
'id' => $response->data[0]->id,
];

$new_first_name = 'test_updated_'. str_random(16);

$update_data = [
'first_name' => $new_first_name,
'last_name' => 'test_updated_'. str_random(16),
'company' => 'test_updated_'. str_random(16),
'country' => 'US',
];

$response = $this->action
(
"PUT",
"Api\\OAuth2\\OAuth2UserRegistrationRequestApiController@update",
$params,
[],
[],
[],
$headers,
json_encode($update_data)
);

$this->assertResponseStatus(201);

$content = $response->getContent();
$response = json_decode($content);
$this->assertTrue($response->first_name == $new_first_name);
}

protected function getScopes()
Expand Down
2 changes: 1 addition & 1 deletion tests/OAuth2UserServiceApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testUpdateMe(){
$response = $this->action
(
"PUT",
"Api\\OAuth2\\OAuth2UserApiController@UpdateMe",
"Api\\OAuth2\\OAuth2UserApiController@updateMe",
$params,
[],
[],
Expand Down
74 changes: 74 additions & 0 deletions tests/OAuth2UserUpdateApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php namespace Tests;
/**
* Copyright 2019 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\libs\OAuth2\IUserScopes;
use Auth\User;
use LaravelDoctrine\ORM\Facades\EntityManager;

/**
* Class OAuth2UserUpdateApiTest
* @package Tests
*/

final class OAuth2UserUpdateApiTest extends OAuth2ProtectedApiTest
{
public function testUserUpdate()
{
$user = EntityManager::getRepository(User::class)->findOneBy(['identifier' => 'sebastian.marcet']);

$this->assertTrue(!is_null($user));

$new_first_name = 'test_'. str_random(16);

$data = [
'first_name' => $new_first_name,
'last_name' => 'test_'. str_random(16),
'company' => 'test_'. str_random(16),
];

$params = [
'id' => $user->id,
];

$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];

$response = $this->action
(
"PUT",
"Api\\OAuth2\\OAuth2UserApiController@update",
$params,
[],
[],
[],
$headers,
json_encode($data)
);

$this->assertResponseStatus(201);

$content = $response->getContent();
$response = json_decode($content);
$this->assertTrue($response->first_name == $new_first_name);
}

protected function getScopes()
{
return [
"openid",
IUserScopes::Write,
];
}
}
4 changes: 3 additions & 1 deletion tests/OIDCPasswordlessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public function testCodeEmailFlowNoRefreshToken(){
}

public function testCodeEmailFlowConsecutiveOTP(){

$scope = sprintf('%s profile email address',
OAuth2Protocol::OpenIdConnect_Scope,
);
Expand All @@ -251,6 +252,7 @@ public function testCodeEmailFlowConsecutiveOTP(){

$this->assertResponseStatus(200);
$otp1 = null;

Mail::assertNotQueued(OAuth2PasswordlessOTPMail::class, function(OAuth2PasswordlessOTPMail $email) use(&$otp1){
$otp1 = $email->otp;
});
Expand Down Expand Up @@ -286,7 +288,7 @@ public function testCodeEmailFlowConsecutiveOTP(){
$repository = EntityManager::getRepository(OAuth2OTP::class);

$otp1 = $repository->getByValue($otp1);
$this->assertTrue(is_null($otp1));
$this->assertTrue(!is_null($otp1));
$otp2 = $repository->getByValue($otp2);
$this->assertTrue(!is_null($otp2));
}
Expand Down
27 changes: 1 addition & 26 deletions tests/ServicesTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,13 @@ public function setUp():void
$this->redis->flushall();
}

public function testConsentService(){
$service = App::make(IUserConsentService::class);
$client_repository = EntityManager::getRepository(Client::class);
$client = $client_repository->getById(106);

$consent = $service->getOneByUserAndClientAndScopes(
763,
$client->getClientId(),
"openid offline_access profile https://openstackid-resources.openstack.org/summits/read https://openstackid-resources.openstack.org/summits/write https://openstackid-resources.openstack.org/summits/read-external-orders https://openstackid-resources.openstack.org/summits/confirm-external-orders"
);
}

public function testServerExtensionsService(){
$service = App::make(IServerExtensionsService::class);
$this->assertTrue($service instanceof IServerExtensionsService);
$extensions = $service->getAllActiveExtensions();

$this->assertTrue(!is_null($extensions));
$this->assertTrue(count($extensions));
$this->assertTrue(count($extensions) > 0);
}

public function testBlackListPolicy(){

$ip_helper_mock = Mockery::mock(IUserIPHelperProvider::class);
$ip_helper_mock->shouldReceive('getCurrentUserIpAddress')->andReturn("174.1.1.1");

$this->app->instance(IUserIPHelperProvider::class, $ip_helper_mock);

$delay_counter_measure = App::make(\Services\SecurityPolicies\DelayCounterMeasure::class);
$blacklist_security_policy = App::make(\Services\SecurityPolicies\BlacklistSecurityPolicy::class);
$blacklist_security_policy->setCounterMeasure($delay_counter_measure);
$ex = new ReplayAttackException();
$blacklist_security_policy->apply($ex);
}
}