From abb0002c4cc8bd1f36b7050a978c95dffe9b06a0 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Tue, 2 Jul 2024 15:20:14 +0100 Subject: [PATCH] Update roles/home/aichat.nix --- roles/home/aichat.nix | 138 ++++++++++++------------------------------ 1 file changed, 39 insertions(+), 99 deletions(-) diff --git a/roles/home/aichat.nix b/roles/home/aichat.nix index d95cfd1..8af249c 100644 --- a/roles/home/aichat.nix +++ b/roles/home/aichat.nix @@ -148,117 +148,57 @@ roles = '' ### INPUT: - diff --git a/structs/routing_test.go b/structs/routing_test.go - index 2e19254..04191cc 100644 - --- a/structs/routing_test.go - +++ b/structs/routing_test.go - @@ -3,7 +3,7 @@ package structs_test - import ( - "testing" - - - "github.com/stretchr/testify/assert" - + "github.com/stretchr/testify/require" + diff --git a/pkg/khttp/client.go b/pkg/khttp/client.go + index a53064c..3aff938 100644 + --- a/pkg/khttp/client.go + +++ b/pkg/khttp/client.go + @@ -11,14 +11,17 @@ import ( "github.pie.apple.com/kerosene/Core/structs" - "gorm.io/datatypes" ) - @@ -15,18 +15,18 @@ func TestNewRoutingBuilder(t *testing.T) { - scheme := "http" - builder, err := structs.NewRoutingBuilder(name, proxy, port, scheme) - - assert.NoError(t, err) - + require.NoError(t, err) + +// TODO: https://github.pie.apple.com/Kerosene/Core/issues/43 + +// feat: Centralise and remove over use of os.Environment + const ( + - // Environment variables and file names. + authFilesDirEnvVar = "WHISPER_AUTH_FILES_DIR" + keyName = "decrypted_key.pem" + certName = "cert.pem" + ) - routing, err := builder.Build() - - assert.NoError(t, err) - - - - assert.Equal(t, name, routing.Name) - - assert.Equal(t, proxy, routing.Proxy) - - assert.Equal(t, port, routing.Port) - - assert.Equal(t, scheme, routing.Scheme) - - assert.Equal(t, datatypes.JSON(`[]`), routing.NameServers) - - assert.Nil(t, routing.Providers) - - assert.Nil(t, routing.Assets) - + require.NoError(t, err) + // Error for missing environment variable. + +// TODO: refactor: move errors into centralized errors.go files + +// https://github.pie.apple.com/Kerosene/Core/issues/57 + var errMissingEnvironmentVariable = fmt.Errorf("%s environment variable is not set", authFilesDirEnvVar) + + // AuthConfig holds authentication file paths. + @@ -31,9 +34,11 @@ type AuthConfig struct { + // NewAuthConfig creates an AuthConfig from environment variables. + func NewAuthConfig() (*AuthConfig, error) { + dir := os.Getenv(authFilesDirEnvVar) + - + require.Equal(t, name, routing.Name) - + require.Equal(t, proxy, routing.Proxy) - + require.Equal(t, port, routing.Port) - + require.Equal(t, scheme, routing.Scheme) - + require.Equal(t, datatypes.JSON(`[]`), routing.NameServers) - + require.Nil(t, routing.Providers) - + require.Nil(t, routing.Assets) - } - - func TestRoutingBuilderMethods(t *testing.T) { - @@ -39,7 +39,7 @@ func TestRoutingBuilderMethods(t *testing.T) { - assets := []structs.Asset{} - - builder, err := structs.NewRoutingBuilder(name, proxy, port, scheme) - - assert.NoError(t, err) - + require.NoError(t, err) - - routing, err := builder. - WithProviders(providers). - @@ -47,38 +47,24 @@ func TestRoutingBuilderMethods(t *testing.T) { - WithAssets(assets). - Build() - - - assert.NoError(t, err) - - assert.Equal(t, providers, routing.Providers) - - assert.Equal(t, nameServers, routing.NameServers) - - assert.Equal(t, assets, routing.Assets) - + require.NoError(t, err) - + require.Equal(t, providers, routing.Providers) - + require.Equal(t, nameServers, routing.NameServers) - + require.Equal(t, assets, routing.Assets) - } - - -// func TestRoutingBuilderValidation(t *testing.T) { - -// builder, err := structs.NewRoutingBuilder("exampleName", "exampleProxy", 8080, "http") - -// assert.NoError(t, err) - - - -// // Test successful build - -// routing, err := builder.Build() - -// assert.NoError(t, err) - -// assert.NotNil(t, routing) - - - -// // Test invalid build with missing required fields - -// builder.routing.Name = "" - -// routing, err = builder.Build() - -// assert.Error(t, err) - -// assert.Nil(t, routing) - -// } - - - func TestRoutingBuilderSanityChecks(t *testing.T) { - + t.Parallel() + if dir == "" { + return nil, errMissingEnvironmentVariable + } + - _, err := structs.NewRoutingBuilder("", "exampleProxy", 8080, "http") - - assert.Error(t, err) - - assert.Equal(t, structs.ErrInvalidName, err) - + require.Error(t, err) - + require.Equal(t, structs.ErrInvalidName, err) + return &AuthConfig{ + Dir: dir, + CertFile: filepath.Join(dir, certName), + @@ -211,7 +216,7 @@ func setupMTLSOnlyTransport(certData string, keyData string) (*http.Transport, e - _, err = structs.NewRoutingBuilder("exampleName", "", 8080, "http") - - assert.Error(t, err) - - assert.Equal(t, structs.ErrInvalidProxy, err) - + require.Error(t, err) - + require.Equal(t, structs.ErrInvalidProxy, err) - - _, err = structs.NewRoutingBuilder("exampleName", "exampleProxy", 8080, "") - - assert.Error(t, err) - - assert.Equal(t, structs.ErrInvalidScheme, err) - + require.Error(t, err) - + require.Equal(t, structs.ErrInvalidScheme, err) + // Make scheme and Auth Type separate and load from DB. + func parseProxyURL(scheme string, routing structs.Routing) (*url.URL, error) { + - return url.Parse(fmt.Sprintf("%s://%s:%s", scheme, routing.Proxy, routing.Port)) + + return url.Parse(fmt.Sprintf("%s://%s:%d", scheme, routing.Proxy, routing.Port)) } + + // loadX509KeyPair loads an X509 key pair from the specified cert and key files. ### OUTPUT: - refactor(structs_tests): update test assertion library to 'require' and refactor tests for better readability + fix/refactor(khttp/client): use correct format specifier for and add TODOs - - Replaced the 'assert' library with 'require' in routing_test.go - - Refined code formatting and added comments - - Removed commented out test functions - - Added `t.Parallel()` + - Parsed proxy URL using `fmt.Sprintf()` with correct format specifier for port + - Added TODOs to centralize errors and remove overuse of `os.Environment` (#43, #57) - name: createpr