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: /tmp/stakwork/sphinx-tribes/handlers/people.go
package people import ( "bytes" "encoding/json" "errors" "io" "net/http" "net/http/httptest" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) // Mocking the database and auth dependencies type MockDB struct { mock.Mock } func (m *MockDB) GetPersonByPubkey(pubkey string) db.Person { args := m.Called(pubkey) return args.Get(0).(db.Person) } func (m *MockDB) CreateOrEditPerson(person db.Person) (db.Person, error) { args := m.Called(person) return args.Get(0).(db.Person), args.Error(1) } func (m *MockDB) PersonUniqueNameFromName(name string) (string, error) { args := m.Called(name) return args.String(0), args.Error(1) } func (m *MockDB) ProcessAlerts(person db.Person) { m.Called(person) } type MockAuth struct { mock.Mock } func (m *MockAuth) EncodeJwt(pubkey string) (string, error) { args := m.Called(pubkey) return args.String(0), args.Error(1) } func TestUpsertLogin(t *testing.T) { mockDB := new(MockDB) mockAuth := new(MockAuth) ph := &peopleHandler{db: mockDB, auth: mockAuth} tests := []struct { name string inputBody interface{} setupMocks func() expectedStatus int expectedJWT string }{ { name: "Valid New Person Creation", inputBody: db.Person{ OwnerPubKey: "newPubKey", OwnerAlias: "newAlias", ID: 0, }, setupMocks: func() { mockDB.On("GetPersonByPubkey", "newPubKey").Return(db.Person{ID: 0}) mockDB.On("PersonUniqueNameFromName", "newAlias").Return("uniqueName", nil) mockDB.On("CreateOrEditPerson", mock.Anything).Return(db.Person{}, nil) mockAuth.On("EncodeJwt", "newPubKey").Return("jwtToken", nil) }, expectedStatus: http.StatusOK, expectedJWT: "jwtToken", }, { name: "Valid Existing Person Update", inputBody: db.Person{ OwnerPubKey: "existingPubKey", ID: 1, }, setupMocks: func() { mockDB.On("GetPersonByPubkey", "existingPubKey").Return(db.Person{ID: 1}) mockDB.On("CreateOrEditPerson", mock.Anything).Return(db.Person{}, nil) mockAuth.On("EncodeJwt", "existingPubKey").Return("jwtToken", nil) }, expectedStatus: http.StatusOK, expectedJWT: "jwtToken", }, { name: "Empty Request Body", inputBody: nil, setupMocks: func() {}, expectedStatus: http.StatusBadRequest, expectedJWT: "", }, { name: "Invalid JSON Format", inputBody: "invalid json", setupMocks: func() {}, expectedStatus: http.StatusNotAcceptable, expectedJWT: "", }, { name: "New Person with Non-zero ID", inputBody: db.Person{ OwnerPubKey: "newPubKey", ID: 1, }, setupMocks: func() { mockDB.On("GetPersonByPubkey", "newPubKey").Return(db.Person{ID: 0}) }, expectedStatus: http.StatusUnauthorized, expectedJWT: "", }, { name: "Edit with Mismatched ID", inputBody: db.Person{ OwnerPubKey: "existingPubKey", ID: 2, }, setupMocks: func() { mockDB.On("GetPersonByPubkey", "existingPubKey").Return(db.Person{ID: 1}) }, expectedStatus: http.StatusUnauthorized, expectedJWT: "", }, { name: "Database Error on CreateOrEditPerson", inputBody: db.Person{ OwnerPubKey: "existingPubKey", ID: 1, }, setupMocks: func() { mockDB.On("GetPersonByPubkey", "existingPubKey").Return(db.Person{ID: 1}) mockDB.On("CreateOrEditPerson", mock.Anything).Return(db.Person{}, errors.New("db error")) }, expectedStatus: http.StatusBadRequest, expectedJWT: "", }, { name: "JWT Token Generation Failure", inputBody: db.Person{ OwnerPubKey: "existingPubKey", ID: 1, }, setupMocks: func() { mockDB.On("GetPersonByPubkey", "existingPubKey").Return(db.Person{ID: 1}) mockDB.On("CreateOrEditPerson", mock.Anything).Return(db.Person{}, nil) mockAuth.On("EncodeJwt", "existingPubKey").Return("", errors.New("jwt error")) }, expectedStatus: http.StatusOK, expectedJWT: "", }, { name: "Large Extras Data", inputBody: db.Person{ OwnerPubKey: "existingPubKey", ID: 1, Extras: make(map[string]interface{}, 1000), }, setupMocks: func() { mockDB.On("GetPersonByPubkey", "existingPubKey").Return(db.Person{ID: 1}) mockDB.On("CreateOrEditPerson", mock.Anything).Return(db.Person{}, nil) mockAuth.On("EncodeJwt", "existingPubKey").Return("jwtToken", nil) }, expectedStatus: http.StatusOK, expectedJWT: "jwtToken", }, { name: "NewTicketTime Trigger", inputBody: db.Person{ OwnerPubKey: "existingPubKey", ID: 1, NewTicketTime: 123456, }, setupMocks: func() { mockDB.On("GetPersonByPubkey", "existingPubKey").Return(db.Person{ID: 1}) mockDB.On("CreateOrEditPerson", mock.Anything).Return(db.Person{}, nil) mockAuth.On("EncodeJwt", "existingPubKey").Return("jwtToken", nil) mockDB.On("ProcessAlerts", mock.Anything).Return() }, expectedStatus: http.StatusOK, expectedJWT: "jwtToken", }, { name: "Invalid Public Key for JWT", inputBody: db.Person{ OwnerPubKey: "invalid!PubKey", ID: 1, }, setupMocks: func() { mockDB.On("GetPersonByPubkey", "invalid!PubKey").Return(db.Person{ID: 1}) mockDB.On("CreateOrEditPerson", mock.Anything).Return(db.Person{}, nil) mockAuth.On("EncodeJwt", "invalid!PubKey").Return("", errors.New("invalid public key")) }, expectedStatus: http.StatusOK, expectedJWT: "", }, { name: "Missing Required Fields", inputBody: map[string]interface{}{ "OwnerAlias": "alias", }, setupMocks: func() {}, expectedStatus: http.StatusNotAcceptable, expectedJWT: "", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tt.setupMocks() var body io.Reader if tt.inputBody != nil { jsonBody, _ := json.Marshal(tt.inputBody) body = bytes.NewReader(jsonBody) } req := httptest.NewRequest(http.MethodPost, "/upsertlogin", body) w := httptest.NewRecorder() ph.UpsertLogin(w, req) resp := w.Result() defer resp.Body.Close() assert.Equal(t, tt.expectedStatus, resp.StatusCode) if tt.expectedJWT != "" { var responseData map[string]string json.NewDecoder(resp.Body).Decode(&responseData) assert.Equal(t, tt.expectedJWT, responseData["jwt"]) } }) } }
The text was updated successfully, but these errors were encountered:
@tomsmith8 I can help?
Sorry, something went wrong.
@tomsmith8 assign?
@tomsmith8 assign
sophieturner0
Successfully merging a pull request may close this issue.
Unit Test Coverage for "UpsertLogin"
Stakwork Run
Unit Test Code
File: /tmp/stakwork/sphinx-tribes/handlers/people.go
The text was updated successfully, but these errors were encountered: