Path: blob/master/src/applications/auth/__tests__/PhabricatorAuthSSHKeyTestCase.php
12256 views
<?php12final class PhabricatorAuthSSHKeyTestCase extends PhabricatorTestCase {34protected function getPhabricatorTestCaseConfiguration() {5return array(6self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true,7);8}910public function testRevokeSSHKey() {11$user = $this->generateNewTestUser();12$raw_key = 'ssh-rsa hunter2';1314$ssh_key = PhabricatorAuthSSHKey::initializeNewSSHKey($user, $user);1516// Add the key to the user's account.17$xactions = array();18$xactions[] = $ssh_key->getApplicationTransactionTemplate()19->setTransactionType(PhabricatorAuthSSHKeyTransaction::TYPE_NAME)20->setNewValue('key1');21$xactions[] = $ssh_key->getApplicationTransactionTemplate()22->setTransactionType(PhabricatorAuthSSHKeyTransaction::TYPE_KEY)23->setNewValue($raw_key);24$this->applyTransactions($user, $ssh_key, $xactions);2526$ssh_key->reload();27$this->assertTrue((bool)$ssh_key->getIsActive());2829// Revoke it.30$xactions = array();31$xactions[] = $ssh_key->getApplicationTransactionTemplate()32->setTransactionType(PhabricatorAuthSSHKeyTransaction::TYPE_DEACTIVATE)33->setNewValue(true);34$this->applyTransactions($user, $ssh_key, $xactions);3536$ssh_key->reload();37$this->assertFalse((bool)$ssh_key->getIsActive());3839// Try to add the revoked key back. This should fail with a validation40// error because the key was previously revoked by the user.41$revoked_key = PhabricatorAuthSSHKey::initializeNewSSHKey($user, $user);42$xactions = array();43$xactions[] = $ssh_key->getApplicationTransactionTemplate()44->setTransactionType(PhabricatorAuthSSHKeyTransaction::TYPE_NAME)45->setNewValue('key2');46$xactions[] = $ssh_key->getApplicationTransactionTemplate()47->setTransactionType(PhabricatorAuthSSHKeyTransaction::TYPE_KEY)48->setNewValue($raw_key);4950$caught = null;51try {52$this->applyTransactions($user, $ssh_key, $xactions);53} catch (PhabricatorApplicationTransactionValidationException $ex) {54$errors = $ex->getErrors();55$this->assertEqual(1, count($errors));56$caught = head($errors)->getType();57}5859$this->assertEqual(PhabricatorAuthSSHKeyTransaction::TYPE_KEY, $caught);60}6162private function applyTransactions(63PhabricatorUser $actor,64PhabricatorAuthSSHKey $key,65array $xactions) {6667$content_source = $this->newContentSource();6869$editor = $key->getApplicationTransactionEditor()70->setActor($actor)71->setContinueOnNoEffect(true)72->setContinueOnMissingFields(true)73->setContentSource($content_source)74->applyTransactions($key, $xactions);75}7677}787980