Skip to content

Commit 0cf8cf2

Browse files
authored
Merge pull request #124 from plesk/Add-test-for-update-certificate
Add test for Update Certificate
2 parents 94bcd0b + 9580357 commit 0cf8cf2

File tree

3 files changed

+105
-12
lines changed

3 files changed

+105
-12
lines changed

src/Api/Operator/Certificate.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,49 @@ public function generate(array $properties): Struct\Info
2121
return new Struct\Info($response);
2222
}
2323

24-
public function install(array $properties, string $request, string $privateKey): bool
24+
/**
25+
* @param array $properties
26+
* @param string|Struct\Info $certificate
27+
* @param string|null $privateKey
28+
*/
29+
public function install(array $properties, $certificate, ?string $privateKey = null): bool
30+
{
31+
return $this->callApi('install', $properties, $certificate, $privateKey);
32+
}
33+
34+
/**
35+
* @param array $properties
36+
* @param Struct\Info $certificate
37+
*/
38+
public function update(array $properties, Struct\Info $certificate): bool
39+
{
40+
return $this->callApi('update', $properties, $certificate);
41+
}
42+
43+
/**
44+
* @param string $method
45+
* @param array $properties
46+
* @param string|Struct\Info $certificate
47+
* @param string|null $privateKey
48+
*/
49+
private function callApi(string $method, array $properties, $certificate, ?string $privateKey = null): bool
2550
{
2651
$packet = $this->client->getPacket();
2752

28-
$installTag = $packet->addChild($this->wrapperTag)->addChild('install');
53+
$installTag = $packet->addChild($this->wrapperTag)->addChild($method);
2954
foreach ($properties as $name => $value) {
3055
$installTag->{$name} = $value;
3156
}
3257

3358
$contentTag = $installTag->addChild('content');
34-
$contentTag->addChild('csr', $request);
35-
$contentTag->addChild('pvt', $privateKey);
36-
59+
if (is_string($certificate)) {
60+
$contentTag->addChild('csr', $certificate);
61+
$contentTag->addChild('pvt', $privateKey);
62+
} elseif ($certificate instanceof \PleskX\Api\Struct\Certificate\Info) {
63+
foreach ($certificate->getMapping() as $name => $value) {
64+
$contentTag->{$name} = $value;
65+
}
66+
}
3767
$result = $this->client->request($packet);
3868

3969
return 'ok' == (string) $result->status;

src/Api/Struct/Certificate/Info.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,32 @@
77

88
class Info extends AbstractStruct
99
{
10-
public string $request;
11-
public string $privateKey;
10+
public ?string $request = null;
11+
public ?string $privateKey = null;
12+
public ?string $publicKey = null;
13+
public ?string $publicKeyCA = null;
1214

13-
public function __construct(\SimpleXMLElement $apiResponse)
15+
public function __construct($input)
1416
{
15-
$this->initScalarProperties($apiResponse, [
16-
['csr' => 'request'],
17-
['pvt' => 'privateKey'],
17+
if ($input instanceof \SimpleXMLElement) {
18+
$this->initScalarProperties($input, [
19+
['csr' => 'request'],
20+
['pvt' => 'privateKey'],
21+
]);
22+
} else {
23+
foreach ($input as $name => $value) {
24+
$this->$name = $value;
25+
}
26+
}
27+
}
28+
29+
public function getMapping(): array
30+
{
31+
return array_filter([
32+
'csr' => $this->request,
33+
'pvt' => $this->privateKey,
34+
'cert' => $this->publicKey,
35+
'ca' => $this->publicKeyCA,
1836
]);
1937
}
2038
}

tests/CertificateTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace PleskXTest;
55

6+
use PleskX\Api\Struct\Certificate as Struct;
7+
68
class CertificateTest extends AbstractTestCase
79
{
810
private array $certificateProperties = [
@@ -14,6 +16,13 @@ class CertificateTest extends AbstractTestCase
1416
'email' => 'info@plesk.com',
1517
'name' => 'plesk.com',
1618
];
19+
private array $distinguishedNames = [
20+
"countryName" => "CH",
21+
"stateOrProvinceName" => "Schaffhausen",
22+
"localityName" => "Schaffhausen",
23+
"organizationName" => "Plesk",
24+
"emailAddress" => "info@plesk.com"
25+
];
1726

1827
public function testGenerate()
1928
{
@@ -37,16 +46,52 @@ public function testInstall()
3746
static::$client->certificate()->delete('test', ['admin' => true]);
3847
}
3948

49+
public function testUpdate()
50+
{
51+
$payLoad = [
52+
'name' => 'test',
53+
'admin' => true,
54+
];
55+
$certificate = static::$client->certificate()->generate($this->certificateProperties);
56+
static::$client->certificate()->install($payLoad, $certificate);
57+
58+
$certificate = $this->generateCertificateOpenSsl($this->distinguishedNames);
59+
$result = static::$client->certificate()->update($payLoad, $certificate);
60+
$this->assertTrue($result);
61+
62+
static::$client->certificate()->delete('test', ['admin' => true]);
63+
}
64+
4065
public function testDelete()
4166
{
4267
$certificate = static::$client->certificate()->generate($this->certificateProperties);
4368

4469
static::$client->certificate()->install([
4570
'name' => 'test',
4671
'admin' => true,
47-
], $certificate->request, $certificate->privateKey);
72+
], $certificate);
4873

4974
$result = static::$client->certificate()->delete('test', ['admin' => true]);
5075
$this->assertTrue($result);
5176
}
77+
78+
private function generateCertificateOpenSsl(array $dn): Struct\Info
79+
{
80+
$privkey = openssl_pkey_new([
81+
"private_key_bits" => 2048,
82+
"private_key_type" => OPENSSL_KEYTYPE_RSA,
83+
]);
84+
$csr = openssl_csr_new($dn, $privkey, ['digest_alg' => 'sha256']);
85+
$x509 = openssl_csr_sign($csr, null, $privkey, $days = 365, ['digest_alg' => 'sha256']);
86+
87+
openssl_csr_export($csr, $csrout);
88+
openssl_x509_export($x509, $certout);
89+
openssl_pkey_export($privkey, $pkeyout);
90+
91+
return new Struct\Info([
92+
'publicKey' => $certout,
93+
'request' => $csrout,
94+
'privateKey' => $pkeyout
95+
]);
96+
}
5297
}

0 commit comments

Comments
 (0)