~bigbes/sr-ht-dolt

ref: 6af58599373a90f23c8b8ace22f03fe72c5ecde0 sr-ht-dolt/cmd/doltsrht/main_test.go -rw-r--r-- 3.5 KiB
6af58599 — Eugene Blikh web: take the login redirect from chrome.LoginURLFor 9 days ago
                                                                                
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
125
126
127
128
129
130
131
132
133
134
package main

import (
	"strings"
	"testing"

	"github.com/vaughan0/go-ini"
)

// loadConf builds an ini.File directly from a literal, bypassing
// config.LoadConfig so the tests need no config.ini on disk and no
// internal-ipnet parsing.
func loadConf(t *testing.T, body string) ini.File {
	t.Helper()
	conf, err := ini.Load(strings.NewReader(body))
	if err != nil {
		t.Fatalf("ini.Load: %v", err)
	}
	return conf
}

func TestResolveSettingsDefaults(t *testing.T) {
	conf := loadConf(t, `[dolt.sr.ht]
origin=https://dolt.example.org
connection-string=postgres://u@localhost/d?sslmode=disable
`)

	got, err := resolveSettings(conf)
	if err != nil {
		t.Fatalf("resolveSettings: %v", err)
	}

	if got.connString != "postgres://u@localhost/d?sslmode=disable" {
		t.Errorf("connString = %q", got.connString)
	}
	if got.httpHost != "dolt.example.org" {
		t.Errorf("httpHost = %q, want dolt.example.org", got.httpHost)
	}
	if got.reposRoot != defaultReposRoot {
		t.Errorf("reposRoot = %q, want %q", got.reposRoot, defaultReposRoot)
	}
	if got.staticDir != defaultStaticDir {
		t.Errorf("staticDir = %q, want %q", got.staticDir, defaultStaticDir)
	}
	if got.remotesapiAddr != defaultRemotesapiAddr {
		t.Errorf("remotesapiAddr = %q, want %q", got.remotesapiAddr, defaultRemotesapiAddr)
	}
	if got.credsapiAddr != defaultCredsapiAddr {
		t.Errorf("credsapiAddr = %q, want %q", got.credsapiAddr, defaultCredsapiAddr)
	}
}

func TestResolveSettingsOverrides(t *testing.T) {
	conf := loadConf(t, `[dolt.sr.ht]
origin=https://dolt.example.org:8443
connection-string=postgres://u@localhost/d
repos=/srv/dolt
static-dir=/usr/share/sourcehut/dolt.sr.ht/static
remotesapi-listen=0.0.0.0:6306
credsapi-listen=0.0.0.0:6308
`)

	got, err := resolveSettings(conf)
	if err != nil {
		t.Fatalf("resolveSettings: %v", err)
	}

	if got.reposRoot != "/srv/dolt" {
		t.Errorf("reposRoot = %q", got.reposRoot)
	}
	if got.staticDir != "/usr/share/sourcehut/dolt.sr.ht/static" {
		t.Errorf("staticDir = %q", got.staticDir)
	}
	if got.remotesapiAddr != "0.0.0.0:6306" {
		t.Errorf("remotesapiAddr = %q", got.remotesapiAddr)
	}
	if got.credsapiAddr != "0.0.0.0:6308" {
		t.Errorf("credsapiAddr = %q", got.credsapiAddr)
	}
	// The port is part of the sealed-URL authority and is preserved.
	if got.httpHost != "dolt.example.org:8443" {
		t.Errorf("httpHost = %q, want dolt.example.org:8443", got.httpHost)
	}
}

func TestResolveSettingsMissingConnString(t *testing.T) {
	conf := loadConf(t, `[dolt.sr.ht]
origin=https://dolt.example.org
`)

	if _, err := resolveSettings(conf); err == nil {
		t.Fatal("expected error for missing connection-string, got nil")
	}
}

func TestResolveSettingsMissingOrigin(t *testing.T) {
	conf := loadConf(t, `[dolt.sr.ht]
connection-string=postgres://u@localhost/d
`)

	if _, err := resolveSettings(conf); err == nil {
		t.Fatal("expected error for missing origin, got nil")
	}
}

func TestHostFromOrigin(t *testing.T) {
	cases := []struct {
		origin  string
		want    string
		wantErr bool
	}{
		{"https://dolt.srht.bigb.es", "dolt.srht.bigb.es", false},
		{"https://dolt.srht.bigb.es:443", "dolt.srht.bigb.es:443", false},
		{"http://127.0.0.1:5307", "127.0.0.1:5307", false},
		{"", "", true},
		{"not-a-url-with-no-host", "", true},
	}
	for _, c := range cases {
		got, err := hostFromOrigin(c.origin)
		if c.wantErr {
			if err == nil {
				t.Errorf("hostFromOrigin(%q): expected error", c.origin)
			}
			continue
		}
		if err != nil {
			t.Errorf("hostFromOrigin(%q): %v", c.origin, err)
			continue
		}
		if got != c.want {
			t.Errorf("hostFromOrigin(%q) = %q, want %q", c.origin, got, c.want)
		}
	}
}