From 2b1c4921b31db74474304de3eca9b1c66e9a8d9f Mon Sep 17 00:00:00 2001 From: ju5t Date: Sat, 1 Jun 2024 20:25:53 +0200 Subject: [PATCH] tests: add tests --- tests/DirectAdminTest.php | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/DirectAdminTest.php diff --git a/tests/DirectAdminTest.php b/tests/DirectAdminTest.php new file mode 100644 index 0000000..fbc30b6 --- /dev/null +++ b/tests/DirectAdminTest.php @@ -0,0 +1,70 @@ + 'username', + ]); + }); + + $response = app(DirectAdmin::class)->call('CMD_API_DOMAIN_OWNERS'); + + expect($response->toArray()) + ->toMatchArray([ + 'domain.com' => 'username', + ]); +}); + +it('processes api call directly', function () { + Http::fake(function () { + return Http::response([ + 'domain.com' => 'username', + ]); + }); + + $response = app(DirectAdmin::class)->CMD_API_DOMAIN_OWNERS(); + + expect($response->toArray()) + ->toMatchArray([ + 'domain.com' => 'username', + ]); +}); + +it('fails when incorrect credentials are used', function () { + Http::fake(fn () => Http::response([], 401)); + + app(DirectAdmin::class)->call('NOT_LOGGED_IN'); +})->throws(AuthenticationFailed::class); + +it('fails when user does not have access to resource', function () { + Http::fake(fn () => Http::response([], 403)); + + app(DirectAdmin::class)->call('NO_ACCESS'); +})->throws(Unauthorized::class); + +it('fails if command is unknown', function () { + Http::fake(fn () => Http::response([], 405)); + + app(DirectAdmin::class)->call('UNKNOWN_COMMAND'); +})->throws(CommandNotFound::class); + +it('fails if server returns invalid JSON', function () { + Http::fake(function () { + return Http::response([ + 'error' => 'Invalid JSON', + ], 500); + }); + + app(DirectAdmin::class)->call('INVALID_JSON'); +})->throws(InvalidResponse::class); + +