2022-05-08 20:49:53 +02:00
package parser
import (
"errors"
2023-10-09 01:16:53 +02:00
"os"
2025-03-24 05:16:34 +11:00
"regexp"
2022-05-08 20:49:53 +02:00
"testing"
2023-08-30 23:04:35 +02:00
"github.com/stretchr/testify/assert"
2022-05-08 20:49:53 +02:00
)
func mockReadfilebytesConfigurationsCore ( filePath string ) ( [ ] byte , error ) {
configurationsCoreBytes := [ ] byte ( `
core :
logging :
debug : false
debugReportCaller : false
logDisableTimestamp : true
2022-05-17 23:36:06 +02:00
logsPath : . / logs
2023-08-30 23:04:35 +02:00
tracings :
rabbit - mq :
enabled : true
2024-03-02 15:29:43 +01:00
uri : "amqp://user:password@localhost/"
2024-08-01 20:05:05 +02:00
beelzebub - cloud :
enabled : true
uri : "amqp://user:password@localhost/"
auth - token : "iejfdjsl-aosdajosoidaj-dunfkjnfkjsdnkn" ` )
2022-05-08 20:49:53 +02:00
return configurationsCoreBytes , nil
}
func mockReadfilebytesFormatError ( filePath string ) ( [ ] byte , error ) {
configurationsCoreBytes := [ ] byte ( ` {{ } ` )
return configurationsCoreBytes , nil
}
func mockReadfilebytesError ( filePath string ) ( [ ] byte , error ) {
return nil , errors . New ( "mockErrorReadFileBytes" )
}
func mockReadDirError ( dirPath string ) ( [ ] string , error ) {
return nil , errors . New ( "mockErrorReadFileBytes" )
}
func mockReadDirValid ( dirPath string ) ( [ ] string , error ) {
return [ ] string { "" } , nil
}
func mockReadfilebytesBeelzebubServiceConfiguration ( filePath string ) ( [ ] byte , error ) {
beelzebubServiceConfiguration := [ ] byte ( `
apiVersion : "v1"
protocol : "http"
address : ":8080"
2025-02-14 06:54:22 +11:00
tlsCertPath : "/tmp/cert.crt"
tlsKeyPath : "/tmp/cert.key"
2025-07-01 23:07:49 +02:00
tools :
- name : "tool:user-account-manager"
description : "Tool for querying and modifying user account details. Requires administrator privileges."
params :
- name : "user_id"
description : "The ID of the user account to manage."
- name : "action"
description : "The action to perform on the user account, possible values are: get_details, reset_password, deactivate_account"
handler : "reset_password ok"
2022-05-08 20:49:53 +02:00
commands :
- regex : "wp-admin"
handler : "login"
headers :
2024-07-21 20:11:18 +02:00
- "Content-Type: text/html"
2025-03-24 05:16:34 +11:00
- name : "wp-admin"
regex : "wp-admin"
handler : "login"
headers :
- "Content-Type: text/html"
2025-03-13 12:36:46 +05:30
fallbackCommand :
handler : "404 Not Found!"
statusCode : 404
2024-07-21 20:11:18 +02:00
plugin :
openAISecretKey : "qwerty"
llmModel : "llama3"
2025-02-16 22:48:59 +01:00
llmProvider : "ollama"
2024-07-21 20:11:18 +02:00
host : "localhost:1563"
2025-01-14 08:45:30 +01:00
prompt : "hello world"
2024-07-21 20:11:18 +02:00
` )
2022-05-08 20:49:53 +02:00
return beelzebubServiceConfiguration , nil
}
func TestReadConfigurationsCoreError ( t * testing . T ) {
configurationsParser := Init ( "mockConfigurationsCorePath" , "mockConfigurationsServicesDirectory" )
2022-05-12 22:46:15 +02:00
configurationsParser . readFileBytesByFilePathDependency = mockReadfilebytesError
2022-05-08 20:49:53 +02:00
beelzebubCoreConfigurations , err := configurationsParser . ReadConfigurationsCore ( )
assert . Nil ( t , beelzebubCoreConfigurations )
assert . Error ( t , err )
assert . Equal ( t , "in file mockConfigurationsCorePath: mockErrorReadFileBytes" , err . Error ( ) )
2022-05-12 22:46:15 +02:00
configurationsParser . readFileBytesByFilePathDependency = mockReadfilebytesFormatError
2022-05-08 20:49:53 +02:00
beelzebubCoreConfigurations , err = configurationsParser . ReadConfigurationsCore ( )
assert . Nil ( t , beelzebubCoreConfigurations )
assert . Error ( t , err )
assert . Equal ( t , "in file mockConfigurationsCorePath: yaml: line 1: did not find expected ',' or '}'" , err . Error ( ) )
}
func TestReadConfigurationsCoreValid ( t * testing . T ) {
configurationsParser := Init ( "" , "" )
2022-05-12 22:46:15 +02:00
configurationsParser . readFileBytesByFilePathDependency = mockReadfilebytesConfigurationsCore
2022-05-08 20:49:53 +02:00
coreConfigurations , err := configurationsParser . ReadConfigurationsCore ( )
assert . Nil ( t , err )
assert . NotNil ( t , coreConfigurations . Core )
assert . NotNil ( t , coreConfigurations . Core . Logging )
assert . Equal ( t , coreConfigurations . Core . Logging . Debug , false )
assert . Equal ( t , coreConfigurations . Core . Logging . LogDisableTimestamp , true )
assert . Equal ( t , coreConfigurations . Core . Logging . DebugReportCaller , false )
assert . Equal ( t , coreConfigurations . Core . Logging . LogsPath , "./logs" )
2023-08-30 23:04:35 +02:00
assert . Equal ( t , coreConfigurations . Core . Tracings . RabbitMQ . Enabled , true )
assert . Equal ( t , coreConfigurations . Core . Tracings . RabbitMQ . URI , "amqp://user:password@localhost/" )
2024-08-01 20:05:05 +02:00
assert . Equal ( t , coreConfigurations . Core . BeelzebubCloud . Enabled , true )
assert . Equal ( t , coreConfigurations . Core . BeelzebubCloud . URI , "amqp://user:password@localhost/" )
assert . Equal ( t , coreConfigurations . Core . BeelzebubCloud . AuthToken , "iejfdjsl-aosdajosoidaj-dunfkjnfkjsdnkn" )
2022-05-08 20:49:53 +02:00
}
func TestReadConfigurationsServicesFail ( t * testing . T ) {
configurationsParser := Init ( "" , "" )
2022-05-12 22:46:15 +02:00
configurationsParser . readFileBytesByFilePathDependency = mockReadfilebytesError
2022-05-14 16:51:00 +02:00
configurationsParser . gelAllFilesNameByDirNameDependency = mockReadDirError
2022-05-08 20:49:53 +02:00
beelzebubServiceConfiguration , err := configurationsParser . ReadConfigurationsServices ( )
assert . Nil ( t , beelzebubServiceConfiguration )
assert . Error ( t , err )
}
func TestReadConfigurationsServicesValid ( t * testing . T ) {
configurationsParser := Init ( "" , "" )
2022-05-12 22:46:15 +02:00
configurationsParser . readFileBytesByFilePathDependency = mockReadfilebytesBeelzebubServiceConfiguration
2022-05-14 16:51:00 +02:00
configurationsParser . gelAllFilesNameByDirNameDependency = mockReadDirValid
2022-05-08 20:49:53 +02:00
beelzebubServicesConfiguration , err := configurationsParser . ReadConfigurationsServices ( )
2024-07-21 20:11:18 +02:00
assert . Nil ( t , err )
2022-05-08 20:49:53 +02:00
firstBeelzebubServiceConfiguration := beelzebubServicesConfiguration [ 0 ]
assert . Equal ( t , firstBeelzebubServiceConfiguration . Protocol , "http" )
assert . Equal ( t , firstBeelzebubServiceConfiguration . ApiVersion , "v1" )
assert . Equal ( t , firstBeelzebubServiceConfiguration . Address , ":8080" )
2025-03-24 05:16:34 +11:00
assert . Equal ( t , len ( firstBeelzebubServiceConfiguration . Commands ) , 2 )
assert . Equal ( t , len ( firstBeelzebubServiceConfiguration . Commands ) , 2 )
assert . Equal ( t , firstBeelzebubServiceConfiguration . Commands [ 0 ] . RegexStr , "wp-admin" )
assert . Equal ( t , firstBeelzebubServiceConfiguration . Commands [ 0 ] . Regex . String ( ) , "wp-admin" )
2022-05-08 20:49:53 +02:00
assert . Equal ( t , firstBeelzebubServiceConfiguration . Commands [ 0 ] . Handler , "login" )
assert . Equal ( t , len ( firstBeelzebubServiceConfiguration . Commands [ 0 ] . Headers ) , 1 )
assert . Equal ( t , firstBeelzebubServiceConfiguration . Commands [ 0 ] . Headers [ 0 ] , "Content-Type: text/html" )
2025-03-24 05:16:34 +11:00
assert . Equal ( t , firstBeelzebubServiceConfiguration . Commands [ 1 ] . Name , "wp-admin" )
2025-03-13 12:36:46 +05:30
assert . Equal ( t , firstBeelzebubServiceConfiguration . FallbackCommand . Handler , "404 Not Found!" )
assert . Equal ( t , firstBeelzebubServiceConfiguration . FallbackCommand . StatusCode , 404 )
2024-07-21 20:11:18 +02:00
assert . Equal ( t , firstBeelzebubServiceConfiguration . Plugin . OpenAISecretKey , "qwerty" )
assert . Equal ( t , firstBeelzebubServiceConfiguration . Plugin . LLMModel , "llama3" )
2025-02-16 22:48:59 +01:00
assert . Equal ( t , firstBeelzebubServiceConfiguration . Plugin . LLMProvider , "ollama" )
2024-07-21 20:11:18 +02:00
assert . Equal ( t , firstBeelzebubServiceConfiguration . Plugin . Host , "localhost:1563" )
2025-01-14 08:45:30 +01:00
assert . Equal ( t , firstBeelzebubServiceConfiguration . Plugin . Prompt , "hello world" )
2025-02-14 06:54:22 +11:00
assert . Equal ( t , firstBeelzebubServiceConfiguration . TLSCertPath , "/tmp/cert.crt" )
assert . Equal ( t , firstBeelzebubServiceConfiguration . TLSKeyPath , "/tmp/cert.key" )
2025-07-01 23:07:49 +02:00
assert . Equal ( t , firstBeelzebubServiceConfiguration . TLSKeyPath , "/tmp/cert.key" )
assert . Equal ( t , len ( firstBeelzebubServiceConfiguration . Tools ) , 1 )
assert . Equal ( t , firstBeelzebubServiceConfiguration . Tools [ 0 ] . Name , "tool:user-account-manager" )
assert . Equal ( t , firstBeelzebubServiceConfiguration . Tools [ 0 ] . Description , "Tool for querying and modifying user account details. Requires administrator privileges." )
assert . Equal ( t , len ( firstBeelzebubServiceConfiguration . Tools [ 0 ] . Params ) , 2 )
assert . Equal ( t , firstBeelzebubServiceConfiguration . Tools [ 0 ] . Params [ 0 ] . Name , "user_id" )
assert . Equal ( t , firstBeelzebubServiceConfiguration . Tools [ 0 ] . Params [ 0 ] . Description , "The ID of the user account to manage." )
assert . Equal ( t , firstBeelzebubServiceConfiguration . Tools [ 0 ] . Handler , "reset_password ok" )
2022-05-08 20:49:53 +02:00
}
2023-10-09 01:16:53 +02:00
func TestGelAllFilesNameByDirName ( t * testing . T ) {
var dir = t . TempDir ( )
files , err := gelAllFilesNameByDirName ( dir )
assert . Nil ( t , err )
assert . Equal ( t , 0 , len ( files ) )
}
func TestGelAllFilesNameByDirNameFiles ( t * testing . T ) {
var dir = t . TempDir ( )
testFiles := [ ] string { "file1.yaml" , "file2.yaml" , "file3.txt" , "subdir" , "file4.yaml" }
for _ , filename := range testFiles {
filePath := dir + "/" + filename
file , err := os . Create ( filePath )
assert . NoError ( t , err )
file . Close ( )
}
files , err := gelAllFilesNameByDirName ( dir )
assert . Nil ( t , err )
assert . Equal ( t , 3 , len ( files ) )
}
func TestGelAllFilesNameByDirNameError ( t * testing . T ) {
files , err := gelAllFilesNameByDirName ( "nosuchfile" )
assert . Nil ( t , files )
2025-03-13 12:36:46 +05:30
// Windows and Linux return slightly different error strings, but share a common prefix, so check for that.
assert . Contains ( t , err . Error ( ) , "open nosuchfile: " )
2023-10-09 01:16:53 +02:00
}
func TestReadFileBytesByFilePath ( t * testing . T ) {
var dir = t . TempDir ( )
filePath := dir + "/test.yaml"
f , err := os . Create ( filePath )
assert . NoError ( t , err )
f . Close ( )
bytes , err := readFileBytesByFilePath ( filePath )
assert . NoError ( t , err )
assert . Equal ( t , "" , string ( bytes ) )
}
2025-03-24 05:16:34 +11:00
func TestCompileCommandRegex ( t * testing . T ) {
tests := [ ] struct {
name string
config BeelzebubServiceConfiguration
expectedError bool
} {
{
name : "Valid Regex" ,
config : BeelzebubServiceConfiguration {
Commands : [ ] Command {
{ RegexStr : "^/api/v1/.*$" } ,
{ RegexStr : "wp-admin" } ,
} ,
} ,
expectedError : false ,
} ,
{
name : "Empty Regex" ,
config : BeelzebubServiceConfiguration {
Commands : [ ] Command {
{ RegexStr : "" } ,
{ RegexStr : "" } ,
} ,
} ,
expectedError : false ,
} ,
{
name : "Invalid Regex" ,
config : BeelzebubServiceConfiguration {
Commands : [ ] Command {
{ RegexStr : "[" } ,
} ,
} ,
expectedError : true ,
} ,
{
name : "Mixed valid and Invalid Regex" ,
config : BeelzebubServiceConfiguration {
Commands : [ ] Command {
{ RegexStr : "^/api/v1/.*$" } ,
{ RegexStr : "[" } ,
{ RegexStr : "test" } ,
} ,
} ,
expectedError : true ,
} ,
{
name : "No commands" ,
config : BeelzebubServiceConfiguration { } ,
expectedError : false ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
err := tt . config . CompileCommandRegex ( )
if tt . expectedError {
assert . Error ( t , err )
} else {
assert . NoError ( t , err )
for _ , command := range tt . config . Commands {
if command . RegexStr != "" {
assert . NotNil ( t , command . Regex )
_ , err := regexp . Compile ( command . RegexStr )
assert . NoError ( t , err )
} else {
assert . Nil ( t , command . Regex )
}
}
}
} )
}
}