diff --git a/internal/postgres/post_test.go b/internal/postgres/post_test.go index 389e339..1b1bc83 100644 --- a/internal/postgres/post_test.go +++ b/internal/postgres/post_test.go @@ -223,7 +223,7 @@ func TestGetPostBySourceURL(t *testing.T) { sourceURL: "1234", }, want: nil, - wantErr: false, + wantErr: true, }, { name: "Test 3: No sourceURL", @@ -233,7 +233,7 @@ func TestGetPostBySourceURL(t *testing.T) { sourceURL: "", }, want: nil, - wantErr: false, + wantErr: true, }, } for _, tt := range tests { @@ -323,7 +323,7 @@ func TestGetPostBySourceID(t *testing.T) { sourceID: "1234", }, want: nil, - wantErr: false, + wantErr: true, }, { name: "Test 3: No sourceID", @@ -333,7 +333,7 @@ func TestGetPostBySourceID(t *testing.T) { sourceID: "", }, want: nil, - wantErr: false, + wantErr: true, }, } for _, tt := range tests { diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go index c072241..6be3bde 100644 --- a/internal/postgres/relationships.go +++ b/internal/postgres/relationships.go @@ -113,6 +113,10 @@ func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrove return false, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} } + if len(anthroveUserID) != 25 { + return false, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} + } + result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { diff --git a/internal/postgres/relationships_test.go b/internal/postgres/relationships_test.go index e56b461..a758ad2 100644 --- a/internal/postgres/relationships_test.go +++ b/internal/postgres/relationships_test.go @@ -20,14 +20,20 @@ func TestCheckUserToPostLink(t *testing.T) { defer container.Terminate(ctx) // Setup Test - err = CreateUser(ctx, gormDB, "1") + + validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + invalidUserID := models.AnthroveUserID("XXX") + + validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + + err = CreateUser(ctx, gormDB, validUserID) if err != nil { t.Fatal(err) } post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), + ID: validPostID, }, Rating: "safe", } @@ -37,7 +43,7 @@ func TestCheckUserToPostLink(t *testing.T) { t.Fatal(err) } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(post.ID)) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID) if err != nil { t.Fatal(err) } @@ -60,7 +66,7 @@ func TestCheckUserToPostLink(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validUserID, anthrovePostID: post.ID, }, want: true, @@ -71,33 +77,33 @@ func TestCheckUserToPostLink(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validUserID, anthrovePostID: "qadw", }, want: false, - wantErr: false, + wantErr: true, }, { name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID", args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "123", + anthroveUserID: invalidUserID, anthrovePostID: post.ID, }, want: false, - wantErr: false, + wantErr: true, }, { name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID", args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "123", + anthroveUserID: invalidUserID, anthrovePostID: "123456", }, want: false, - wantErr: false, + wantErr: true, }, } for _, tt := range tests { @@ -219,14 +225,20 @@ func TestEstablishUserToPostLink(t *testing.T) { defer container.Terminate(ctx) // Setup Test - err = CreateUser(ctx, gormDB, "1") + + validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + invalidUserID := models.AnthroveUserID("XXX") + + validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + + err = CreateUser(ctx, gormDB, validUserID) if err != nil { t.Fatal(err) } post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), + ID: validPostID, }, Rating: "safe", } @@ -253,7 +265,7 @@ func TestEstablishUserToPostLink(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validUserID, anthrovePostID: post.ID, }, wantErr: false, @@ -263,18 +275,18 @@ func TestEstablishUserToPostLink(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validUserID, anthrovePostID: "123456", }, wantErr: true, }, { - name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID", + name: "Test 3: invalid AnthroveUserID and valid AnthrovePostID", args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "123", - anthrovePostID: "1234", + anthroveUserID: invalidUserID, + anthrovePostID: post.ID, }, wantErr: true, }, @@ -283,7 +295,7 @@ func TestEstablishUserToPostLink(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "123", + anthroveUserID: invalidUserID, anthrovePostID: "123456", }, wantErr: true, diff --git a/internal/postgres/source_test.go b/internal/postgres/source_test.go index 80c0dc8..b618017 100644 --- a/internal/postgres/source_test.go +++ b/internal/postgres/source_test.go @@ -2,6 +2,7 @@ package postgres import ( "context" + "fmt" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/test" "gorm.io/gorm" @@ -19,7 +20,10 @@ func TestCreateSourceNode(t *testing.T) { // Setup Test + validPostID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Post1")) + validSource := &models.Source{ + BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID}, DisplayName: "e621", Domain: "e621.net", Icon: "icon.e621.net", @@ -65,7 +69,7 @@ func TestCreateSourceNode(t *testing.T) { db: gormDB, anthroveSource: validSource, }, - wantErr: false, + wantErr: true, }, } for _, tt := range tests { diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go index 1983612..04c270a 100644 --- a/internal/postgres/user_test.go +++ b/internal/postgres/user_test.go @@ -21,6 +21,9 @@ func TestCreateUser(t *testing.T) { // Setup Test + validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + invalidUserID := models.AnthroveUserID("XXX") + // Test type args struct { ctx context.Context @@ -37,7 +40,7 @@ func TestCreateUser(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validUserID, }, wantErr: false, }, @@ -46,7 +49,7 @@ func TestCreateUser(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "", + anthroveUserID: invalidUserID, }, wantErr: true, }, @@ -71,9 +74,14 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { // Setup Test + validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + invalidUserID := models.AnthroveUserID("XXX") + + validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) + source := &models.Source{ BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")), + ID: validSourceID, }, DisplayName: "e621", Domain: "e621.net", @@ -103,7 +111,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validUserID, sourceID: source.ID, userID: "e1", username: "marius", @@ -115,7 +123,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "2", + anthroveUserID: invalidUserID, sourceID: source.ID, userID: "e1", username: "marius", @@ -139,7 +147,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validUserID, sourceID: "fa.net", userID: "e1", username: "marius", @@ -151,7 +159,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validUserID, sourceID: source.ID, userID: "", username: "marius", @@ -163,7 +171,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validUserID, sourceID: source.ID, userID: "aa", username: "", @@ -190,8 +198,11 @@ func TestGetAllAnthroveUserIDs(t *testing.T) { defer container.Terminate(ctx) // Setup Test + validUserID01 := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + validUserID02 := models.AnthroveUserID(fmt.Sprintf("%025s", "User2")) + validUserID03 := models.AnthroveUserID(fmt.Sprintf("%025s", "User3")) - users := []models.AnthroveUserID{"1", "2", "3"} + users := []models.AnthroveUserID{validUserID01, validUserID02, validUserID03} for _, user := range users { err = CreateUser(ctx, gormDB, user) @@ -246,6 +257,9 @@ func TestGetUserSourceBySourceID(t *testing.T) { // Setup Test + validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + invalidUserID := models.AnthroveUserID("XXX") + expectedResult := make(map[string]models.UserSource) expectedResult["e621"] = models.UserSource{ UserID: "e1", @@ -269,7 +283,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { t.Fatal(err) } - err = CreateUserWithRelationToSource(ctx, gormDB, "1", models.AnthroveSourceID(expectedResult["e621"].SourceID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) + err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, models.AnthroveSourceID(expectedResult["e621"].SourceID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) if err != nil { t.Fatal(err) } @@ -292,7 +306,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validUserID, sourceID: source.ID, }, want: expectedResult, @@ -303,7 +317,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "22", + anthroveUserID: invalidUserID, sourceID: source.ID, }, want: nil, @@ -314,7 +328,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validUserID, sourceID: "fa", }, want: nil, @@ -368,6 +382,8 @@ func TestGetUserSourceBySourceID(t *testing.T) { } } +// TODO: FIX THE FOUR REMAINING TESTS + func TestGetUserFavoriteNodeWithPagination(t *testing.T) { // Setup trow away containert ctx := context.Background()