From 1471ed28f0ddca064f760933dc0a8db248cfeca7 Mon Sep 17 00:00:00 2001 From: Vargha Csongor Date: Tue, 22 Apr 2025 12:23:12 +0200 Subject: [PATCH] add person and relationship tests --- .../create_person_and_relationship_child.json | 17 ++++++ ...create_person_and_relationship_parent.json | 15 +++++ ...reate_person_and_relationship_sibling.json | 16 +++++ ...create_person_and_relationship_spouse.json | 15 +++++ .../person_and_relationship.go | 61 ++++++++++++++++++- .../internal/api/person_and_relationship.go | 25 +++++++- apps/db-adapter/main_test.go | 9 +-- 7 files changed, 151 insertions(+), 7 deletions(-) create mode 100644 apps/db-adapter/integration-tests/payloads/create_person_and_relationship_child.json create mode 100644 apps/db-adapter/integration-tests/payloads/create_person_and_relationship_parent.json create mode 100644 apps/db-adapter/integration-tests/payloads/create_person_and_relationship_sibling.json create mode 100644 apps/db-adapter/integration-tests/payloads/create_person_and_relationship_spouse.json diff --git a/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_child.json b/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_child.json new file mode 100644 index 0000000..0201df2 --- /dev/null +++ b/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_child.json @@ -0,0 +1,17 @@ +{ + "person": { + "first_name": "Johannes", + "last_name": "Doe", + "born": "2000-07-01", + "limit": 1000, + "mothers_first_name": "Frances", + "mothers_last_name": "Soft", + "email": "dj@example.com" + }, + "type": "child", + "relationship": { + "verified": true, + "notes": "Test notes", + "from": "2023-01-01" + } +} \ No newline at end of file diff --git a/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_parent.json b/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_parent.json new file mode 100644 index 0000000..0212145 --- /dev/null +++ b/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_parent.json @@ -0,0 +1,15 @@ +{ + "person": { + "first_name": "Ferdinand", + "last_name": "Fritz", + "born": "1940-06-01", + "limit": 1000, + "mothers_first_name": "Feras", + "mothers_last_name": "Frea", + "email": "FFd@example.com" + }, + "type": "parent", + "relationship": { + "verified": true + } +} \ No newline at end of file diff --git a/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_sibling.json b/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_sibling.json new file mode 100644 index 0000000..57ec277 --- /dev/null +++ b/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_sibling.json @@ -0,0 +1,16 @@ +{ + "person": { + "first_name": "Sandra", + "last_name": "Doe", + "born": "1987-07-01", + "limit": 1000, + "mothers_first_name": "Mary", + "mothers_last_name": "Smith", + "email": "SD@example.com" + }, + "type": "sibling", + "relationship": { + "verified": true, + "notes": "Good siblings" + } +} \ No newline at end of file diff --git a/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_spouse.json b/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_spouse.json new file mode 100644 index 0000000..f47454b --- /dev/null +++ b/apps/db-adapter/integration-tests/payloads/create_person_and_relationship_spouse.json @@ -0,0 +1,15 @@ +{ + "person": { + "first_name": "Ferdinand", + "last_name": "Fritz", + "born": "1970-04-01", + "limit": 1000, + "mothers_first_name": "Seabruch", + "mothers_last_name": "Klein", + "email": "FF@example.com" + }, + "type": "spouse", + "relationship": { + "verified": true + } +} \ No newline at end of file diff --git a/apps/db-adapter/integration-tests/person_and_relationship.go b/apps/db-adapter/integration-tests/person_and_relationship.go index 8c0dd04..7d221d8 100644 --- a/apps/db-adapter/integration-tests/person_and_relationship.go +++ b/apps/db-adapter/integration-tests/person_and_relationship.go @@ -1,4 +1,63 @@ package integration_tests -func CreatePersonAndRelationship() { +import ( + "bytes" + _ "embed" + "encoding/json" + "net/http" + "testing" + + "github.com/stretchr/testify/require" +) + +//go:embed payloads/create_person_and_relationship_child.json +var create_child []byte + +//go:embed payloads/create_person_and_relationship_parent.json +var create_parent []byte + +//go:embed payloads/create_person_and_relationship_spouse.json +var create_spouse []byte + +//go:embed payloads/create_person_and_relationship_sibling.json +var create_sibling []byte + +func CreateAFamilyTest(dbAdapterUri string, client *http.Client) func(t *testing.T) { + return func(t *testing.T) { + t.Run("CreateChild", CreatePersonAndRelationshipTest(dbAdapterUri, &create_child, client)) + t.Run("CreateParent", CreatePersonAndRelationshipTest(dbAdapterUri, &create_parent, client)) + t.Run("CreateSpouse", CreatePersonAndRelationshipTest(dbAdapterUri, &create_spouse, client)) + t.Run("CreateSibling", CreatePersonAndRelationshipTest(dbAdapterUri, &create_sibling, client)) + } +} + +func CreatePersonAndRelationshipTest(dbAdapterUri string, payload *[]byte, client *http.Client) func(t *testing.T) { + return func(t *testing.T) { + url := dbAdapterUri + "/person_and_relationship/1" + + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, url, bytes.NewBuffer(create_other_person)) + require.NoError(t, err) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-User-ID", "0") + req.Header.Set("X-User-Name", "application/json") + + // Send the request + resp, err := client.Do(req) + require.NoError(t, err) + defer resp.Body.Close() + + var responseBody map[string]any + err = json.NewDecoder(resp.Body).Decode(&responseBody) + require.NoError(t, err) + + // Validate the response + require.Equal(t, http.StatusOK, resp.StatusCode) + + _, ok := responseBody["Id"] + require.True(t, ok) + _, ok = responseBody["person"] + require.True(t, ok) + _, ok = responseBody["relationship"] + require.True(t, ok) + } } diff --git a/apps/db-adapter/internal/api/person_and_relationship.go b/apps/db-adapter/internal/api/person_and_relationship.go index 793accf..5ee1dfd 100644 --- a/apps/db-adapter/internal/api/person_and_relationship.go +++ b/apps/db-adapter/internal/api/person_and_relationship.go @@ -7,6 +7,7 @@ import ( "github.com/gin-gonic/gin" "github.com/neo4j/neo4j-go-driver/v5/neo4j" "github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype" + "github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/internal/api/auth" "github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/internal/memgraph" "github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/pkg/api" "go.uber.org/zap" @@ -23,6 +24,12 @@ func (srv *server) CreatePersonAndRelationship(c *gin.Context, id int, params ap session := srv.db.NewSession(c.Request.Context(), neo4j.SessionConfig{}) defer closeSession(c.Request.Context(), srv.logger, session, srv.dbOpTimeout) + if err := auth.CouldManagePersonUnknownAdmin(c.Request.Context(), session, id, params.XUserID); err != nil { + c.JSON(http.StatusUnauthorized, gin.H{"msg": err.Error()}) + + return + } + trs, err := session.BeginTransaction(c.Request.Context()) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()}) @@ -79,7 +86,7 @@ func (srv *server) CreatePersonAndRelationship(c *gin.Context, id int, params ap convertedRelationship := memgraph.StructToMap(requestBody.Relationship) - var relationShipResultRaw any + var relationShipResultRaw neo4j.ResultWithContext var relationshipError error switch *requestBody.Type { case api.CreatePersonAndRelationshipJSONBodyTypeChild: @@ -126,8 +133,22 @@ func (srv *server) CreatePersonAndRelationship(c *gin.Context, id int, params ap return } + relationshipsSingle, err := relationShipResultRaw.Single(c.Request.Context()) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"msg": "no relationship was created" + err.Error()}) + + return + } + + relationships, ok := relationshipsSingle.Get("relationships") + if !ok { + c.JSON(http.StatusInternalServerError, gin.H{"msg": "no relationship was created"}) + + return + } + c.JSON(http.StatusOK, struct { Person any `json:"person"` Rel any `json:"relationship"` - }{Person: singleRes, Rel: relationShipResultRaw}) + }{Person: singleRes, Rel: relationships}) } diff --git a/apps/db-adapter/main_test.go b/apps/db-adapter/main_test.go index aee4d5d..9d2d116 100644 --- a/apps/db-adapter/main_test.go +++ b/apps/db-adapter/main_test.go @@ -68,13 +68,13 @@ func TestIntegration(t *testing.T) { Context: "./", Dockerfile: "integration-test.dockerfile", }, - ExposedPorts: []string{"8080/tcp"}, + ExposedPorts: []string{"5237/tcp"}, Env: map[string]string{ "MEMGRAPH_URI": memgraphURI, - "HTTP_PORT": ":8080", + "HTTP_PORT": ":5237", }, Networks: []string{net.Name}, - WaitingFor: wait.ForListeningPort("8080/tcp"), + WaitingFor: wait.ForListeningPort("5237/tcp"), } dbAdapterC, err := testcontainers.GenericContainer(t.Context(), testcontainers.GenericContainerRequest{ @@ -96,7 +96,7 @@ func TestIntegration(t *testing.T) { dbAdapterHost, err := dbAdapterC.Host(t.Context()) require.NoError(t, err) - dbAdapterPort, err := dbAdapterC.MappedPort(t.Context(), "8080/tcp") + dbAdapterPort, err := dbAdapterC.MappedPort(t.Context(), "5237/tcp") require.NoError(t, err) dbAdapterURI := "http://" + dbAdapterHost + ":" + dbAdapterPort.Port() @@ -111,6 +111,7 @@ func IntegrationTestFlow(dbAdapterURI string) func(t *testing.T) { t.Run("UpdatePerson", integration_tests.UpdatePersonTest(dbAdapterURI, client)) t.Run("AddInviteCodeToPerson", integration_tests.UpdatePersonWithInviteCodeTest(dbAdapterURI, client)) t.Run("GetPersonById", integration_tests.GetPersonById(dbAdapterURI, client)) + t.Run("CreateFamilyTest", integration_tests.CreateAFamilyTest(dbAdapterURI, client)) t.Run("SoftDeletePerson", integration_tests.SoftDeletePersonTest(dbAdapterURI, client)) t.Run("HardDeletePerson", integration_tests.HardDeletePersonTest(dbAdapterURI, client)) }