|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RESTAPI\Tests; |
| 4 | + |
| 5 | +use RESTAPI\Core\TestCase; |
| 6 | +use RESTAPI\Models\FreeRADIUSClient; |
| 7 | + |
| 8 | +class APIModelsFreeRADIUSClientTestCase extends TestCase { |
| 9 | + public array $required_packages = ['pfSense-pkg-freeradius3']; |
| 10 | + |
| 11 | + /** |
| 12 | + * Checks that we can create, read and delete FreeRADIUSClient models. |
| 13 | + */ |
| 14 | + public function test_crud(): void { |
| 15 | + # Create a new FreeRADIUSClient |
| 16 | + $client = new FreeRADIUSClient( |
| 17 | + addr: '1.2.3.4', |
| 18 | + ip_version: "ipaddr", |
| 19 | + shortname: "testclient", |
| 20 | + secret: "testsecret", |
| 21 | + proto: 'udp', |
| 22 | + nastype: 'dot1x', |
| 23 | + msgauth: false, |
| 24 | + maxconn: 16, |
| 25 | + naslogin: 'testlogin', |
| 26 | + naspassword: 'testpassword', |
| 27 | + description: 'Test client', |
| 28 | + ); |
| 29 | + $client->create(); |
| 30 | + $raddb = file_get_contents('/usr/local/etc/raddb/clients.conf'); |
| 31 | + $this->assert_str_contains($raddb, 'client "testclient" {'); |
| 32 | + $this->assert_str_contains($raddb, 'ipaddr = 1.2.3.4'); |
| 33 | + $this->assert_str_contains($raddb, "proto = udp"); |
| 34 | + $this->assert_str_contains($raddb, "nas_type = dot1x"); |
| 35 | + $this->assert_str_contains($raddb, "login = testlogin"); |
| 36 | + $this->assert_str_contains($raddb, "password = testpassword"); |
| 37 | + $this->assert_str_contains($raddb, "require_message_authenticator = no"); |
| 38 | + $this->assert_str_contains($raddb, 'max_connections = 16'); |
| 39 | + |
| 40 | + |
| 41 | + # Ensure we can read the created user from the config |
| 42 | + $read_client = new FreeRADIUSClient(id: $client->id); |
| 43 | + $this->assert_equals($read_client->addr->value, "1.2.3.4"); |
| 44 | + $this->assert_equals($read_client->ip_version->value, "ipaddr"); |
| 45 | + $this->assert_equals($read_client->shortname->value, "testclient"); |
| 46 | + $this->assert_str_contains($read_client->proto->value, "udp"); |
| 47 | + $this->assert_str_contains($read_client->nastype->value, "dot1x"); |
| 48 | + $this->assert_equals($read_client->msgauth->value, false); |
| 49 | + $this->assert_equals($read_client->maxconn->value, 16); |
| 50 | + $this->assert_equals($read_client->naslogin->value, "testlogin"); |
| 51 | + $this->assert_equals($read_client->naspassword->value, "testpassword"); |
| 52 | + $this->assert_equals($read_client->description->value, "Test client"); |
| 53 | + |
| 54 | + # Ensure we can update the user |
| 55 | + $client = new FreeRADIUSClient( |
| 56 | + id: $read_client->id, |
| 57 | + addr: '4321::1', |
| 58 | + ip_version: "ipv6addr", |
| 59 | + shortname: "newtestclient", |
| 60 | + secret: "newtestsecret", |
| 61 | + proto: 'tcp', |
| 62 | + nastype: 'cisco', |
| 63 | + msgauth: true, |
| 64 | + maxconn: 32, |
| 65 | + naslogin: 'newtestlogin', |
| 66 | + naspassword: 'newtestpassword', |
| 67 | + description: 'New test client', |
| 68 | + ); |
| 69 | + $client->update(); |
| 70 | + $raddb = file_get_contents('/usr/local/etc/raddb/clients.conf'); |
| 71 | + $this->assert_str_does_not_contain($raddb, 'client "testclient" {'); |
| 72 | + $this->assert_str_contains($raddb, 'client "newtestclient" {'); |
| 73 | + $this->assert_str_contains($raddb, 'ipaddr = 4321::1'); |
| 74 | + $this->assert_str_contains($raddb, "proto = tcp"); |
| 75 | + $this->assert_str_contains($raddb, "nas_type = cisco"); |
| 76 | + $this->assert_str_contains($raddb, "login = newtestlogin"); |
| 77 | + $this->assert_str_contains($raddb, "password = newtestpassword"); |
| 78 | + $this->assert_str_contains($raddb, "require_message_authenticator = yes"); |
| 79 | + $this->assert_str_contains($raddb, 'max_connections = 32'); |
| 80 | + |
| 81 | + # Delete the user and ensure it is removed from the database |
| 82 | + $client->delete(); |
| 83 | + $raddb = file_get_contents('/usr/local/etc/raddb/clients.conf'); |
| 84 | + $this->assert_str_does_not_contain($raddb, 'client "newtestclient" {'); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Checks that an error is thrown if the ip_version does not match the value provided |
| 89 | + * in the addr field. |
| 90 | + */ |
| 91 | + public function test_ip_version_mismatch(): void { |
| 92 | + $this->assert_throws_response( |
| 93 | + response_id: "FREERADIUS_CLIENT_ADDR_IPV4_NOT_ALLOWED", |
| 94 | + code: 400, |
| 95 | + callable: function () { |
| 96 | + $client = new FreeRADIUSClient(ip_version: "ipv6addr"); |
| 97 | + $client->validate_addr("1.2.3.4"); |
| 98 | + } |
| 99 | + ); |
| 100 | + $this->assert_throws_response( |
| 101 | + response_id: "FREERADIUS_CLIENT_ADDR_IPV6_NOT_ALLOWED", |
| 102 | + code: 400, |
| 103 | + callable: function () { |
| 104 | + $client = new FreeRADIUSClient(ip_version: "ipaddr"); |
| 105 | + $client->validate_addr("1234::1"); |
| 106 | + } |
| 107 | + ); |
| 108 | + |
| 109 | + # Ensure * is always allowed |
| 110 | + $this->assert_does_not_throw( |
| 111 | + callable: function () { |
| 112 | + $client = new FreeRADIUSClient(ip_version: "ipaddr"); |
| 113 | + $client->validate_addr("*"); |
| 114 | + $client = new FreeRADIUSClient(ip_version: "ipv6addr"); |
| 115 | + $client->validate_addr("*"); |
| 116 | + } |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
0 commit comments