Skip to content

Commit

Permalink
Test AuthURI consists of correct parts
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Feb 27, 2020
1 parent 4c32d77 commit 466f7cc
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/AuthUri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace Authwave;

use Gt\Http\Uri;
use Psr\Http\Message\UriInterface;

class AuthUri extends Uri {
const QUERY_STRING_CIPHER = "cipher";
const QUERY_STRING_INIT_VECTOR = "iv";
const QUERY_STRING_RETURN_PATH = "return";

public function __construct(
UriInterface $baseUri,
Token $token,
string $returnPath
) {
parent::__construct($baseUri);

$this->query = http_build_query([
self::QUERY_STRING_CIPHER => (string)$token->generateCipher(),
self::QUERY_STRING_INIT_VECTOR => (string)$token->getIv(),
self::QUERY_STRING_RETURN_PATH => base64_encode($returnPath),
]);
}
}
57 changes: 57 additions & 0 deletions test/phpunit/AuthUriTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace Authwave\Test;

use Authwave\AuthUri;
use Authwave\InitVector;
use Authwave\Token;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface;

class AuthUriTest extends TestCase {
public function testAuthUriHttps() {
$baseUri = self::createMock(UriInterface::class);
$baseUri->method("__toString")
->willReturn("https://example.com");
$token = self::createMock(Token::class);

$sut = new AuthUri($baseUri, $token, "");
self::assertEquals(
"https",
$sut->getScheme()
);
}

public function testQueryString() {
$mockCipherValue = str_repeat("f", 16);
$mockIvValue = str_repeat("0", 16);
$iv = self::createMock(InitVector::class);
$iv->method("__toString")
->willReturn($mockIvValue);

$baseUri = self::createMock(UriInterface::class);
$token = self::createMock(Token::class);
$token->method("generateCipher")
->willReturn($mockCipherValue);
$token->method("getIv")
->willReturn($iv);

$returnPath = "/examplePage";
$sut = new AuthUri($baseUri, $token, $returnPath);
parse_str($sut->getQuery(), $queryParts);

self::assertEquals(
$mockCipherValue,
$queryParts[AuthUri::QUERY_STRING_CIPHER],
);

self::assertEquals(
$mockIvValue,
$queryParts[AuthUri::QUERY_STRING_INIT_VECTOR]
);

self::assertEquals(
base64_encode($returnPath),
$queryParts[AuthUri::QUERY_STRING_RETURN_PATH]
);
}
}

0 comments on commit 466f7cc

Please sign in to comment.