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] - DeletePerson #2273

Closed
tomsmith8 opened this issue Dec 23, 2024 · 2 comments · Fixed by #2281
Closed

[Unit Tests] - DeletePerson #2273

tomsmith8 opened this issue Dec 23, 2024 · 2 comments · Fixed by #2281
Assignees
Labels

Comments

@tomsmith8
Copy link

Unit Test Coverage for "DeletePerson"


Stakwork Run


Unit Test Code


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


package people

import (
  "context"
  "encoding/json"
  "errors"
  "net/http"
  "net/http/httptest"
  "strconv"
  "testing"

  "github.com/go-chi/chi/v5"
  "github.com/stretchr/testify/assert"
  "github.com/stretchr/testify/mock"
)

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

func (m *MockDB) GetPerson(id uint) Person {
  args := m.Called(id)
  return args.Get(0).(Person)
}

func (m *MockDB) UpdatePerson(id uint, updates map[string]interface{}) error {
  args := m.Called(id, updates)
  return args.Error(0)
}

// Mocking the person struct
type Person struct {
  ID          uint
  OwnerPubKey string
}

// Mocking the peopleHandler struct
type peopleHandler struct {
  db *MockDB
}

// Test cases for DeletePerson function
func TestDeletePerson(t *testing.T) {
  tests := []struct {
  	name             string
  	idString         string
  	pubKeyFromAuth   string
  	existingPerson   Person
  	updateError      error
  	expectedStatus   int
  	expectedResponse interface{}
  }{
  	{
  		name:             "Valid Deletion Request",
  		idString:         "1",
  		pubKeyFromAuth:   "validKey",
  		existingPerson:   Person{ID: 1, OwnerPubKey: "validKey"},
  		expectedStatus:   http.StatusOK,
  		expectedResponse: true,
  	},
  	{
  		name:           "ID is Zero",
  		idString:       "0",
  		expectedStatus: http.StatusUnauthorized,
  	},
  	{
  		name:           "Non-Existent Person",
  		idString:       "999",
  		existingPerson: Person{ID: 0},
  		expectedStatus: http.StatusUnauthorized,
  	},
  	{
  		name:           "Invalid ID Format",
  		idString:       "abc",
  		expectedStatus: http.StatusUnauthorized,
  	},
  	{
  		name:             "Mismatched OwnerPubKey",
  		idString:         "1",
  		pubKeyFromAuth:   "invalidKey",
  		existingPerson:   Person{ID: 1, OwnerPubKey: "validKey"},
  		expectedStatus:   http.StatusUnauthorized,
  	},
  	{
  		name:           "Large ID Value",
  		idString:       strconv.Itoa(2147483647),
  		existingPerson: Person{ID: 2147483647, OwnerPubKey: "validKey"},
  		expectedStatus: http.StatusUnauthorized,
  	},
  	{
  		name:             "Database Update Failure",
  		idString:         "1",
  		pubKeyFromAuth:   "validKey",
  		existingPerson:   Person{ID: 1, OwnerPubKey: "validKey"},
  		updateError:      errors.New("update failed"),
  		expectedStatus:   http.StatusInternalServerError,
  	},
  	{
  		name:           "Context Without Auth Key",
  		idString:       "1",
  		existingPerson: Person{ID: 1, OwnerPubKey: "validKey"},
  		expectedStatus: http.StatusUnauthorized,
  	},
  	{
  		name:           "Empty ID Parameter",
  		idString:       "",
  		expectedStatus: http.StatusUnauthorized,
  	},
  	{
  		name:             "Unauthorized Access Attempt",
  		idString:         "2",
  		pubKeyFromAuth:   "anotherKey",
  		existingPerson:   Person{ID: 2, OwnerPubKey: "validKey"},
  		expectedStatus:   http.StatusUnauthorized,
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		mockDB := new(MockDB)
  		handler := &peopleHandler{db: mockDB}

  		// Mocking GetPerson
  		mockDB.On("GetPerson", mock.AnythingOfType("uint")).Return(tt.existingPerson)

  		// Mocking UpdatePerson
  		if tt.updateError != nil {
  			mockDB.On("UpdatePerson", mock.AnythingOfType("uint"), mock.Anything).Return(tt.updateError)
  		} else {
  			mockDB.On("UpdatePerson", mock.AnythingOfType("uint"), mock.Anything).Return(nil)
  		}

  		req := httptest.NewRequest(http.MethodDelete, "/person/"+tt.idString, nil)
  		ctx := context.WithValue(req.Context(), auth.ContextKey, tt.pubKeyFromAuth)
  		req = req.WithContext(ctx)

  		// Setting URL parameter
  		rctx := chi.NewRouteContext()
  		rctx.URLParams.Add("id", tt.idString)
  		req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

  		// Creating a ResponseRecorder to capture the response
  		rr := httptest.NewRecorder()

  		// Calling the DeletePerson function
  		handler.DeletePerson(rr, req)

  		// Asserting the status code
  		assert.Equal(t, tt.expectedStatus, rr.Code)

  		// Asserting the response body if expected
  		if tt.expectedResponse != nil {
  			var response bool
  			err := json.NewDecoder(rr.Body).Decode(&response)
  			assert.NoError(t, err)
  			assert.Equal(t, tt.expectedResponse, response)
  		}
  	})
  }
}
@MuhammadUmer44
Copy link
Contributor

@tomsmith8 assign me?

@MirzaHanan
Copy link
Contributor

@tomsmith8 Please assign me?

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

Successfully merging a pull request may close this issue.

3 participants