-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOTPModelTest.php
More file actions
102 lines (85 loc) · 3.58 KB
/
Copy pathOTPModelTest.php
File metadata and controls
102 lines (85 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php namespace Tests;
/**
* Copyright 2021 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\Models\OAuth2\Factories\OTPFactory;
use Illuminate\Support\Facades\App;
use LaravelDoctrine\ORM\Facades\EntityManager;
use Models\OAuth2\Client;
use OAuth2\Factories\OAuth2AuthorizationRequestFactory;
use OAuth2\OAuth2Message;
use OAuth2\OAuth2Protocol;
use Utils\Services\IdentifierGenerator;
/**
* Class OTPModelTest
* @package Tests
*/
class OTPModelTest extends BrowserKitTestCase
{
/**
* @var Client
*/
static $aauth2_client;
protected function setUp():void
{
parent::setUp();
}
protected function tearDown():void
{
parent::tearDown();
}
public function testCreateFromRequest(){
$client_repository = EntityManager::getRepository(Client::class);
$clients = $client_repository->findAll();
$this->assertTrue(count($clients) > 0);
$client = $clients[0];
if(!$client instanceof Client) return;
$client->enablePasswordless();
$client->setOtpLifetime(60 * 3);
$client->setOtpLength(6);
EntityManager::persist($client);
$values =
[
OAuth2Protocol::OAuth2Protocol_ClientId => $client->getClientId(),
OAuth2Protocol::OAuth2Protocol_ResponseType => OAuth2Protocol::OAuth2Protocol_ResponseType_OTP,
OAuth2Protocol::OAuth2PasswordlessConnection => OAuth2Protocol::OAuth2PasswordlessConnectionEmail,
OAuth2Protocol::OAuth2PasswordlessSend => OAuth2Protocol::OAuth2PasswordlessSendCode,
OAuth2Protocol::OAuth2PasswordlessEmail => "test@test.com",
OAuth2Protocol::OAuth2Protocol_Scope => "test_scope",
OAuth2Protocol::OAuth2Protocol_Nonce => "123456"
];
$request = OAuth2AuthorizationRequestFactory::getInstance()->build
(
new OAuth2Message($values)
);
$this->assertTrue($request->isValid());
$otp = OTPFactory::buildFromRequest($request, App::make(IdentifierGenerator::class), $client);
EntityManager::persist($client);
EntityManager::flush();
$this->assertTrue($client->getOTPGrantsByEmailNotRedeemed("test@test.com")->count() > 0);
$this->assertTrue(strlen($otp->getValue()) == $client->getOtpLength());
}
public function testCreateFromPayloadNoClient(){
$payload =
[
OAuth2Protocol::OAuth2Protocol_ResponseType => OAuth2Protocol::OAuth2Protocol_ResponseType_OTP,
OAuth2Protocol::OAuth2PasswordlessConnection => OAuth2Protocol::OAuth2PasswordlessConnectionEmail,
OAuth2Protocol::OAuth2PasswordlessSend => OAuth2Protocol::OAuth2PasswordlessSendCode,
OAuth2Protocol::OAuth2PasswordlessEmail => "test@test.com",
OAuth2Protocol::OAuth2Protocol_Scope => "test_scope"
];
$otp = OTPFactory::buildFromPayload($payload, App::make(IdentifierGenerator::class));
EntityManager::persist($otp);
EntityManager::flush();
$this->assertTrue($otp->getId() > 0);
}
}