Skip to content
Snippets Groups Projects
config_test.go 2.83 KiB
Newer Older
  • Learn to ignore specific revisions
  • OZGCloud's avatar
    OZGCloud committed
    /*
     * Copyright (C) 2023-2024
     * Das Land Schleswig-Holstein vertreten durch den
     * Ministerpräsidenten des Landes Schleswig-Holstein
     * Staatskanzlei
     * Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
     *
     * Lizenziert unter der EUPL, Version 1.2 oder - sobald
     * diese von der Europäischen Kommission genehmigt wurden -
     * Folgeversionen der EUPL ("Lizenz");
     * Sie dürfen dieses Werk ausschließlich gemäß
     * dieser Lizenz nutzen.
     * Eine Kopie der Lizenz finden Sie hier:
     *
     * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
     *
     * Sofern nicht durch anwendbare Rechtsvorschriften
     * gefordert oder in schriftlicher Form vereinbart, wird
     * die unter der Lizenz verbreitete Software "so wie sie
     * ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
     * ausdrücklich oder stillschweigend - verbreitet.
     * Die sprachspezifischen Genehmigungen und Beschränkungen
     * unter der Lizenz sind dem Lizenztext zu entnehmen.
     */
    
    package config
    
    import (
    	"os"
    	"testing"
    
    	"github.com/stretchr/testify/assert"
    )
    
    func TestLoadConfig(t *testing.T) {
    
    OZGCloud's avatar
    OZGCloud committed
    	t.Run("should load default config", func(t *testing.T) {
    
    OZGCloud's avatar
    OZGCloud committed
    		config := LoadConfig()
    
    		expectedConfig := Config{}
    		expectedConfig.Grpc.Mock = false
    
    OZGCloud's avatar
    OZGCloud committed
    		expectedConfig.Server.Port = 8082
    		expectedConfig.Logging.Level = "INFO"
    
    OZGCloud's avatar
    OZGCloud committed
    
    		assert.Equal(t, expectedConfig, config)
    	})
    
    
    OZGCloud's avatar
    OZGCloud committed
    	t.Run("should load local config", func(t *testing.T) {
    		assert.NoError(t, os.Setenv(activeProfileParam, "local"), "Setenv "+activeProfileParam+" should not return an error")
    
    OZGCloud's avatar
    OZGCloud committed
    		config := LoadConfig()
    
    OZGCloud's avatar
    OZGCloud committed
    		assert.NoError(t, os.Unsetenv(activeProfileParam), "Unsetenv "+activeProfileParam+" should not return an error")
    
    OZGCloud's avatar
    OZGCloud committed
    
    		expectedConfig := Config{}
    
    OZGCloud's avatar
    OZGCloud committed
    		expectedConfig.Server.Port = 8082
    		expectedConfig.Grpc.Url = "localhost:50051"
    		expectedConfig.Logging.Level = "DEBUG"
    
    OZGCloud's avatar
    OZGCloud committed
    
    		assert.Equal(t, expectedConfig, config)
    	})
    	t.Run("should overwrite config with env", func(t *testing.T) {
    
    OZGCloud's avatar
    OZGCloud committed
    		assert.NoError(t, os.Setenv("GRPC_URL", "zufi:9090"), "Setenv GRPC_URL should not return an error")
    
    OZGCloud's avatar
    OZGCloud committed
    
    		config := LoadConfig()
    
    
    OZGCloud's avatar
    OZGCloud committed
    		assert.NoError(t, os.Unsetenv("GRPC_URL"), "Unsetenv GRPC_URL should not return an error")
    
    OZGCloud's avatar
    OZGCloud committed
    
    		expectedConfig := Config{}
    		expectedConfig.Grpc.Mock = false
    
    OZGCloud's avatar
    OZGCloud committed
    		expectedConfig.Grpc.Url = "zufi:9090"
    		expectedConfig.Server.Port = 8082
    		expectedConfig.Logging.Level = "INFO"
    
    OZGCloud's avatar
    OZGCloud committed
    
    		assert.Equal(t, expectedConfig, config)
    	})
    }
    
    func TestValidateGrpcUrl(t *testing.T) {
    	testData := []struct {
    		grpcUrl        string
    		expectedResult bool
    	}{
    		{"localhost:56789", true},
    		{"test.example.com:20", true},
    		{"invalidUrl", false},
    		{"%test%:2345", false},
    		{"localhost:234567", false},
    		{"example.com:", false},
    		{":6789", false},
    		{"example..com:8080", false},
    		{"example.com:abc", false},
    	}
    
    	for _, data := range testData {
    		result := ValidateGrpcUrl(data.grpcUrl)
    
    		assert.Equal(t, data.expectedResult, result)
    	}
    }