-
Notifications
You must be signed in to change notification settings - Fork 3
/
context_test.go
124 lines (97 loc) · 3.33 KB
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"os"
"testing"
)
func TestLoadFromEnvironment(t *testing.T) {
c := &Context{}
os.Setenv("AUTH_INTERNALPORT", "1111")
os.Setenv("AUTH_EXTERNALPORT", "2222")
os.Setenv("AUTH_LOGLEVEL", "debug")
os.Setenv("AUTH_LOGCOLORS", "true")
os.Setenv("AUTH_MONGOURL", "server.example.com")
os.Setenv("AUTH_INTERNALCACERT", "/lockbox/internal-ca.pem")
os.Setenv("AUTH_INTERNALCERT", "/lockbox/internal-cert.pem")
os.Setenv("AUTH_INTERNALKEY", "/lockbox/internal-key.pem")
os.Setenv("AUTH_EXTERNALCERT", "/lockbox/external-cert.pem")
os.Setenv("AUTH_EXTERNALKEY", "/lockbox/external-key.pem")
if err := c.Load(); err != nil {
t.Fatalf("Error loading configuration: %v", err)
}
if c.InternalPort != 1111 {
t.Errorf("Unexpected internal port: [%d]", c.InternalPort)
}
if c.ExternalPort != 2222 {
t.Errorf("Unexpected external port: [%d]", c.ExternalPort)
}
if c.LogLevel != "debug" {
t.Errorf("Unexpected log level: [%s]", c.LogLevel)
}
if !c.LogColors {
t.Error("Expected log coloring to be enabled")
}
if c.MongoURL != "server.example.com" {
t.Errorf("Unexpected MongoDB URL: [%s]", c.MongoURL)
}
if c.InternalCACert != "/lockbox/internal-ca.pem" {
t.Errorf("Unexpected internal CA certificate path: [%s]", c.InternalCACert)
}
if c.InternalCert != "/lockbox/internal-cert.pem" {
t.Errorf("Unexpected internal certificate path: [%s]", c.InternalCert)
}
if c.InternalKey != "/lockbox/internal-key.pem" {
t.Errorf("Unexpected internal private key path: [%s]", c.InternalKey)
}
if c.ExternalCert != "/lockbox/external-cert.pem" {
t.Errorf("Unexpected external certificate path: [%s]", c.ExternalCert)
}
if c.ExternalKey != "/lockbox/external-key.pem" {
t.Errorf("Unexpected external private key path: [%s]", c.ExternalKey)
}
}
func TestDefaultValues(t *testing.T) {
c := &Context{}
os.Setenv("AUTH_INTERNALPORT", "")
os.Setenv("AUTH_EXTERNALPORT", "")
os.Setenv("AUTH_LOGLEVEL", "")
os.Setenv("AUTH_LOGCOLORS", "")
os.Setenv("AUTH_MONGOURL", "")
os.Setenv("AUTH_INTERNALCACERT", "")
os.Setenv("AUTH_INTERNALCERT", "")
os.Setenv("AUTH_INTERNALKEY", "")
os.Setenv("AUTH_EXTERNALCERT", "")
os.Setenv("AUTH_EXTERNALKEY", "")
if err := c.Load(); err != nil {
t.Fatalf("Error loading configuration: %v", err)
}
if c.InternalPort != 9001 {
t.Errorf("Unexpected internal port: [%d]", c.InternalPort)
}
if c.ExternalPort != 9000 {
t.Errorf("Unexpected external port: [%d]", c.ExternalPort)
}
if c.LogLevel != "info" {
t.Errorf("Unexpected log level: [%s]", c.LogLevel)
}
if c.LogColors {
t.Error("Expected log coloring to be disabled by default")
}
if c.MongoURL != "mongo" {
t.Errorf("Unexpected MongoDB URL: [%s]", c.MongoURL)
}
if c.InternalCACert != "/certificates/ca.pem" {
t.Errorf("Unexpected internal CA certificate path: [%s]", c.InternalCACert)
}
if c.InternalCert != "/certificates/auth-store-cert.pem" {
t.Errorf("Unexpected internal certificate path: [%s]", c.InternalCert)
}
if c.InternalKey != "/certificates/auth-store-key.pem" {
t.Errorf("Unexpected internal private key path: [%s]", c.InternalKey)
}
if c.ExternalCert != "/certificates/external-cert.pem" {
t.Errorf("Unexpected external certificate path: [%s]", c.ExternalCert)
}
if c.ExternalKey != "/certificates/external-key.pem" {
t.Errorf("Unexpected external private key: [%s]", c.ExternalKey)
}
}