We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Stakwork Run
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) }) }
The text was updated successfully, but these errors were encountered:
@tomsmith8 assign me?
Sorry, something went wrong.
@tomsmith8 I can help?
@tomsmith8 assign
No branches or pull requests
Integration Test Coverage for "/person/upsertlogin"
Stakwork Run
Integration Test Code
File: routes/ticket_routes.go
The text was updated successfully, but these errors were encountered: