Vigiar
1
0
Derivar
Você já tinha feito uma derivação do repositório laravel, anteriormente
0
laravel/tests/run.php

140 linhas
7,7 KiB
PHP

<?php
// Unit tests, offline: php tests/run.php
require __DIR__ . '/bootstrap.php';
use InterwebAo\Laravel\ApiException;
use InterwebAo\Laravel\Api\Dns;
use InterwebAo\Laravel\Mail\Payload;
// Key and headers.
$c = new FakeClient(['api_key' => 'sandbox_test', 'language' => 'pt', 'mode' => 'sandbox', 'retries' => 0]);
$c->answers[] = [200, ['x-api-mode' => 'sandbox'], ['data' => ['status' => 'ok']]];
$c->ping();
check('ping sends no key', $c->header(0, 'Authorization') === null);
$c->answers[] = [200, [], ['data' => ['mode' => 'sandbox']]];
$c->token();
check('token sends the Bearer key', $c->header(1, 'Authorization') === 'Bearer sandbox_test');
check('language and mode headers', $c->header(1, 'Accept-Language') === 'pt' && $c->header(1, 'X-API-Mode') === 'sandbox');
check('fixed address', strpos($c->sent[1]['url'], \InterwebAo\Laravel\Client::API_URL . '/v1/token') === 0);
// No key.
putenv(\InterwebAo\Laravel\Client::KEY_ENV);
$n = new FakeClient([]);
try {
$n->token();
check('no key throws', false);
} catch (ApiException $e) {
check('no key throws 401 before any request', $e->getStatus() === 401 && !$n->sent);
}
// POST gets an Idempotency-Key, kept on the retry after 503.
$c = new FakeClient(['api_key' => 'k', 'retries' => 1]);
$c->answers[] = [503, [], ['error' => ['code' => 'disabled_x', 'message' => 'busy']]];
$c->answers[] = [201, ['x-api-simulated' => 'true'], ['data' => ['id' => '9', 'status' => 'sent']]];
$r = $c->sms()->send('+244923123456', 'Hello');
check('SMS sent after one retry', ($r['data']['id'] ?? null) === '9' && count($c->sent) === 2);
check('same Idempotency-Key on the retry', $c->header(0, 'Idempotency-Key') && $c->header(0, 'Idempotency-Key') === $c->header(1, 'Idempotency-Key'));
check('simulated is seen', $c->wasSimulated());
check('SMS body', json_decode($c->sent[0]['body'], true) === ['number' => '+244923123456', 'text' => 'Hello']);
// Errors.
$c = new FakeClient(['api_key' => 'k', 'retries' => 2]);
$c->answers[] = [402, [], ['error' => ['code' => 'insufficient_credit', 'message' => 'Not enough credit.', 'correlation_id' => 'abc', 'details' => ['credit_missing' => 10]]]];
try {
$c->domains()->register('example.co.ao', 1);
check('402 throws', false);
} catch (ApiException $e) {
check('402 is not retried', count($c->sent) === 1);
check('402 details', $e->isInsufficientCredit() && $e->getApiCode() === 'insufficient_credit' && $e->getCorrelationId() === 'abc'
&& $e->getDetails()['credit_missing'] === 10 && $e->hint() !== '');
}
$c = new FakeClient(['api_key' => 'k', 'retries' => 0]);
$c->answers[] = [429, ['retry-after' => '7'], ['error' => ['code' => 'rate_limited', 'message' => 'Too many.']]];
try {
$c->dns()->records('example.co.ao');
} catch (ApiException $e) {
check('429 gives Retry-After', $e->isRateLimited() && $e->getRetryAfter() === 7);
}
$c = new FakeClient(['api_key' => 'k', 'retries' => 0]);
$c->answers[] = [500, [], '<html>oops</html>'];
try {
$c->get('/v1/account');
} catch (ApiException $e) {
check('non-JSON error', $e->getStatus() === 500 && $e->getApiCode() === 'http_500');
}
// DNS names.
check('relative name of www.domain', Dns::relativeName('www.example.co.ao.', 'example.co.ao') === 'www');
check('relative name of the domain', Dns::relativeName('example.co.ao', 'example.co.ao') === '@');
check('relative name kept', Dns::relativeName('Home', 'example.co.ao') === 'home');
// DNS upsert: creates, changes or leaves alone.
$zone = ['data' => [
['id' => 'a1', 'name' => '@', 'type' => 'A', 'value' => '192.0.2.10', 'ttl' => 14400],
['id' => 'b2', 'name' => 'home', 'type' => 'A', 'value' => '192.0.2.20', 'ttl' => 300],
]];
$c = new FakeClient(['api_key' => 'k']);
$c->answers[] = [200, [], $zone];
$r = $c->dns()->upsert('example.co.ao', 'home', 'A', '192.0.2.20', 300);
check('upsert with the same value does not write', count($c->sent) === 1 && $r['id'] === 'b2');
$c->answers[] = [200, [], $zone];
$c->answers[] = [200, [], ['data' => ['id' => 'c3', 'name' => 'home', 'type' => 'A', 'value' => '192.0.2.30', 'ttl' => 300]]];
$r = $c->dns()->upsert('example.co.ao', 'home.example.co.ao', 'a', '192.0.2.30', 300);
check('upsert changes the record with PUT', $c->sent[2]['method'] === 'PUT' && substr($c->sent[2]['url'], -7) === '/dns/b2' && $r['id'] === 'c3');
$c->answers[] = [200, [], $zone];
$c->answers[] = [201, [], ['data' => ['id' => 'd4', 'name' => 'vpn', 'type' => 'AAAA', 'value' => '2001:db8::1']]];
$c->dns()->upsert('example.co.ao', 'vpn', 'AAAA', '2001:db8::1');
check('upsert creates with POST', $c->sent[4]['method'] === 'POST' && json_decode($c->sent[4]['body'], true) === ['name' => 'vpn', 'type' => 'AAAA', 'value' => '2001:db8::1']);
// WHOIS goes without a key when there is none.
$c = new FakeClient([]);
$c->answers[] = [200, [], ['data' => ['status' => 'available']]];
$c->whois()->lookup('example.co.ao', 'USD');
check('WHOIS without a key', $c->header(0, 'Authorization') === null && strpos($c->sent[0]['url'], '/v1/whois/' . rawurlencode('example.co.ao') . '?currency=USD') !== false);
// Mail payload.
$p = Payload::build(['shop@example.co.ao', 'My Shop'], ['ana@example.org'], [], ['audit@example.org'], [], 'Order 1234',
'<p>Hi</p>', 'Hi', ['X-Order-Id' => '1234', 'Message-ID' => '<x@y>', 'X-Mailer' => 'z', 'List-Unsubscribe' => '<mailto:u@x>', 'Received' => 'no'],
[['filename' => 'a.txt', 'content' => base64_encode('A'), 'content_type' => 'text/plain']], ['template_id' => 7, 'transport' => 'interweb_ao']);
check('mail payload fields', $p['from'] === 'shop@example.co.ao' && $p['from_name'] === 'My Shop' && $p['to'] === ['ana@example.org']
&& $p['bcc'] === ['audit@example.org'] && !isset($p['cc']) && $p['subject'] === 'Order 1234' && $p['template_id'] === 7 && !isset($p['transport']));
check('mail payload keeps only own headers', $p['headers'] === ['X-Order-Id' => '1234', 'List-Unsubscribe' => '<mailto:u@x>']);
// SMS channel (with small stand-ins when Laravel is not here).
if (!class_exists('Illuminate\Notifications\Notification')) {
eval('namespace Illuminate\Notifications; class Notification { public $id; }');
}
class OrderReady extends \Illuminate\Notifications\Notification
{
public function toSms($n)
{
return \InterwebAo\Laravel\Notifications\SmsMessage::create('Order 1234 is ready.')->sendAt(new DateTimeImmutable('2030-01-02 09:30:00', new DateTimeZone('+02:00')));
}
}
class Customer
{
public function routeNotificationFor($driver, $n = null)
{
return $driver === 'sms' ? '+244923123456' : null;
}
}
$c = new FakeClient(['api_key' => 'k']);
$c->answers[] = [201, [], ['data' => ['id' => '77']]];
$ch = new \InterwebAo\Laravel\Notifications\SmsChannel($c, 'MYSHOP');
$note = new OrderReady();
$note->id = 'b2f1c7e0-0000-4000-8000-000000000001';
$r = $ch->send(new Customer(), $note);
$b = json_decode($c->sent[0]['body'], true);
check('SMS channel sends', $r['id'] === '77' && $b['number'] === '+244923123456' && $b['sender'] === 'MYSHOP' && $b['send_at'] === '2030-01-02T09:30:00+02:00');
check('SMS channel key comes from the notification id', strpos((string) $c->header(0, 'Idempotency-Key'), 'notification-' . $note->id . '-') === 0);
$c->answers = [];
check('no number, nothing sent', $ch->send(new stdClass(), $note) === null);
// Clear hints for the key check.
$e = new ApiException(400, 'invalid_request', 'The X-API-Mode header does not match the mode of this key.', ['reason' => 'mode_mismatch', 'key_mode' => 'sandbox']);
check('mode mismatch is seen', $e->isModeMismatch() && strpos($e->hint(), 'a sandbox key') !== false);
$e = new ApiException(403, 'insufficient_scope', 'This key does not have the required permission.', ['required' => 'dns:write']);
check('missing permission is named', !$e->isModeMismatch() && strpos($e->hint(), 'dns:write') !== false);
done();