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} #2351

Open
tomsmith8 opened this issue Jan 7, 2025 · 3 comments
Open

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

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

Comments

@tomsmith8
Copy link

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


Stakwork Run


Integration Test Code


File: routes/ticket_routes.go


package integrationtests

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

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

// Mock dependencies
type MockDB struct {
  mock.Mock
}

func (m *MockDB) GetWorkspaceByUuid(uuid string) *Workspace {
  args := m.Called(uuid)
  return args.Get(0).(*Workspace)
}

func (m *MockDB) ProcessDeleteWorkspace(uuid string) error {
  args := m.Called(uuid)
  return args.Error(0)
}

type Workspace struct {
  OwnerPubKey string
}

type MockLogger struct {
  mock.Mock
}

func (m *MockLogger) Info(format string, v ...interface{}) {
  m.Called(format, v)
}

func (m *MockLogger) Error(format string, v ...interface{}) {
  m.Called(format, v)
}

// Test cases

func TestSuccessfulWorkspaceDeletion(t *testing.T) {
  // Setup
  db := new(MockDB)
  logger := new(MockLogger)
  handler := &workspaceHandler{db: db, logger: logger}

  workspace := &Workspace{OwnerPubKey: "validPubKey"}
  db.On("GetWorkspaceByUuid", "valid-uuid").Return(workspace)
  db.On("ProcessDeleteWorkspace", "valid-uuid").Return(nil)

  req := httptest.NewRequest("DELETE", "/workspaces/delete/valid-uuid", nil)
  req = req.WithContext(context.WithValue(req.Context(), "authKey", "validPubKey"))
  w := httptest.NewRecorder()

  // Execute
  handler.DeleteWorkspace(w, req)

  // Assert
  assert.Equal(t, http.StatusOK, w.Code)
  var response Workspace
  json.NewDecoder(w.Body).Decode(&response)
  assert.Equal(t, workspace, &response)
}

func TestUnauthorizedWorkspaceDeletionAttempt(t *testing.T) {
  // Setup
  db := new(MockDB)
  logger := new(MockLogger)
  handler := &workspaceHandler{db: db, logger: logger}

  workspace := &Workspace{OwnerPubKey: "validPubKey"}
  db.On("GetWorkspaceByUuid", "valid-uuid").Return(workspace)

  req := httptest.NewRequest("DELETE", "/workspaces/delete/valid-uuid", nil)
  req = req.WithContext(context.WithValue(req.Context(), "authKey", "invalidPubKey"))
  w := httptest.NewRecorder()

  // Execute
  handler.DeleteWorkspace(w, req)

  // Assert
  assert.Equal(t, http.StatusUnauthorized, w.Code)
}

func TestInvalidUUIDFormat(t *testing.T) {
  // Setup
  handler := &workspaceHandler{}

  req := httptest.NewRequest("DELETE", "/workspaces/delete/invalid-uuid-format", nil)
  w := httptest.NewRecorder()

  // Execute
  handler.DeleteWorkspace(w, req)

  // Assert
  assert.Equal(t, http.StatusBadRequest, w.Code)
}

func TestNonExistentWorkspaceUUID(t *testing.T) {
  // Setup
  db := new(MockDB)
  logger := new(MockLogger)
  handler := &workspaceHandler{db: db, logger: logger}

  db.On("GetWorkspaceByUuid", "non-existent-uuid").Return(nil)

  req := httptest.NewRequest("DELETE", "/workspaces/delete/non-existent-uuid", nil)
  req = req.WithContext(context.WithValue(req.Context(), "authKey", "validPubKey"))
  w := httptest.NewRecorder()

  // Execute
  handler.DeleteWorkspace(w, req)

  // Assert
  assert.Equal(t, http.StatusNotFound, w.Code)
}

func TestDatabaseUnavailability(t *testing.T) {
  // Setup
  db := new(MockDB)
  logger := new(MockLogger)
  handler := &workspaceHandler{db: db, logger: logger}

  workspace := &Workspace{OwnerPubKey: "validPubKey"}
  db.On("GetWorkspaceByUuid", "valid-uuid").Return(workspace)
  db.On("ProcessDeleteWorkspace", "valid-uuid").Return(errors.New("database error"))

  req := httptest.NewRequest("DELETE", "/workspaces/delete/valid-uuid", nil)
  req = req.WithContext(context.WithValue(req.Context(), "authKey", "validPubKey"))
  w := httptest.NewRecorder()

  // Execute
  handler.DeleteWorkspace(w, req)

  // Assert
  assert.Equal(t, http.StatusInternalServerError, w.Code)
}

func TestConcurrentDeletionRequests(t *testing.T) {
  // Setup
  db := new(MockDB)
  logger := new(MockLogger)
  handler := &workspaceHandler{db: db, logger: logger}

  workspace := &Workspace{OwnerPubKey: "validPubKey"}
  db.On("GetWorkspaceByUuid", "valid-uuid").Return(workspace)
  db.On("ProcessDeleteWorkspace", "valid-uuid").Return(nil)

  req := httptest.NewRequest("DELETE", "/workspaces/delete/valid-uuid", nil)
  req = req.WithContext(context.WithValue(req.Context(), "authKey", "validPubKey"))

  // Simulate concurrent requests
  w1 := httptest.NewRecorder()
  w2 := httptest.NewRecorder()

  go handler.DeleteWorkspace(w1, req)
  go handler.DeleteWorkspace(w2, req)

  // Assert
  assert.Equal(t, http.StatusOK, w1.Code)
  assert.Contains(t, []int{http.StatusOK, http.StatusNotFound, http.StatusConflict}, w2.Code)
}
@AhsanFarooqDev
Copy link
Contributor

AhsanFarooqDev commented Jan 7, 2025

@tomsmith8 please assign

@aliraza556
Copy link
Contributor

@tomsmith8 assign me?/

@tomsmith8
Copy link
Author

@elraphty can you review this ticket please

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