Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed May 16, 2023
2 parents 0dc217b + a8f6c33 commit 3c6d784
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/Verify2/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vonage\Verify2;

use Laminas\Diactoros\Request;
use Vonage\Client\APIClient;
use Vonage\Client\APIResource;
use Vonage\Client\Exception\Exception;
Expand All @@ -27,7 +26,7 @@ public function startVerification(BaseVerifyRequest $request): ?array
public function check(string $requestId, $code): bool
{
try {
$response = $this->getAPIResource()->create(['code' => $code], $requestId);
$response = $this->getAPIResource()->create(['code' => $code], '/' . $requestId);
} catch (Exception $e) {
// For horrible reasons in the API Error Handler, throw the error unless it's a 409.
if ($e->getCode() === 409) {
Expand All @@ -39,4 +38,11 @@ public function check(string $requestId, $code): bool

return true;
}

public function cancelRequest(string $requestId): bool
{
$this->api->delete($requestId);

return true;
}
}
2 changes: 1 addition & 1 deletion src/Verify2/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __invoke(ContainerInterface $container): Client
$api->setIsHAL(false)
->setErrorsOn200(false)
->setAuthHandler([new KeypairHandler(), new BasicHandler()])
->setBaseUrl('https://api.nexmo.com/v2/verify/');
->setBaseUrl('https://api.nexmo.com/v2/verify');

return new Client($api);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Verify2/Request/BaseVerifyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ abstract class BaseVerifyRequest implements RequestInterface

protected int $timeout = 300;

protected bool $fraudCheck = true;

protected ?string $clientRef = null;

protected int $length = 4;
Expand Down Expand Up @@ -132,9 +134,22 @@ public function addWorkflow(VerificationWorkflow $verificationWorkflow): static
return $this;
}

public function getFraudCheck(): bool
{
return $this->fraudCheck;
}

public function setFraudCheck(bool $fraudCheck): BaseVerifyRequest
{
$this->fraudCheck = $fraudCheck;

return $this;
}

public function getBaseVerifyUniversalOutputArray(): array
{
$returnArray = [
'fraud_check' => $this->getFraudCheck(),
'locale' => $this->getLocale()->getCode(),
'channel_timeout' => $this->getTimeout(),
'code_length' => $this->getLength(),
Expand Down
50 changes: 48 additions & 2 deletions test/Verify2/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function setUp(): void
->setErrorsOn200(false)
->setClient($this->vonageClient->reveal())
->setAuthHandler([new Client\Credentials\Handler\BasicHandler(), new Client\Credentials\Handler\KeypairHandler()])
->setBaseUrl('https://api.nexmo.com/v2/verify/');
->setBaseUrl('https://api.nexmo.com/v2/verify');

$this->verify2Client = new Verify2Client($this->api);
}
Expand Down Expand Up @@ -93,11 +93,12 @@ public function testCanRequestSMS(): void
$uri = $request->getUri();
$uriString = $uri->__toString();
$this->assertEquals(
'https://api.nexmo.com/v2/verify/',
'https://api.nexmo.com/v2/verify',
$uriString
);

$this->assertRequestJsonBodyContains('locale', 'en-us', $request);
$this->assertRequestJsonBodyContains('fraud_check', true, $request);
$this->assertRequestJsonBodyContains('channel_timeout', 300, $request);
$this->assertRequestJsonBodyContains('client_ref', $payload['client_ref'], $request);
$this->assertRequestJsonBodyContains('code_length', 4, $request);
Expand All @@ -115,6 +116,29 @@ public function testCanRequestSMS(): void
$this->assertArrayHasKey('request_id', $result);
}

public function testCanBypassFraudCheck(): void
{
$payload = [
'to' => '07785254785',
'client_ref' => 'my-verification',
'brand' => 'my-brand',
];

$smsVerification = new SMSRequest($payload['to'], $payload['brand']);
$smsVerification->setFraudCheck(false);

$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
$this->assertRequestJsonBodyContains('fraud_check', false, $request);

return true;
}))->willReturn($this->getResponse('verify-request-success', 202));

$result = $this->verify2Client->startVerification($smsVerification);

$this->assertIsArray($result);
$this->assertArrayHasKey('request_id', $result);
}

/**
* @dataProvider localeProvider
*/
Expand Down Expand Up @@ -591,6 +615,28 @@ public function testCheckHandlesThrottle(): void
$result = $this->verify2Client->check('c11236f4-00bf-4b89-84ba-88b25df97315', '24525');
}

public function testWillCancelVerification(): void
{
$requestId = 'c11236f4-00bf-4b89-84ba-88b25df97315';

$this->vonageClient->send(Argument::that(function (Request $request) {
$uri = $request->getUri();
$uriString = $uri->__toString();
$this->assertEquals(
'https://api.nexmo.com/v2/verify/c11236f4-00bf-4b89-84ba-88b25df97315',
$uriString
);

$this->assertEquals('DELETE', $request->getMethod());

return true;
}))->willReturn($this->getResponse('verify-cancel-success', 204));

$result = $this->verify2Client->cancelRequest($requestId);

$this->assertTrue($result);
}

/**
* This method gets the fixtures and wraps them in a Response object to mock the API
*/
Expand Down
Empty file.

0 comments on commit 3c6d784

Please sign in to comment.