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 #2294

Closed
tomsmith8 opened this issue Dec 24, 2024 · 3 comments
Closed

[Integration Tests] - /person/upsertlogin #2294

tomsmith8 opened this issue Dec 24, 2024 · 3 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"
  "strings"
  "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 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)

  peopleHandler := NewPeopleHandler(db.TestDB)

  t.Run("successful creation of a new person", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(peopleHandler.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("successful update of an existing person", func(t *testing.T) {
  	existingPerson, err := createTestPerson(db.TestDB)
  	assert.NoError(t, err)

  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(peopleHandler.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("invalid JSON body", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(peopleHandler.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)
  	assert.Contains(t, rr.Body.String(), "Sent wrong body data")
  })

  t.Run("invalid public key format", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(peopleHandler.UpsertLogin)

  	person := db.Person{
  		OwnerAlias:  "alias",
  		OwnerPubKey: "invalid!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.StatusBadRequest, rr.Code)
  	assert.Contains(t, rr.Body.String(), "invalid public key")
  })

  t.Run("unauthorized edit attempt", func(t *testing.T) {
  	existingPerson, err := createTestPerson(db.TestDB)
  	assert.NoError(t, err)

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

  	existingPerson.ID = existingPerson.ID + 1 // Simulate unauthorized edit
  	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.StatusUnauthorized, rr.Code)
  	assert.Contains(t, rr.Body.String(), "cant edit someone else")
  })

  t.Run("database unavailability", func(t *testing.T) {
  	// Simulate database failure by closing the connection
  	db.TestDB.Close()

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

  	person := db.Person{
  		OwnerAlias:  "alias",
  		OwnerPubKey: "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.StatusInternalServerError, rr.Code)
  })

  t.Run("simultaneous upsert requests", func(t *testing.T) {
  	// This test would require a more complex setup to simulate concurrency
  	// and is typically handled with a stress testing tool or framework.
  })

  t.Run("new ticket time processing", func(t *testing.T) {
  	rr := httptest.NewRecorder()
  	handler := http.HandlerFunc(peopleHandler.UpsertLogin)

  	person := db.Person{
  		OwnerAlias:    "alias",
  		OwnerPubKey:   "pubkey",
  		NewTicketTime: 123456789,
  	}
  	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)
  	// Verify that ProcessAlerts was called (requires mocking)
  })

  t.Run("JWT token generation failure", func(t *testing.T) {
  	// Simulate JWT failure by mocking auth.EncodeJwt to return an error
  	originalEncodeJwt := auth.EncodeJwt
  	auth.EncodeJwt = func(pubkey string) (string, error) {
  		return "", errors.New("jwt generation failed")
  	}
  	defer func() { auth.EncodeJwt = originalEncodeJwt }()

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

  	person := db.Person{
  		OwnerAlias:  "alias",
  		OwnerPubKey: "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.StatusInternalServerError, rr.Code)
  	assert.Contains(t, rr.Body.String(), "Cannot generate jwt token")
  })

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

  	largeExtras := strings.Repeat("x", 10000)
  	person := db.Person{
  		OwnerAlias:  "alias",
  		OwnerPubKey: "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

aliraza556 commented Dec 24, 2024

@tomsmith8 assign me?

@Shoaibdev7
Copy link
Contributor

@tomsmith8 I can help?

@tomsmith8 tomsmith8 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 24, 2024
@MahtabBukhari
Copy link
Contributor

@tomsmith8 assign

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