Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
Adding ability to create test accounts using a file to the testing tr…
Browse files Browse the repository at this point in the history
…ait (#328)
  • Loading branch information
tiffneybare authored May 10, 2022
1 parent a4517b4 commit 51d7290
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 53 deletions.
29 changes: 29 additions & 0 deletions docs/testusers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Test user accounts

To create test user accounts when running tests, add a file to the root of your project called testUsers.json

Add the accounts for each role you will be testing.

```
[
{
"name": "testapiuser",
"mail": "[email protected]",
"role": "api_user"
},
{
"name": "testadmin",
"mail": "[email protected]",
"role": "administrator"
}
]
```

When you run `dktl dkan:test-cypress` for core tests, or `dktl project:test-cypress` for project tests,
the users will be generated, the tests will run, then then user accounts will be deleted.

## Create Test Users
To create the accounts without running tests, run `dktl dkan:qa-users-create` or `dktl qauc`.

## Delete Test Users
To delete the accounts, run `dktl dkan:qa-users-delete` or `dktl qaud`
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ nav:
- 'patching.md'
- 'Custom commands': 'extend.md'
- 'Modifying the Docker containers': 'docker_config.md'
- 'Test users': 'testusers.md'
- 'troubleshooting.md'
markdown_extensions:
- admonition
Expand Down
72 changes: 59 additions & 13 deletions src/Command/DkanCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
/**
* This is project's console commands configuration for Robo task runner.
*
* @param $arg
* use 'frontend' to run frontend cypress tests.
*
* @see http://robo.li/
* @param $arg use 'frontend' to run frontend cypress tests.
*/
class DkanCommands extends \Robo\Tasks
{
Expand All @@ -28,48 +30,77 @@ public function dkanDocs()
$this->io()->text("Docs site: $url/modules/contrib/dkan/docs/index.html");
}

/**
* Create QA users.
*
* @command dkan:qa-users-create
* @aliases qauc
*/
public function dkanQaUsersCreate()
{
return $this->createTestUsers();
}

/**
* Remove QA users.
*
* @command dkan:qa-users-delete
* @aliases qaud
*/
public function dkanQaUsersDelete()
{
return $this->deleteTestUsers();
}

/**
* Run DKAN Cypress Tests.
*/
public function dkanTestCypress(array $args)
{
$this->apiUser();
$this->editorUser();
$this->createTestUsers();

$this->taskExec("npm cache verify && npm install")
->dir("docroot/modules/contrib/dkan")
->run();

$cypress = $this->taskExec('CYPRESS_baseUrl="http://$DKTL_PROXY_DOMAIN" npx cypress run')
->dir("docroot/modules/contrib/dkan");

foreach ($args as $arg) {
$cypress->arg($arg);
$cypress->arg($arg);
}

return $cypress->run();
$cypress->run();
$this->deleteTestUsers();
}

/**
* Run DKAN Dredd Tests.
*/
public function dkanTestDredd()
{
$this->apiUser();
$this->createTestUsers();
$this->taskExec("npm install dredd")
->dir("docroot/modules/contrib/dkan")
->run();

return $this->taskExec("npx dredd --hookfiles=./dredd-hooks.js")
$this->taskExec("npx dredd --hookfiles=./dredd-hooks.js")
->dir("docroot/modules/contrib/dkan/dredd")
->run();
$this->deleteTestUsers();
}

/**
* Run DKAN PhpUnit Tests. Additional phpunit CLI options can be passed.
*
* @param array $args
* Arguments to append to phpunit command.
*
* @see https://phpunit.de/manual/6.5/en/textui.html#textui.clioptions
* @param array $args Arguments to append to phpunit command.
*/
public function dkanTestPhpunit(array $args)
{
$this->apiUser();
$this->createTestUsers();
$proj_dir = Util::getProjectDirectory();
$phpunit_executable = $this->getPhpUnitExecutable();

Expand All @@ -81,15 +112,16 @@ public function dkanTestPhpunit(array $args)
$phpunitExec->arg($arg);
}

return $phpunitExec->run();
$phpunitExec->run();
$this->deleteTestUsers();
}

/**
* Run DKAN PhpUnit Tests and send a coverage report to CodeClimate.
*/
public function dkanTestPhpunitCoverage($code_climate_reporter_id)
{
$this->apiUser();
$this->createTestUsers();
$proj_dir = Util::getProjectDirectory();
$dkanDir = "{$proj_dir}/docroot/modules/contrib/dkan";

Expand Down Expand Up @@ -119,21 +151,30 @@ public function dkanTestPhpunitCoverage($code_climate_reporter_id)
->dir($dkanDir)
->silent(true)
->run();
$this->deleteTestUsers();
return $result;
}

/**
* Include CodeClimate report.
*/
private function installCodeClimateTestReporter($dkanDir)
{
if (!file_exists("{$dkanDir}/cc-test-reporter")) {
$this->taskExec(
"curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > "
. "./cc-test-reporter"
. "./cc-test-reporter"
)
->dir($dkanDir)->run();
$this->taskExec("chmod +x ./cc-test-reporter")->dir($dkanDir)->run();
}
}

/**
* Determine path to PHPUnit executable.
*
* @return string
*/
private function getPhpUnitExecutable()
{
$proj_dir = Util::getProjectDirectory();
Expand All @@ -148,14 +189,19 @@ private function getPhpUnitExecutable()
return $phpunit_executable;
}

/**
* Ensure current git branch is not in a detached state.
*
* @return bool
* Flag for whether the current branch branch is detached.
*/
private function inGitDetachedState($dkanDirPath)
{
$output = [];
exec("cd {$dkanDirPath} && git rev-parse --abbrev-ref HEAD", $output);
return (isset($output[0]) && $output[0] == 'HEAD');
}


/**
* Create a new demo project.
*
Expand Down
116 changes: 88 additions & 28 deletions src/Command/ProjectCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,110 @@

namespace DkanTools\Command;

use DkanTools\Util\Util;
use DkanTools\Util\TestUserTrait;

use Robo\Tasks;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

/**
* This project's console commands configuration for Robo task runner.
*
* @see http://robo.li/
*/
class ProjectCommands extends \Robo\Tasks
class ProjectCommands extends Tasks
{
use TestUserTrait;

/**
* Path to project cypress tests.
*
* @var string
*/
protected const TESTS_DIR = 'src/tests';

/**
* Run project cypress tests.
*
* @param array $args
* Cypress command arguments.
*/
public function projectTestCypress(array $args)
public function projectTestCypress(array $args): void
{
$this->apiUser();
$this->editorUser();

$result = $this->taskExec("npm link ../../../../usr/local/bin/node_modules/cypress")
->dir("src/tests")
// Prepare environment to run cypress.
$this->symlinkOrInstallCypress();
$this->installNpmDependencies();
$this->createTestUsers();

// Run Cypress.
$this->io()->say('Running cypress...');
$config_option = file_exists(self::TESTS_DIR . '/cypress.json') ? ' --config-file cypress.json' : '';
$this->taskExec('CYPRESS_baseUrl="http://$DKTL_PROXY_DOMAIN" npx cypress run' . $config_option)
->dir(self::TESTS_DIR)
->args($args)
->run();
if ($result->getExitCode() != 0) {
$this->io()->error('Could not symlink package folder');
return $result;
}

$task = $this
->taskExec('npm install --force')
->dir("src/tests");
$result = $task->run();
if ($result->getExitCode() != 0) {
$this->io()->error('Could not insall test dependencies.');
return $result;
}
$this->io()->success('Installation of test dependencies successful.');

$cypress = $this->taskExec('CYPRESS_baseUrl="http://$DKTL_PROXY_DOMAIN" npx cypress run')
->dir("src/tests");
// Clean up environment.
$this->deleteTestUsers();
}

foreach ($args as $arg) {
$cypress->arg($arg);
/**
* Attempt to symlink Cypress command.
*
* @throws \RuntimeException
* On failure.
*/
protected function symlinkOrInstallCypress(): void
{
$this->io()->say("Determining if cypress is installed in it's standard location...");
$cypress_path = '/usr/local/bin/node_modules/cypress';
if (is_dir($cypress_path)) {
$this->io()->say('Cypress installed in standard location; symlinking cypress package folder...');
// Symlink cypress package folder.
$result = $this->taskExec('npm link ' . $cypress_path)
->dir(self::TESTS_DIR)
->run();
// Handle errors.
if ($result->getExitCode() !== 0) {
throw new \RuntimeException('Failed to symlink cypress package folder');
}
}
else {
$this->io()->warning('Cypress installation not found in standard location; Attempting to install cypress locally...');
$result = $this->taskExec('npm install cypress')
->dir(self::TESTS_DIR)
->run();
if ($result->getExitCode() !== 0) {
throw new \RuntimeException('Failed to install cypress');
}
$this->io()->success('Successfully installed cypress!');
}
}

return $cypress->run();
/**
* Attempt to install npm test dependencies.
*
* @throws \RuntimeException
* On failure.
*/
protected function installNpmDependencies(): void
{
$this->io()->say('Installing test dependencies...');
$result = $this->taskExec('npm install --force')
->dir(self::TESTS_DIR)
->run();
if ($result->getExitCode() !== 0) {
throw new \RuntimeException('Failed to install test dependencies');
}
}

/**
* Run Site PhpUnit Tests. Additional phpunit CLI options can be passed.
*
* @param array $args
* Arguments to append to phpunit command.
*
* @see https://phpunit.de/manual/6.5/en/textui.html#textui.clioptions
* @param array $args Arguments to append to phpunit command.
*/
public function projectTestPhpunit(array $args)
{
Expand All @@ -72,6 +123,9 @@ public function projectTestPhpunit(array $args)
return $phpunitExec->run();
}

/**
* Determine path to PHPUnit executable.
*/
private function getPhpUnitExecutable()
{
$proj_dir = Util::getProjectDirectory();
Expand All @@ -86,6 +140,12 @@ private function getPhpUnitExecutable()
return $phpunit_executable;
}

/**
* Ensure current git branch is not in a detached state.
*
* @return bool
* Flag for whether the current branch branch is detached.
*/
private function inGitDetachedState($dkanDirPath)
{
$output = [];
Expand Down
Loading

0 comments on commit 51d7290

Please sign in to comment.