<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Marketing\Emailing;

use CpCreation\VitiCore\Marketing\Emailing\BrevoSecretCipher;
use PHPUnit\Framework\TestCase;

final class BrevoSecretCipherTest extends TestCase
{
    public function testItEncryptsTheApiKeyWithARandomizedAuthenticatedEnvelope(): void
    {
        $cipher = new BrevoSecretCipher('test-application-secret');

        $first = $cipher->encrypt('api-key-value');
        $second = $cipher->encrypt('api-key-value');

        self::assertNotSame($first, $second);
        self::assertStringNotContainsString('api-key-value', $first);
        self::assertSame('api-key-value', $cipher->decrypt($first));
    }

    public function testItRejectsATamperedEnvelope(): void
    {
        $cipher = new BrevoSecretCipher('test-application-secret');
        $envelope = $cipher->encrypt('api-key-value');

        $this->expectException(\RuntimeException::class);
        $cipher->decrypt(substr($envelope, 0, -1) . ('A' === substr($envelope, -1) ? 'B' : 'A'));
    }
}
