all repos — flounder @ 2db17c0358b3c441a1bf395e3fba9c8a4880e7d2

A small site builder for the Gemini protocol

Add banned usernames list, first unit test
alex wennerberg alex@alexwennerberg.com
Sun, 24 Jan 2021 21:31:27 -0800
commit

2db17c0358b3c441a1bf395e3fba9c8a4880e7d2

parent

b60b64a17d3a924e44fd4dafb511c1f5c4adc5d7

2 files changed, 24 insertions(+), 0 deletions(-)

jump to
A flounder_test.go

@@ -0,0 +1,16 @@

+package main + +import "testing" + +func TestIsOKUsername(t *testing.T) { + for _, u := range []string{"www", "proxy", "%", "", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"} { + if isOkUsername(u) == nil { + t.Errorf("Username " + u + " should be considered invalid, but wasn't") + } + } + for _, u := range []string{"-", "alex", "1"} { + if isOkUsername(u) != nil { + t.Errorf("Username " + u + " should be considered valid, but wasn't") + } + } +}
M http.gohttp.go

@@ -434,6 +434,8 @@ }

const ok = "-0123456789abcdefghijklmnopqrstuvwxyz" +var bannedUsernames = []string{"www", "proxy", "grafana"} + func isOkUsername(s string) error { if len(s) < 1 { return fmt.Errorf("Username is too short")

@@ -446,8 +448,14 @@ if !strings.Contains(ok, strings.ToLower(string(char))) {

return fmt.Errorf("Username contains invalid characters. Valid characters include lowercase letters, numbers, and hyphens.") } } + for _, username := range bannedUsernames { + if username == s { + return fmt.Errorf("Username is not allowed.") + } + } return nil } + func registerHandler(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { data := struct {