Skip to content

Commit

Permalink
Fixing the tests for auth package
Browse files Browse the repository at this point in the history
I had to change loginHeaders() for the Basic and Digest authenticator. I've had a short discussion with Jose about this and we agreed on doing a BC breaking change here.
  • Loading branch information
burzum authored and markstory committed Nov 16, 2017
1 parent 09e1444 commit 42a46a9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
8 changes: 5 additions & 3 deletions src/Auth/BasicAuthenticate.php
Expand Up @@ -94,20 +94,22 @@ public function getUser(ServerRequest $request)
public function unauthenticated(ServerRequest $request, Response $response)
{
$Exception = new UnauthorizedException();
$Exception->responseHeader([$this->loginHeaders($request)]);
$Exception->responseHeader($this->loginHeaders($request));
throw $Exception;
}

/**
* Generate the login headers
*
* @param \Cake\Http\ServerRequest $request Request object.
* @return string Headers for logging in.
* @return array Headers for logging in.
*/
public function loginHeaders(ServerRequest $request)
{
$realm = $this->getConfig('realm') ?: $request->getEnv('SERVER_NAME');

return sprintf('WWW-Authenticate: Basic realm="%s"', $realm);
return [
'WWW-Authenticate' => sprintf('Basic realm="%s"', $realm)
];
}
}
6 changes: 4 additions & 2 deletions src/Auth/DigestAuthenticate.php
Expand Up @@ -212,7 +212,7 @@ public static function password($username, $password, $realm)
* Generate the login headers
*
* @param \Cake\Http\ServerRequest $request Request object.
* @return string Headers for logging in.
* @return array Headers for logging in.
*/
public function loginHeaders(ServerRequest $request)
{
Expand Down Expand Up @@ -240,7 +240,9 @@ public function loginHeaders(ServerRequest $request)
}
}

return 'WWW-Authenticate: Digest ' . implode(',', $opts);
return [
'WWW-Authenticate' => 'Digest ' . implode(',', $opts)
];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Auth/BasicAuthenticateTest.php
Expand Up @@ -181,7 +181,7 @@ public function testAuthenticateChallenge()

$this->assertNotEmpty($e);

$expected = ['WWW-Authenticate: Basic realm="localhost"'];
$expected = ['WWW-Authenticate' => 'Basic realm="localhost"'];
$this->assertEquals($expected, $e->responseHeader());
}

Expand Down
28 changes: 14 additions & 14 deletions tests/TestCase/Auth/DigestAuthenticateTest.php
Expand Up @@ -126,7 +126,7 @@ public function testAuthenticateWrongUsername()
'cnonce' => '0a4f113b'
];
$data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
$request->env('PHP_AUTH_DIGEST', $this->digestHeader($data));
$request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));

$this->auth->unauthenticated($request, $this->response);
}
Expand All @@ -151,10 +151,10 @@ public function testAuthenticateChallenge()

$this->assertNotEmpty($e);

$header = $e->responseHeader()[0];
$header = $e->responseHeader();
$this->assertRegexp(
'/^WWW\-Authenticate: Digest realm="localhost",qop="auth",nonce="[a-zA-Z0-9=]+",opaque="123abc"$/',
$e->responseHeader()[0]
'/^Digest realm="localhost",qop="auth",nonce="[a-zA-Z0-9=]+",opaque="123abc"$/',
$header['WWW-Authenticate']
);
}

Expand All @@ -178,15 +178,15 @@ public function testAuthenticateChallengeIncludesStaleAttributeOnStaleNonce()
'qop' => 'auth',
];
$data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
$request->env('PHP_AUTH_DIGEST', $this->digestHeader($data));
$request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));

try {
$this->auth->unauthenticated($request, $this->response);
} catch (UnauthorizedException $e) {
}
$this->assertNotEmpty($e);

$header = $e->responseHeader()[0];
$header = $e->responseHeader()['WWW-Authenticate'];
$this->assertContains('stale=true', $header);
}

Expand All @@ -211,7 +211,7 @@ public function testAuthenticateFailsOnStaleNonce()
'qop' => 'auth',
];
$data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
$request->env('PHP_AUTH_DIGEST', $this->digestHeader($data));
$request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
$result = $this->auth->authenticate($request, $this->response);
$this->assertFalse($result, 'Stale nonce should fail');
}
Expand Down Expand Up @@ -239,7 +239,7 @@ public function testAuthenticateValidUsernamePasswordNoNonce()
'qop' => 'auth',
];
$data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
$request->env('PHP_AUTH_DIGEST', $this->digestHeader($data));
$request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
$result = $this->auth->authenticate($request, $this->response);
$this->assertFalse($result, 'Empty nonce should fail');
}
Expand All @@ -265,7 +265,7 @@ public function testAuthenticateSuccess()
'qop' => 'auth',
];
$data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
$request->env('PHP_AUTH_DIGEST', $this->digestHeader($data));
$request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));

$result = $this->auth->authenticate($request, $this->response);
$expected = [
Expand Down Expand Up @@ -301,7 +301,7 @@ public function testAuthenticateSuccessHiddenPasswordField()
'qop' => 'auth',
];
$data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
$request->env('PHP_AUTH_DIGEST', $this->digestHeader($data));
$request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));

$result = $this->auth->authenticate($request, $this->response);
$expected = [
Expand Down Expand Up @@ -336,7 +336,7 @@ public function testAuthenticateSuccessSimulatedRequestMethod()
'qop' => 'auth',
];
$data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
$request->env('PHP_AUTH_DIGEST', $this->digestHeader($data));
$request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));

$result = $this->auth->authenticate($request, $this->response);
$expected = [
Expand Down Expand Up @@ -373,7 +373,7 @@ public function testAuthenticateFailReChallenge()
'qop' => 'auth',
];
$data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
$request->env('PHP_AUTH_DIGEST', $this->digestHeader($data));
$request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
$this->auth->unauthenticated($request, $this->response);
}

Expand All @@ -393,8 +393,8 @@ public function testLoginHeaders()
$result = $this->auth->loginHeaders($request);

$this->assertRegexp(
'/^WWW\-Authenticate: Digest realm="localhost",qop="auth",nonce="[a-zA-Z0-9=]+",opaque="[a-f0-9]+"$/',
$result
'/^Digest realm="localhost",qop="auth",nonce="[a-zA-Z0-9=]+",opaque="[a-f0-9]+"$/',
$result['WWW-Authenticate']
);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -510,7 +510,7 @@ public function testRedirectBeforeRedirectModifyingUrl()
$Controller = new Controller(null, new Response());

$Controller->getEventManager()->on('Controller.beforeRedirect', function (Event $event, $url, Response $response) {
$response->location('https://book.cakephp.org');
$response = $response->withLocation('https://book.cakephp.org');
});

$response = $Controller->redirect('http://cakephp.org', 301);
Expand All @@ -531,13 +531,13 @@ public function testRedirectBeforeRedirectModifyingStatusCode()
$Controller = new Controller(null, $Response);

$Controller->getEventManager()->on('Controller.beforeRedirect', function (Event $event, $url, Response $response) {
$response->statusCode(302);
$response = $response->withStatus(302);
});

$response = $Controller->redirect('http://cakephp.org', 301);

$this->assertEquals('http://cakephp.org', $response->header()['Location']);
$this->assertEquals(302, $response->statusCode());
$this->assertEquals('http://cakephp.org', $response->getHeaderLine('Location'));
$this->assertEquals(302, $response->getStatusCode());
}

public function testRedirectBeforeRedirectListenerReturnResponse()
Expand Down

0 comments on commit 42a46a9

Please sign in to comment.