Skip to content

Commit 8a65685

Browse files
author
Greg Bowler
committed
Test login redirects to localhost on http
1 parent edf3807 commit 8a65685

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/AuthUri.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ private function normaliseBaseUri(string $baseUri):Uri {
4040
?? "https";
4141
$host = parse_url($baseUri, PHP_URL_HOST)
4242
?? parse_url($baseUri, PHP_URL_PATH);
43+
$port = parse_url($baseUri, PHP_URL_PORT)
44+
?? 80;
4345

4446
$uri = (new Uri())
4547
->withScheme($scheme)
46-
->withHost($host);
48+
->withHost($host)
49+
->withPort($port);
4750

4851
if($uri->getHost() !== "localhost"
4952
&& $uri->getScheme() !== "https") {

src/Authenticator.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public function login():void {
6262
}
6363

6464
$token = new Token($this->clientKey, $this->clientSecret);
65-
$loginUri = new AuthUri($token, $this->authwaveHost);
65+
$loginUri = new AuthUri(
66+
$token,
67+
$this->redirectPath,
68+
$this->authwaveHost
69+
);
6670
$this->redirectHandler->redirect($loginUri);
6771
}
6872

test/phpunit/AuthenticatorTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,27 @@ public function testLoginRedirects() {
8989
);
9090
$sut->login();
9191
}
92+
93+
public function testLoginRedirectsLocalhost() {
94+
$_SESSION = [];
95+
96+
$redirectHandler = self::createMock(RedirectHandler::class);
97+
$redirectHandler->expects(self::once())
98+
->method("redirect")
99+
->with(self::callback(fn(UriInterface $uri) =>
100+
$uri->getScheme() === "http"
101+
&& $uri->getHost() === "localhost"
102+
&& $uri->getPort() === 8081
103+
));
104+
105+
$sut = new Authenticator(
106+
"test-key",
107+
"test-secret",
108+
"/",
109+
"http://localhost:8081",
110+
null,
111+
$redirectHandler
112+
);
113+
$sut->login();
114+
}
92115
}

0 commit comments

Comments
 (0)