|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RESTAPI\Tests; |
| 4 | + |
| 5 | +use RESTAPI\Core\TestCase; |
| 6 | +use RESTAPI\Models\FreeRADIUSUser; |
| 7 | + |
| 8 | +class APIModelsFreeRADIUSUserTestCase extends TestCase { |
| 9 | + public array $required_packages = ['pfSense-pkg-freeradius3']; |
| 10 | + |
| 11 | + /** |
| 12 | + * Checks that we can create, read and delete FreeRADIUSUser models. |
| 13 | + */ |
| 14 | + public function test_crud(): void { |
| 15 | + # Create a new FreeRADIUSUser with password and ensure it is present in the database |
| 16 | + $user = new FreeRADIUSUser( |
| 17 | + username: 'testuser', |
| 18 | + password: 'testpassword', |
| 19 | + password_encryption: 'Cleartext-Password', |
| 20 | + motp_enable: false, |
| 21 | + description: 'Test User', |
| 22 | + ); |
| 23 | + $user->create(); |
| 24 | + $raddb = file_get_contents('/usr/local/etc/raddb/users'); |
| 25 | + $this->assert_str_contains($raddb, 'testuser" Cleartext-Password := "testpassword"'); |
| 26 | + |
| 27 | + # Ensure we can read the created user from the config |
| 28 | + $read_user = new FreeRADIUSUser(id: $user->id); |
| 29 | + $this->assert_equals($read_user->username->value, 'testuser'); |
| 30 | + $this->assert_equals($read_user->password_encryption->value, 'Cleartext-Password'); |
| 31 | + $this->assert_equals($read_user->motp_enable->value, false); |
| 32 | + $this->assert_equals($read_user->description->value, 'Test User'); |
| 33 | + |
| 34 | + # Ensure we can update the user |
| 35 | + $user = new FreeRADIUSUser( |
| 36 | + id: $read_user->id, |
| 37 | + username: 'motptestuser', |
| 38 | + motp_enable: true, |
| 39 | + motp_authmethod: 'motp', |
| 40 | + motp_secret: 'abcdef0123456789', |
| 41 | + motp_pin: '1234', |
| 42 | + motp_offset: 30, |
| 43 | + ); |
| 44 | + $user->update(); |
| 45 | + $raddb = file_get_contents('/usr/local/etc/raddb/users'); |
| 46 | + $this->assert_str_does_not_contain($raddb, 'testuser" Cleartext-Password := "testpassword"'); |
| 47 | + $this->assert_str_contains($raddb, '"motptestuser" Auth-Type = motp'); |
| 48 | + $this->assert_str_contains($raddb, 'MOTP-Init-Secret = abcdef0123456789'); |
| 49 | + $this->assert_str_contains($raddb, 'MOTP-PIN = 1234'); |
| 50 | + $this->assert_str_contains($raddb, 'MOTP-Offset = 30'); |
| 51 | + |
| 52 | + # Delete the user and ensure it is removed from the database |
| 53 | + $user->delete(); |
| 54 | + $raddb = file_get_contents('/usr/local/etc/raddb/users'); |
| 55 | + $this->assert_str_does_not_contain($raddb, '"motptestuser" Auth-Type = motp'); |
| 56 | + } |
| 57 | +} |
0 commit comments