diff --git a/bin/teamcity-keykeeper b/bin/teamcity-keykeeper old mode 100644 new mode 100755 index 39aefdf..505b33a --- a/bin/teamcity-keykeeper +++ b/bin/teamcity-keykeeper @@ -16,33 +16,32 @@ use JBZoo\TeamcityKeyKeeper\KeyGetCommand; use JBZoo\TeamcityKeyKeeper\KeyRestoreCommand; use JBZoo\TeamcityKeyKeeper\KeySaveCommand; +use JBZoo\TeamcityKeyKeeper\QbUpdateCommand; +use JBZoo\Utils\Sys; use Symfony\Component\Console\Application; umask(0000); set_time_limit(0); -define('PATH_ROOT', __DIR__ . '/../'); -define('PATH_STORAGE', PATH_ROOT . '/storage'); +define('PATH_ROOT', dirname(__DIR__) . '/'); +require_once PATH_ROOT . '/vendor/autoload.php'; -$vendorPaths = [ - realpath(__DIR__ . '/vendor/autoload.php'), - realpath(__DIR__ . '/../vendor/autoload.php'), - realpath(__DIR__ . '/../../vendor/autoload.php'), - realpath(__DIR__ . '/../../../vendor/autoload.php'), - realpath(__DIR__ . '/../../../../vendor/autoload.php'), - realpath(__DIR__ . '/../../../../../vendor/autoload.php'), - realpath('./vendor/autoload.php'), -]; +if ($homePath = Sys::getHome()) { + define('PATH_STORAGE_DEFAULT', $homePath . '/.teamcity-keykeeper'); +} else { + define('PATH_STORAGE_DEFAULT', PATH_ROOT . '/storage'); +} -foreach ($vendorPaths as $vendorPath) { - if (file_exists($vendorPath)) { - require_once $vendorPath; - break; - } +if (!is_dir(PATH_STORAGE_DEFAULT) && + !mkdir($concurrentDirectory = PATH_STORAGE_DEFAULT, 0777, true) && + !is_dir($concurrentDirectory) +) { + throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory)); } $application = new Application(); $application->add(new KeySaveCommand()); $application->add(new KeyRestoreCommand()); $application->add(new KeyGetCommand()); +$application->add(new QbUpdateCommand()); $application->run(); diff --git a/composer.json b/composer.json index 9daa6cb..83f8e81 100644 --- a/composer.json +++ b/composer.json @@ -21,8 +21,9 @@ }, "require-dev" : { - "jbzoo/phpunit" : "^2.0", - "symfony/var-dumper" : "^4.1" + "jbzoo/phpunit" : "^2.0", + "symfony/var-dumper" : "^4.1", + "quickbooks/v3-php-sdk" : "^5.0.3" }, "bin" : [ diff --git a/src/Helper.php b/src/Helper.php index 0693e30..c8c4f96 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -22,7 +22,7 @@ */ class Helper { - public const DEFAULT_GROUP = 'default'; + const DEFAULT_GROUP = 'default'; /** * @param string $key @@ -86,7 +86,7 @@ public static function getStorage($storageName = self::DEFAULT_GROUP) private static function getStoragePath($group) { $group = self::cleanGroup($group); - return PATH_STORAGE . "/{$group}.json"; + return PATH_STORAGE_DEFAULT . "/{$group}.json"; } /** @@ -127,7 +127,7 @@ private static function log($message, $group) { $group = self::cleanGroup($group); /** @noinspection ForgottenDebugOutputInspection */ - error_log(date('Y-m-d H:i:s') . " {$message}\n", 3, PATH_STORAGE . "/{$group}_log.log"); + error_log(date('Y-m-d H:i:s') . " {$message}\n", 3, PATH_STORAGE_DEFAULT . "/{$group}_log.log"); } /** diff --git a/src/QbUpdateCommand.php b/src/QbUpdateCommand.php new file mode 100644 index 0000000..2cb131b --- /dev/null +++ b/src/QbUpdateCommand.php @@ -0,0 +1,83 @@ +setName('qb:update') + ->setDescription('Update tokens for QuickBooks'); + } + + /** + * @inheritdoc + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $scope = 'com.intuit.quickbooks.accounting'; + + $config = [ + 'auth_mode' => CoreConstants::OAUTH2, + 'scope' => $scope, + 'ClientID' => $this->getConfig('qb_client_id'), + 'ClientSecret' => $this->getConfig('qb_client_secret'), + 'QBORealmID' => $this->getConfig('qb_realm_id'), + 'RedirectURI' => $this->getConfig('qb_redirect_url'), + 'baseUrl' => CoreConstants::DEVELOPMENT_SANDBOX, + 'accessTokenKey' => $this->getConfig('qb_access_token'), + 'refreshTokenKey' => $this->getConfig('qb_refresh_token'), + ]; + + $dataService = DataService::Configure($config); + + $qbToken = $dataService->getOAuth2LoginHelper()->refreshToken(); + $this->saveConfig('qb_access_token', $qbToken->getAccessToken()); + $this->saveConfig('qb_refresh_token', $qbToken->getRefreshToken()); + + Cli::out($dataService->getCompanyInfo()->CompanyName); + } + + /** + * @param string $keyName + * @return string + */ + protected function getConfig($keyName) + { + return trim(Cli::exec('teamcity-keykeeper key:get', ['name' => $keyName])); + } + + /** + * @param string $keyName + * @param string $value + */ + protected function saveConfig($keyName, $value) + { + $response = trim(Cli::exec('teamcity-keykeeper key:save', ['name' => $keyName, 'value' => $value])); + Cli::out($response); + } +} diff --git a/tests/KeyKeeperTest.php b/tests/KeyKeeperTest.php index 4fcd950..f4fdbd0 100644 --- a/tests/KeyKeeperTest.php +++ b/tests/KeyKeeperTest.php @@ -16,6 +16,7 @@ use JBZoo\Utils\Cli; use JBZoo\Utils\Str; +use JBZoo\Utils\Sys; /** * Class PackageTest @@ -28,9 +29,11 @@ class KeyKeeperTest extends PHPUnit */ protected $bin = ''; + protected $testGroup = 'phpunit'; + public static function setUpBeforeClass() { - $storageFile = __DIR__ . '/../storage/default_log.log'; + $storageFile = Sys::getHome() . '/.teamcity-keykeeper/phpuni_log.log'; if (file_exists($storageFile)) { unlink($storageFile); } @@ -38,7 +41,7 @@ public static function setUpBeforeClass() public function setUp() { - $storageFile = __DIR__ . '/../storage/default.json'; + $storageFile = Sys::getHome() . '/.teamcity-keykeeper/phpunit.json'; if (file_exists($storageFile)) { unlink($storageFile); } @@ -51,11 +54,13 @@ public function testSaveAndRestoreKey() $value = Str::random(10000); $name = Str::random(); - $saveResult = trim(Cli::exec("{$this->bin} key:save --name='{$name}' --value='{$value}'")); + $saveResult = trim(Cli::exec("{$this->bin} key:save " . + "--name='{$name}' --value='{$value}' --group='{$this->testGroup}'" + )); $name = strtoupper($name); isContain("Key '{$name}' saved", $saveResult); - $restoreResult = trim(Cli::exec("{$this->bin} key:restore --name='{$name}'")); + $restoreResult = trim(Cli::exec("{$this->bin} key:restore --name='{$name}' --group='{$this->testGroup}'")); isSame("##teamcity[setParameter name='env.{$name}' value='{$value}']", $restoreResult); } @@ -64,39 +69,44 @@ public function testSpecialChars() $value = 'qwerty1234567890-!@#$%^&*()_+.<>,;{}№'; $name = 'qwerty1234567890'; - $saveResult = trim(Cli::exec("{$this->bin} key:save", ['name' => $name, 'value' => $value])); + $saveResult = trim(Cli::exec("{$this->bin} key:save", [ + 'group' => $this->testGroup, + 'name' => $name, + 'value' => $value, + ])); + $name = strtoupper($name); isContain("Key '{$name}' saved", $saveResult); - $restoreResult = trim(Cli::exec("{$this->bin} key:restore", ['name' => $name])); + $restoreResult = trim(Cli::exec("{$this->bin} key:restore", ['name' => $name, 'group' => $this->testGroup])); isSame("##teamcity[setParameter name='env.{$name}' value='{$value}']", $restoreResult); } public function testGetAllKeys() { - Cli::exec("{$this->bin} key:save --name='key1' --value='value1'"); - Cli::exec("{$this->bin} key:save --name='key2' --value='value2'"); + Cli::exec("{$this->bin} key:save --name='key1' --value='value1' --group='{$this->testGroup}'"); + Cli::exec("{$this->bin} key:save --name='key2' --value='value2' --group='{$this->testGroup}'"); - $restoreResult = trim(Cli::exec("{$this->bin} key:restore --all")); + $restoreResult = trim(Cli::exec("{$this->bin} key:restore --all --group='{$this->testGroup}'")); isContain("##teamcity[setParameter name='env.KEY1' value='value1']", $restoreResult); isContain("##teamcity[setParameter name='env.KEY2' value='value2']", $restoreResult); } public function testRemoveKeysWithOption() { - Cli::exec("{$this->bin} key:save --name='key' --value='value1'"); - Cli::exec("{$this->bin} key:save --name='key' --value=''"); + Cli::exec("{$this->bin} key:save --name='key' --value='value1' --group='{$this->testGroup}'"); + Cli::exec("{$this->bin} key:save --name='key' --value='' --group='{$this->testGroup}'"); - $restoreResult = trim(Cli::exec("{$this->bin} key:restore --all")); + $restoreResult = trim(Cli::exec("{$this->bin} key:restore --all --group='{$this->testGroup}'")); isContain("##teamcity[setParameter name='env.KEY' value='']", $restoreResult); } public function testRemoveKeysWithoutOption() { - Cli::exec("{$this->bin} key:save --name='key' --value='value1'"); - Cli::exec("{$this->bin} key:save --name='key'"); + Cli::exec("{$this->bin} key:save --name='key' --value='value1' --group='{$this->testGroup}'"); + Cli::exec("{$this->bin} key:save --name='key' --group='{$this->testGroup}'"); - $restoreResult = trim(Cli::exec("{$this->bin} key:restore --all")); + $restoreResult = trim(Cli::exec("{$this->bin} key:restore --all --group='{$this->testGroup}'")); isContain("##teamcity[setParameter name='env.KEY' value='']", $restoreResult); } @@ -105,8 +115,8 @@ public function testGetCleanValue() $key = Str::random(); $value = 'qwerty1234567890-!@#$%^&*()_+.<>,;{}№\'"'; - Cli::exec("{$this->bin} key:save", ['name' => $key, 'value' => $value]); + Cli::exec("{$this->bin} key:save", ['name' => $key, 'value' => $value, 'group' => $this->testGroup]); - isSame($value, Cli::exec("{$this->bin} key:get --name='{$key}'")); + isSame($value, Cli::exec("{$this->bin} key:get --name='{$key}' --group='{$this->testGroup}'")); } } diff --git a/tests/QuickBooksTest.php b/tests/QuickBooksTest.php new file mode 100644 index 0000000..afd1b0f --- /dev/null +++ b/tests/QuickBooksTest.php @@ -0,0 +1,30 @@ +