Skip to content

Commit 0363cb6

Browse files
committed
test: clean up tests
1 parent 650f39c commit 0363cb6

File tree

3 files changed

+51
-33
lines changed

3 files changed

+51
-33
lines changed

packages/support/src/Json/Exception/JsonCouldNotBeDecoded.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tempest\Support\Json\Exception;
66

77
use InvalidArgumentException;
8+
use Tempest\Support\Json\Exception\JsonException;
89

910
final class JsonCouldNotBeDecoded extends InvalidArgumentException implements JsonException
1011
{

tests/Integration/Database/EncryptedAttributeTest.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Tempest\Integration\Database;
44

5+
use PHPUnit\Framework\Attributes\Test;
56
use Tempest\Cryptography\Encryption\Encrypter;
67
use Tempest\Database\DatabaseMigration;
78
use Tempest\Database\Encrypted;
@@ -13,20 +14,23 @@
1314
use Tempest\Database\Table;
1415
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1516

17+
use function Tempest\Database\query;
18+
1619
final class EncryptedAttributeTest extends FrameworkIntegrationTestCase
1720
{
1821
private Encrypter $encrypter {
1922
get => $this->container->get(Encrypter::class);
2023
}
2124

22-
public function test_encrypted_attribute_encrypts_value_on_insert(): void
25+
#[Test]
26+
public function encrypts_value_on_insert(): void
2327
{
2428
$this->migrate(CreateMigrationsTable::class, CreateUserWithEncryptedDataTable::class);
2529

26-
$user = new UserWithEncryptedData(
30+
$user = query(UserWithEncryptedData::class)->create(
2731
email: 'test@example.com',
2832
secret: 'sensitive information', // @mago-expect security/no-literal-password
29-
)->save()->refresh();
33+
);
3034

3135
$this->assertSame('sensitive information', $user->secret);
3236

@@ -35,14 +39,15 @@ public function test_encrypted_attribute_encrypts_value_on_insert(): void
3539
$this->assertNotSame('sensitive information', $encrypted['secret']);
3640
}
3741

38-
public function test_encrypted_attribute_encrypts_value_on_update(): void
42+
#[Test]
43+
public function encrypts_value_on_update(): void
3944
{
4045
$this->migrate(CreateMigrationsTable::class, CreateUserWithEncryptedDataTable::class);
4146

42-
$user = new UserWithEncryptedData(
47+
$user = query(UserWithEncryptedData::class)->create(
4348
email: 'test@example.com',
4449
secret: 'original secret', // @mago-expect security/no-literal-password
45-
)->save()->refresh();
50+
);
4651

4752
$user->update(secret: 'new secret')->refresh(); // @mago-expect security/no-literal-password
4853

@@ -54,40 +59,41 @@ public function test_encrypted_attribute_encrypts_value_on_update(): void
5459
$this->assertNotSame('new secret', $encrypted['secret']);
5560
}
5661

57-
public function test_encrypted_attribute_does_not_re_encrypt_already_encrypted_values(): void
62+
#[Test]
63+
public function does_not_re_encrypt_already_encrypted_values(): void
5864
{
5965
$this->migrate(CreateMigrationsTable::class, CreateUserWithEncryptedDataTable::class);
6066

61-
$alreadyEncrypted = $this->encrypter->encrypt('sensitive data');
62-
63-
$user = new UserWithEncryptedData(
67+
$user = query(UserWithEncryptedData::class)->create(
6468
email: 'test@example.com',
65-
secret: $alreadyEncrypted,
66-
)->save()->refresh();
69+
secret: $this->encrypter->encrypt('sensitive data'),
70+
);
6771

6872
$this->assertSame('sensitive data', $user->secret);
6973
}
7074

71-
public function test_encrypted_attribute_handles_null_values(): void
75+
#[Test]
76+
public function handles_null_values(): void
7277
{
7378
$this->migrate(CreateMigrationsTable::class, CreateUserWithNullableEncryptedDataTable::class);
7479

75-
$user = new UserWithNullableEncryptedData(
80+
$user = query(UserWithNullableEncryptedData::class)->create(
7681
email: 'test@example.com',
7782
secret: null,
78-
)->save()->refresh();
83+
);
7984

8085
$this->assertNull($user->secret);
8186
}
8287

83-
public function test_encrypted_attribute_handles_empty_strings(): void
88+
#[Test]
89+
public function handles_empty_strings(): void
8490
{
8591
$this->migrate(CreateMigrationsTable::class, CreateUserWithEncryptedDataTable::class);
8692

87-
$user = new UserWithEncryptedData(
93+
$user = query(UserWithEncryptedData::class)->create(
8894
email: 'test@example.com',
8995
secret: '',
90-
)->save()->refresh();
96+
);
9197

9298
$this->assertSame('', $user->secret);
9399
}

tests/Integration/Database/HashedAttributeTest.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Tempest\Integration\Database;
44

5+
use PHPUnit\Framework\Attributes\Test;
56
use Tempest\Cryptography\Password\PasswordHasher;
67
use Tempest\Database\DatabaseMigration;
78
use Tempest\Database\Hashed;
@@ -11,33 +12,43 @@
1112
use Tempest\Database\QueryStatements\CreateTableStatement;
1213
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1314

15+
use function Tempest\Database\query;
16+
1417
final class HashedAttributeTest extends FrameworkIntegrationTestCase
1518
{
1619
private PasswordHasher $hasher {
1720
get => $this->container->get(PasswordHasher::class);
1821
}
1922

20-
public function test_hashed_attribute_hashes_value_on_insert(): void
23+
#[Test]
24+
public function hashes_value_on_insert(): void
2125
{
2226
$this->migrate(CreateMigrationsTable::class, CreateUserWithHashTable::class);
2327

24-
$user = new UserWithHash(
28+
$user = query(UserWithHash::class)->create(
2529
email: 'test@example.com',
2630
password: 'plaintext-password', // @mago-expect security/no-literal-password
27-
)->save()->refresh();
31+
);
32+
33+
// The current behavior when creating a model is to not refresh it.
34+
// In this case, it might be a potential security issue?
35+
$this->assertSame('plaintext-password', $user->password);
36+
37+
$user->refresh();
2838

2939
$this->assertNotSame('plaintext-password', $user->password);
3040
$this->assertTrue($this->hasher->verify('plaintext-password', $user->password));
3141
}
3242

33-
public function test_hashed_attribute_hashes_value_on_update(): void
43+
#[Test]
44+
public function hashes_value_on_update(): void
3445
{
3546
$this->migrate(CreateMigrationsTable::class, CreateUserWithHashTable::class);
3647

37-
$user = new UserWithHash(
48+
$user = query(UserWithHash::class)->create(
3849
email: 'test@example.com',
3950
password: 'original-password', // @mago-expect security/no-literal-password
40-
)->save()->refresh();
51+
)->refresh();
4152

4253
$originalHash = $user->password;
4354

@@ -50,29 +61,29 @@ public function test_hashed_attribute_hashes_value_on_update(): void
5061
$this->assertFalse($this->hasher->verify('original-password', $user->password));
5162
}
5263

53-
public function test_hashed_attribute_does_not_rehash_already_hashed_values(): void
64+
#[Test]
65+
public function does_not_rehash_already_hashed_values(): void
5466
{
5567
$this->migrate(CreateMigrationsTable::class, CreateUserWithHashTable::class);
5668

57-
$alreadyHashed = $this->hasher->hash('plaintext-password');
58-
59-
$user = new UserWithHash(
69+
$user = query(UserWithHash::class)->create(
6070
email: 'test@example.com',
61-
password: $alreadyHashed,
62-
)->save()->refresh();
71+
password: $alreadyHashed = $this->hasher->hash('plaintext-password'),
72+
)->refresh();
6373

6474
$this->assertSame($alreadyHashed, $user->password);
6575
$this->assertTrue($this->hasher->verify('plaintext-password', $user->password));
6676
}
6777

68-
public function test_hashed_attribute_handles_null_values(): void
78+
#[Test]
79+
public function handles_null_values(): void
6980
{
7081
$this->migrate(CreateMigrationsTable::class, CreateUserWithNullablePasswordTable::class);
7182

72-
$user = new UserWithNullablePassword(
83+
$user = query(UserWithNullablePassword::class)->create(
7384
email: 'test@example.com',
7485
password: null,
75-
)->save()->refresh();
86+
)->refresh();
7687

7788
$this->assertNull($user->password);
7889
}

0 commit comments

Comments
 (0)