From 9f41697eb2e0f6d88a5a784f6c433a6307b1c5e6 Mon Sep 17 00:00:00 2001 From: Marc Ole Bulling Date: Mon, 24 Jan 2022 13:18:45 +0100 Subject: [PATCH] Refactored config to json --- internal/configuration/Configuration.go | 7 +---- internal/models/Configuration.go | 23 ++++++++++++++ internal/models/Configuration_test.go | 42 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 internal/models/Configuration_test.go diff --git a/internal/configuration/Configuration.go b/internal/configuration/Configuration.go index cebe43f..62db277 100644 --- a/internal/configuration/Configuration.go +++ b/internal/configuration/Configuration.go @@ -75,12 +75,7 @@ func save() { } defer file.Close() - configJson, err := json.MarshalIndent(serverSettings, "", " ") - if err != nil { - fmt.Println("Error encoding configuration:", err) - os.Exit(1) - } - _, err = io.Copy(file, bytes.NewReader(configJson)) + _, err = io.Copy(file, bytes.NewReader(serverSettings.ToJson())) if err != nil { fmt.Println("Error writing configuration:", err) os.Exit(1) diff --git a/internal/models/Configuration.go b/internal/models/Configuration.go index d6f747a..218eb0e 100644 --- a/internal/models/Configuration.go +++ b/internal/models/Configuration.go @@ -1,5 +1,10 @@ package models +import ( + "encoding/json" + "log" +) + // Configuration is a struct that contains the global configuration type Configuration struct { Authentication AuthenticationConfig `json:"Authentication"` @@ -13,3 +18,21 @@ type Configuration struct { UseSsl bool `json:"UseSsl"` MaxFileSizeMB int `json:"MaxFileSizeMB"` } + +// ToJson returns an idented JSon representation +func (c Configuration) ToJson() []byte { + result, err := json.MarshalIndent(c, "", " ") + if err != nil { + log.Fatal("Error encoding configuration:", err) + } + return result +} + +// ToString returns the object as an unidented Json string used for test units +func (c Configuration) ToString() string { + result, err := json.Marshal(c) + if err != nil { + log.Fatal(err) + } + return string(result) +} diff --git a/internal/models/Configuration_test.go b/internal/models/Configuration_test.go new file mode 100644 index 0000000..0f352fb --- /dev/null +++ b/internal/models/Configuration_test.go @@ -0,0 +1,42 @@ +package models + +import ( + "Gokapi/internal/test" + "strings" + "testing" +) + +var testConfig = Configuration{ + Authentication: AuthenticationConfig{ + Method: 0, + SaltAdmin: "saltadmin", + SaltFiles: "saltfiles", + Username: "admin", + Password: "adminpwhashed", + HeaderKey: "", + OauthProvider: "", + OAuthClientId: "", + OAuthClientSecret: "", + HeaderUsers: nil, + OauthUsers: nil, + }, + Port: ":12345", + ServerUrl: "https://testserver.com/", + RedirectUrl: "https://test.com", + ConfigVersion: 11, + LengthId: 5, + DataDir: "test", + MaxMemory: 50, + UseSsl: true, + MaxFileSizeMB: 20, +} + +func TestConfiguration_ToJson(t *testing.T) { + test.IsEqualBool(t, strings.Contains(string(testConfig.ToJson()), "\"SaltAdmin\": \"saltadmin\""), true) +} + +func TestConfiguration_ToString(t *testing.T) { + test.IsEqualString(t, testConfig.ToString(), exptectedUnidentedOutput) +} + +const exptectedUnidentedOutput = "{\"Authentication\":{\"Method\":0,\"SaltAdmin\":\"saltadmin\",\"SaltFiles\":\"saltfiles\",\"Username\":\"admin\",\"Password\":\"adminpwhashed\",\"HeaderKey\":\"\",\"OauthProvider\":\"\",\"OAuthClientId\":\"\",\"OAuthClientSecret\":\"\",\"HeaderUsers\":null,\"OauthUsers\":null},\"Port\":\":12345\",\"ServerUrl\":\"https://testserver.com/\",\"RedirectUrl\":\"https://test.com\",\"ConfigVersion\":11,\"LengthId\":5,\"DataDir\":\"test\",\"MaxMemory\":50,\"UseSsl\":true,\"MaxFileSizeMB\":20}"