From 70fdb05ea86d679d210489429a0ade3875367930 Mon Sep 17 00:00:00 2001 From: LimitedDelusions <88751373+LimitedDelusions@users.noreply.github.com> Date: Sun, 17 May 2026 10:57:18 -0400 Subject: [PATCH] Use handler database for person referral lookup --- handlers/people.go | 2 +- handlers/people_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/handlers/people.go b/handlers/people.go index a62eda3dc..9318c6c19 100644 --- a/handlers/people.go +++ b/handlers/people.go @@ -89,7 +89,7 @@ func (ph *peopleHandler) CreatePerson(w http.ResponseWriter, r *http.Request) { if referredBy != "" { // get the referral and populate the pubkey - referral := db.DB.GetPersonByUuid(referredBy) + referral := ph.db.GetPersonByUuid(referredBy) // if referral exists if referral.ID != 0 { person.ReferredBy = referral.ID diff --git a/handlers/people_test.go b/handlers/people_test.go index 3840e8240..8da1fdbd3 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -152,6 +152,47 @@ func TestCreatePerson(t *testing.T) { assert.EqualValues(t, person, fetchedUpdatedPerson) }) + t.Run("should create user with referred_by person id from referral uuid", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(pHandler.CreatePerson) + + referrer := db.Person{ + Uuid: uuid.New().String(), + OwnerAlias: "referrer", + UniqueName: "referrer", + OwnerPubKey: uuid.New().String(), + Tags: pq.StringArray{}, + Extras: db.PropertyMap{}, + GithubIssues: db.PropertyMap{}, + } + + createdReferrer, err := db.TestDB.CreateOrEditPerson(referrer) + if err != nil { + t.Fatal(err) + } + + referredPerson := db.Person{ + OwnerAlias: "referred-person", + OwnerPubKey: uuid.New().String(), + Tags: pq.StringArray{}, + Extras: db.PropertyMap{}, + GithubIssues: db.PropertyMap{}, + } + requestBody, _ := json.Marshal(referredPerson) + ctx := context.WithValue(context.Background(), auth.ContextKey, referredPerson.OwnerPubKey) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/?referred_by="+createdReferrer.Uuid, bytes.NewReader(requestBody)) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + fetchedCreatedPerson := db.TestDB.GetPersonByPubkey(referredPerson.OwnerPubKey) + + assert.Equal(t, http.StatusOK, rr.Code, "invalid status received") + assert.Equal(t, createdReferrer.ID, fetchedCreatedPerson.ReferredBy) + }) + t.Run("Should return a 200 status code when existing user hits the endpoint", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(pHandler.CreatePerson)