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" "testing" "time" "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) ph := NewPeopleHandler(db.TestDB) t.Run("Create New Person", 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 token string assert.NoError(t, json.Unmarshal(rr.Body.Bytes(), &token)) assert.NotEmpty(t, token) }) t.Run("Update Existing Person", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(ph.UpsertLogin) existingPerson, err := createTestPerson(db.TestDB) assert.NoError(t, err) 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 token string assert.NoError(t, json.Unmarshal(rr.Body.Bytes(), &token)) assert.NotEmpty(t, token) }) t.Run("Invalid JSON Format", 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("Missing Required Fields", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(ph.UpsertLogin) person := db.Person{ OwnerAlias: "alias-without-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.StatusNotAcceptable, rr.Code) }) t.Run("Unauthorized Edit Attempt", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(ph.UpsertLogin) person := db.Person{ ID: 9999, // Non-existing ID 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.StatusUnauthorized, rr.Code) }) t.Run("Edit Someone Else's Record", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(ph.UpsertLogin) person1, err := createTestPerson(db.TestDB) assert.NoError(t, err) person2, err := createTestPerson(db.TestDB) assert.NoError(t, err) person1.ID = person2.ID // Attempt to edit person2 with person1's data body, _ := json.Marshal(person1) 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("Simultaneous Create Requests", func(t *testing.T) { rr1 := httptest.NewRecorder() rr2 := httptest.NewRecorder() handler := http.HandlerFunc(ph.UpsertLogin) person := db.Person{ OwnerAlias: "simultaneous-alias", OwnerPubKey: "simultaneous-pubkey", } body, _ := json.Marshal(person) req1, err := setupTestRequest(http.MethodPost, "/person/upsertlogin", body, nil) assert.NoError(t, err) req2, err := setupTestRequest(http.MethodPost, "/person/upsertlogin", body, nil) assert.NoError(t, err) go handler.ServeHTTP(rr1, req1) go handler.ServeHTTP(rr2, req2) time.Sleep(1 * time.Second) // Wait for goroutines to finish assert.Equal(t, http.StatusOK, rr1.Code) assert.Equal(t, http.StatusOK, rr2.Code) }) t.Run("Simultaneous Update Requests", func(t *testing.T) { rr1 := httptest.NewRecorder() rr2 := httptest.NewRecorder() handler := http.HandlerFunc(ph.UpsertLogin) existingPerson, err := createTestPerson(db.TestDB) assert.NoError(t, err) existingPerson.OwnerAlias = "simultaneous-update-alias" body, _ := json.Marshal(existingPerson) req1, err := setupTestRequest(http.MethodPost, "/person/upsertlogin", body, nil) assert.NoError(t, err) req2, err := setupTestRequest(http.MethodPost, "/person/upsertlogin", body, nil) assert.NoError(t, err) go handler.ServeHTTP(rr1, req1) go handler.ServeHTTP(rr2, req2) time.Sleep(1 * time.Second) // Wait for goroutines to finish assert.Equal(t, http.StatusOK, rr1.Code) assert.Equal(t, http.StatusOK, rr2.Code) }) t.Run("Process Alerts on New Ticket Time", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(ph.UpsertLogin) person := db.Person{ OwnerAlias: "alert-alias", OwnerPubKey: "alert-pubkey", NewTicketTime: 1, } 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("JWT Token Generation Failure", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(ph.UpsertLogin) person := db.Person{ OwnerAlias: "jwt-fail-alias", OwnerPubKey: "jwt-fail-pubkey", } body, _ := json.Marshal(person) req, err := setupTestRequest(http.MethodPost, "/person/upsertlogin", body, nil) assert.NoError(t, err) // Simulate JWT generation failure auth.EncodeJwt = func(pubKey string) (string, error) { return "", errors.New("JWT generation failed") } handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusInternalServerError, rr.Code) }) t.Run("Large Payload Handling", 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()] = uuid.New().String() } person := db.Person{ OwnerAlias: "large-payload-alias", OwnerPubKey: "large-payload-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) }) t.Run("High Volume of Requests", func(t *testing.T) { handler := http.HandlerFunc(ph.UpsertLogin) for i := 0; i < 100; i++ { rr := httptest.NewRecorder() person := db.Person{ OwnerAlias: "high-volume-alias", OwnerPubKey: "high-volume-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) } }) t.Run("Cascading Changes in Related Entities", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(ph.UpsertLogin) person, err := createTestPerson(db.TestDB) assert.NoError(t, err) person.OwnerAlias = "cascade-alias" 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 related entities are updated (mocked or checked in a real scenario) }) t.Run("Transition from Non-Existing to Existing", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(ph.UpsertLogin) person := db.Person{ OwnerAlias: "transition-alias", OwnerPubKey: "transition-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) // Update the same person person.OwnerAlias = "updated-transition-alias" 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 Please assign me?
@tomsmith8 please assign
@elraphty Can you review this ticket please
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: