From 818be80e8bb233043c5e6002983cd8d75b106e53 Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Fri, 13 Dec 2024 07:57:54 +0500 Subject: [PATCH 1/2] feat: add payment fields to connection codes --- db/structs.go | 3 +++ handlers/auth.go | 19 +++++++++++++++---- tribes.sql | 5 ++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/db/structs.go b/db/structs.go index bad3aa492..3b73425c2 100644 --- a/db/structs.go +++ b/db/structs.go @@ -226,6 +226,9 @@ type ConnectionCodes struct { ConnectionString string `json:"connection_string"` IsUsed bool `json:"is_used"` DateCreated *time.Time `json:"date_created"` + Pubkey string `json:"pubkey"` + RouteHint string `json:"route_hint"` + SatsAmount int64 `json:"sats_amount"` } type ConnectionCodesShort struct { diff --git a/handlers/auth.go b/handlers/auth.go index 21bb3af4d..c9238ae4d 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -64,26 +64,34 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque body, err := io.ReadAll(r.Body) if err != nil { fmt.Println("ReadAll Error", err) + w.WriteHeader(http.StatusBadRequest) + return } r.Body.Close() err = json.Unmarshal(body, &codeBody) if err != nil { - fmt.Println("Could not umarshal connection code body") + fmt.Println("Could not unmarshal connection code body") w.WriteHeader(http.StatusNotAcceptable) return } if codeBody.Pubkey != "" && codeBody.RouteHint == "" { - fmt.Println("route hint missing") - w.WriteHeader(http.StatusNotAcceptable) + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode("Route hint is required when pubkey is provided") return } if codeBody.RouteHint != "" && codeBody.Pubkey == "" { - fmt.Println("pubkey missing missing") w.WriteHeader(http.StatusNotAcceptable) + json.NewEncoder(w).Encode("pubkey is required when Route hint is provided") + return + } + + if codeBody.SatsAmount == 0 { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode("Sats amount must be greater than 0") return } @@ -100,6 +108,9 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque newCode := db.ConnectionCodes{ ConnectionString: code, IsUsed: false, + Pubkey: codeBody.Pubkey, + RouteHint: codeBody.RouteHint, + SatsAmount: int64(codeBody.SatsAmount), } codeArr = append(codeArr, newCode) } diff --git a/tribes.sql b/tribes.sql index ffe65a710..83bbdcb11 100644 --- a/tribes.sql +++ b/tribes.sql @@ -141,5 +141,8 @@ CREATE TABLE connectioncodes { id SERIAL PRIMARY KEY, connection_string TEXT, is_used boolean, - date_created timestamptz + date_created timestamptz, + pubkey TEXT, + route_hint TEXT, + sats_amount bigint } \ No newline at end of file From 7defa120e7476e7ce7a96a060acadffce3692d4a Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Fri, 13 Dec 2024 15:37:17 +0500 Subject: [PATCH 2/2] fix: fields to connection codes --- db/structs.go | 3 -- handlers/auth.go | 9 ------ handlers/auth_test.go | 69 ++++++++++++++++++++++++++++++++++++++++--- tribes.sql | 5 +--- 4 files changed, 66 insertions(+), 20 deletions(-) diff --git a/db/structs.go b/db/structs.go index 3b73425c2..bad3aa492 100644 --- a/db/structs.go +++ b/db/structs.go @@ -226,9 +226,6 @@ type ConnectionCodes struct { ConnectionString string `json:"connection_string"` IsUsed bool `json:"is_used"` DateCreated *time.Time `json:"date_created"` - Pubkey string `json:"pubkey"` - RouteHint string `json:"route_hint"` - SatsAmount int64 `json:"sats_amount"` } type ConnectionCodesShort struct { diff --git a/handlers/auth.go b/handlers/auth.go index c9238ae4d..0070d2001 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -89,12 +89,6 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque return } - if codeBody.SatsAmount == 0 { - w.WriteHeader(http.StatusBadRequest) - json.NewEncoder(w).Encode("Sats amount must be greater than 0") - return - } - if codeBody.SatsAmount == 0 { codeBody.SatsAmount = 100 } else { @@ -108,9 +102,6 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque newCode := db.ConnectionCodes{ ConnectionString: code, IsUsed: false, - Pubkey: codeBody.Pubkey, - RouteHint: codeBody.RouteHint, - SatsAmount: int64(codeBody.SatsAmount), } codeArr = append(codeArr, newCode) } diff --git a/handlers/auth_test.go b/handlers/auth_test.go index f4a720302..c917135f5 100644 --- a/handlers/auth_test.go +++ b/handlers/auth_test.go @@ -93,7 +93,7 @@ func TestCreateConnectionCode(t *testing.T) { body, _ := json.Marshal(data) - req, err := http.NewRequest("POST", "/connectioncodes", bytes.NewBuffer(body)) + req, err := http.NewRequest(http.MethodPost, "/connectioncodes", bytes.NewBuffer(body)) if err != nil { t.Fatal(err) } @@ -106,7 +106,7 @@ func TestCreateConnectionCode(t *testing.T) { t.Run("should return error for malformed request body", func(t *testing.T) { body := []byte(`{"number": "0"}`) - req, err := http.NewRequest("POST", "/connectioncodes", bytes.NewBuffer(body)) + req, err := http.NewRequest(http.MethodPost, "/connectioncodes", bytes.NewBuffer(body)) if err != nil { t.Fatal(err) } @@ -120,7 +120,47 @@ func TestCreateConnectionCode(t *testing.T) { t.Run("should return error for invalid json", func(t *testing.T) { body := []byte(`{"nonumber":0`) - req, err := http.NewRequest("POST", "/connectioncodes", bytes.NewBuffer(body)) + req, err := http.NewRequest(http.MethodPost, "/connectioncodes", bytes.NewBuffer(body)) + if err != nil { + t.Fatal(err) + } + rr := httptest.NewRecorder() + handler := http.HandlerFunc(aHandler.CreateConnectionCode) + + handler.ServeHTTP(rr, req) + assert.Equal(t, http.StatusNotAcceptable, rr.Code) + }) + + t.Run("should return error if pubkey is provided without route hint", func(t *testing.T) { + data := db.InviteBody{ + Number: 1, + Pubkey: "Test_pubkey", + RouteHint: "", + } + + body, _ := json.Marshal(data) + + req, err := http.NewRequest(http.MethodPost, "/connectioncodes", bytes.NewBuffer(body)) + if err != nil { + t.Fatal(err) + } + rr := httptest.NewRecorder() + handler := http.HandlerFunc(aHandler.CreateConnectionCode) + + handler.ServeHTTP(rr, req) + assert.Equal(t, http.StatusBadRequest, rr.Code) + }) + + t.Run("should return error if route hint is provided without pubkey", func(t *testing.T) { + data := db.InviteBody{ + Number: 1, + Pubkey: "", + RouteHint: "Test_Route_hint", + } + + body, _ := json.Marshal(data) + + req, err := http.NewRequest(http.MethodPost, "/connectioncodes", bytes.NewBuffer(body)) if err != nil { t.Fatal(err) } @@ -167,7 +207,7 @@ func TestGetConnectionCode(t *testing.T) { db.TestDB.CreateConnectionCode(codeArr) - req, err := http.NewRequest("GET", "/connectioncodes", nil) + req, err := http.NewRequest(http.MethodGet, "/connectioncodes", nil) if err != nil { t.Fatal(err) } @@ -186,7 +226,28 @@ func TestGetConnectionCode(t *testing.T) { assert.True(t, timeDifference <= tolerance, "Expected DateCreated to be within tolerance") }) + t.Run("should return empty fields if no connection codes exist", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(aHandler.GetConnectionCode) + req, err := http.NewRequest(http.MethodGet, "/connectioncodes", nil) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code) + + var response db.ConnectionCodesShort + err = json.Unmarshal(rr.Body.Bytes(), &response) + if err != nil { + t.Fatal("Failed to unmarshal response:", err) + } + + assert.Empty(t, response.ConnectionString) + assert.Nil(t, response.DateCreated) + }) } func TestGetIsAdmin(t *testing.T) { diff --git a/tribes.sql b/tribes.sql index 83bbdcb11..ffe65a710 100644 --- a/tribes.sql +++ b/tribes.sql @@ -141,8 +141,5 @@ CREATE TABLE connectioncodes { id SERIAL PRIMARY KEY, connection_string TEXT, is_used boolean, - date_created timestamptz, - pubkey TEXT, - route_hint TEXT, - sats_amount bigint + date_created timestamptz } \ No newline at end of file