Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 - Add authentication tools #54

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/Features/Traits/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Blumilk\BLT\Features\Traits;

use Behat\Gherkin\Node\TableNode;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Container\BindingResolutionException;
use PHPUnit\Framework\Assert;
use Symfony\Component\HttpFoundation\Response;

trait Authentication
{
Expand Down Expand Up @@ -52,6 +54,103 @@ public function userAuthenticatedInSessionIs(string $value, string $field): void
Assert::assertEquals($value, $auth->user()?->{$field});
}

/**
* @Given there is an authenticated user with email :email
* @throws BindingResolutionException
*/
public function thereIsAuthenticatedUserWithEmail(string $email): void
{
$auth = $this->getContainer()->make(Guard::class);
$auth->login($this->getUserModel()::query()->where("email", $email)->first());
}

/**
* @Given there are users in the database:
*/
public function thereAreUsersInTheDatabase(TableNode $table): void
{
foreach ($table->getColumnsHash() as $userData) {
$this->getUserModel()::firstOrCreate($userData);
}
}

/**
* @Given there is an unauthenticated user
* @throws BindingResolutionException
*/
public function thereIsAnUnauthenticatedUser(): void
{
$this->thereIsNoUserAuthenticatedInSession();
}

/**
* @Then the authenticated user's email should be :email
PiotrFedak marked this conversation as resolved.
Show resolved Hide resolved
* @throws BindingResolutionException
*/
public function authenticatedUsersEmailShouldBe(string $email): void
{
$auth = $this->getContainer()->make(Guard::class);
Assert::assertEquals($email, $auth->user()->email);
}

/**
* @Then the authenticated user should have the attribute :attribute with value :value
* @throws BindingResolutionException
*/
public function authenticatedUserShouldHaveAttribute(string $attribute, $value): void
{
$auth = $this->getContainer()->make(Guard::class);
$user = $auth->user();
Assert::assertEquals($value, $user->{$attribute});
}

/**
* @Given the user is authenticated with attributes:
*/
public function userIsAuthenticatedWithAttributes(TableNode $table): void
{
$attributes = $table->getRowsHash();
$user = $this->getUserModel()::firstOrCreate($attributes);
$auth = $this->getContainer()->make(Guard::class);
$auth->login($user);
}

/**
* @Then the user should be able to logout
* @throws BindingResolutionException
*/
public function userShouldBeAbleToLogout(): void
{
$auth = $this->getContainer()->make(Guard::class);
$auth->logout();
Assert::assertNull($auth->user());
}

/**
* @Then the authenticated user should be redirected to :url
* @throws BindingResolutionException
*/
public function authenticatedUserShouldBeRedirectedTo(string $url): void
{
$response = $this->response;
Assert::assertTrue(
$response->isRedirect($url),
"Authenticated user was not redirected to $url.",
);
}

/**
* @Then the user with email :email should not be able to access :url
* @throws BindingResolutionException
*/
public function userWithEmailShouldNotBeAbleToAccess(string $email, string $url): void
{
$auth = $this->getContainer()->make(Guard::class);
$auth->login($this->getUserModel()::query()->where("email", $email)->first());
$response = $this->call("GET", $url);
Assert::assertEquals(Response::HTTP_FORBIDDEN, $response->status());
}

/**
* @return class-string
*/
Expand Down
Loading