CI: Migrate Sonar Scan job to GHA
[nonrtric.git] / auth-token-fetch / config_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package main
22
23 import (
24         "os"
25         "testing"
26
27         log "github.com/sirupsen/logrus"
28         "github.com/stretchr/testify/require"
29 )
30
31 func TestNew_envVarsSetConfigContainSetValues(t *testing.T) {
32         assertions := require.New(t)
33         os.Setenv("LOG_LEVEL", "Debug")
34         os.Setenv("CERT_PATH", "CERT_PATH")
35         os.Setenv("CERT_KEY_PATH", "CERT_KEY_PATH")
36         os.Setenv("CREDS_GRANT_TYPE", "CREDS_GRANT_TYPE")
37         os.Setenv("CREDS_CLIENT_SECRET", "CREDS_CLIENT_SECRET")
38         os.Setenv("CREDS_CLIENT_ID", "CREDS_CLIENT_ID")
39         os.Setenv("OUTPUT_FILE", "OUTPUT_FILE")
40         os.Setenv("AUTH_SERVICE_URL", "AUTH_SERVICE_URL")
41         os.Setenv("REFRESH_MARGIN_SECONDS", "33")
42         os.Setenv("ROOT_CA_CERTS_PATH", "ROOT_CA_CERTS_PATH")
43
44         t.Cleanup(func() {
45                 os.Clearenv()
46         })
47         wantConfig := Config{
48                 LogLevel:                log.DebugLevel,
49                 CertPath:                "CERT_PATH",
50                 KeyPath:                 "CERT_KEY_PATH",
51                 AuthServiceUrl:          "AUTH_SERVICE_URL",
52                 GrantType:               "CREDS_GRANT_TYPE",
53                 ClientSecret:            "CREDS_CLIENT_SECRET",
54                 ClientId:                "CREDS_CLIENT_ID",
55                 AuthTokenOutputFileName: "OUTPUT_FILE",
56                 CACertsPath:             "ROOT_CA_CERTS_PATH",
57                 RefreshMarginSeconds:    33,
58         }
59         got := NewConfig()
60         assertions.Equal(nil, validateConfiguration(got))
61
62         assertions.Equal(&wantConfig, got)
63 }
64
65 func TestNew_defaultValues(t *testing.T) {
66         assertions := require.New(t)
67
68         wantConfig := Config{
69                 LogLevel:                log.InfoLevel,
70                 CertPath:                "security/tls.crt",
71                 KeyPath:                 "security/tls.key",
72                 AuthServiceUrl:          "https://localhost:39687/example-singlelogin-sever/login",
73                 GrantType:               "",
74                 ClientSecret:            "",
75                 ClientId:                "",
76                 AuthTokenOutputFileName: "/tmp/authToken.txt",
77                 CACertsPath:             "",
78                 RefreshMarginSeconds:    5,
79         }
80         got := NewConfig()
81         assertions.Equal(nil, validateConfiguration(got))
82
83         assertions.Equal(&wantConfig, got)
84 }
85
86 func TestNew_invalidValues(t *testing.T) {
87         assertions := require.New(t)
88
89         os.Setenv("LOG_LEVEL", "Junk")
90         os.Setenv("REFRESH_MARGIN_SECONDS", "Junk")
91         t.Cleanup(func() {
92                 os.Clearenv()
93         })
94
95         got := NewConfig()
96         assertions.Equal(log.InfoLevel, got.LogLevel)
97         assertions.Equal(5, got.RefreshMarginSeconds)
98 }