-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSaveTicketSystemPreserveSecretsTest.php
More file actions
236 lines (196 loc) · 9.1 KB
/
Copy pathSaveTicketSystemPreserveSecretsTest.php
File metadata and controls
236 lines (196 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/*
* Copyright (c) 2025-2026 Netresearch DTT GmbH
* SPDX-License-Identifier: AGPL-3.0-only
*/
declare(strict_types=1);
namespace Tests\Controller;
use App\Entity\Activity;
use App\Entity\TicketSystem;
use Symfony\Component\HttpFoundation\Request;
use Tests\AbstractWebTestCase;
/**
* /ticketsystem/save must not wipe stored credentials when the edit form
* submits them blank.
*
* GetTicketSystemsAction strips password/public+private key/OAuth consumer
* key+secret from the list payload, so the SolidJS edit form opens those
* fields empty. A blank submission therefore has to mean "keep the stored
* value", not "overwrite it with an empty string".
*
* @internal
*
* @coversNothing
*/
final class SaveTicketSystemPreserveSecretsTest extends AbstractWebTestCase
{
private const array SECRETS = [
'password' => 'stored-password',
'publicKey' => 'stored-public-key',
'privateKey' => 'stored-private-key',
'oauthConsumerKey' => 'stored-consumer-key',
'oauthConsumerSecret' => 'stored-consumer-secret',
'oauth2ClientSecret' => 'stored-oauth2-secret',
];
private function seedSecrets(): void
{
$container = $this->client->getContainer();
/** @var \Doctrine\Bundle\DoctrineBundle\Registry $doctrine */
$doctrine = $container->get('doctrine');
$em = $doctrine->getManager();
$ticketSystem = $em->getRepository(TicketSystem::class)->find(1);
self::assertInstanceOf(TicketSystem::class, $ticketSystem);
$ticketSystem->setPassword(self::SECRETS['password']);
$ticketSystem->setPublicKey(self::SECRETS['publicKey']);
$ticketSystem->setPrivateKey(self::SECRETS['privateKey']);
$ticketSystem->setOauthConsumerKey(self::SECRETS['oauthConsumerKey']);
$ticketSystem->setOauthConsumerSecret(self::SECRETS['oauthConsumerSecret']);
$ticketSystem->setOauth2ClientSecret(self::SECRETS['oauth2ClientSecret']);
$em->persist($ticketSystem);
$em->flush();
}
/**
* Re-fetch ticket system 1 from a cleared manager (so it reflects the saved
* row, not the in-memory one mutated during the request).
*/
private function refetchTicketSystem(): TicketSystem
{
$container = $this->client->getContainer();
/** @var \Doctrine\Bundle\DoctrineBundle\Registry $doctrine */
$doctrine = $container->get('doctrine');
$em = $doctrine->getManager();
$em->clear();
$ticketSystem = $em->getRepository(TicketSystem::class)->find(1);
self::assertInstanceOf(TicketSystem::class, $ticketSystem);
return $ticketSystem;
}
public function testBlankCredentialsKeepStoredValuesAndOtherFieldsUpdate(): void
{
$this->logInSession('unittest');
$this->seedSecrets();
// Edit the system, changing only the name and leaving every credential
// field blank (exactly what the secret-free edit form submits).
$this->client->request(Request::METHOD_POST, '/ticketsystem/save', [
'id' => 1,
'name' => 'testSystemRenamed',
'type' => 'JIRA',
'url' => 'https://jira.example.test',
], [], ['HTTP_ACCEPT' => 'application/json']);
$this->assertStatusCode(200);
// The save response must not echo the stored credentials back to the
// client (the list endpoint strips them too — same SECRET_KEYS).
$body = (string) $this->client->getResponse()->getContent();
foreach (self::SECRETS as $secret) {
self::assertStringNotContainsString($secret, $body, 'save response leaked a credential');
}
$ticketSystem = $this->refetchTicketSystem();
// Non-secret fields updated as submitted.
self::assertSame('testSystemRenamed', $ticketSystem->getName());
self::assertSame('https://jira.example.test', $ticketSystem->getUrl());
// Credentials preserved, not wiped.
self::assertSame(self::SECRETS['password'], $ticketSystem->getPassword());
self::assertSame(self::SECRETS['publicKey'], $ticketSystem->getPublicKey());
self::assertSame(self::SECRETS['privateKey'], $ticketSystem->getPrivateKey());
self::assertSame(self::SECRETS['oauthConsumerKey'], $ticketSystem->getOauthConsumerKey());
self::assertSame(self::SECRETS['oauthConsumerSecret'], $ticketSystem->getOauthConsumerSecret());
self::assertSame(self::SECRETS['oauth2ClientSecret'], $ticketSystem->getOauth2ClientSecret());
}
public function testNonBlankCredentialsAreUpdated(): void
{
$this->logInSession('unittest');
$this->seedSecrets();
$this->client->request(Request::METHOD_POST, '/ticketsystem/save', [
'id' => 1,
'name' => 'testSystem',
'type' => 'JIRA',
'password' => 'rotated-password',
'oauthConsumerSecret' => 'rotated-secret',
'oauth2ClientSecret' => 'rotated-oauth2-secret',
], [], ['HTTP_ACCEPT' => 'application/json']);
$this->assertStatusCode(200);
$ticketSystem = $this->refetchTicketSystem();
// Supplied credentials overwrite; untouched ones keep their stored value.
self::assertSame('rotated-password', $ticketSystem->getPassword());
self::assertSame('rotated-secret', $ticketSystem->getOauthConsumerSecret());
self::assertSame('rotated-oauth2-secret', $ticketSystem->getOauth2ClientSecret());
self::assertSame(self::SECRETS['privateKey'], $ticketSystem->getPrivateKey());
}
public function testDeploymentTypeAndOauth2ClientIdMapOntoEntity(): void
{
$this->logInSession('unittest');
$this->seedSecrets();
// The non-secret Cloud fields flow through the ObjectMapper onto the
// entity. oauth2ClientId is non-secret, so it is sent (and updated)
// normally; deploymentType discriminates the transport.
$this->client->request(Request::METHOD_POST, '/ticketsystem/save', [
'id' => 1,
'name' => 'testSystem',
'type' => 'JIRA',
'deploymentType' => 'CLOUD',
'oauth2ClientId' => 'mapped-client-id',
], [], ['HTTP_ACCEPT' => 'application/json']);
$this->assertStatusCode(200);
$ticketSystem = $this->refetchTicketSystem();
self::assertSame('CLOUD', $ticketSystem->getDeploymentTypeRaw());
self::assertSame('mapped-client-id', $ticketSystem->getOauth2ClientId());
}
public function testSavePersistsSyncConfiguration(): void
{
$this->logInSession('unittest');
$this->client->request(Request::METHOD_POST, '/ticketsystem/save', [
'id' => 1,
'name' => 'testSystem',
'type' => 'JIRA',
'syncDefaultActivityId' => 1,
], [], ['HTTP_ACCEPT' => 'application/json']);
$this->assertStatusCode(200);
// toSafeArray() emits the relation as ids (camelCase + snake_case).
/** @var array<string, mixed> $body */
$body = json_decode((string) $this->client->getResponse()->getContent(), true);
self::assertSame(1, $body['sync_default_activity']);
$ticketSystem = $this->refetchTicketSystem();
$syncDefaultActivity = $ticketSystem->getSyncDefaultActivity();
self::assertInstanceOf(Activity::class, $syncDefaultActivity);
self::assertSame(1, $syncDefaultActivity->getId());
}
public function testSaveClearsSyncConfigurationWithNulls(): void
{
$this->logInSession('unittest');
$this->seedSyncConfiguration();
// The admin form always sends the full config — omitted/null sync
// fields mean "clear the setting", not "keep it".
$this->client->request(Request::METHOD_POST, '/ticketsystem/save', [
'id' => 1,
'name' => 'testSystem',
'type' => 'JIRA',
], [], ['HTTP_ACCEPT' => 'application/json']);
$this->assertStatusCode(200);
$ticketSystem = $this->refetchTicketSystem();
self::assertNull($ticketSystem->getSyncDefaultActivity());
}
public function testSaveRejectsUnknownSyncDefaultActivity(): void
{
$this->logInSession('unittest');
$this->client->request(Request::METHOD_POST, '/ticketsystem/save', [
'id' => 1,
'name' => 'testSystem',
'type' => 'JIRA',
'syncDefaultActivityId' => 999,
], [], ['HTTP_ACCEPT' => 'application/json']);
$this->assertStatusCode(422);
}
private function seedSyncConfiguration(): void
{
$container = $this->client->getContainer();
/** @var \Doctrine\Bundle\DoctrineBundle\Registry $doctrine */
$doctrine = $container->get('doctrine');
$em = $doctrine->getManager();
$ticketSystem = $em->getRepository(TicketSystem::class)->find(1);
self::assertInstanceOf(TicketSystem::class, $ticketSystem);
$syncDefaultActivity = $em->getRepository(Activity::class)->find(1);
self::assertInstanceOf(Activity::class, $syncDefaultActivity);
$ticketSystem->setSyncDefaultActivity($syncDefaultActivity);
$em->persist($ticketSystem);
$em->flush();
}
}