Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] && [Corrected Function] - DeleteBountyAssignee #2300

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 2 additions & 49 deletions handlers/bounties.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package handlers

import (
"encoding/json"
"io"
"log"
"net/http"
"reflect"
"strconv"

"github.com/lib/pq"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/logger"
"net/http"
"reflect"
)

func GetWantedsHeader(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -44,49 +40,6 @@ func GetBountiesLeaderboard(w http.ResponseWriter, _ *http.Request) {
json.NewEncoder(w).Encode(leaderBoard)
}

func DeleteBountyAssignee(w http.ResponseWriter, r *http.Request) {
invoice := db.DeleteBountyAssignee{}
body, err := io.ReadAll(r.Body)
var deletedAssignee bool

r.Body.Close()

err = json.Unmarshal(body, &invoice)

if err != nil {
logger.Log.Error("%v", err)
w.WriteHeader(http.StatusNotAcceptable)
return
}

owner_key := invoice.Owner_pubkey
date := invoice.Created

createdUint, _ := strconv.ParseUint(date, 10, 32)
b, err := db.DB.GetBountyByCreated(uint(createdUint))

if err == nil && b.OwnerID == owner_key {
b.Assignee = ""
b.AssignedHours = 0
b.CommitmentFee = 0
b.BountyExpires = ""

db.DB.UpdateBounty(b)

deletedAssignee = true
} else {
log.Printf("Could not delete bounty assignee")

deletedAssignee = false

w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(deletedAssignee)
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(deletedAssignee)
}

func MigrateBounties(w http.ResponseWriter, r *http.Request) {
peeps := db.DB.GetAllPeople()

Expand Down
48 changes: 48 additions & 0 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -1534,3 +1534,51 @@ func (h *bountyHandler) GenerateBountyCardResponse(bounties []db.NewBounty) []db

return bountyCardResponse
}

func (h *bountyHandler) DeleteBountyAssignee(w http.ResponseWriter, r *http.Request) {
invoice := db.DeleteBountyAssignee{}
body, err := io.ReadAll(r.Body)
var deletedAssignee bool

r.Body.Close()

err = json.Unmarshal(body, &invoice)

if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
return
}

owner_key := invoice.Owner_pubkey
date := invoice.Created

if owner_key == "" || date == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(false)
return
}

createdUint, _ := strconv.ParseUint(date, 10, 32)
b, err := h.db.GetBountyByCreated(uint(createdUint))

if err == nil && b.OwnerID == owner_key {
b.Assignee = ""
b.AssignedHours = 0
b.CommitmentFee = 0
b.BountyExpires = ""

h.db.UpdateBounty(b)

deletedAssignee = true
} else {
log.Printf("Could not delete bounty assignee")

deletedAssignee = false

w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(deletedAssignee)
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(deletedAssignee)
}
169 changes: 169 additions & 0 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2392,3 +2392,172 @@ func TestGetBountyCards(t *testing.T) {
assert.Empty(t, cardWithoutAssignee.AssigneePic)
})
}

func TestDeleteBountyAssignee(t *testing.T) {

teardownSuite := SetupSuite(t)
defer teardownSuite(t)

mockHttpClient := mocks.NewHttpClient(t)

bHandler := NewBountyHandler(mockHttpClient, db.TestDB)

db.CleanTestData()

db.TestDB.CreateOrEditBounty(db.NewBounty{
Type: "coding",
Title: "Bounty 1",
Description: "Description for Bounty 1",
WorkspaceUuid: "work-1",
OwnerID: "validOwner",
Price: 1500,
Created: 1234567890,
})

db.TestDB.CreateOrEditBounty(db.NewBounty{
Type: "design",
Title: "Bounty 2",
Description: "Description for Bounty 2",
WorkspaceUuid: "work-2",
OwnerID: "nonExistentOwner",
Price: 2000,
Created: 1234567891,
})

db.TestDB.CreateOrEditBounty(db.NewBounty{
Type: "design",
Title: "Bounty 2",
Description: "Description for Bounty 2",
WorkspaceUuid: "work-2",
OwnerID: "validOwner",
Price: 2000,
Created: 0,
})

tests := []struct {
name string
input interface{}
mockSetup func()
expectedStatus int
expectedBody bool
}{
{
name: "Valid Input - Successful Deletion",
input: db.DeleteBountyAssignee{
Owner_pubkey: "validOwner",
Created: "1234567890",
},
expectedStatus: http.StatusOK,
expectedBody: true,
},
{
name: "Empty JSON Body",
input: nil,
expectedStatus: http.StatusNotAcceptable,
expectedBody: false,
},
{
name: "Invalid JSON Format",
input: `{"Owner_pubkey": "abc", "Created": }`,
expectedStatus: http.StatusNotAcceptable,
expectedBody: false,
},
{
name: "Non-Existent Bounty",
input: db.DeleteBountyAssignee{
Owner_pubkey: "nonExistentOwner",
Created: "1234567890",
},
expectedStatus: http.StatusBadRequest,
expectedBody: false,
},
{
name: "Mismatched Owner Key",
input: db.DeleteBountyAssignee{
Owner_pubkey: "wrongOwner",
Created: "1234567890",
},
expectedStatus: http.StatusBadRequest,
expectedBody: false,
},
{
name: "Invalid Data Types",
input: db.DeleteBountyAssignee{
Owner_pubkey: "validOwners",
Created: "invalidDate",
},
expectedStatus: http.StatusBadRequest,
expectedBody: false,
},
{
name: "Null Values",
input: db.DeleteBountyAssignee{
Owner_pubkey: "",
Created: "",
},
expectedStatus: http.StatusBadRequest,
expectedBody: false,
},
{
name: "Large JSON Body",
input: map[string]interface{}{
"Owner_pubkey": "validOwner",
"Created": "1234567890",
"Extra": make([]byte, 10000),
},
expectedStatus: http.StatusOK,
expectedBody: true,
},
{
name: "Boundary Date Value",
input: db.DeleteBountyAssignee{
Owner_pubkey: "validOwner",
Created: "0",
},
expectedStatus: http.StatusOK,
expectedBody: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

var body []byte
if tt.input != nil {
switch v := tt.input.(type) {
case string:
body = []byte(v)
default:
var err error
body, err = json.Marshal(tt.input)
if err != nil {
t.Fatalf("Failed to marshal input: %v", err)
}
}
}

req := httptest.NewRequest(http.MethodDelete, "/gobounties/assignee", bytes.NewReader(body))

w := httptest.NewRecorder()

bHandler.DeleteBountyAssignee(w, req)

resp := w.Result()
defer resp.Body.Close()

assert.Equal(t, tt.expectedStatus, resp.StatusCode)

if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusBadRequest {

var result bool
err := json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
t.Fatalf("Failed to decode response body: %v", err)
}

assert.Equal(t, tt.expectedBody, result)
}
})
}

}
2 changes: 1 addition & 1 deletion routes/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func BountyRoutes() chi.Router {
r.Put("/payment/status/{id}", bountyHandler.UpdateBountyPaymentStatus)

r.Post("/", bountyHandler.CreateOrEditBounty)
r.Delete("/assignee", handlers.DeleteBountyAssignee)
r.Delete("/assignee", bountyHandler.DeleteBountyAssignee)
r.Delete("/{pubkey}/{created}", bountyHandler.DeleteBounty)
r.Post("/paymentstatus/{created}", handlers.UpdatePaymentStatus)
r.Post("/completedstatus/{created}", handlers.UpdateCompletedStatus)
Expand Down
Loading