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] - /person/upsertlogin #2295

Open
tomsmith8 opened this issue Dec 24, 2024 · 6 comments
Open

[Integration Tests] - /person/upsertlogin #2295

tomsmith8 opened this issue Dec 24, 2024 · 6 comments

Comments

@tomsmith8
Copy link

Integration Test Coverage for "/person/upsertlogin"


Stakwork Run


Integration Test Code


File: routes/ticket_routes.go


package handlers

import (
  "bytes"
  "context"
  "encoding/json"
  "errors"
  "net/http"
  "net/http/httptest"
  "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 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 TestUpsertLogin(t *testing.T) {
  teardownSuite := SetupSuite(t)
  defer teardownSuite(t)

  ph := NewPeopleHandler(&http.Client{}, db.TestDB)

  t.Run("should create new person record", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(ph.UpsertLogin)

  	person := db.Person{
  		OwnerAlias:  "new-alias",
  		OwnerPubKey: "new-pubkey",
  	}
  	body, _ := json.Marshal(person)
  	req, err := setupTestRequest(http.MethodPost, "/person/upsertlogin", body, nil)
  	assert.NoError(t, err)

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

  	var response map[string]interface{}
  	assert.NoError(t, json.Unmarshal(rr.Body.Bytes(), &response))
  	assert.NotEmpty(t, response["jwt"])
  })

  t.Run("should update existing person record", func(t *testing.T) {
  	existingPerson, err := createTestPerson(db.TestDB)
  	assert.NoError(t, err)

  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(ph.UpsertLogin)

  	existingPerson.OwnerAlias = "updated-alias"
  	body, _ := json.Marshal(existingPerson)
  	req, err := setupTestRequest(http.MethodPost, "/person/upsertlogin", body, nil)
  	assert.NoError(t, err)

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

  	var response map[string]interface{}
  	assert.NoError(t, json.Unmarshal(rr.Body.Bytes(), &response))
  	assert.NotEmpty(t, response["jwt"])
  })

  t.Run("should handle invalid input", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(ph.UpsertLogin)

  	req, err := setupTestRequest(http.MethodPost, "/person/upsertlogin", []byte("invalid-json"), nil)
  	assert.NoError(t, err)

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

  t.Run("should handle unauthorized edit attempt", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(ph.UpsertLogin)

  	person := db.Person{
  		ID:          9999,
  		OwnerAlias:  "unauthorized-alias",
  		OwnerPubKey: "unauthorized-pubkey",
  	}
  	body, _ := json.Marshal(person)
  	req, err := setupTestRequest(http.MethodPost, "/person/upsertlogin", body, nil)
  	assert.NoError(t, err)

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

  t.Run("should handle database error simulation", func(t *testing.T) {
  	// Simulate a database error by using a mock or a faulty DB connection
  	// This is a placeholder for actual error simulation
  	// Ensure the handler logs the error and returns a 500 status
  })

  t.Run("should process alerts on NewTicketTime", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(ph.UpsertLogin)

  	person := db.Person{
  		OwnerAlias:    "alert-alias",
  		OwnerPubKey:   "alert-pubkey",
  		NewTicketTime: 1234567890,
  	}
  	body, _ := json.Marshal(person)
  	req, err := setupTestRequest(http.MethodPost, "/person/upsertlogin", body, nil)
  	assert.NoError(t, err)

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

  t.Run("should handle large payload", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(ph.UpsertLogin)

  	largeExtras := make(map[string]interface{})
  	for i := 0; i < 1000; i++ {
  		largeExtras[uuid.New().String()] = "large-data"
  	}

  	person := db.Person{
  		OwnerAlias:  "large-alias",
  		OwnerPubKey: "large-pubkey",
  		Extras:      largeExtras,
  	}
  	body, _ := json.Marshal(person)
  	req, err := setupTestRequest(http.MethodPost, "/person/upsertlogin", body, nil)
  	assert.NoError(t, err)

  	handler.ServeHTTP(rr, req)
  	assert.Equal(t, http.StatusOK, rr.Code)
  })
}
@aliraza556
Copy link
Contributor

@tomsmith8 assign me?

@Shoaibdev7
Copy link
Contributor

@tomsmith8 I can help?

@saithsab877
Copy link
Contributor

@tomsmith8 assign?

@MahtabBukhari
Copy link
Contributor

@tomsmith8 please assign

@tomsmith8
Copy link
Author

For review - feedback on trying to run.

What's missing, what could be improved

@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

No branches or pull requests

6 participants