Skip to content
Snippets Groups Projects
Select Git revision
  • 6202c27a772430bc8bb3c5c4361c1b1efaa1231d
  • master default protected
  • add-frequency-to-form
  • dev protected
  • ckan-2.11.0
  • add-package-custom-fields
  • fix-adding-datasets-for-users-and-editors
  • add-auth-subroute
  • 71-migrate-custom-fields-to-ckanext-scheming
  • add-author-maintainer-information
  • fix-inline-flex-btns
  • fix-known-spatial-uri-validation
  • py3
  • 47-aktuelle-resource-einer-collection-wird-nicht-mehr-gefunden
  • 10-eingabe-der-dct-accrualperiodicity-in-weboberflache
  • v1.3
  • 2.5.3
  • 2.5.2
  • 2.5.1
  • 2.5.0
  • 2.4.7
  • 2.4.6
  • 2.4.5
  • 2.4.4
  • 2.4.3
  • 2.4.2
  • 2.4.1
  • 2.4.0
  • 2.3.1
  • 2.3.0
  • 2.2.0
  • 2.1.0
  • 2.0.0
  • 1.4.3
  • 1.4.2
  • 1.4.1
36 results

plugin.py

Blame
  • config_test.go 3.82 KiB
    /*
     * 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) {
    	t.Run("should load config from file", func(t *testing.T) {
    		config := LoadConfig()
    
    		expectedConfig := Config{}
    		expectedConfig.Http.Server.Port = 8080
    		expectedConfig.Grpc.Collaboration.Server.Port = 50052
    		expectedConfig.Grpc.Collaboration.Router.Port = 50051
    		expectedConfig.Grpc.Registration.Server.Url = "localhost:50052"
    		expectedConfig.Grpc.Mock = false
    		expectedConfig.Logging.Level = "DEBUG"
    
    		assert.Equal(t, expectedConfig, config)
    	})
    
    	t.Run("should load config from env", func(t *testing.T) {
    		envVars := map[string]string{
    			"HTTP_SERVER_PORT":               "9090",
    			"GRPC_COLLABORATION_SERVER_PORT": "50052",
    			"GRPC_COLLABORATION_ROUTER_PORT": "50051",
    			"GRPC_REGISTRATION_SERVER_URL":   "localhost:99999",
    			"LOGGING_LEVEL":                  "ERROR",
    		}
    
    		for key, value := range envVars {
    			assert.NoError(t, os.Setenv(key, value), "Setenv "+key+" should not return an error")
    		}
    
    		config := LoadConfig("testdata/empty_test_config.yml")
    
    		for key := range envVars {
    			assert.NoError(t, os.Unsetenv(key), "Unsetenv "+key+" should not return an error")
    		}
    
    		expectedConfig := Config{}
    		expectedConfig.Http.Server.Port = 9090
    		expectedConfig.Grpc.Collaboration.Server.Port = 50052
    		expectedConfig.Grpc.Collaboration.Router.Port = 50051
    		expectedConfig.Grpc.Registration.Server.Url = "localhost:99999"
    		expectedConfig.Grpc.Mock = false
    		expectedConfig.Logging.Level = "ERROR"
    
    		assert.Equal(t, expectedConfig, config)
    	})
    
    	t.Run("should overwrite config with env", func(t *testing.T) {
    		envVars := map[string]string{
    			"HTTP_SERVER_PORT":             "9090",
    			"GRPC_REGISTRATION_SERVER_URL": "localhost:99999",
    		}
    
    		for key, value := range envVars {
    			assert.NoError(t, os.Setenv(key, value), "Setenv "+key+" should not return an error")
    		}
    
    		config := LoadConfig()
    
    		for key := range envVars {
    			assert.NoError(t, os.Unsetenv(key), "Unsetenv "+key+" should not return an error")
    		}
    
    		expectedConfig := Config{}
    		expectedConfig.Http.Server.Port = 9090
    		expectedConfig.Grpc.Collaboration.Server.Port = 50052
    		expectedConfig.Grpc.Collaboration.Router.Port = 50051
    		expectedConfig.Grpc.Registration.Server.Url = "localhost:99999"
    		expectedConfig.Grpc.Mock = false
    		expectedConfig.Logging.Level = "DEBUG"
    
    		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)
    	}
    }