Skip to content

Commit

Permalink
REST: Test cases for adding issues
Browse files Browse the repository at this point in the history
- Several test cases for valid/invalid create issue request.
- Test cases don’t run by default until bootstrapping is supported via a script.
  • Loading branch information
vboctor committed Apr 23, 2017
1 parent 827b098 commit 91d8faf
Show file tree
Hide file tree
Showing 4 changed files with 479 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/bootstrap.php.sample
Expand Up @@ -57,3 +57,17 @@ $GLOBALS['MANTIS_TESTSUITE_PROJECT_ID'] = 1;
*/
date_default_timezone_set('America/Los_Angeles');

/**
* Enable REST API Tests
*/
$GLOBALS['MANTIS_TESTSUITE_REST_ENABLED'] = true;

/**
* The base url for Mantis REST APIs
*/
$GLOBALS['MANTIS_TESTSUITE_REST_HOST'] = 'http://localhost/mantisbt/api/rest';

/**
* The API token to use
*/
$GLOBALS['MANTIS_TESTSUITE_API_TOKEN'] = '';
61 changes: 61 additions & 0 deletions tests/rest/AllTests.php
@@ -0,0 +1,61 @@
<?php
# Mantis - a php based bugtracking system

# Mantis is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Mantis is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mantis. If not, see <http://www.gnu.org/licenses/>.

/**
* Mantis Webservice Tests
*
* @package Tests
* @subpackage UnitTests
* @copyright Copyright MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/

/**
* Test config
*/
require_once __DIR__ . '/RestIssueAddTest.php';

/**
* Soap Test Suite
* @package Tests
* @subpackage UnitTests
* @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/
class RestAllTests extends PHPUnit_Framework_TestSuite
{
/**
* setUp
* @return void
*/
protected function setUp() {
if( !extension_loaded( 'curl' ) ) {
$this->markTestSuiteSkipped( 'The CURL extension is not available.' );
}
}

/**
* Initializes REST Test Suite
* @return RestAllTests
*/
public static function suite() {
$t_suite = new RestAllTests( 'REST API' );

$t_suite->addTestSuite( 'RestIssueAddTest' );

return $t_suite;
}
}
221 changes: 221 additions & 0 deletions tests/rest/RestBase.php
@@ -0,0 +1,221 @@
<?php
# MantisBT - A PHP based bugtracking system

# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.

/**
* Mantis Webservice Tests
*
* @package Tests
* @subpackage UnitTests
* @copyright Copyright MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/

$t_root_path = dirname( dirname( dirname( __FILE__ ) ) ) . DIRECTORY_SEPARATOR;

# MantisBT constants
require_once ( $t_root_path . DIRECTORY_SEPARATOR . 'core/constant_inc.php' );

/**
* Base class for REST API test cases
*
* @requires extension curl
* @group REST
*/
class RestBase extends PHPUnit_Framework_TestCase {
/**
* @var string Base path for REST API
*/
protected $base_path = '';

/**
* @var string Username
*/
protected $userName = 'administrator';

/**
* @var string The API token to use for authentication
*/
protected $token = '';

/**
* @var string Email address
*/
protected $email = 'root@localhost';

/**
* @var int User ID
*/
protected $userId = '1';

/**
* @var int Project ID
*/
protected $projectId = 1;

/**
* @var array Array of Issue IDs to delete
*/
private $issueIdsToDelete = array();

/**
* setUp
* @return void
*/
protected function setUp() {
if( !isset( $GLOBALS['MANTIS_TESTSUITE_REST_ENABLED'] ) ||
!$GLOBALS['MANTIS_TESTSUITE_REST_ENABLED'] ) {
$this->markTestSkipped( 'The REST API tests are disabled.' );
}

$this->assertTrue( array_key_exists( 'MANTIS_TESTSUITE_REST_HOST', $GLOBALS ) &&
!empty( $GLOBALS['MANTIS_TESTSUITE_REST_HOST'] ),
"You must define 'MANTIS_TESTSUITE_REST_HOST' in your bootstrap file" );

$this->base_path = trim( $GLOBALS['MANTIS_TESTSUITE_REST_HOST'], '/' );

if( array_key_exists( 'MANTIS_TESTSUITE_USERNAME', $GLOBALS ) ) {
$this->userName = $GLOBALS['MANTIS_TESTSUITE_USERNAME'];
} else {
$this->userName = 'administrator';
}

$this->assertTrue( array_key_exists( 'MANTIS_TESTSUITE_API_TOKEN', $GLOBALS ) &&
!empty( $GLOBALS['MANTIS_TESTSUITE_API_TOKEN'] ),
"You must define 'MANTIS_TESTSUITE_API_TOKEN' in your bootstrap file" );

$this->token = $GLOBALS['MANTIS_TESTSUITE_API_TOKEN'];

if( array_key_exists( 'MANTIS_TESTSUITE_PROJECT_ID', $GLOBALS ) ) {
$this->projectId = $GLOBALS['MANTIS_TESTSUITE_PROJECT_ID'];
} else {
$this->projectId = 1;
}
}

/**
* tearDown
* @return void
*/
protected function tearDown() {
foreach ( $this->issueIdsToDelete as $t_issue_id_to_delete ) {
$this->delete( '/issues', 'id=' . $t_issue_id_to_delete );
}
}

protected function delete( $p_relative_path, $p_query_string ) {
$t_curl = curl_init();

curl_setopt_array( $t_curl, array(
CURLOPT_URL => $this->base_path . $p_relative_path . '?' . $p_query_string,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_HTTPHEADER => array(
'authorization: ' . $this->token,
'cache-control: no-cache',
),
));

$t_response = curl_exec( $t_curl );
$t_error = curl_error( $t_curl );

curl_close( $t_curl );

if ( $t_error ) {
return "cURL Error #:" . $t_error;
}

return $t_response;
}

/**
* @param string $p_relative_path The relative path under `/api/rest/` e.g. `/issues`.
* @param string $p_payload The payload object, it will be json encoded before sending.
* @return mixed|string The response object.
*/
protected function post( $p_relative_path, $p_payload ) {
$t_curl = curl_init();

curl_setopt_array( $t_curl, array(
CURLOPT_URL => $this->base_path . $p_relative_path,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 0,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode( $p_payload ),
CURLOPT_HTTPHEADER => array(
'authorization: ' . $this->token,
'cache-control: no-cache',
'content-type: application/json',
),
));

$t_response = curl_exec( $t_curl );
$t_error = curl_error( $t_curl );
file_put_contents( '/tmp/response.txt', $this->base_path . $p_relative_path . ' ' . $this->token . ' ' . var_export( json_encode( $p_payload ), true ) . ' ' . var_export( $t_response, true ) . ' ' . var_export( $t_error, true ) );
if( $t_response == false ) {
return 'cURL Error #:' . $t_error;
}

curl_close( $t_curl );

return json_decode( $t_response, true );
}

/**
* return integer The default project id.
* @return integer
*/
protected function getProjectId() {
return $this->projectId;
}

/**
* return string The default category.
* @return string
*/
protected function getCategory() {
return 'General';
}

/**
* getIssueToAdd
* @param string $p_test_case Test case identifier.
* @return array
*/
protected function getIssueToAdd( $p_test_case ) {
return array(
'summary' => $p_test_case . ': test issue: ' . rand( 1, 1000000 ),
'description' => 'description of test issue.',
'project' => array( 'id' => $this->getProjectId() ),
'category' => array( 'name' => $this->getCategory() ) );
}

/**
* Registers an issue for deletion after the test method has run
*
* @param integer $p_issue_id Issue identifier.
* @return void
*/
protected function deleteAfterRun( $p_issue_id ) {
$this->issueIdsToDelete[] = $p_issue_id;
}
}

0 comments on commit 91d8faf

Please sign in to comment.