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

[Integration Tests] - /workspaces/delete/{uuid} #2349

Closed
tomsmith8 opened this issue Jan 7, 2025 · 2 comments
Closed

[Integration Tests] - /workspaces/delete/{uuid} #2349

tomsmith8 opened this issue Jan 7, 2025 · 2 comments
Assignees

Comments

@tomsmith8
Copy link

Integration Test Coverage for "/workspaces/delete/{uuid}"


Stakwork Run


Integration Test Code


File: routes/ticket_routes.go


package handlers

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

  "github.com/go-chi/chi"
  "github.com/google/uuid"
  "github.com/stakwork/sphinx-tribes/auth"
  "github.com/stakwork/sphinx-tribes/db"
  "github.com/stretchr/testify/assert"
)

// --- Helper Functions ---
func createTestPerson(testDB *db.DB) (db.Person, error) {
  person := db.Person{
  	Uuid:        uuid.New().String(),
  	OwnerAlias:  "test-alias",
  	UniqueName:  "test-unique-name",
  	OwnerPubKey: "test-pubkey",
  	PriceToMeet: 0,
  	Description: "test-description",
  }
  if err := testDB.CreateOrEditPerson(person); err != nil {
  	return db.Person{}, err
  }
  return person, nil
}

func createTestWorkspace(testDB *db.DB, ownerPubKey string) (db.Workspace, error) {
  workspace := db.Workspace{
  	Uuid:        uuid.New().String(),
  	Name:        "test-workspace" + uuid.New().String(),
  	OwnerPubKey: ownerPubKey,
  	Github:      "https://github.com/test",
  	Website:     "https://www.testwebsite.com",
  	Description: "test-description",
  }
  if err := testDB.CreateOrEditWorkspace(workspace); err != nil {
  	return db.Workspace{}, err
  }
  return workspace, nil
}

func setupTestRequest(method, url string, body []byte, contextValues map[interface{}]interface{}) (*http.Request, error) {
  req, err := http.NewRequest(method, url, bytes.NewReader(body))
  if err != nil {
  	return nil, err
  }
  for key, value := range contextValues {
  	req = req.WithContext(context.WithValue(req.Context(), key, value))
  }
  return req, nil
}

// --- Tests ---
func TestDeleteWorkspace(t *testing.T) {
  teardownSuite := SetupSuite(t)
  defer teardownSuite(t)

  tHandler := NewWorkspaceHandler(&http.Client{}, db.TestDB)
  person, err := createTestPerson(db.TestDB)
  assert.NoError(t, err)

  workspace, err := createTestWorkspace(db.TestDB, person.OwnerPubKey)
  assert.NoError(t, err)

  t.Run("should delete workspace successfully", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(tHandler.DeleteWorkspace)

  	rctx := chi.NewRouteContext()
  	rctx.URLParams.Add("uuid", workspace.Uuid)
  	req, err := setupTestRequest(http.MethodDelete, "/workspaces/delete/"+workspace.Uuid, nil, map[interface{}]interface{}{chi.RouteCtxKey: rctx, auth.ContextKey: person.OwnerPubKey})
  	assert.NoError(t, err)

  	handler.ServeHTTP(rr, req)
  	assert.Equal(t, http.StatusOK, rr.Code)
  })

  t.Run("should return 401 if unauthorized", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(tHandler.DeleteWorkspace)

  	rctx := chi.NewRouteContext()
  	rctx.URLParams.Add("uuid", workspace.Uuid)
  	req, err := setupTestRequest(http.MethodDelete, "/workspaces/delete/"+workspace.Uuid, nil, map[interface{}]interface{}{chi.RouteCtxKey: rctx, auth.ContextKey: "invalid-pubkey"})
  	assert.NoError(t, err)

  	handler.ServeHTTP(rr, req)
  	assert.Equal(t, http.StatusUnauthorized, rr.Code)
  })

  t.Run("should return 400 for invalid UUID format", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(tHandler.DeleteWorkspace)

  	rctx := chi.NewRouteContext()
  	rctx.URLParams.Add("uuid", "invalid-uuid")
  	req, err := setupTestRequest(http.MethodDelete, "/workspaces/delete/invalid-uuid", nil, map[interface{}]interface{}{chi.RouteCtxKey: rctx, auth.ContextKey: person.OwnerPubKey})
  	assert.NoError(t, err)

  	handler.ServeHTTP(rr, req)
  	assert.Equal(t, http.StatusBadRequest, rr.Code)
  })

  t.Run("should return 404 if workspace not found", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(tHandler.DeleteWorkspace)

  	fakeUUID := uuid.New().String()
  	rctx := chi.NewRouteContext()
  	rctx.URLParams.Add("uuid", fakeUUID)
  	req, err := setupTestRequest(http.MethodDelete, "/workspaces/delete/"+fakeUUID, nil, map[interface{}]interface{}{chi.RouteCtxKey: rctx, auth.ContextKey: person.OwnerPubKey})
  	assert.NoError(t, err)

  	handler.ServeHTTP(rr, req)
  	assert.Equal(t, http.StatusNotFound, rr.Code)
  })

  t.Run("should handle database error during deletion", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(tHandler.DeleteWorkspace)

  	// Mock ProcessDeleteWorkspace to return an error
  	db.TestDB.On("ProcessDeleteWorkspace", workspace.Uuid).Return(errors.New("database error"))

  	rctx := chi.NewRouteContext()
  	rctx.URLParams.Add("uuid", workspace.Uuid)
  	req, err := setupTestRequest(http.MethodDelete, "/workspaces/delete/"+workspace.Uuid, nil, map[interface{}]interface{}{chi.RouteCtxKey: rctx, auth.ContextKey: person.OwnerPubKey})
  	assert.NoError(t, err)

  	handler.ServeHTTP(rr, req)
  	assert.Equal(t, http.StatusInternalServerError, rr.Code)
  })

  t.Run("should handle concurrent deletion requests", func(t *testing.T) {
  	var wg sync.WaitGroup
  	handler := http.HandlerFunc(tHandler.DeleteWorkspace)

  	for i := 0; i < 10; i++ {
  		wg.Add(1)
  		go func() {
  			defer wg.Done()
  			rr := httptest.NewRecorder()

  			rctx := chi.NewRouteContext()
  			rctx.URLParams.Add("uuid", workspace.Uuid)
  			req, err := setupTestRequest(http.MethodDelete, "/workspaces/delete/"+workspace.Uuid, nil, map[interface{}]interface{}{chi.RouteCtxKey: rctx, auth.ContextKey: person.OwnerPubKey})
  			assert.NoError(t, err)

  			handler.ServeHTTP(rr, req)
  			assert.Contains(t, []int{http.StatusOK, http.StatusNotFound}, rr.Code)
  		}()
  	}
  	wg.Wait()
  })

  t.Run("should log actions correctly", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(tHandler.DeleteWorkspace)

  	rctx := chi.NewRouteContext()
  	rctx.URLParams.Add("uuid", workspace.Uuid)
  	req, err := setupTestRequest(http.MethodDelete, "/workspaces/delete/"+workspace.Uuid, nil, map[interface{}]interface{}{chi.RouteCtxKey: rctx, auth.ContextKey: person.OwnerPubKey})
  	assert.NoError(t, err)

  	handler.ServeHTTP(rr, req)
  	assert.Equal(t, http.StatusOK, rr.Code)

  	// Verify logs (mock logger to capture logs and assert)
  	// This part assumes a mock logger is set up to capture logs
  	// assert.Contains(t, mockLogger.Logs, "expected log message")
  })

  t.Run("should handle large number of deletion requests", func(t *testing.T) {
  	for i := 0; i < 100; i++ {
  		rr := httptest.NewRecorder()
  		handler := http.HandlerFunc(tHandler.DeleteWorkspace)

  		rctx := chi.NewRouteContext()
  		rctx.URLParams.Add("uuid", workspace.Uuid)
  		req, err := setupTestRequest(http.MethodDelete, "/workspaces/delete/"+workspace.Uuid, nil, map[interface{}]interface{}{chi.RouteCtxKey: rctx, auth.ContextKey: person.OwnerPubKey})
  		assert.NoError(t, err)

  		handler.ServeHTTP(rr, req)
  		assert.Contains(t, []int{http.StatusOK, http.StatusNotFound}, rr.Code)
  	}
  })
}
@MahtabBukhari
Copy link
Contributor

MahtabBukhari commented Jan 7, 2025

@tomsmith8 assign

@MirzaHanan
Copy link
Contributor

@tomsmith8 assign me?

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

No branches or pull requests

4 participants