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] - DeleteBountyAssignee #2280

Closed
tomsmith8 opened this issue Dec 23, 2024 · 6 comments · Fixed by #2300
Closed

[Unit Tests] - DeleteBountyAssignee #2280

tomsmith8 opened this issue Dec 23, 2024 · 6 comments · Fixed by #2300
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for "DeleteBountyAssignee"


Stakwork Run


Unit Test Code


File: /tmp/stakwork/sphinx-tribes/handlers/bounties.go


package handlers

import (
  "bytes"
  "encoding/json"
  "errors"
  "io"
  "net/http"
  "net/http/httptest"
  "testing"

  "github.com/stretchr/testify/assert"
  "github.com/stretchr/testify/mock"
)

// Mocking the database interactions
type MockDB struct {
  mock.Mock
}

func (m *MockDB) GetBountyByCreated(created uint) (Bounty, error) {
  args := m.Called(created)
  return args.Get(0).(Bounty), args.Error(1)
}

func (m *MockDB) UpdateBounty(b Bounty) error {
  args := m.Called(b)
  return args.Error(0)
}

// Mock structures
type Bounty struct {
  OwnerID        string
  Assignee       string
  AssignedHours  int
  CommitmentFee  int
  BountyExpires  string
}

type DeleteBountyAssignee struct {
  Owner_pubkey string `json:"Owner_pubkey"`
  Created      string `json:"Created"`
}

var db = &MockDB{}

func TestDeleteBountyAssignee(t *testing.T) {
  tests := []struct {
  	name           string
  	input          interface{}
  	mockSetup      func()
  	expectedStatus int
  	expectedBody   bool
  }{
  	{
  		name: "Valid Input - Successful Deletion",
  		input: DeleteBountyAssignee{
  			Owner_pubkey: "validOwner",
  			Created:      "1234567890",
  		},
  		mockSetup: func() {
  			db.On("GetBountyByCreated", uint(1234567890)).Return(Bounty{OwnerID: "validOwner"}, nil)
  			db.On("UpdateBounty", mock.Anything).Return(nil)
  		},
  		expectedStatus: http.StatusOK,
  		expectedBody:   true,
  	},
  	{
  		name:           "Empty JSON Body",
  		input:          nil,
  		mockSetup:      func() {},
  		expectedStatus: http.StatusNotAcceptable,
  		expectedBody:   false,
  	},
  	{
  		name: "Invalid JSON Format",
  		input: `{"Owner_pubkey": "abc", "Created": }`,
  		mockSetup:      func() {},
  		expectedStatus: http.StatusNotAcceptable,
  		expectedBody:   false,
  	},
  	{
  		name: "Non-Existent Bounty",
  		input: DeleteBountyAssignee{
  			Owner_pubkey: "nonExistentOwner",
  			Created:      "1234567890",
  		},
  		mockSetup: func() {
  			db.On("GetBountyByCreated", uint(1234567890)).Return(Bounty{}, errors.New("not found"))
  		},
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   false,
  	},
  	{
  		name: "Mismatched Owner Key",
  		input: DeleteBountyAssignee{
  			Owner_pubkey: "wrongOwner",
  			Created:      "1234567890",
  		},
  		mockSetup: func() {
  			db.On("GetBountyByCreated", uint(1234567890)).Return(Bounty{OwnerID: "validOwner"}, nil)
  		},
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   false,
  	},
  	{
  		name: "Invalid Data Types",
  		input: DeleteBountyAssignee{
  			Owner_pubkey: "validOwner",
  			Created:      "invalidDate",
  		},
  		mockSetup:      func() {},
  		expectedStatus: http.StatusNotAcceptable,
  		expectedBody:   false,
  	},
  	{
  		name: "Null Values",
  		input: DeleteBountyAssignee{
  			Owner_pubkey: "",
  			Created:      "",
  		},
  		mockSetup:      func() {},
  		expectedStatus: http.StatusNotAcceptable,
  		expectedBody:   false,
  	},
  	{
  		name: "Large JSON Body",
  		input: map[string]interface{}{
  			"Owner_pubkey": "validOwner",
  			"Created":      "1234567890",
  			"Extra":        make([]byte, 10000),
  		},
  		mockSetup: func() {
  			db.On("GetBountyByCreated", uint(1234567890)).Return(Bounty{OwnerID: "validOwner"}, nil)
  			db.On("UpdateBounty", mock.Anything).Return(nil)
  		},
  		expectedStatus: http.StatusOK,
  		expectedBody:   true,
  	},
  	{
  		name: "Boundary Date Value",
  		input: DeleteBountyAssignee{
  			Owner_pubkey: "validOwner",
  			Created:      "0",
  		},
  		mockSetup: func() {
  			db.On("GetBountyByCreated", uint(0)).Return(Bounty{OwnerID: "validOwner"}, nil)
  			db.On("UpdateBounty", mock.Anything).Return(nil)
  		},
  		expectedStatus: http.StatusOK,
  		expectedBody:   true,
  	},
  	{
  		name: "Database Connection Failure",
  		input: DeleteBountyAssignee{
  			Owner_pubkey: "validOwner",
  			Created:      "1234567890",
  		},
  		mockSetup: func() {
  			db.On("GetBountyByCreated", uint(1234567890)).Return(Bounty{}, errors.New("db error"))
  		},
  		expectedStatus: http.StatusInternalServerError,
  		expectedBody:   false,
  	},
  }

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

  		var body []byte
  		if tt.input != nil {
  			switch v := tt.input.(type) {
  			case string:
  				body = []byte(v)
  			default:
  				body, _ = json.Marshal(tt.input)
  			}
  		}
  		req := httptest.NewRequest(http.MethodDelete, "/gobounties/assignee", bytes.NewReader(body))
  		w := httptest.NewRecorder()

  		DeleteBountyAssignee(w, req)

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

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

  		var result bool
  		if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusBadRequest {
  			err := json.NewDecoder(resp.Body).Decode(&result)
  			assert.NoError(t, err)
  			assert.Equal(t, tt.expectedBody, result)
  		}
  	})
  }
}
@aliraza556
Copy link
Contributor

@tomsmith8 assign me?

@MahtabBukhari
Copy link
Contributor

MahtabBukhari commented Dec 23, 2024

@tomsmith8 assign me

@saithsab877
Copy link
Contributor

@tomsmith8 Please assign me?

@MuhammadUmer44
Copy link
Contributor

@tomsmith8 Please assign me. My all previous completed.

@MahtabBukhari
Copy link
Contributor

@tomsmith8 Could you please assign me?

@AhsanFarooqDev
Copy link
Contributor

@tomsmith8 can I help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants