<?php

namespace Buckhill\License\tests\units\Encryption;

use mageekguy\atoum;
use Buckhill\License\Model\Seal;
use Buckhill\License\Model\License;
use Buckhill\License\Encryption\Encrypter as Encrypt;

class Encrypter extends atoum
{
    public function testCreateFromString()
    {
        $this->given($encrypter = Encrypt::createFromString(file_get_contents('.keys/testing.pem'), '1234'))
            ->then
                ->object($encrypter)->isInstanceOf(Encrypt::class)
            ;
    }

    public function testSeal()
    {
        $this
            ->given($encrypter = Encrypt::createFromString(file_get_contents('.keys/testing.pem'), '1234'))
            ->and($encryptedLicense = $this->getEncryptedLicense())
            ->then
                ->object($seal = $encrypter->seal($encryptedLicense))->isInstanceOf(Seal::class)
                ->string($seal->toString())->isNotEmpty()
            ;
    }



    /**
     * @return \Buckhill\License\Model\EncryptedLicense
     */
    protected function getEncryptedLicense()
    {
        $license = License::load($this->getValidLicenseInfo());

        return $license->encrypt(true);
    }

    /**
     * @return array
     */
    protected function getValidLicenseInfo()
    {
        return [
            'license' => [
                'id' => '6E6F5965A10A11E6A1670800274509D8',
                'startsAt' => 1481730870,
                'endsAt' => 1513266900,
                'gracePeriod' => '+7 days',
                'allowedNumberOfUsers' => 10,
                'numberOfProcessingServers' => 2
            ],

            /**
             * List of domains where license will function. If license is used at the domain that's not in this list,
             * the middleware checking the license will return an error, thus disabling the software
             */
            'domain' => ['localhost', '127.0.0.1'],


            /**
             * Information about two factor authentication (MFAStack functionality)
             */
            'twoFactor' => [
                'enabled' => false,
                'numberOfKeys' => [
                    'global' => 0,
                    'perPerson' => 0
                ]
            ],

            /**
             * Information about product that's being licensed
             */
            'product' => [
                'id' => 1,
                'title' => 'AuthStack IAM',
                'url' => 'https://www.buckhill.co.uk/products/authstack'
            ],

            /**
             * Information about contact person
             */
            'contact' => [
                'person' => 'Nikola B.',
                'email' => 'nikola@buckhill.co.uk'
            ],

            /**
             * Information about notification E-mail
             */
            'notificationEmail' => 'nikola@buckhill.co.uk',

            /**
             * Encryption information. This should be stored to permanent storage
             */
            'encryption' => [
                'alg' => 'AES-256-CTR',
                'key' => 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
                'iv' => '59BkphAozPiOfqPH'
            ]
        ];
    }
}
