From 1cd9411fd55aa972a378d9e035c8cc138ce1c1ae Mon Sep 17 00:00:00 2001 From: "aqueel192@gmail.com" Date: Wed, 11 Feb 2026 00:58:06 +0530 Subject: [PATCH] Merge filevalidation package into validator package Signed-off-by: aqueel192@gmail.com --- packages/emails/emails.go | 12 +- pkg/iam/organization_service.go | 7 +- pkg/probo/audit_service.go | 7 +- pkg/probo/evidence_service.go | 3 +- pkg/probo/file_service.go | 4 +- pkg/probo/organization_service.go | 3 +- pkg/probo/service.go | 46 ++++---- pkg/probo/trust_center_file_service.go | 3 +- pkg/probo/trust_center_service.go | 7 +- pkg/probo/vendor_compliance_report_service.go | 4 +- .../validator_file.go} | 12 +- .../validator_file_test.go} | 109 +++++++----------- 12 files changed, 91 insertions(+), 126 deletions(-) rename pkg/{filevalidation/validator.go => validator/validator_file.go} (95%) rename pkg/{filevalidation/validator_test.go => validator/validator_file_test.go} (81%) diff --git a/packages/emails/emails.go b/packages/emails/emails.go index 678c4fca1..861b0b093 100644 --- a/packages/emails/emails.go +++ b/packages/emails/emails.go @@ -31,7 +31,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/s3" "go.probo.inc/probo/pkg/baseurl" "go.probo.inc/probo/pkg/filemanager" - "go.probo.inc/probo/pkg/filevalidation" + "go.probo.inc/probo/pkg/validator" ) //go:embed dist @@ -41,11 +41,11 @@ var ( //go:embed assets staticAssets embed.FS - staticAssetsValidator = filevalidation.NewValidator( - filevalidation.WithMaxFileSize(5*1024*1024), - filevalidation.WithCategories( - filevalidation.CategoryImage, - filevalidation.CategoryVideo, + staticAssetsValidator = validator.NewFileValidator( + validator.WithMaxFileSize(5*1024*1024), + validator.WithCategories( + validator.CategoryImage, + validator.CategoryVideo, ), ) diff --git a/pkg/iam/organization_service.go b/pkg/iam/organization_service.go index 89c91d7d6..bb7ff11c7 100644 --- a/pkg/iam/organization_service.go +++ b/pkg/iam/organization_service.go @@ -25,7 +25,6 @@ import ( "go.gearno.de/kit/pg" "go.probo.inc/probo/packages/emails" "go.probo.inc/probo/pkg/coredata" - "go.probo.inc/probo/pkg/filevalidation" "go.probo.inc/probo/pkg/gid" "go.probo.inc/probo/pkg/iam/scim" "go.probo.inc/probo/pkg/mail" @@ -134,7 +133,7 @@ const ( func (req CreateOrganizationRequest) Validate() error { v := validator.New() - fv := filevalidation.NewValidator(filevalidation.WithCategories(filevalidation.CategoryImage)) + fv := validator.NewFileValidator(validator.WithCategories(validator.CategoryImage)) if req.LogoFile != nil { err := fv.Validate(req.LogoFile.Filename, req.LogoFile.ContentType, req.LogoFile.Size) @@ -157,7 +156,7 @@ func (req CreateOrganizationRequest) Validate() error { func (req UpdateOrganizationRequest) Validate() error { v := validator.New() - fv := filevalidation.NewValidator(filevalidation.WithCategories(filevalidation.CategoryImage)) + fv := validator.NewFileValidator(validator.WithCategories(validator.CategoryImage)) v.Check(req.Name, "name", validator.SafeTextNoNewLine(255)) v.Check(req.Description, "description", validator.SafeText(ContentMaxLength)) @@ -1907,7 +1906,7 @@ func (s OrganizationService) CreateSCIMBridge( ConnectorID: &connectorID, Type: bridgeType, State: coredata.SCIMBridgeStateActive, // Active immediately since connector already exists - ExcludedUserNames: []string{}, + ExcludedUserNames: []string{}, CreatedAt: now, UpdatedAt: now, } diff --git a/pkg/probo/audit_service.go b/pkg/probo/audit_service.go index 50805b09c..cbebc729d 100644 --- a/pkg/probo/audit_service.go +++ b/pkg/probo/audit_service.go @@ -24,7 +24,6 @@ import ( "go.gearno.de/crypto/uuid" "go.gearno.de/kit/pg" "go.probo.inc/probo/pkg/coredata" - "go.probo.inc/probo/pkg/filevalidation" "go.probo.inc/probo/pkg/gid" "go.probo.inc/probo/pkg/page" "go.probo.inc/probo/pkg/validator" @@ -93,9 +92,9 @@ func (uarr *UploadAuditReportRequest) Validate() error { return err } - fv := filevalidation.NewValidator( - filevalidation.WithCategories(filevalidation.CategoryDocument), - filevalidation.WithMaxFileSize(25*1024*1024), + fv := validator.NewFileValidator( + validator.WithCategories(validator.CategoryDocument), + validator.WithMaxFileSize(25*1024*1024), ) if err := fv.Validate(uarr.File.Filename, uarr.File.ContentType, uarr.File.Size); err != nil { return fmt.Errorf("invalid audit report file: %w", err) diff --git a/pkg/probo/evidence_service.go b/pkg/probo/evidence_service.go index 64c28d4a9..78b0ac78b 100644 --- a/pkg/probo/evidence_service.go +++ b/pkg/probo/evidence_service.go @@ -22,7 +22,6 @@ import ( "go.gearno.de/crypto/uuid" "go.gearno.de/kit/pg" "go.probo.inc/probo/pkg/coredata" - "go.probo.inc/probo/pkg/filevalidation" "go.probo.inc/probo/pkg/gid" "go.probo.inc/probo/pkg/page" "go.probo.inc/probo/pkg/validator" @@ -31,7 +30,7 @@ import ( type ( EvidenceService struct { svc *TenantService - fileValidator *filevalidation.FileValidator + fileValidator *validator.FileValidator } UploadMeasureEvidenceRequest struct { diff --git a/pkg/probo/file_service.go b/pkg/probo/file_service.go index 230b61901..0a70b3c8b 100644 --- a/pkg/probo/file_service.go +++ b/pkg/probo/file_service.go @@ -26,8 +26,8 @@ import ( "go.gearno.de/crypto/uuid" "go.gearno.de/kit/pg" "go.probo.inc/probo/pkg/coredata" - "go.probo.inc/probo/pkg/filevalidation" "go.probo.inc/probo/pkg/gid" + "go.probo.inc/probo/pkg/validator" ) type ( @@ -76,7 +76,7 @@ func (s FileService) Get( func (s FileService) UploadAndSaveFile( ctx context.Context, - fileValidator *filevalidation.FileValidator, + fileValidator *validator.FileValidator, s3Metadata map[string]string, req *FileUpload) (*coredata.File, error) { objectKey, err := uuid.NewV7() diff --git a/pkg/probo/organization_service.go b/pkg/probo/organization_service.go index 56463dd78..d62fd4ddf 100644 --- a/pkg/probo/organization_service.go +++ b/pkg/probo/organization_service.go @@ -25,7 +25,6 @@ import ( "go.gearno.de/crypto/uuid" "go.gearno.de/kit/pg" "go.probo.inc/probo/pkg/coredata" - "go.probo.inc/probo/pkg/filevalidation" "go.probo.inc/probo/pkg/gid" "go.probo.inc/probo/pkg/validator" ) @@ -33,7 +32,7 @@ import ( type ( OrganizationService struct { svc *TenantService - fileValidator *filevalidation.FileValidator + fileValidator *validator.FileValidator } CreateOrganizationRequest struct { diff --git a/pkg/probo/service.go b/pkg/probo/service.go index 3ceb78646..04dc7a443 100644 --- a/pkg/probo/service.go +++ b/pkg/probo/service.go @@ -28,12 +28,12 @@ import ( "go.probo.inc/probo/pkg/coredata" "go.probo.inc/probo/pkg/crypto/cipher" "go.probo.inc/probo/pkg/filemanager" - "go.probo.inc/probo/pkg/filevalidation" "go.probo.inc/probo/pkg/gid" "go.probo.inc/probo/pkg/html2pdf" "go.probo.inc/probo/pkg/iam" "go.probo.inc/probo/pkg/mail" "go.probo.inc/probo/pkg/slack" + "go.probo.inc/probo/pkg/validator" ) const ( @@ -174,15 +174,15 @@ func (s *Service) WithTenant(tenantID gid.TenantID) *TenantService { tenantService.Tasks = &TaskService{svc: tenantService} tenantService.Evidences = &EvidenceService{ svc: tenantService, - fileValidator: filevalidation.NewValidator( - filevalidation.WithCategories( - filevalidation.CategoryDocument, - filevalidation.CategorySpreadsheet, - filevalidation.CategoryPresentation, - filevalidation.CategoryData, - filevalidation.CategoryText, - filevalidation.CategoryImage, - filevalidation.CategoryVideo, + fileValidator: validator.NewFileValidator( + validator.WithCategories( + validator.CategoryDocument, + validator.CategorySpreadsheet, + validator.CategoryPresentation, + validator.CategoryData, + validator.CategoryText, + validator.CategoryImage, + validator.CategoryVideo, ), ), } @@ -194,16 +194,16 @@ func (s *Service) WithTenant(tenantID gid.TenantID) *TenantService { } tenantService.Organizations = &OrganizationService{ svc: tenantService, - fileValidator: filevalidation.NewValidator( - filevalidation.WithCategories(filevalidation.CategoryImage), + fileValidator: validator.NewFileValidator( + validator.WithCategories(validator.CategoryImage), ), } tenantService.Controls = &ControlService{svc: tenantService} tenantService.Risks = &RiskService{svc: tenantService} tenantService.VendorComplianceReports = &VendorComplianceReportService{ svc: tenantService, - fileValidator: filevalidation.NewValidator( - filevalidation.WithCategories(filevalidation.CategoryDocument), + fileValidator: validator.NewFileValidator( + validator.WithCategories(validator.CategoryDocument), ), } tenantService.VendorBusinessAssociateAgreements = &VendorBusinessAssociateAgreementService{svc: tenantService} @@ -221,16 +221,16 @@ func (s *Service) WithTenant(tenantID gid.TenantID) *TenantService { tenantService.TrustCenterReferences = &TrustCenterReferenceService{svc: tenantService} tenantService.TrustCenterFiles = &TrustCenterFileService{ svc: tenantService, - fileValidator: filevalidation.NewValidator( - filevalidation.WithCategories( - filevalidation.CategoryData, - filevalidation.CategoryDocument, - filevalidation.CategoryImage, - filevalidation.CategoryPresentation, - filevalidation.CategorySpreadsheet, - filevalidation.CategoryText, + fileValidator: validator.NewFileValidator( + validator.WithCategories( + validator.CategoryData, + validator.CategoryDocument, + validator.CategoryImage, + validator.CategoryPresentation, + validator.CategorySpreadsheet, + validator.CategoryText, ), - filevalidation.WithMaxFileSize(10*1024*1024), // 10MB + validator.WithMaxFileSize(10*1024*1024), // 10MB ), } tenantService.Nonconformities = &NonconformityService{svc: tenantService} diff --git a/pkg/probo/trust_center_file_service.go b/pkg/probo/trust_center_file_service.go index 2cdede344..21e784ff8 100644 --- a/pkg/probo/trust_center_file_service.go +++ b/pkg/probo/trust_center_file_service.go @@ -28,7 +28,6 @@ import ( "go.gearno.de/crypto/uuid" "go.gearno.de/kit/pg" "go.probo.inc/probo/pkg/coredata" - "go.probo.inc/probo/pkg/filevalidation" "go.probo.inc/probo/pkg/gid" "go.probo.inc/probo/pkg/page" "go.probo.inc/probo/pkg/validator" @@ -37,7 +36,7 @@ import ( type ( TrustCenterFileService struct { svc *TenantService - fileValidator *filevalidation.FileValidator + fileValidator *validator.FileValidator } CreateTrustCenterFileRequest struct { diff --git a/pkg/probo/trust_center_service.go b/pkg/probo/trust_center_service.go index 994068aaf..88dbc9192 100644 --- a/pkg/probo/trust_center_service.go +++ b/pkg/probo/trust_center_service.go @@ -30,7 +30,6 @@ import ( "go.gearno.de/kit/pg" "go.probo.inc/probo/packages/emails" "go.probo.inc/probo/pkg/coredata" - "go.probo.inc/probo/pkg/filevalidation" "go.probo.inc/probo/pkg/gid" "go.probo.inc/probo/pkg/validator" ) @@ -82,9 +81,9 @@ func (utcndar *UploadTrustCenterNDARequest) Validate() error { } func (req *UpdateTrustCenterBrandRequest) Validate() error { - fv := filevalidation.NewValidator( - filevalidation.WithCategories(filevalidation.CategoryImage), - filevalidation.WithMaxFileSize(maxBrandFileSize), + fv := validator.NewFileValidator( + validator.WithCategories(validator.CategoryImage), + validator.WithMaxFileSize(maxBrandFileSize), ) if req.LogoFile != nil && *req.LogoFile != nil { diff --git a/pkg/probo/vendor_compliance_report_service.go b/pkg/probo/vendor_compliance_report_service.go index 511d6c4bf..51a4fa1cd 100644 --- a/pkg/probo/vendor_compliance_report_service.go +++ b/pkg/probo/vendor_compliance_report_service.go @@ -21,7 +21,7 @@ import ( "go.gearno.de/kit/pg" "go.probo.inc/probo/pkg/coredata" - "go.probo.inc/probo/pkg/filevalidation" + "go.probo.inc/probo/pkg/gid" "go.probo.inc/probo/pkg/page" "go.probo.inc/probo/pkg/validator" @@ -30,7 +30,7 @@ import ( type ( VendorComplianceReportService struct { svc *TenantService - fileValidator *filevalidation.FileValidator + fileValidator *validator.FileValidator } VendorComplianceReportCreateRequest struct { diff --git a/pkg/filevalidation/validator.go b/pkg/validator/validator_file.go similarity index 95% rename from pkg/filevalidation/validator.go rename to pkg/validator/validator_file.go index 61734b24f..f5d75a784 100644 --- a/pkg/filevalidation/validator.go +++ b/pkg/validator/validator_file.go @@ -12,7 +12,7 @@ // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -package filevalidation +package validator import ( "fmt" @@ -106,9 +106,9 @@ type FileValidator struct { Categories []string } -type Option func(v *FileValidator) *FileValidator +type FileValidatorOption func(v *FileValidator) *FileValidator -func WithCategories(categories ...string) Option { +func WithCategories(categories ...string) FileValidatorOption { return func(v *FileValidator) *FileValidator { v.Categories = categories @@ -133,7 +133,7 @@ func WithCategories(categories ...string) Option { } } -func WithMaxFileSize(maxFileSize int64) Option { +func WithMaxFileSize(maxFileSize int64) FileValidatorOption { return func(v *FileValidator) *FileValidator { v.MaxFileSize = maxFileSize @@ -141,7 +141,7 @@ func WithMaxFileSize(maxFileSize int64) Option { } } -func NewValidator(opts ...Option) *FileValidator { +func NewFileValidator(opts ...FileValidatorOption) *FileValidator { v := &FileValidator{ MaxFileSize: DefaultMaxFileSize, AllowedMimeTypes: make(map[string]bool), @@ -156,7 +156,7 @@ func NewValidator(opts ...Option) *FileValidator { } // WithMaxFileSize sets the maximum file size and returns the validator -func (v *FileValidator) WithMaxFileSize(maxSize int64) *FileValidator { +func (v *FileValidator) SetMaxFileSize(maxSize int64) *FileValidator { v.MaxFileSize = maxSize return v } diff --git a/pkg/filevalidation/validator_test.go b/pkg/validator/validator_file_test.go similarity index 81% rename from pkg/filevalidation/validator_test.go rename to pkg/validator/validator_file_test.go index 5b36d69a3..cc1853a0e 100644 --- a/pkg/filevalidation/validator_test.go +++ b/pkg/validator/validator_file_test.go @@ -12,15 +12,14 @@ // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -package filevalidation +package validator import ( - "fmt" "strings" "testing" ) -func TestNewValidator(t *testing.T) { +func TestNewFileValidator(t *testing.T) { tests := []struct { name string categories []string @@ -60,7 +59,7 @@ func TestNewValidator(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - v := NewValidator(WithCategories(tc.categories...)) + v := NewFileValidator(WithCategories(tc.categories...)) // Test expected mime types are allowed for _, mime := range tc.expectedMimes { @@ -95,15 +94,15 @@ func TestNewValidator(t *testing.T) { } func TestWithMaxFileSize(t *testing.T) { - v := NewValidator() + v := NewFileValidator() originalSize := v.MaxFileSize // Test changing the max file size newSize := int64(10 * 1024 * 1024) // 10MB - v = v.WithMaxFileSize(newSize) + v = v.SetMaxFileSize(newSize) if v.MaxFileSize != newSize { - t.Errorf("Expected max file size to be %d after WithMaxFileSize, got %d", newSize, v.MaxFileSize) + t.Errorf("Expected max file size to be %d after SetMaxFileSize, got %d", newSize, v.MaxFileSize) } if originalSize == v.MaxFileSize { @@ -111,7 +110,7 @@ func TestWithMaxFileSize(t *testing.T) { } } -func TestValidate(t *testing.T) { +func TestValidateFile(t *testing.T) { tests := []struct { name string validator *FileValidator @@ -123,7 +122,7 @@ func TestValidate(t *testing.T) { }{ { name: "Valid PDF file", - validator: NewValidator(WithCategories(CategoryDocument)), + validator: NewFileValidator(WithCategories(CategoryDocument)), filename: "test.pdf", contentType: "application/pdf", fileSize: 1024 * 1024, // 1MB @@ -131,7 +130,7 @@ func TestValidate(t *testing.T) { }, { name: "File too large", - validator: NewValidator(WithCategories(CategoryDocument)).WithMaxFileSize(1024 * 1024), // 1MB max + validator: NewFileValidator(WithCategories(CategoryDocument)).SetMaxFileSize(1024 * 1024), // 1MB max filename: "test.pdf", contentType: "application/pdf", fileSize: 2 * 1024 * 1024, // 2MB @@ -140,7 +139,7 @@ func TestValidate(t *testing.T) { }, { name: "Disallowed content type", - validator: NewValidator(WithCategories(CategoryDocument)), + validator: NewFileValidator(WithCategories(CategoryDocument)), filename: "test.jpg", contentType: "image/jpeg", fileSize: 1024 * 1024, @@ -149,7 +148,7 @@ func TestValidate(t *testing.T) { }, { name: "Missing file extension", - validator: NewValidator(WithCategories(CategoryDocument)), + validator: NewFileValidator(WithCategories(CategoryDocument)), filename: "testfile", contentType: "application/pdf", fileSize: 1024, @@ -158,7 +157,7 @@ func TestValidate(t *testing.T) { }, { name: "Disallowed file extension", - validator: NewValidator(WithCategories(CategoryDocument)), + validator: NewFileValidator(WithCategories(CategoryDocument)), filename: "test.exe", contentType: "application/octet-stream", fileSize: 1024, @@ -167,7 +166,7 @@ func TestValidate(t *testing.T) { }, { name: "Content type doesn't match extension", - validator: NewValidator(WithCategories(CategoryImage, CategoryDocument)), + validator: NewFileValidator(WithCategories(CategoryImage, CategoryDocument)), filename: "test.pdf", contentType: "image/jpeg", fileSize: 1024, @@ -176,7 +175,7 @@ func TestValidate(t *testing.T) { }, { name: "Valid image file", - validator: NewValidator(WithCategories(CategoryImage)), + validator: NewFileValidator(WithCategories(CategoryImage)), filename: "test.jpg", contentType: "image/jpeg", fileSize: 1024 * 1024, @@ -184,7 +183,7 @@ func TestValidate(t *testing.T) { }, { name: "Valid with multiple categories", - validator: NewValidator(WithCategories(CategoryImage, CategoryDocument)), + validator: NewFileValidator(WithCategories(CategoryImage, CategoryDocument)), filename: "test.jpg", contentType: "image/jpeg", fileSize: 1024 * 1024, @@ -213,7 +212,7 @@ func TestValidate(t *testing.T) { } } -func TestValidateEdgeCases(t *testing.T) { +func TestValidateFileEdgeCases(t *testing.T) { tests := []struct { name string validator *FileValidator @@ -225,7 +224,7 @@ func TestValidateEdgeCases(t *testing.T) { }{ { name: "Zero file size", - validator: NewValidator(WithCategories(CategoryText)), + validator: NewFileValidator(WithCategories(CategoryText)), filename: "empty.txt", contentType: "text/plain", fileSize: 0, @@ -234,7 +233,7 @@ func TestValidateEdgeCases(t *testing.T) { }, { name: "Exact max file size", - validator: NewValidator(WithCategories(CategoryText)).WithMaxFileSize(1024), + validator: NewFileValidator(WithCategories(CategoryText)).SetMaxFileSize(1024), filename: "exact.txt", contentType: "text/plain", fileSize: 1024, @@ -242,7 +241,7 @@ func TestValidateEdgeCases(t *testing.T) { }, { name: "File with uppercase extension", - validator: NewValidator(WithCategories(CategoryDocument)), + validator: NewFileValidator(WithCategories(CategoryDocument)), filename: "test.PDF", contentType: "application/pdf", fileSize: 1024, @@ -250,7 +249,7 @@ func TestValidateEdgeCases(t *testing.T) { }, { name: "File with multiple extensions", - validator: NewValidator(), + validator: NewFileValidator(), filename: "test.tar.gz", contentType: "application/gzip", fileSize: 1024, @@ -259,7 +258,7 @@ func TestValidateEdgeCases(t *testing.T) { }, { name: "Empty filename", - validator: NewValidator(WithCategories(CategoryText)), + validator: NewFileValidator(WithCategories(CategoryText)), filename: "", contentType: "text/plain", fileSize: 1024, @@ -268,7 +267,7 @@ func TestValidateEdgeCases(t *testing.T) { }, { name: "Empty content type", - validator: NewValidator(WithCategories(CategoryText)), + validator: NewFileValidator(WithCategories(CategoryText)), filename: "test.txt", contentType: "", fileSize: 1024, @@ -316,7 +315,7 @@ func TestFileCategories(t *testing.T) { for _, tc := range categoryTests { t.Run(tc.category, func(t *testing.T) { - v := NewValidator(WithCategories(tc.category)) + v := NewFileValidator(WithCategories(tc.category)) // Test that the validator accepts files of this category err := v.Validate("test"+tc.validExt, tc.validMimeType, 1024) @@ -373,16 +372,11 @@ func TestFileTypes(t *testing.T) { } } -// Helper function to check if a string contains a substring -func contains(s, substr string) bool { - return strings.Contains(s, substr) -} - func TestMultipleExtensionsPerMimeType(t *testing.T) { // Test MIME types that have multiple allowed extensions for _, fileType := range FileTypes { if len(fileType.Extensions) > 1 { - v := NewValidator(WithCategories(fileType.Category)) + v := NewFileValidator(WithCategories(fileType.Category)) // All extensions for this MIME type should be valid for _, ext := range fileType.Extensions { @@ -408,7 +402,7 @@ func TestExtensionsWithMultipleMimeTypes(t *testing.T) { // Find extensions that have multiple MIME types for ext, mimeTypes := range extToMimes { if len(mimeTypes) > 1 { - v := NewValidator( + v := NewFileValidator( WithCategories( CategoryData, CategoryDocument, @@ -432,9 +426,9 @@ func TestExtensionsWithMultipleMimeTypes(t *testing.T) { } } -// BenchmarkValidate benchmarks the Validate function -func BenchmarkValidate(b *testing.B) { - v := NewValidator(WithCategories(CategoryDocument)) +// BenchmarkValidateFile benchmarks the Validate function +func BenchmarkValidateFile(b *testing.B) { + v := NewFileValidator(WithCategories(CategoryDocument)) b.ResetTimer() for i := 0; i < b.N; i++ { @@ -442,36 +436,10 @@ func BenchmarkValidate(b *testing.B) { } } -// BenchmarkNewValidator benchmarks the NewValidator function -func BenchmarkNewValidator(b *testing.B) { +// BenchmarkNewFileValidator benchmarks the NewFileValidator function +func BenchmarkNewFileValidator(b *testing.B) { for i := 0; i < b.N; i++ { - _ = NewValidator(WithCategories(CategoryDocument, CategoryImage)) - } -} - -// TestUtilFunctions tests utility functions -func TestUtilFunctions(t *testing.T) { - // Test contains function - testCases := []struct { - str string - substr string - expected bool - }{ - {"hello world", "hello", true}, - {"hello world", "world", true}, - {"hello world", "goodbye", false}, - {"hello world", "", true}, // Empty string is always contained - {"", "hello", false}, // Empty string contains nothing except empty string - {"", "", true}, // Empty string contains empty string - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("contains(%q,%q)", tc.str, tc.substr), func(t *testing.T) { - result := contains(tc.str, tc.substr) - if result != tc.expected { - t.Errorf("contains(%q,%q) = %v; want %v", tc.str, tc.substr, result, tc.expected) - } - }) + _ = NewFileValidator(WithCategories(CategoryDocument, CategoryImage)) } } @@ -498,20 +466,23 @@ func TestValidateCustomAllowedExtensions(t *testing.T) { // Test the content type doesn't match extension path v2 := &FileValidator{ MaxFileSize: DefaultMaxFileSize, - AllowedMimeTypes: map[string]bool{"application/pdf": true, "image/jpeg": true, "image/png": true}, + AllowedMimeTypes: map[string]bool{"application/pdf": true, "image/jpeg": true}, AllowedExtensions: map[string][]string{ ".pdf": {"application/pdf"}, ".jpg": {"image/jpeg"}, - ".png": {"image/png"}, }, } - // This will trigger the content type mismatch path - err = v2.Validate("test.jpg", "image/png", 1024) + err = v2.Validate("test.pdf", "image/jpeg", 1024) if err == nil { - t.Error("Expected error for content type not matching extension, got none") + t.Error("Expected error for content type/extension mismatch, got none") } - if !contains(err.Error(), "content type \"image/png\" does not match extension \".jpg\"") { + if !contains(err.Error(), "does not match extension") { t.Errorf("Unexpected error message: %s", err.Error()) } } + +// Helper function to check if a string contains a substring +func contains(s, substr string) bool { + return strings.Contains(s, substr) +}