Skip to content

Commit 1b6a907

Browse files
working tests for authentication
1 parent e46e18d commit 1b6a907

11 files changed

+54
-39
lines changed

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ services:
6767
NEO4J_server_bolt_advertised__address: neo4j:7687
6868
NEO4J_server_http_advertised__address: neo4j:7474
6969

70+
71+
7072
server1:
7173
<<: *common-cluster
7274
hostname: server1

testkit-backend/src/Backend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private function loadRequestHandler(string $name): RequestHandlerInterface
107107
RequestHandlerInterface::class,
108108
get_debug_type($action)
109109
);
110-
throw new UnexpectedValueException($str);
110+
throw new UnexpectedValueException((string)$str);
111111
}
112112

113113
return $action;

testkit-backend/src/Handlers/AbstractRunner.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,18 @@ public function handle($request): ResultResponse
5757
$params[$key] = $this->decodeToValue($value);
5858
}
5959
$result = $session->run($request->getCypher(), $params);
60-
} catch (Neo4jException|InvalidTransactionStateException $exception) {
60+
} catch (Neo4jException $exception) {
6161
$this->logger->debug($exception->__toString());
62-
if ($exception instanceof InvalidTransactionStateException
63-
|| str_contains($exception->getMessage(), 'Neo.ClientError.Security.Unauthorized')
64-
|| str_contains($exception->getMessage(), 'ClientError')
65-
) {
66-
$this->repository->addRecords($id, new DriverErrorResponse(
67-
$this->getId($request),
68-
$exception instanceof Neo4jException ? $exception->getNeo4jCode() : 'n/a',
69-
$exception->getMessage(),
70-
$exception->getCode()
71-
));
72-
} else {
62+
$this->repository->addRecords($id, new DriverErrorResponse(
63+
$this->getId($request),
64+
$exception
65+
));
66+
67+
return new ResultResponse($id, []);
68+
} catch (\Throwable $exception) {
7369
$this->repository->addRecords($id, new FrontendErrorResponse(
7470
$exception->getMessage()
7571
));
76-
}
77-
78-
return new ResultResponse($id, []);
7972
}
8073
$this->repository->addRecords($id, $result);
8174

testkit-backend/src/Handlers/SessionReadTransaction.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
namespace Laudis\Neo4j\TestkitBackend\Handlers;
1515

1616
use Laudis\Neo4j\Databags\TransactionConfiguration;
17+
use Laudis\Neo4j\Exception\Neo4jException;
18+
use Laudis\Neo4j\TestkitBackend\Backend;
1719
use Laudis\Neo4j\TestkitBackend\Contracts\RequestHandlerInterface;
1820
use Laudis\Neo4j\TestkitBackend\Contracts\TestkitResponseInterface;
1921
use Laudis\Neo4j\TestkitBackend\MainRepository;
2022
use Laudis\Neo4j\TestkitBackend\Requests\SessionReadTransactionRequest;
23+
use Laudis\Neo4j\TestkitBackend\Responses\DriverErrorResponse;
24+
use Laudis\Neo4j\TestkitBackend\Responses\FrontendErrorResponse;
25+
use Laudis\Neo4j\TestkitBackend\Responses\ResultResponse;
2126
use Laudis\Neo4j\TestkitBackend\Responses\RetryableTryResponse;
2227
use Symfony\Component\Uid\Uuid;
2328

@@ -50,12 +55,21 @@ public function handle($request): TestkitResponseInterface
5055
$config = $config->withMetaData($request->getTxMeta());
5156
}
5257

53-
// TODO - Create beginReadTransaction and beginWriteTransaction
54-
$transaction = $session->beginTransaction(null, $config);
5558
$id = Uuid::v4();
59+
try {
60+
// TODO - Create beginReadTransaction and beginWriteTransaction
61+
$transaction = $session->beginTransaction(null, $config);
5662

57-
$this->repository->addTransaction($id, $transaction);
58-
$this->repository->bindTransactionToSession($request->getSessionId(), $id);
63+
$this->repository->addTransaction($id, $transaction);
64+
$this->repository->bindTransactionToSession($request->getSessionId(), $id);
65+
} catch (Neo4jException $exception) {
66+
$this->repository->addRecords($id, new DriverErrorResponse(
67+
$id,
68+
$exception
69+
));
70+
71+
return new DriverErrorResponse($id, $exception);
72+
}
5973

6074
return new RetryableTryResponse($id);
6175
}

testkit-backend/src/Handlers/TransactionCommit.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Laudis\Neo4j\TestkitBackend\Handlers;
1515

1616
use Laudis\Neo4j\Exception\InvalidTransactionStateException;
17+
use Laudis\Neo4j\Exception\Neo4jException;
1718
use Laudis\Neo4j\TestkitBackend\Contracts\RequestHandlerInterface;
1819
use Laudis\Neo4j\TestkitBackend\Contracts\TestkitResponseInterface;
1920
use Laudis\Neo4j\TestkitBackend\MainRepository;
@@ -42,8 +43,8 @@ public function handle($request): TestkitResponseInterface
4243

4344
try {
4445
$tsx->commit();
45-
} catch (InvalidTransactionStateException $e) {
46-
return new DriverErrorResponse($request->getTxId(), '', $e->getMessage());
46+
} catch (Neo4jException $e) {
47+
return new DriverErrorResponse($request->getTxId(), $e);
4748
}
4849

4950
return new TransactionResponse($request->getTxId());

testkit-backend/src/Handlers/TransactionRollback.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Laudis\Neo4j\TestkitBackend\Handlers;
1515

1616
use Laudis\Neo4j\Exception\InvalidTransactionStateException;
17+
use Laudis\Neo4j\Exception\Neo4jException;
1718
use Laudis\Neo4j\TestkitBackend\Contracts\RequestHandlerInterface;
1819
use Laudis\Neo4j\TestkitBackend\Contracts\TestkitResponseInterface;
1920
use Laudis\Neo4j\TestkitBackend\MainRepository;
@@ -42,8 +43,8 @@ public function handle($request): TestkitResponseInterface
4243

4344
try {
4445
$tsx->rollback();
45-
} catch (InvalidTransactionStateException $e) {
46-
return new DriverErrorResponse($request->getTxId(), '', $e->getMessage());
46+
} catch (Neo4jException $e) {
47+
return new DriverErrorResponse($request->getTxId(), $e);
4748
}
4849

4950
return new TransactionResponse($request->getTxId());

testkit-backend/src/MainRepository.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,7 @@ public function removeTransaction(Uuid $id): void
143143
unset($this->transactions[$id->toRfc4122()]);
144144
}
145145

146-
/**
147-
* @return UnmanagedTransactionInterface<SummarizedResult<CypherMap<OGMTypes>>>
148-
*/
149-
public function getTransaction(Uuid $id): UnmanagedTransactionInterface
146+
public function getTransaction(Uuid $id): ?UnmanagedTransactionInterface
150147
{
151148
return $this->transactions[$id->toRfc4122()];
152149
}

testkit-backend/src/RequestFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ public function create(string $name, iterable $data): object
7979
{
8080
$class = self::MAPPINGS[$name];
8181

82+
if ($name === 'AuthorizationToken') {
83+
return new AuthorizationTokenRequest(
84+
$data['scheme'],
85+
$data['realm'] ?? '',
86+
$data['principal'],
87+
$data['credentials']
88+
);
89+
}
90+
8291
$params = [];
8392
foreach ($data as $value) {
8493
if (is_array($value) && isset($value['name'], $value['data'])) {

testkit-backend/src/Requests/AuthorizationTokenRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class AuthorizationTokenRequest
1717
{
1818
public function __construct(
1919
public readonly string $scheme,
20+
public readonly string $realm,
2021
public readonly string $principal,
2122
public readonly string $credentials
2223
) {}

testkit-backend/src/Responses/DriverErrorResponse.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Laudis\Neo4j\TestkitBackend\Responses;
1515

16+
use Laudis\Neo4j\Exception\Neo4jException;
1617
use Laudis\Neo4j\TestkitBackend\Contracts\TestkitResponseInterface;
1718
use Symfony\Component\Uid\Uuid;
1819

@@ -22,16 +23,12 @@
2223
final class DriverErrorResponse implements TestkitResponseInterface
2324
{
2425
private Uuid $id;
25-
private string $errorType;
26-
private string $message;
27-
private string $code;
26+
private Neo4jException $exception;
2827

29-
public function __construct(Uuid $id, string $errorType, string $message , string $code)
28+
public function __construct(Uuid $id, Neo4jException $exception)
3029
{
3130
$this->id = $id;
32-
$this->errorType = $errorType;
33-
$this->message = $message;
34-
$this->code = $code;
31+
$this->exception = $exception;
3532
}
3633

3734
public function jsonSerialize(): array
@@ -40,9 +37,8 @@ public function jsonSerialize(): array
4037
'name' => 'DriverError',
4138
'data' => [
4239
'id' => $this->id->toRfc4122(),
43-
'errorType' => $this->errorType,
44-
'msg' => $this->message,
45-
'code' => $this->code,
40+
'code' => $this->exception->getNeo4jCode(),
41+
'msg' => $this->exception->getNeo4jMessage()
4642
],
4743
];
4844
}

0 commit comments

Comments
 (0)