<?php

namespace AuthStack\EncryptedLicense\tests\units;

use AuthStack\EncryptedLicense\Encrypter as Enc;
use AuthStack\EncryptedLicense\License;
use mageekguy\atoum;

class Encrypter extends atoum
{
    public function testEncryptionAndDecryption()
    {
        $this->given($license = new License([
            'startsAt' => time() - 100,
            'endsAt' => (new \DateTime("now", new \DateTimeZone('UTC')))->modify('+3 months')->getTimestamp(),
            'domain' => ['test.authstack.co.uk', 'idp.authstack.co.uk'],
            'gracePeriod' => '+7 days'
        ]))
            ->then
            ->object($license)->isInstanceOf(License::class)
            ->string($encrypted = Enc::encrypt($license, $this->getPrivateKey()))->hasLength(strlen($encrypted))
            ->object($decrypted = Enc::decrypt($encrypted, $this->getPublicKey()))->isInstanceOf(License::class)
            ->boolean($decrypted->isStarted())->isTrue()
            ->boolean($decrypted->isExpired())->isFalse()
            ->boolean($decrypted->validateDomain('test.authstack.co.uk'))->isTrue()
            ->boolean($decrypted->validateDomain('nonexistent.domain.com'))->isFalse()
        ;
    }

    public function testLicenceExpiry()
    {
        $this
            ->given($license = new License([
                'startsAt' => time() - 100,
                'endsAt' => (new \DateTime("now", new \DateTimeZone('UTC')))->modify('-2 days')->getTimestamp(),
                'domain' => ['test.authstack.co.uk', 'idp.authstack.co.uk'],
                'gracePeriod' => '+3 days'
            ]))
            ->and($expired = new License([
                'startsAt' => time() - 100,
                'endsAt' => (new \DateTime("now", new \DateTimeZone('UTC')))->modify('-4 days')->getTimestamp(),
                'domain' => ['test.authstack.co.uk', 'idp.authstack.co.uk'],
                'gracePeriod' => '+1 days'
            ]))
            ->then
                ->boolean($license->isExpired())->isTrue()
                ->boolean($license->isInGracePeriod())->isTrue()
                ->boolean($license->isValid())->isTrue()

                ->boolean($expired->isExpired())->isTrue()
                ->boolean($expired->isInGracePeriod())->isFalse()
                ->boolean($expired->isValid())->isFalse()
            ;
    }

    protected function getPrivateKey(): string
    {
        return <<<EOF
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCB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ienKQ5/QKBgDrt59F96TrSxgz+67t3Yr1BsDTVqUVnf/0V29spDtodLVOxB2YQ
tegYTzrOWyQ3+qsQ74TE/uDwDM3I05N3oC7nj50z3yiZ/DRHaiWdvNskpUlR3cxI
gaV1SI5M1uvn7dLFzfOt/GdFDhr/K+BOhKlXTlJQxGSoCQxW25GgJbwBAoGAMskf
BN1V9cD3fO4L7O9CKX/nxa35ejJc3PD0Hsj7GYjynawi1qMHSMRpCcmZ8TrDnhxW
pdtsFOtc3bnB8dT6KDgpBvwUVk2UzliQmdCSzfdUfP28VolOHi7gJaxc69V9EB0m
sXu9lP9iQstX/F6/iaUZPZtDEyssNafUcbZQVpkCgYEA4NdZB48TLc3NJzQ/2zVq
Hb2eApbMhSw7lY26pN0F98lK3r+xtciUPpTna4XoowkfaGz1Iq+Qby09VyRc63CK
G3lG6Zn/R9mI+4JLknsWr8X8laGwF0gksb/DPaxfHXdD0Y8V05lTq/uu/soxeHNg
h1sDx7Bro0c8d3jKLG44J1Q=
-----END PRIVATE KEY-----

EOF;

    }

    protected function getPublicKey(): string
    {
        return <<<EOF
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvDjvVZlAyIMOnpUnd7Gs
9DJ3vA1gDKn9ViCWxdj78Z6ZMb1j/mEc9rf12hMnT4k7lTm9Z6aRT803SsxM+CPP
BqHs6i5kI33YqA7wRh9WOPrLOCmuSeVpb+6SxJLAYAiXj+kOzRZSD+ZK1mo6Y0Cs
xKrExb1lPGD6nfTMsUCU+SsOamJH3mBjKHLrG5vw85okX+NazCz2njHvRWLoG5m7
gMppB00MI1K3BLQqgMhuJM9FGCVt6tja8kJAY7sqM/xW/citILQwkMPzJJCnkQnT
XCR4YGRrbkw/Qj+99rq+li6K+NzKi6x/O48Ald4RN+81jTowzl7mWzOvZllPgdKX
PQIDAQAB
-----END PUBLIC KEY-----

EOF;

    }
}