Commit 70f9d79e authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

add users database tests and fix bugs found

parent efda7e1e
......@@ -36,15 +36,14 @@ test_web_backend_unit:
- cd src/web/backend
- vendor/bin/phpunit --testsuite Unit
test_web_backend_feature:
stage: test
script:
- cd src/web/backend
- vendor/bin/phpunit --testsuite Feature
test_web_backend_integration:
test_web_backend_database:
stage: test
script:
- cd src/web/backend
- vendor/bin/phpunit --testsuite Integration
- vendor/bin/phpunit --testsuite Database
......@@ -19,8 +19,8 @@
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Integration">
<directory suffix="Test.php">./tests/Integration</directory>
<testsuite name="Database">
<directory suffix="Test.php">./tests/Database</directory>
</testsuite>
</testsuites>
<filter>
......@@ -32,10 +32,6 @@
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<!-- <server name="DB_CONNECTION" value="sqlite"/>-->
<!-- <server name="DB_DATABASE" value=":memory:"/>-->
<!-- <server name="MAIL_DRIVER" value="array"/>-->
<!-- <server name="QUEUE_CONNECTION" value="sync"/>-->
<server name="SESSION_DRIVER" value="array"/>
</php>
</phpunit>
......@@ -5,7 +5,6 @@ namespace Source\Gateways\Users;
use Source\Entities\User;
// TODO: Write tests for this
class DatabaseUsersRepository implements UsersRepository
{
/**
......@@ -22,7 +21,11 @@ class DatabaseUsersRepository implements UsersRepository
return self::makeUserFromDbUser($user);
}
public static function makeUserFromDbUser(\App\User $user): User
/**
* @param \App\User $user
* @return \Source\Entities\User
*/
protected static function makeUserFromDbUser(\App\User $user): User
{
return new User(
$user->id,
......@@ -66,26 +69,40 @@ class DatabaseUsersRepository implements UsersRepository
return self::makeUserFromDbUser($newUser);
}
public function fillBasicUserAttrs(User $user, \App\User $dbUser, bool $includeDoorcode = true): \App\User
/**
* @param \Source\Entities\User $user
* @param \App\User $dbUser
* @return \App\User
*/
protected function fillBasicUserAttrs(User $user, \App\User $dbUser): \App\User
{
$dbUser->first_name = $user->getFirstName();
$dbUser->last_name = $user->getLastName();
$dbUser->display_name = $user->getDisplayName();
$dbUser->emplid = $user->getEmplid();
$dbUser->email = $user->getEmail();
$dbUser->password = bcrypt($user->getPassword());
$dbUser->expires_at = $user->getExpiresAt();
// If the password exists and is the same as provided, don't change
// Else regenerate
if (!isset($dbUser->password) || (isset($dbUser->password) && $dbUser->password !== $user->getPassword())) {
$dbUser->password = bcrypt($user->getPassword());
}
// If the doorcode exists and is the same as provided, don't change
// Else regenerate
if (!isset($dbUser->doorcode) || (isset($dbUser->doorcode) && $dbUser->doorcode !== $user->getDoorcode())) {
$dbUser->doorcode = $this->secureDoorcode($user->getDoorcode());
$dbUser->doorcode = self::secureDoorcode($user->getDoorcode());
}
return $dbUser;
}
protected function secureDoorcode(?string $doorcode): ?string
/**
* @param string|null $doorcode
* @return string|null
*/
public static function secureDoorcode(?string $doorcode): ?string
{
if (!$doorcode) {
return null;
......@@ -157,7 +174,7 @@ class DatabaseUsersRepository implements UsersRepository
*/
public function findByDoorcode(string $doorcode): ?User
{
$doorcodeSearch = $this->secureDoorcode($doorcode);
$doorcodeSearch = self::secureDoorcode($doorcode);
$user = \App\User::where('doorcode', $doorcodeSearch)->first();
......@@ -176,6 +193,10 @@ class DatabaseUsersRepository implements UsersRepository
{
$user = \App\User::where('email', strtolower($email))->first();
if (!$user) {
return null;
}
return self::makeUserFromDbUser($user);
}
}
<?php
namespace Tests\Database;
use Source\Entities\User;
use Tests\DatabaseTestCase;
use Source\Gateways\Users\DatabaseUsersRepository;
class UserDatabaseTest extends DatabaseTestCase
{
/**
* @var \Source\Gateways\Users\DatabaseUsersRepository
*/
protected DatabaseUsersRepository $users;
public function setUp(): void
{
parent::setUp();
$this->users = new DatabaseUsersRepository();
}
/**
* @test
*/
public function it_gets_all_users_with_none(): void
{
$all = $this->users->all();
$this->assertEquals([], $all);
}
/**
* @test
*/
public function it_gets_all_users(): void
{
$this->users->create(new User(0, '', '', '', null, 'email@email.com', null, null));
$this->users->create(new User(0, '', '', '', null, 'email2@email.com', null, null));
$all = $this->users->all();
$this->assertCount(2, $all);
}
/**
* @test
*/
public function it_creates_a_user(): void
{
$user = $this->users->create(new User(100344, 'first', 'last', 'disp', 'emplid', 'email', 'password', 'doorcode'));
$this->assertEquals($this->users->get(1), $user);
$user = $this->users->get(1);
$this->assertEquals(
'cc356dd9dac1c54e8ad414ea2df7de0da19261724ccca9f449c3e33e1e5d996824241948a7e4bc466447aa9c3efccbfe8a32ced03fea8b5cd8c564f741382ee4',
$user->getDoorcode()
);
$this->assertTrue(password_verify('password', $user->getPassword()));
}
/** @test */
public function it_updates_a_user(): void
{
$user = $this->users->create(new User(100344, 'first', 'last', 'disp', 'emplid', 'email', 'password', 'doorcode'));
// Test that it won't change password and doorcode if they are equal to whats in DB
$this->users->update(1, new User(
0,
'new first',
'new last',
'new display',
null,
'eeem',
$user->getPassword(),
$user->getDoorcode()
));
$user = $this->users->get(1);
$this->assertTrue(password_verify('password', $user->getPassword()));
$this->assertEquals(
DatabaseUsersRepository::secureDoorcode('doorcode'),
$user->getDoorcode()
);
$this->assertEquals('new first', $user->getFirstName());
$this->assertEquals('new last', $user->getLastName());
$this->assertEquals('new display', $user->getDisplayName());
$this->assertEquals(null, $user->getEmplid());
$this->assertEquals('eeem', $user->getEmail());
$this->users->update(1, new User(
0,
'new first',
'new last',
'new display',
null,
'eeem',
'new',
'new'
));
$user = $this->users->get(1);
$this->assertTrue(password_verify('new', $user->getPassword()));
$this->assertEquals(
DatabaseUsersRepository::secureDoorcode('new'),
$user->getDoorcode()
);
}
/**
* @test
*/
public function it_deletes_a_user(): void
{
$this->users->create(new User(100344, 'first', 'last', 'disp', 'emplid', 'email', 'password', 'doorcode'));
$this->assertTrue($this->users->delete(1));
$this->assertFalse($this->users->delete(2));
}
/**
* @test
*/
public function it_gets_a_user(): void
{
$user = $this->users->create(new User(100344, 'first', 'last', 'disp', 'emplid', 'email', 'password', 'doorcode'));
$userToCompare = $this->users->get(1);
$this->assertEquals($user, $userToCompare);
}
/**
* @test
*/
public function it_checks_existence_of_users(): void
{
$this->users->create(new User(100344, 'first', 'last', 'disp', 'emplid', 'email', 'password', 'doorcode'));
$this->assertTrue($this->users->exists(1));
$this->assertFalse($this->users->exists(2));
}
/**
* @test
*/
public function it_can_find_users(): void
{
$user = $this->users->create(new User(100344, 'first', 'last', 'disp', 'emplid', 'email', 'password', 'doorcode'));
$this->assertEquals($user, $this->users->findByCredentials('email', 'password'));
$this->assertNull($this->users->findByCredentials('email', 'aasdf'));
$this->assertNull($this->users->findByCredentials('asdf', 'password'));
$this->assertEquals($user, $this->users->findByEmail('email'));
$this->assertNull($this->users->findByEmail('email1'));
$this->assertEquals($user, $this->users->findByDoorcode('doorcode'));
$this->assertNull($this->users->findByDoorcode('what doorcode'));
}
}
<?php
namespace Tests;
use Illuminate\Foundation\Testing\DatabaseMigrations;
abstract class DatabaseTestCase extends TestCase
{
use DatabaseMigrations;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment