taldir

Directory service to resolve wallet mailboxes by messenger addresses
Log | Files | Refs | Submodules | README | LICENSE

commit 9fccf6b0bdb508b5de1e8b0718ddfa6452db761e
parent e256cbcc73bad947729f7527bdf4bced3af376d1
Author: Martin Schanzenbach <schanzen@gnunet.org>
Date:   Fri, 25 Apr 2025 23:17:53 +0200

fmt

Diffstat:
Mcmd/taldir-server/main_test.go | 438++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mpkg/rest/taldir.go | 4++--
2 files changed, 221 insertions(+), 221 deletions(-)

diff --git a/cmd/taldir-server/main_test.go b/cmd/taldir-server/main_test.go @@ -21,6 +21,7 @@ package main_test import ( "bytes" "crypto/sha512" + "fmt" "io" "log" "net/http" @@ -28,14 +29,13 @@ import ( "os" "strings" "testing" - "fmt" gnunetutil "gnunet/util" + "github.com/schanzen/taler-go/pkg/merchant" "gopkg.in/ini.v1" "gorm.io/driver/sqlite" _ "taler.net/taldir/cmd/taldir-server" - "github.com/schanzen/taler-go/pkg/merchant" "taler.net/taldir/internal/util" taldir "taler.net/taldir/pkg/rest" ) @@ -106,278 +106,278 @@ func TestMain(m *testing.M) { os.Exit(1) } db := sqlite.Open("file::memory:?cache=shared") - merchServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/config" { - w.WriteHeader(http.StatusOK) - w.Write([]byte(merchantConfigResponse)) - return - } - if !strings.HasPrefix(r.URL.Path, "/private/orders") { - log.Printf("Expected to request '/private/orders', got: %s\n", r.URL.Path) - return - } - if r.Method == http.MethodPost { - jsonResp := fmt.Sprintf("{\"order_id\":\"%s\"}", "uniqueOrderId") - w.WriteHeader(http.StatusOK) - w.Write([]byte(jsonResp)) - } else { - if r.Header.Get("PaidIndicator") == "yes" { - jsonResp := "{\"order_status\":\"paid\"}" - w.WriteHeader(http.StatusOK) - w.Write([]byte(jsonResp)) - } else { - jsonResp := "{\"order_status\":\"unpaid\", \"taler_pay_uri\": \"somepaytouri\"}" - w.WriteHeader(http.StatusOK) - w.Write([]byte(jsonResp)) - } - } - })) - defer merchServer.Close() - merch := merchant.NewMerchant(merchServer.URL, "supersecret") - t.Initialize(taldir.TaldirConfig{ - Ini: cfg, - Version: "testing", - Datahome: "./testdata", - Db: db, - Merchant: merch, - }) - log.Println("hello") - code := m.Run() - t.ClearDatabase() - os.Exit(code) + merchServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/config" { + w.WriteHeader(http.StatusOK) + w.Write([]byte(merchantConfigResponse)) + return + } + if !strings.HasPrefix(r.URL.Path, "/private/orders") { + log.Printf("Expected to request '/private/orders', got: %s\n", r.URL.Path) + return + } + if r.Method == http.MethodPost { + jsonResp := fmt.Sprintf("{\"order_id\":\"%s\"}", "uniqueOrderId") + w.WriteHeader(http.StatusOK) + w.Write([]byte(jsonResp)) + } else { + if r.Header.Get("PaidIndicator") == "yes" { + jsonResp := "{\"order_status\":\"paid\"}" + w.WriteHeader(http.StatusOK) + w.Write([]byte(jsonResp)) + } else { + jsonResp := "{\"order_status\":\"unpaid\", \"taler_pay_uri\": \"somepaytouri\"}" + w.WriteHeader(http.StatusOK) + w.Write([]byte(jsonResp)) + } + } + })) + defer merchServer.Close() + merch := merchant.NewMerchant(merchServer.URL, "supersecret") + t.Initialize(taldir.TaldirConfig{ + Ini: cfg, + Version: "testing", + Datahome: "./testdata", + Db: db, + Merchant: merch, + }) + log.Println("hello") + code := m.Run() + t.ClearDatabase() + os.Exit(code) } func getHAddress(addr string) string { - h := sha512.New() - h.Write([]byte(addr)) - return gnunetutil.EncodeBinaryToString(h.Sum(nil)) + h := sha512.New() + h.Write([]byte(addr)) + return gnunetutil.EncodeBinaryToString(h.Sum(nil)) } func TestNoEntry(s *testing.T) { - t.ClearDatabase() + t.ClearDatabase() - h_addr := getHAddress("jdoe@example.com") - req, _ := http.NewRequest("GET", "/"+h_addr, nil) - response := executeRequest(req) + h_addr := getHAddress("jdoe@example.com") + req, _ := http.NewRequest("GET", "/"+h_addr, nil) + response := executeRequest(req) - if http.StatusNotFound != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusNotFound, response.Code) - } + if http.StatusNotFound != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusNotFound, response.Code) + } } func executeRequest(req *http.Request) *httptest.ResponseRecorder { - rr := httptest.NewRecorder() - t.Router.ServeHTTP(rr, req) - return rr + rr := httptest.NewRecorder() + t.Router.ServeHTTP(rr, req) + return rr } func TestRegisterRequest(s *testing.T) { - t.ClearDatabase() + t.ClearDatabase() - req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) - response := executeRequest(req) + req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) + response := executeRequest(req) - if http.StatusAccepted != response.Code { - s.Errorf("Expected response code %d. Got %d, with %s\n", http.StatusAccepted, response.Code, response.Body) - } - file, err := os.Open("validation_code") - if err != nil { - s.Errorf("No validation code file found!\n") - } - code, err := io.ReadAll(file) - if err != nil { - s.Errorf("Error reading validation code file contents!\n") - } - h_addr := getHAddress("abc@test") - trimCode := strings.Trim(string(code), " \r\n") - solution := util.GenerateSolution("myinbox@xyz", trimCode) - solutionJSON := "{\"solution\": \"" + solution + "\"}" - req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) - response = executeRequest(req) - if http.StatusNoContent != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusNoContent, response.Code) - } + if http.StatusAccepted != response.Code { + s.Errorf("Expected response code %d. Got %d, with %s\n", http.StatusAccepted, response.Code, response.Body) + } + file, err := os.Open("validation_code") + if err != nil { + s.Errorf("No validation code file found!\n") + } + code, err := io.ReadAll(file) + if err != nil { + s.Errorf("Error reading validation code file contents!\n") + } + h_addr := getHAddress("abc@test") + trimCode := strings.Trim(string(code), " \r\n") + solution := util.GenerateSolution("myinbox@xyz", trimCode) + solutionJSON := "{\"solution\": \"" + solution + "\"}" + req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) + response = executeRequest(req) + if http.StatusNoContent != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusNoContent, response.Code) + } } func TestRegisterQRPageRequest(s *testing.T) { - t.ClearDatabase() + t.ClearDatabase() - req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) - response := executeRequest(req) + req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) + response := executeRequest(req) - if http.StatusAccepted != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) - } - req, _ = http.NewRequest("GET", "/register/NonSenseAddr/NonSenseCode", nil) - response = executeRequest(req) - if http.StatusNotFound != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusNotFound, response.Code) - } + if http.StatusAccepted != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) + } + req, _ = http.NewRequest("GET", "/register/NonSenseAddr/NonSenseCode", nil) + response = executeRequest(req) + if http.StatusNotFound != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusNotFound, response.Code) + } - file, err := os.Open("validation_code") - if err != nil { - s.Errorf("No validation code file found!\n") - } - code, err := io.ReadAll(file) - if err != nil { - s.Errorf("Error reading validation code file contents!\n") - } - h_addr := getHAddress("abc@test") - trimCode := strings.Trim(string(code), " \r\n") - req, _ = http.NewRequest("GET", "/register/"+h_addr+"/"+trimCode+"?address="+"abc@test", nil) - response = executeRequest(req) - if http.StatusOK != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusOK, response.Code) - } + file, err := os.Open("validation_code") + if err != nil { + s.Errorf("No validation code file found!\n") + } + code, err := io.ReadAll(file) + if err != nil { + s.Errorf("Error reading validation code file contents!\n") + } + h_addr := getHAddress("abc@test") + trimCode := strings.Trim(string(code), " \r\n") + req, _ = http.NewRequest("GET", "/register/"+h_addr+"/"+trimCode+"?address="+"abc@test", nil) + response = executeRequest(req) + if http.StatusOK != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusOK, response.Code) + } } func TestReRegisterRequest(s *testing.T) { - t.ClearDatabase() + t.ClearDatabase() - req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) - response := executeRequest(req) + req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) + response := executeRequest(req) - if http.StatusAccepted != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) - } - file, err := os.Open("validation_code") - if err != nil { - s.Errorf("No validation code file found!\n") - } - code, err := io.ReadAll(file) - if err != nil { - s.Errorf("Error reading validation code file contents!\n") - } - h_addr := getHAddress("abc@test") - trimCode := strings.Trim(string(code), " \r\n") - solution := util.GenerateSolution("myinbox@xyz", trimCode) - solutionJSON := "{\"solution\": \"" + solution + "\"}" - req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) - response = executeRequest(req) - if http.StatusNoContent != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusNoContent, response.Code) - } - req, _ = http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequestUnmodified)) - response = executeRequest(req) + if http.StatusAccepted != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) + } + file, err := os.Open("validation_code") + if err != nil { + s.Errorf("No validation code file found!\n") + } + code, err := io.ReadAll(file) + if err != nil { + s.Errorf("Error reading validation code file contents!\n") + } + h_addr := getHAddress("abc@test") + trimCode := strings.Trim(string(code), " \r\n") + solution := util.GenerateSolution("myinbox@xyz", trimCode) + solutionJSON := "{\"solution\": \"" + solution + "\"}" + req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) + response = executeRequest(req) + if http.StatusNoContent != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusNoContent, response.Code) + } + req, _ = http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequestUnmodified)) + response = executeRequest(req) - if http.StatusOK != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusOK, response.Code) - } + if http.StatusOK != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusOK, response.Code) + } } func TestReRegisterRequestTooMany(s *testing.T) { - t.ClearDatabase() + t.ClearDatabase() - req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) - response := executeRequest(req) + req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) + response := executeRequest(req) - if http.StatusAccepted != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) - } - req, _ = http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) - response = executeRequest(req) + if http.StatusAccepted != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) + } + req, _ = http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) + response = executeRequest(req) - if http.StatusAccepted != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) - } - req, _ = http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) - response = executeRequest(req) + if http.StatusAccepted != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) + } + req, _ = http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) + response = executeRequest(req) - if http.StatusAccepted != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) - } - req, _ = http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) - response = executeRequest(req) + if http.StatusAccepted != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) + } + req, _ = http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) + response = executeRequest(req) - if http.StatusTooManyRequests != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusTooManyRequests, response.Code) - } + if http.StatusTooManyRequests != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusTooManyRequests, response.Code) + } } func TestSolutionRequestTooMany(s *testing.T) { - t.ClearDatabase() + t.ClearDatabase() - req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) - response := executeRequest(req) + req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) + response := executeRequest(req) - if http.StatusAccepted != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) - } - h_addr := getHAddress("abc@test") - solution := util.GenerateSolution("myinbox@xyz", "wrongSolution") - solutionJSON := "{\"solution\": \"" + solution + "\"}" - req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) - response = executeRequest(req) - if http.StatusForbidden != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusForbidden, response.Code) - } - req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) - response = executeRequest(req) - if http.StatusForbidden != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusForbidden, response.Code) - } - req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) - response = executeRequest(req) - if http.StatusForbidden != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusForbidden, response.Code) - } - req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) - response = executeRequest(req) - if http.StatusTooManyRequests != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusTooManyRequests, response.Code) - } + if http.StatusAccepted != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) + } + h_addr := getHAddress("abc@test") + solution := util.GenerateSolution("myinbox@xyz", "wrongSolution") + solutionJSON := "{\"solution\": \"" + solution + "\"}" + req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) + response = executeRequest(req) + if http.StatusForbidden != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusForbidden, response.Code) + } + req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) + response = executeRequest(req) + if http.StatusForbidden != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusForbidden, response.Code) + } + req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) + response = executeRequest(req) + if http.StatusForbidden != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusForbidden, response.Code) + } + req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) + response = executeRequest(req) + if http.StatusTooManyRequests != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusTooManyRequests, response.Code) + } } func TestRegisterRequestWrongTargetUri(s *testing.T) { - t.ClearDatabase() + t.ClearDatabase() - req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) - response := executeRequest(req) + req, _ := http.NewRequest("POST", "/register/test", bytes.NewBuffer(validRegisterRequest)) + response := executeRequest(req) - if http.StatusAccepted != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) - } - file, err := os.Open("validation_code") - if err != nil { - s.Errorf("No validation code file found!\n") - } - code, err := io.ReadAll(file) - if err != nil { - s.Errorf("Error reading validation code file contents!\n") - } - h_addr := getHAddress("abc@test") - trimCode := strings.Trim(string(code), " \r\n") - solution := util.GenerateSolution("myinox@xyz", trimCode) - solutionJSON := "{\"solution\": \"" + solution + "\"}" - req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) - response = executeRequest(req) - if http.StatusForbidden != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusForbidden, response.Code) - } + if http.StatusAccepted != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusAccepted, response.Code) + } + file, err := os.Open("validation_code") + if err != nil { + s.Errorf("No validation code file found!\n") + } + code, err := io.ReadAll(file) + if err != nil { + s.Errorf("Error reading validation code file contents!\n") + } + h_addr := getHAddress("abc@test") + trimCode := strings.Trim(string(code), " \r\n") + solution := util.GenerateSolution("myinox@xyz", trimCode) + solutionJSON := "{\"solution\": \"" + solution + "\"}" + req, _ = http.NewRequest("POST", "/"+h_addr, bytes.NewBuffer([]byte(solutionJSON))) + response = executeRequest(req) + if http.StatusForbidden != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusForbidden, response.Code) + } } func TestUnsupportedMethod(s *testing.T) { - t.ClearDatabase() + t.ClearDatabase() - req, _ := http.NewRequest("POST", "/register/email", bytes.NewBuffer(validRegisterRequest)) - response := executeRequest(req) + req, _ := http.NewRequest("POST", "/register/email", bytes.NewBuffer(validRegisterRequest)) + response := executeRequest(req) - if http.StatusNotFound != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusNotFound, response.Code) - } + if http.StatusNotFound != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusNotFound, response.Code) + } } func TestPaymentRequiredMethod(s *testing.T) { - t.ClearDatabase() - t.MonthlyFee = "KUDOS:5" - req, _ := http.NewRequest("POST", "/register/test-cost", bytes.NewBuffer(validRegisterRequest)) - - response := executeRequest(req) - t.MonthlyFee = "KUDOS:0" - if http.StatusPaymentRequired != response.Code { - s.Errorf("Expected response code %d. Got %d\n", http.StatusPaymentRequired, response.Code) - } + t.ClearDatabase() + t.MonthlyFee = "KUDOS:5" + req, _ := http.NewRequest("POST", "/register/test-cost", bytes.NewBuffer(validRegisterRequest)) + + response := executeRequest(req) + t.MonthlyFee = "KUDOS:0" + if http.StatusPaymentRequired != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusPaymentRequired, response.Code) + } } diff --git a/pkg/rest/taldir.go b/pkg/rest/taldir.go @@ -930,7 +930,7 @@ func (t *Taldir) getFileName(relativeFileName string, datahome string) string { if errors.Is(err, os.ErrNotExist) { _, err := os.Stat(datahome + "/" + relativeFileName) if errors.Is(err, os.ErrNotExist) { - log.Printf("Tried fallback not found %s\n",datahome + "/" + relativeFileName) + log.Printf("Tried fallback not found %s\n", datahome+"/"+relativeFileName) return "" } return datahome + "/" + relativeFileName @@ -1072,7 +1072,7 @@ func (t *Taldir) Initialize(cfg TaldirConfig) { t.Host = cfg.Ini.Section("taldir").Key("base_url").MustString("http://localhost") t.Merchant = cfg.Merchant registrationCost, _ := talerutil.ParseAmount(t.MonthlyFee) - merchConfig, err := t.Merchant.GetConfig() + merchConfig, err := t.Merchant.GetConfig() if err != nil { log.Fatal(err) os.Exit(1)