main_test.go (2363B)
1 package main 2 3 import ( 4 "bytes" 5 "net" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 11 "git.taler.net/taler-merchant-demos/internal/config" 12 ) 13 14 func TestBrowserURL(t *testing.T) { 15 tests := []struct { 16 addr net.Addr 17 want string 18 }{ 19 {&net.TCPAddr{IP: net.IPv6unspecified, Port: 8080}, "http://localhost:8080/"}, 20 {&net.TCPAddr{IP: net.IPv4zero, Port: 8081}, "http://localhost:8081/"}, 21 {&net.TCPAddr{IP: net.IPv6loopback, Port: 8082}, "http://[::1]:8082/"}, 22 {&net.UnixAddr{Name: "/run/taler/demo.sock", Net: "unix"}, "/run/taler/demo.sock"}, 23 } 24 for _, test := range tests { 25 if got := browserURL(test.addr); got != test.want { 26 t.Errorf("browserURL(%q) = %q, want %q", test.addr, got, test.want) 27 } 28 } 29 } 30 31 func TestConfigDumpCommand(t *testing.T) { 32 path := filepath.Join(t.TempDir(), "demo.conf") 33 if err := os.WriteFile(path, []byte("[taler]\ncurrency = KUDOS\n"), 0o600); err != nil { 34 t.Fatal(err) 35 } 36 t.Setenv("TALER_MERCHANT_DEMOS_BASE_CONFIG", t.TempDir()) 37 t.Setenv("TALER_MERCHANT_DEMOS_PREFIX", "") 38 39 for _, args := range [][]string{ 40 {"config", "dump", "-c", path}, 41 {"-c", path, "config", "dump"}, 42 } { 43 var stdout bytes.Buffer 44 var stderr bytes.Buffer 45 if err := runWithOutput(args, &stdout, &stderr); err != nil { 46 t.Fatalf("%v failed: %v; stderr: %s", args, err, stderr.String()) 47 } 48 for _, want := range []string{ 49 "# Configuration file load order:\n# " + path, 50 "# " + path + ":2\ncurrency = KUDOS", 51 } { 52 if !strings.Contains(stdout.String(), want) { 53 t.Errorf("%v output does not contain %q:\n%s", args, want, stdout.String()) 54 } 55 } 56 } 57 } 58 59 func TestReadPublicURLs(t *testing.T) { 60 path := filepath.Join(t.TempDir(), "demo.conf") 61 contents := `[frontend-demo] 62 landing_url = https://landing.example/ 63 bank_url = https://bank.example/ 64 blog_url = https://blog.example/ 65 donations_url = https://donations.example/ 66 ` 67 if err := os.WriteFile(path, []byte(contents), 0o600); err != nil { 68 t.Fatal(err) 69 } 70 t.Setenv("TALER_MERCHANT_DEMOS_BASE_CONFIG", t.TempDir()) 71 cfg, err := config.Load(path) 72 if err != nil { 73 t.Fatal(err) 74 } 75 got, err := readPublicURLs(cfg) 76 if err != nil { 77 t.Fatal(err) 78 } 79 if got.Landing != "https://landing.example/" || 80 got.Bank != "https://bank.example/" || 81 got.Blog != "https://blog.example/" || 82 got.Donations != "https://donations.example/" { 83 t.Fatalf("public URLs = %#v", got) 84 } 85 }