From 841af8adb508d45e12da48e73ec4c4fab6efc244 Mon Sep 17 00:00:00 2001 From: AhsanFarooqDev Date: Fri, 21 Jun 2024 01:35:27 +0500 Subject: [PATCH 1/4] Refactor TestCreateOrEditPerson UT --- handlers/people_test.go | 74 +++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/handlers/people_test.go b/handlers/people_test.go index 6fb59861f..49a7e199e 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -53,8 +53,9 @@ func TestGetPersonByPuKey(t *testing.T) { func TestCreateOrEditPerson(t *testing.T) { ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") - mockDb := mocks.NewDatabase(t) - pHandler := NewPeopleHandler(mockDb) + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + pHandler := NewPeopleHandler(db.TestDB) t.Run("should return error if body is not a valid json", func(t *testing.T) { rr := httptest.NewRecorder() @@ -111,34 +112,45 @@ func TestCreateOrEditPerson(t *testing.T) { t.Fatal(err) } - mockDb.On("GetPersonByPubkey", "test-key").Return(db.Person{}).Once() handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusUnauthorized, rr.Code, "invalid status received") - mockDb.AssertExpectations(t) }) t.Run("should create user with unique name from owner_alias", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(pHandler.CreateOrEditPerson) - bodyJson := []byte(`{"owner_pubkey": "test-key", "owner_alias": "test-user"}`) - req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(bodyJson)) + person := db.Person{ + ID: 2, + Uuid: "perosn_2_uuid", + OwnerAlias: "person", + UniqueName: "person", + OwnerPubKey: "test-key", + PriceToMeet: 0, + Description: "this is test user 1", + Tags: pq.StringArray{}, + Extras: db.PropertyMap{}, + GithubIssues: db.PropertyMap{}, + } + requestBody, _ := json.Marshal(person) + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(requestBody)) if err != nil { t.Fatal(err) } - mockDb.On("GetPersonByPubkey", "test-key").Return(db.Person{}).Once() - mockDb.On("PersonUniqueNameFromName", "test-user").Return("unique-name", nil).Once() - mockDb.On("CreateOrEditPerson", mock.MatchedBy(func(p db.Person) bool { - return p.UniqueName == "unique-name" && - p.OwnerPubKey == "test-key" && - p.OwnerAlias == "test-user" - })).Return(db.Person{}, nil).Once() + db.TestDB.CreateOrEditPerson(person) + + fetchedPerson := db.TestDB.GetPersonByUuid(person.Uuid) + handler.ServeHTTP(rr, req) + person.Created = fetchedPerson.Created + person.Updated = fetchedPerson.Updated + assert.Equal(t, http.StatusOK, rr.Code, "invalid status received") - mockDb.AssertExpectations(t) + assert.EqualValues(t, person, fetchedPerson) }) t.Run("should return error if trying to update other user", func(t *testing.T) { @@ -151,33 +163,45 @@ func TestCreateOrEditPerson(t *testing.T) { t.Fatal(err) } - mockDb.On("GetPersonByPubkey", "test-key").Return(db.Person{ID: 2}).Once() handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusUnauthorized, rr.Code, "invalid status received") - mockDb.AssertExpectations(t) + }) t.Run("should update user successfully", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(pHandler.CreateOrEditPerson) - bodyJson := []byte(`{"owner_pubkey": "test-key", "owner_alias": "test-user", "id": 1, "img": "img-url"}`) - req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(bodyJson)) + updatePerson := db.Person{ + ID: 2, + Uuid: "perosn_2_uuid", + OwnerAlias: "person", + UniqueName: "person", + OwnerPubKey: "test-key", + PriceToMeet: 100, + Description: "this is Updated test user 1", + Tags: pq.StringArray{}, + Extras: db.PropertyMap{}, + GithubIssues: db.PropertyMap{}, + } + requestBody, _ := json.Marshal(updatePerson) + + req, err := http.NewRequestWithContext(ctx, http.MethodPut, "/", bytes.NewReader(requestBody)) if err != nil { t.Fatal(err) } - mockDb.On("GetPersonByPubkey", "test-key").Return(db.Person{ID: 1}).Once() - mockDb.On("CreateOrEditPerson", mock.MatchedBy(func(p db.Person) bool { - return p.OwnerPubKey == "test-key" && - p.OwnerAlias == "test-user" && - p.Img == "img-url" - })).Return(db.Person{}, nil).Once() + db.TestDB.CreateOrEditPerson(updatePerson) + fetchedUpdatedPerson := db.TestDB.GetPersonByUuid(updatePerson.Uuid) + handler.ServeHTTP(rr, req) + updatePerson.Created = fetchedUpdatedPerson.Created + updatePerson.Updated = fetchedUpdatedPerson.Updated + assert.Equal(t, http.StatusOK, rr.Code, "invalid status received") - mockDb.AssertExpectations(t) + assert.EqualValues(t, updatePerson, fetchedUpdatedPerson) }) } From f1d66cbbc6f08435736528ef3352666271cba5bb Mon Sep 17 00:00:00 2001 From: AhsanFarooqDev Date: Fri, 21 Jun 2024 09:14:36 +0500 Subject: [PATCH 2/4] Refactor TestCreateOrEditPerson UT fixed error --- handlers/people_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/handlers/people_test.go b/handlers/people_test.go index 49a7e199e..802056359 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -52,7 +52,7 @@ func TestGetPersonByPuKey(t *testing.T) { func TestCreateOrEditPerson(t *testing.T) { - ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") + ctx := context.WithValue(context.Background(), auth.ContextKey, "test_key") teardownSuite := SetupSuite(t) defer teardownSuite(t) pHandler := NewPeopleHandler(db.TestDB) @@ -107,7 +107,7 @@ func TestCreateOrEditPerson(t *testing.T) { handler := http.HandlerFunc(pHandler.CreateOrEditPerson) bodyJson := []byte(`{"owner_pubkey": "test-key", "id": 100}`) - req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(bodyJson)) + req, err := http.NewRequestWithContext(ctx, http.MethodPut, "/", bytes.NewReader(bodyJson)) if err != nil { t.Fatal(err) } @@ -122,13 +122,13 @@ func TestCreateOrEditPerson(t *testing.T) { handler := http.HandlerFunc(pHandler.CreateOrEditPerson) person := db.Person{ - ID: 2, - Uuid: "perosn_2_uuid", + ID: 4, + Uuid: "perosn_4_uuid", OwnerAlias: "person", UniqueName: "person", - OwnerPubKey: "test-key", + OwnerPubKey: "test_key", PriceToMeet: 0, - Description: "this is test user 1", + Description: "this is test user 4", Tags: pq.StringArray{}, Extras: db.PropertyMap{}, GithubIssues: db.PropertyMap{}, @@ -174,13 +174,13 @@ func TestCreateOrEditPerson(t *testing.T) { handler := http.HandlerFunc(pHandler.CreateOrEditPerson) updatePerson := db.Person{ - ID: 2, - Uuid: "perosn_2_uuid", + ID: 4, + Uuid: "perosn_4_uuid", OwnerAlias: "person", UniqueName: "person", - OwnerPubKey: "test-key", + OwnerPubKey: "test_key", PriceToMeet: 100, - Description: "this is Updated test user 1", + Description: "this is Updated test user 4", Tags: pq.StringArray{}, Extras: db.PropertyMap{}, GithubIssues: db.PropertyMap{}, From 0e7ee199e0c871c55a106c050e8fdbfcdefda44d Mon Sep 17 00:00:00 2001 From: AhsanFarooqDev Date: Fri, 21 Jun 2024 11:30:08 +0500 Subject: [PATCH 3/4] Refactor TestCreateOrEditPerson UT fixed unit test error --- handlers/people_test.go | 69 +++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/handlers/people_test.go b/handlers/people_test.go index 7b334e3b0..e9c333c17 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -57,8 +57,6 @@ func TestGetPersonByPuKey(t *testing.T) { } func TestCreateOrEditPerson(t *testing.T) { - - ctx := context.WithValue(context.Background(), auth.ContextKey, "test_key") teardownSuite := SetupSuite(t) defer teardownSuite(t) pHandler := NewPeopleHandler(db.TestDB) @@ -68,6 +66,7 @@ func TestCreateOrEditPerson(t *testing.T) { handler := http.HandlerFunc(pHandler.CreateOrEditPerson) invalidJson := []byte(`{"key": "value"`) + ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson)) if err != nil { t.Fatal(err) @@ -83,7 +82,8 @@ func TestCreateOrEditPerson(t *testing.T) { handler := http.HandlerFunc(pHandler.CreateOrEditPerson) bodyJson := []byte(`{"key": "value"}`) - req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "/", bytes.NewReader(bodyJson)) + ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(bodyJson)) if err != nil { t.Fatal(err) } @@ -98,6 +98,7 @@ func TestCreateOrEditPerson(t *testing.T) { handler := http.HandlerFunc(pHandler.CreateOrEditPerson) bodyJson := []byte(`{"owner_pubkey": "other-key"}`) + ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(bodyJson)) if err != nil { t.Fatal(err) @@ -108,12 +109,13 @@ func TestCreateOrEditPerson(t *testing.T) { assert.Equal(t, http.StatusUnauthorized, rr.Code, "invalid status received") }) - t.Run("should return error if trying to update no existing user", func(t *testing.T) { + t.Run("should return error if trying to update non-existing user", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(pHandler.CreateOrEditPerson) bodyJson := []byte(`{"owner_pubkey": "test-key", "id": 100}`) - req, err := http.NewRequestWithContext(ctx, http.MethodPut, "/", bytes.NewReader(bodyJson)) + ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(bodyJson)) if err != nil { t.Fatal(err) } @@ -128,35 +130,28 @@ func TestCreateOrEditPerson(t *testing.T) { handler := http.HandlerFunc(pHandler.CreateOrEditPerson) person := db.Person{ - ID: 4, - Uuid: "perosn_4_uuid", + Uuid: uuid.New().String(), OwnerAlias: "person", UniqueName: "person", - OwnerPubKey: "test_key", + OwnerPubKey: uuid.New().String(), PriceToMeet: 0, - Description: "this is test user 4", + Description: "this is test user 1", Tags: pq.StringArray{}, Extras: db.PropertyMap{}, GithubIssues: db.PropertyMap{}, } requestBody, _ := json.Marshal(person) + ctx := context.WithValue(context.Background(), auth.ContextKey, person.OwnerPubKey) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(requestBody)) if err != nil { t.Fatal(err) } - db.TestDB.CreateOrEditPerson(person) - - fetchedPerson := db.TestDB.GetPersonByUuid(person.Uuid) - handler.ServeHTTP(rr, req) - person.Created = fetchedPerson.Created - person.Updated = fetchedPerson.Updated - assert.Equal(t, http.StatusOK, rr.Code, "invalid status received") - assert.EqualValues(t, person, fetchedPerson) }) t.Run("should return error if trying to update other user", func(t *testing.T) { @@ -164,6 +159,7 @@ func TestCreateOrEditPerson(t *testing.T) { handler := http.HandlerFunc(pHandler.CreateOrEditPerson) bodyJson := []byte(`{"owner_pubkey": "test-key", "owner_alias": "test-user", "id": 1}`) + ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(bodyJson)) if err != nil { t.Fatal(err) @@ -172,40 +168,59 @@ func TestCreateOrEditPerson(t *testing.T) { handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusUnauthorized, rr.Code, "invalid status received") - }) t.Run("should update user successfully", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(pHandler.CreateOrEditPerson) + // First, create a person + person := db.Person{ + Uuid: uuid.New().String(), + OwnerAlias: "person", + UniqueName: "person", + OwnerPubKey: uuid.New().String(), + Description: "this is a test user", + Img: "img-url", + } + createdPerson, err := db.TestDB.CreateOrEditPerson(person) + if err != nil { + t.Fatal(err) + } + + // Update the created person updatePerson := db.Person{ - ID: 4, - Uuid: "perosn_4_uuid", + ID: createdPerson.ID, + Uuid: createdPerson.Uuid, OwnerAlias: "person", UniqueName: "person", - OwnerPubKey: "test_key", - PriceToMeet: 100, - Description: "this is Updated test user 4", + OwnerPubKey: createdPerson.OwnerPubKey, + Description: "this is updated test user", Tags: pq.StringArray{}, Extras: db.PropertyMap{}, GithubIssues: db.PropertyMap{}, + Img: "img-url", + PriceToMeet: 100, } requestBody, _ := json.Marshal(updatePerson) - req, err := http.NewRequestWithContext(ctx, http.MethodPut, "/", bytes.NewReader(requestBody)) + db.TestDB.CreateOrEditPerson(updatePerson) + + ctx := context.WithValue(context.Background(), auth.ContextKey, updatePerson.OwnerPubKey) + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(requestBody)) if err != nil { t.Fatal(err) } - db.TestDB.CreateOrEditPerson(updatePerson) fetchedUpdatedPerson := db.TestDB.GetPersonByUuid(updatePerson.Uuid) - handler.ServeHTTP(rr, req) - + updatePerson.ID = fetchedUpdatedPerson.ID updatePerson.Created = fetchedUpdatedPerson.Created updatePerson.Updated = fetchedUpdatedPerson.Updated + handler.ServeHTTP(rr, req) + assert.Equal(t, http.StatusOK, rr.Code, "invalid status received") assert.EqualValues(t, updatePerson, fetchedUpdatedPerson) }) From b194cef583fb15eb415a0409636d26fc8a9ef98b Mon Sep 17 00:00:00 2001 From: AhsanFarooqDev Date: Fri, 21 Jun 2024 12:13:23 +0500 Subject: [PATCH 4/4] Refactor TestCreateOrEditPerson UT resolved duplicate key value error --- handlers/people_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/handlers/people_test.go b/handlers/people_test.go index e9c333c17..c46f27740 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -139,7 +139,15 @@ func TestCreateOrEditPerson(t *testing.T) { Tags: pq.StringArray{}, Extras: db.PropertyMap{}, GithubIssues: db.PropertyMap{}, + Img: "img-url", } + + db.TestDB.CreateOrEditPerson(person) + + fetchedUpdatedPerson := db.TestDB.GetPersonByUuid(person.Uuid) + + person.ID = fetchedUpdatedPerson.ID + requestBody, _ := json.Marshal(person) ctx := context.WithValue(context.Background(), auth.ContextKey, person.OwnerPubKey) @@ -149,9 +157,14 @@ func TestCreateOrEditPerson(t *testing.T) { t.Fatal(err) } + person.Created = fetchedUpdatedPerson.Created + person.Updated = fetchedUpdatedPerson.Updated + handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusOK, rr.Code, "invalid status received") + + assert.EqualValues(t, person, fetchedUpdatedPerson) }) t.Run("should return error if trying to update other user", func(t *testing.T) {