Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/auth/__tests__/PhabricatorAuthSSHKeyTestCase.php
12256 views
1
<?php
2
3
final class PhabricatorAuthSSHKeyTestCase extends PhabricatorTestCase {
4
5
protected function getPhabricatorTestCaseConfiguration() {
6
return array(
7
self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true,
8
);
9
}
10
11
public function testRevokeSSHKey() {
12
$user = $this->generateNewTestUser();
13
$raw_key = 'ssh-rsa hunter2';
14
15
$ssh_key = PhabricatorAuthSSHKey::initializeNewSSHKey($user, $user);
16
17
// Add the key to the user's account.
18
$xactions = array();
19
$xactions[] = $ssh_key->getApplicationTransactionTemplate()
20
->setTransactionType(PhabricatorAuthSSHKeyTransaction::TYPE_NAME)
21
->setNewValue('key1');
22
$xactions[] = $ssh_key->getApplicationTransactionTemplate()
23
->setTransactionType(PhabricatorAuthSSHKeyTransaction::TYPE_KEY)
24
->setNewValue($raw_key);
25
$this->applyTransactions($user, $ssh_key, $xactions);
26
27
$ssh_key->reload();
28
$this->assertTrue((bool)$ssh_key->getIsActive());
29
30
// Revoke it.
31
$xactions = array();
32
$xactions[] = $ssh_key->getApplicationTransactionTemplate()
33
->setTransactionType(PhabricatorAuthSSHKeyTransaction::TYPE_DEACTIVATE)
34
->setNewValue(true);
35
$this->applyTransactions($user, $ssh_key, $xactions);
36
37
$ssh_key->reload();
38
$this->assertFalse((bool)$ssh_key->getIsActive());
39
40
// Try to add the revoked key back. This should fail with a validation
41
// error because the key was previously revoked by the user.
42
$revoked_key = PhabricatorAuthSSHKey::initializeNewSSHKey($user, $user);
43
$xactions = array();
44
$xactions[] = $ssh_key->getApplicationTransactionTemplate()
45
->setTransactionType(PhabricatorAuthSSHKeyTransaction::TYPE_NAME)
46
->setNewValue('key2');
47
$xactions[] = $ssh_key->getApplicationTransactionTemplate()
48
->setTransactionType(PhabricatorAuthSSHKeyTransaction::TYPE_KEY)
49
->setNewValue($raw_key);
50
51
$caught = null;
52
try {
53
$this->applyTransactions($user, $ssh_key, $xactions);
54
} catch (PhabricatorApplicationTransactionValidationException $ex) {
55
$errors = $ex->getErrors();
56
$this->assertEqual(1, count($errors));
57
$caught = head($errors)->getType();
58
}
59
60
$this->assertEqual(PhabricatorAuthSSHKeyTransaction::TYPE_KEY, $caught);
61
}
62
63
private function applyTransactions(
64
PhabricatorUser $actor,
65
PhabricatorAuthSSHKey $key,
66
array $xactions) {
67
68
$content_source = $this->newContentSource();
69
70
$editor = $key->getApplicationTransactionEditor()
71
->setActor($actor)
72
->setContinueOnNoEffect(true)
73
->setContinueOnMissingFields(true)
74
->setContentSource($content_source)
75
->applyTransactions($key, $xactions);
76
}
77
78
}
79
80