Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth\Basic: improve tests #557

Merged
merged 4 commits into from
Oct 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 145 additions & 23 deletions tests/Auth/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,204 @@
use WpOrg\Requests\Auth\Basic;
use WpOrg\Requests\Exception;
use WpOrg\Requests\Requests;
use WpOrg\Requests\Response;
use WpOrg\Requests\Tests\TestCase;

/**
* @covers \WpOrg\Requests\Auth\Basic
*/
final class BasicTest extends TestCase {

/**
* @dataProvider transportProvider
*
* @param string $transport Transport to use.
*
* @return void
*/
public function testUsingArray($transport) {
if (!call_user_func(array($transport, 'test'))) {
$this->markTestSkipped($transport . ' is not available');
return;
}
$this->skipWhenTransportNotAvailable($transport);

$options = array(
'auth' => array('user', 'passwd'),
'transport' => $transport,
);
$request = Requests::get(httpbin('/basic-auth/user/passwd'), array(), $options);
$this->assertSame(200, $request->status_code);

// Verify the request succeeded.
$this->assertInstanceOf(
Response::class,
$request,
'GET request did not return an instance of `Requests\Response`'
);
$this->assertSame(
200,
$request->status_code,
'GET request failed. Expected status: 200. Received status: ' . $request->status_code
);

// Verify the response confirms that the request was authenticated.
$result = json_decode($request->body);
$this->assertTrue($result->authenticated);
$this->assertSame('user', $result->user);
$this->assertIsObject($result, 'Decoded response body is not an object');

$this->assertObjectHasAttribute(
'authenticated',
$result,
'Property "authenticated" not available in decoded response'
);
$this->assertTrue($result->authenticated, 'Authentication failed');

$this->assertObjectHasAttribute(
'user',
$result,
'Property "user" not available in decoded response'
);
$this->assertSame('user', $result->user, 'Unexpected value encountered for "user"');
}

/**
* @dataProvider transportProvider
*
* @param string $transport Transport to use.
*
* @return void
*/
public function testUsingInstantiation($transport) {
if (!call_user_func(array($transport, 'test'))) {
$this->markTestSkipped($transport . ' is not available');
return;
}
$this->skipWhenTransportNotAvailable($transport);

$options = array(
'auth' => new Basic(array('user', 'passwd')),
'transport' => $transport,
);
$request = Requests::get(httpbin('/basic-auth/user/passwd'), array(), $options);
$this->assertSame(200, $request->status_code);

// Verify the request succeeded.
$this->assertInstanceOf(
Response::class,
$request,
'GET request did not return an instance of `Requests\Response`'
);
$this->assertSame(
200,
$request->status_code,
'GET request failed. Expected status: 200. Received status: ' . $request->status_code
);

// Verify the response confirms that the request was authenticated.
$result = json_decode($request->body);
$this->assertTrue($result->authenticated);
$this->assertSame('user', $result->user);
$this->assertIsObject($result, 'Decoded response body is not an object');

$this->assertObjectHasAttribute(
'authenticated',
$result,
'Property "authenticated" not available in decoded response'
);
$this->assertTrue($result->authenticated, 'Authentication failed');

$this->assertObjectHasAttribute(
'user',
$result,
'Property "user" not available in decoded response'
);
$this->assertSame('user', $result->user, 'Unexpected value encountered for "user"');
}

/**
* @dataProvider transportProvider
*
* @param string $transport Transport to use.
*
* @return void
*/
public function testPOSTUsingInstantiation($transport) {
if (!call_user_func(array($transport, 'test'))) {
$this->markTestSkipped($transport . ' is not available');
return;
}
$this->skipWhenTransportNotAvailable($transport);

$options = array(
'auth' => new Basic(array('user', 'passwd')),
'transport' => $transport,
);
$data = 'test';
$request = Requests::post(httpbin('/post'), array(), $data, $options);
$this->assertSame(200, $request->status_code);

// Verify the request succeeded.
$this->assertInstanceOf(
Response::class,
$request,
'POST request did not return an instance of `Requests\Response`'
);
$this->assertSame(
200,
$request->status_code,
'POST request failed. Expected status: 200. Received status: ' . $request->status_code
);

// Verify the response confirms that the request was authenticated.
$result = json_decode($request->body);

$this->assertIsObject($result, 'Decoded response body is not an object');
$this->assertObjectHasAttribute(
'headers',
$result,
'Property "headers" not available in decoded response'
);
$this->assertObjectHasAttribute(
'Authorization',
$result->headers,
'Property "headers->Authorization" not available in decoded response'
);

$auth = $result->headers->Authorization;
$auth = explode(' ', $auth);
$this->assertArrayHasKey(1, $auth, 'Authorization header failed to be split into two parts');
$this->assertSame(base64_encode('user:passwd'), $auth[1], 'Unexpected authorization string in headers');

$this->assertSame(base64_encode('user:passwd'), $auth[1]);
$this->assertSame('test', $result->data);
$this->assertObjectHasAttribute(
'data',
$result,
'Property "data" not available in decoded response'
);
$this->assertSame('test', $result->data, 'Unexpected data value encountered');
}

public function testMissingPassword() {
/**
* Verify that an exception is thrown when the class is instantiated with an invalid number of arguments.
*
* @dataProvider dataInvalidArgumentCount
*
* @param mixed $input Input data.
*
* @return void
*/
public function testInvalidArgumentCount($input) {
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid number of arguments');
new Basic(array('user'));

new Basic($input);
}

/**
* Data Provider.
*
* @return array
*/
public function dataInvalidArgumentCount() {
return array(
'empty array' => array(array()),
'array with only one element' => array(array('user')),
'array with extra element' => array(array('user', 'psw', 'port')),
);
}

/**
* Helper function to skip select tests when the transport under test is not available.
*
* @param string $transport Transport to use.
*
* @return void
*/
public function skipWhenTransportNotAvailable($transport) {
if (!$transport::test()) {
$this->markTestSkipped('Transport "' . $transport . '" is not available');
}
}
}