Fetch of authorization token
[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 }