2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2018-06-09 16:27:47 +08:00
|
|
|
"path/filepath"
|
2018-06-11 01:43:42 +08:00
|
|
|
"testing"
|
2018-05-27 22:40:42 +08:00
|
|
|
)
|
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
func TestConfigParseBasics(t *testing.T) {
|
2018-06-08 01:47:23 +08:00
|
|
|
os.Setenv("COMMENTO_ORIGIN", "https://commento.io")
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
if err := configParse(); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
t.Errorf("unexpected error when parsing config: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-10 05:19:19 +08:00
|
|
|
if os.Getenv("BIND_ADDRESS") != "127.0.0.1" {
|
2018-06-13 02:52:06 +08:00
|
|
|
t.Errorf("expected COMMENTO_BIND_ADDRESS=127.0.0.1, but COMMENTO_BIND_ADDRESS=%s instead", os.Getenv("BIND_ADDRESS"))
|
2018-06-10 05:19:19 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv("COMMENTO_BIND_ADDRESS", "192.168.1.100")
|
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
if err := configParse(); err != nil {
|
2018-06-10 05:19:19 +08:00
|
|
|
t.Errorf("unexpected error when parsing config: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("BIND_ADDRESS") != "192.168.1.100" {
|
2018-06-13 02:52:06 +08:00
|
|
|
t.Errorf("expected COMMENTO_BIND_ADDRESS=192.168.1.100, but COMMENTO_BIND_ADDRESS=%s instead", os.Getenv("BIND_ADDRESS"))
|
2018-06-10 05:19:19 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
// This test feels kinda stupid, but whatever.
|
|
|
|
if os.Getenv("PORT") != "8080" {
|
|
|
|
t.Errorf("expected PORT=8080, but PORT=%s instead", os.Getenv("PORT"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-08 01:47:23 +08:00
|
|
|
os.Setenv("COMMENTO_PORT", "1886")
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
if err := configParse(); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
t.Errorf("unexpected error when parsing config: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("PORT") != "1886" {
|
|
|
|
t.Errorf("expected PORT=1886, but PORT=%s instead", os.Getenv("PORT"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-06-09 16:27:47 +08:00
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
func TestConfigParseNoOrigin(t *testing.T) {
|
2018-06-09 16:33:06 +08:00
|
|
|
os.Setenv("COMMENTO_ORIGIN", "")
|
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
if err := configParse(); err == nil {
|
2018-06-09 16:33:06 +08:00
|
|
|
t.Errorf("expected error not found parsing config without ORIGIN")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
func TestConfigParseStatic(t *testing.T) {
|
2018-06-09 16:27:47 +08:00
|
|
|
os.Setenv("COMMENTO_ORIGIN", "https://commento.io")
|
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
if err := configParse(); err != nil {
|
2018-06-09 16:27:47 +08:00
|
|
|
t.Errorf("unexpected error when parsing config: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
binPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot load binary path: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("STATIC") != binPath {
|
|
|
|
t.Errorf("COMMENTO_STATIC != %s when unset", binPath)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv("COMMENTO_STATIC", "/usr/")
|
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
if err := configParse(); err != nil {
|
2018-06-09 16:27:47 +08:00
|
|
|
t.Errorf("unexpected error when parsing config: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("STATIC") != "/usr" {
|
|
|
|
t.Errorf("COMMENTO_STATIC != /usr when unset")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
func TestConfigParseStaticDNE(t *testing.T) {
|
2018-06-09 16:27:47 +08:00
|
|
|
os.Setenv("COMMENTO_ORIGIN", "https://commento.io")
|
|
|
|
os.Setenv("COMMENTO_STATIC", "/does/not/exist/surely/")
|
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
if err := configParse(); err == nil {
|
2018-06-09 16:27:47 +08:00
|
|
|
t.Errorf("expected error not found when a non-existant directory is used")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
func TestConfigParseStaticNotADirectory(t *testing.T) {
|
2018-06-09 16:27:47 +08:00
|
|
|
os.Setenv("COMMENTO_ORIGIN", "https://commento.io")
|
|
|
|
os.Setenv("COMMENTO_STATIC", os.Args[0])
|
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
if err := configParse(); err != errorNotADirectory {
|
2018-06-09 16:27:47 +08:00
|
|
|
t.Errorf("expected error not found when a file is used")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|