diff --git a/docs/diagrams/use-case-overview.mmd b/docs/diagrams/use-case-overview.mmd index 096511f..dda7da2 100644 --- a/docs/diagrams/use-case-overview.mmd +++ b/docs/diagrams/use-case-overview.mmd @@ -7,57 +7,59 @@ Grantee["Grantee"] %% Use Cases Grouped by Domain subgraph Blob_Management ["Blob Management"] - UC1["Manage All Blobs"] - UC2["Upload Own Blobs"] - UC3["Delete Own Blobs"] - UC4["Download Blob with Permission"] - UC5["View Blob with Permission"] + UC1["UC1: Manage All Blobs"] + UC2["UC2: Upload Own Blobs"] + UC3["UC3: Delete Own Blobs"] + UC4["UC4: Download Blob with Permission"] + UC5["UC5: View Blob with Permission"] + UC6["UC6: Manage Blob Metadata"] + UC7["UC7: Manage Own Blob Metadata"] + UC8["UC8: View Public Blob Metadata"] end subgraph Key_Management ["Key Management"] - UC6["Manage Cryptographic Keys"] - UC7["Manage Own Cryptographic Keys"] -end - -subgraph Metadata_Management ["Metadata Management"] - UC8["Manage Metadata"] - UC9["Manage Own Metadata"] - UC10["View public Metadata"] + UC9["UC9: Manage Cryptographic Keys"] + UC10["UC10: Manage Own Cryptographic Keys"] + UC11["UC11: Manage Key Metadata"] + UC12["UC12: Manage Own Key Metadata"] + UC13["UC13: View Public Key Metadata"] end subgraph Key_Operations ["Key Operations"] - UC11["Encrypt Own Blobs"] - UC12["Decrypt Own Blobs"] - UC13["Hash Own Blobs"] - UC14["Verify Blob Signature"] + UC14["UC14: Encrypt Own Blobs"] + UC15["UC15: Decrypt Own Blobs"] + UC16["UC16: Hash Own Blobs"] + UC17["UC17: Verify Blob Signature"] end subgraph Permission_Management ["Permission Management"] - UC15["Grant Download Permission"] - UC16["Grant View Permission"] + UC18["UC18: Grant Download Permission"] + UC19["UC19: Grant View Permission"] end %% Actor -> Use Cases Admin --> UC1 -Admin --> UC6 -Admin --> UC7 +Admin --> UC9 +Admin --> UC10 +Admin --> UC11 Owner --> UC2 Owner --> UC3 Owner --> UC4 +Owner --> UC5 +Owner --> UC6 +Owner --> UC7 Owner --> UC8 -Owner --> UC9 -Owner --> UC10 -Owner --> UC11 -Owner --> UC12 -Owner --> UC13 Owner --> UC14 Owner --> UC15 Owner --> UC16 +Owner --> UC17 +Owner --> UC18 +Owner --> UC19 Grantee --> UC4 Grantee --> UC5 -Grantee --> UC12 +Grantee --> UC15 %% Class definitions for actors classDef actor fill:#ADD8E6,stroke:#333,stroke-width:2px; diff --git a/internal/app/.gitkeep b/internal/app/.gitkeep deleted file mode 100644 index 89eb1f3..0000000 --- a/internal/app/.gitkeep +++ /dev/null @@ -1 +0,0 @@ -Service impl \ No newline at end of file diff --git a/internal/app/services/blob.go b/internal/app/services/blob.go new file mode 100644 index 0000000..90a51e1 --- /dev/null +++ b/internal/app/services/blob.go @@ -0,0 +1,8 @@ +package services + +type BlobUploadService struct { +} +type BlobMetadataService struct { +} +type BlobDownloadService struct { +} diff --git a/internal/app/services/key.go b/internal/app/services/key.go new file mode 100644 index 0000000..edaca67 --- /dev/null +++ b/internal/app/services/key.go @@ -0,0 +1,10 @@ +package services + +type CryptKeyUploadService struct { +} +type CryptoKeyMetadataService struct { +} +type CryptoKeyDownloadService struct { +} +type CryptoKeyOperationService struct { +} diff --git a/internal/domain/blobs/contract.go b/internal/domain/blobs/contract.go index f40b2ef..d24a792 100644 --- a/internal/domain/blobs/contract.go +++ b/internal/domain/blobs/contract.go @@ -1,28 +1,30 @@ package blobs -// BlobManagement defines methods for managing blob operations. -type BlobManagement interface { - // Upload handles the upload of blobs from file paths. - // Returns the created Blobs metadata and any error encountered. - Upload(filePath []string) ([]*Blob, error) +// BlobUploadService defines methods for uploading blobs. +type BlobUploadService interface { + // Upload handles the upload of blobs from the specified file paths. + // It returns a slice of Blob for the uploaded blobs and any error encountered during the upload process. + Upload(filePaths []string) ([]*BlobMeta, error) +} - // Download retrieves a blob by its ID and name, returning the metadata and file data. - // Returns the Blob metadata, file data as a byte slice, and any error. - Download(blobId, blobName string) (*Blob, []byte, error) +// BlobMetadataService defines methods for retrieving Blob and deleting a blob along with its metadata. +type BlobMetadataService interface { + // List retrieves all blobs' metadata considering a query filter when set. + // It returns a slice of Blob and any error encountered during the retrieval. + List(query *BlobMetaQuery) ([]*BlobMeta, error) - // DeleteByID removes a blob by its ID. - // Returns any error encountered. - DeleteByID(blobId string) error -} + // GetByID retrieves the metadata of a blob by its unique ID. + // It returns the Blob and any error encountered during the retrieval process. + GetByID(blobID string) (*BlobMeta, error) -// BlobMetadataManagement defines the methods for managing Blob metadata -type BlobMetadataManagement interface { - // Create creates a new blob - Create(blob *Blob) (*Blob, error) - // GetByID retrieves blob by ID - GetByID(blobID string) (*Blob, error) - // UpdateByID updates a blob's metadata - UpdateByID(blobID string, updates *Blob) (*Blob, error) - // DeleteByID deletes a blob by ID + // DeleteByID deletes a blob and its associated metadata by ID. + // It returns any error encountered during the deletion process. DeleteByID(blobID string) error } + +// BlobDownloadService defines methods for downloading blobs. +type BlobDownloadService interface { + // Download retrieves a blob by its ID and name. + // It returns the Blob, the file data as a byte slice, and any error encountered during the download process. + Download(blobID, blobName string) (*BlobMeta, []byte, error) +} diff --git a/internal/domain/blobs/model.go b/internal/domain/blobs/model.go index 81f05ba..ec9ca70 100644 --- a/internal/domain/blobs/model.go +++ b/internal/domain/blobs/model.go @@ -8,24 +8,24 @@ import ( "github.com/go-playground/validator/v10" ) -// Blob represents metadata on the actual blob being stored -type Blob struct { - ID string `gorm:"primaryKey" validate:"required,uuid4"` // ID is required and must be a valid UUID - UploadTime time.Time `validate:"required"` // UploadTime is required - UserID string `validate:"required,uuid4"` // UserID is required and must be a valid UUID - Name string `validate:"required,min=1,max=255"` // Name is required, and its length must be between 1 and 255 characters - Size int64 `validate:"required,min=1"` // Size must be greater than 0 - Type string `validate:"required,min=1,max=50"` // Type is required, and its length must be between 1 and 50 characters - EncryptionAlgorithm string `validate:"omitempty,oneof=AES RSA ECDSA"` // EncryptionAlgorithm is optional and must be one of the listed algorithms - HashAlgorithm string `validate:"omitempty,oneof=SHA256 SHA512 MD5"` // HashAlgorithm is optional and must be one of the listed algorithms - IsEncrypted bool `validate:"-"` // IsEncrypted is required (true/false) - IsSigned bool `validate:"-"` // IsSigned is required (true/false) - CryptographicKey keys.CryptographicKey `gorm:"foreignKey:KeyID" validate:"required"` // CryptographicKey is required - KeyID string `validate:"omitempty,uuid4"` // KeyID is optional and must be a valid UUID +// BlobMeta represents metadata on the actual blob metadata being stored +type BlobMeta struct { + ID string `gorm:"primaryKey" validate:"required,uuid4"` // ID is required and must be a valid UUID + UploadTime time.Time `validate:"required"` // UploadTime is required + UserID string `validate:"required,uuid4"` // UserID is required and must be a valid UUID + Name string `validate:"required,min=1,max=255"` // Name is required, and its length must be between 1 and 255 characters + Size int64 `validate:"required,min=1"` // Size must be greater than 0 + Type string `validate:"required,min=1,max=50"` // Type is required, and its length must be between 1 and 50 characters + EncryptionAlgorithm string `validate:"omitempty,oneof=AES RSA ECDSA"` // EncryptionAlgorithm is optional and must be one of the listed algorithms + HashAlgorithm string `validate:"omitempty,oneof=SHA256 SHA512 MD5"` // HashAlgorithm is optional and must be one of the listed algorithms + IsEncrypted bool `validate:"-"` // IsEncrypted is required (true/false) + IsSigned bool `validate:"-"` // IsSigned is required (true/false) + CryptoKey keys.CryptoKeyMeta `gorm:"foreignKey:KeyID" validate:"required"` // CryptoKey is required + KeyID string `validate:"omitempty,uuid4"` // KeyID is optional and must be a valid UUID } -// Validate for validating Blob struct -func (b *Blob) Validate() error { +// Validate for validating BlobMeta struct +func (b *BlobMeta) Validate() error { // Initialize the validator validate := validator.New() diff --git a/internal/domain/blobs/query.go b/internal/domain/blobs/query.go new file mode 100644 index 0000000..8677c87 --- /dev/null +++ b/internal/domain/blobs/query.go @@ -0,0 +1,59 @@ +package blobs + +import ( + "fmt" + "time" + + "github.com/go-playground/validator/v10" +) + +// BlobMetaQuery represents metadata on the actual blob being stored. +type BlobMetaQuery struct { + UploadTime time.Time `validate:"required"` // UploadTime is required + Name string `validate:"required,min=1,max=255"` // Name is required and its length must be between 1 and 255 characters + Size int64 `validate:"required,min=1"` // Size must be greater than 0 + Type string `validate:"required,min=1,max=50"` // Type is required, and its length must be between 1 and 50 characters + + // Pagination properties + Limit int `validate:"omitempty,min=1"` // Limit is optional but if provided, should be at least 1 + Offset int `validate:"omitempty,min=0"` // Offset is optional but should be 0 or greater for pagination + + // Sorting properties + SortBy string `validate:"omitempty,oneof=ID Type CreatedAt ExpiresAt"` // SortBy is optional but can be one of the fields to sort by + SortOrder string `validate:"omitempty,oneof=asc desc"` // SortOrder is optional, default is ascending ('asc'), can also be 'desc' +} + +// NewBlobMetaQuery creates a BlobMetaQuery with default values. +func NewBlobMetaQuery() *BlobMetaQuery { + return &BlobMetaQuery{ + Limit: 10, // Default limit to 10 results per page + Offset: 0, // Default offset to 0 for pagination + SortBy: "CreatedAt", // Default sort by CreatedAt + SortOrder: "asc", // Default sort order ascending + } +} + +// Validate validates the BlobMetaQuery struct based on the defined rules. +func (b *BlobMetaQuery) Validate() error { + // Initialize the validator + validate := validator.New() + + // Validate the struct fields + err := validate.Struct(b) + if err != nil { + // Collect all validation errors + var validationErrors []string + for _, err := range err.(validator.ValidationErrors) { + validationErrors = append(validationErrors, fmt.Sprintf("Field: %s, Tag: %s", err.Field(), err.Tag())) + } + return fmt.Errorf("Validation failed: %v", validationErrors) + } + + // Custom validation logic: Check that ExpiresAt (if exists) is after CreatedAt + if !b.UploadTime.IsZero() && b.UploadTime.After(time.Now()) { + return fmt.Errorf("UploadTime cannot be in the future") + } + + // Return nil if no validation errors are found + return nil +} diff --git a/internal/domain/keys/contract.go b/internal/domain/keys/contract.go index 4a2b473..ac5db35 100644 --- a/internal/domain/keys/contract.go +++ b/internal/domain/keys/contract.go @@ -1,84 +1,108 @@ package keys -// Define KeyType as a custom type (based on int) +// KeyType defines a custom type for key types based on an integer. type KeyType int -// Enum-like values using iota +// Enum-like values for different key types, using iota to generate sequential values. const ( - AsymmetricPublic KeyType = iota - AsymmetricPrivate - Symmetric + AsymmetricPublic KeyType = iota // Public key in asymmetric cryptography (e.g., RSA, ECDSA) + AsymmetricPrivate // Private key in asymmetric cryptography (e.g., RSA, ECDSA) + Symmetric // Symmetric key (e.g., AES) ) -// KeyManagement defines methods for managing cryptographic key operations. -type KeyManagement interface { - // Upload handles the upload of blobs from file paths. - // Returns the created Blobs metadata and any error encountered. - Upload(filePath []string) ([]*CryptographicKey, error) +// CryptKeyUploadService defines methods for uploading cryptographic keys. +type CryptKeyUploadService interface { + // Upload uploads cryptographic keys from specified file paths. + // It returns a slice of CryptoKeyMeta and any error encountered during the upload process. + Upload(filePaths []string) ([]*CryptoKeyMeta, error) +} - // Download retrieves a cryptographic key by its ID and key type, returning the metadata and key data. - // Returns the key metadata, key data as a byte slice, and any error. - Download(keyId string, keyType KeyType) (*CryptographicKey, []byte, error) +// CryptoKeyMetadataService defines methods for managing cryptographic key metadata and deleting keys. +type CryptoKeyMetadataService interface { + // List retrieves all cryptographic keys metadata considering a query filter when set. + // It returns a slice of CryptoKeyMeta and any error encountered during the retrieval process. + List(query *CryptoKeyQuery) ([]*CryptoKeyMeta, error) - // DeleteByID removes a cryptographic key by its ID. - // Returns any error encountered. - DeleteByID(keyId string) error -} + // GetByID retrieves the metadata of a cryptographic key by its unique ID. + // It returns the CryptoKeyMeta and any error encountered during the retrieval process. + GetByID(keyID string) (*CryptoKeyMeta, error) -// CryptographicKeyMetadataManagement defines the methods for managing CryptographicKey metadata -type CryptographicKeyMetadataManagement interface { - // Create creates a new cryptographic key - Create(key *CryptographicKey) (*CryptographicKey, error) - // GetByID retrieves cryptographic key by ID - GetByID(keyID string) (*CryptographicKey, error) - // UpdateByID updates cryptographic key metadata - UpdateByID(keyID string, updates *CryptographicKey) (*CryptographicKey, error) - // DeleteByID deletes a cryptographic key by ID + // DeleteByID deletes a cryptographic key and its associated metadata by ID. + // It returns any error encountered during the deletion process. DeleteByID(keyID string) error } -// KeyOperations defines methods for key management, encryption, signing, and PKCS#11 operations. -type KeyOperations interface { +// CryptoKeyDownloadService defines methods for downloading cryptographic keys. +type CryptoKeyDownloadService interface { + // Download retrieves a cryptographic key by its ID and type. + // It returns the CryptoKeyMeta, the key data as a byte slice, and any error encountered during the download process. + Download(keyID string, keyType KeyType) (*CryptoKeyMeta, []byte, error) +} - // ---Key generation--- +// KeyOperations defines methods for cryptographic key management, encryption, signing, and PKCS#11 operations. +type CryptoKeyOperationService interface { - // GenerateKey generates keys for specified type and size (e.g., AES, RSA, ECDSA) + // --- Key Generation --- + + // GenerateKey generates a cryptographic key of the specified type and size (e.g., AES, RSA, ECDSA). + // It returns the generated key as a byte slice and any error encountered during the key generation. GenerateKey(keyType string, keySize int) ([]byte, error) - // ---Key storage and retrieval--- + // --- Key Storage and Retrieval --- - // SaveKey saves a key to a file + // SaveKey saves a cryptographic key to a specified file. + // It returns any error encountered during the saving process. SaveKey(key []byte, filename string) error - // LoadKey loads a key from a file + + // LoadKey loads a cryptographic key from a specified file. + // It returns the loaded key as a byte slice and any error encountered during the loading process. LoadKey(filename string) ([]byte, error) - // ---Encryption and Decryption (Symmetric algorithms like AES)--- + // --- Encryption and Decryption (Symmetric algorithms like AES) --- - // EncryptWithSymmetricKey encrypts data with symmetric keys (e.g. AES) + // EncryptWithSymmetricKey encrypts data using a symmetric key (e.g., AES). + // It returns the encrypted data as a byte slice and any error encountered during encryption. EncryptWithSymmetricKey(plainText []byte, key []byte) ([]byte, error) - // DecryptWithSymmetricKey decrypts data with symmetric keys (e.g. AES) + + // DecryptWithSymmetricKey decrypts data using a symmetric key (e.g., AES). + // It returns the decrypted data as a byte slice and any error encountered during decryption. DecryptWithSymmetricKey(cipherText []byte, key []byte) ([]byte, error) - // ---Asymmetric Encryption (RSA, ECDSA, PKCS#11)--- + // --- Asymmetric Encryption (RSA, ECDSA, PKCS#11) --- - // EncryptWithPublicKey encrypts with public key using asymmetric algorithms (RSA, ECDSA) and optionally a PKCS#11 interface + // EncryptWithPublicKey encrypts data with a public key using asymmetric encryption algorithms (e.g., RSA, ECDSA). + // It optionally supports PKCS#11 hardware tokens for key storage. + // It returns the encrypted data as a byte slice and any error encountered during encryption. EncryptWithPublicKey(plainText []byte, publicKey interface{}) ([]byte, error) - // DecryptWithPrivateKey decrypt with private key using asymmetric algorithms (RSA, ECDSA) and optionally a PKCS#11 interface + + // DecryptWithPrivateKey decrypts data with a private key using asymmetric encryption algorithms (e.g., RSA, ECDSA). + // It optionally supports PKCS#11 hardware tokens for key storage. + // It returns the decrypted data as a byte slice and any error encountered during decryption. DecryptWithPrivateKey(cipherText []byte, privateKey interface{}) ([]byte, error) - // ---Signing and Verification (For RSA, ECDSA)--- + // --- Signing and Verification (For RSA, ECDSA) --- - // SignWithPrivateKey signs message with private key using asymmetric algorithms (RSA, ECDSA) and optionally a PKCS#11 interface + // SignWithPrivateKey signs a message using a private key with asymmetric algorithms (e.g., RSA, ECDSA). + // It optionally supports PKCS#11 hardware tokens for key storage. + // It returns the signature and any error encountered during the signing process. SignWithPrivateKey(message []byte, privateKey interface{}) ([]byte, error) - // VerifyWithPublicKey verifies signatures with public key using asymmetric algorithms (RSA, ECDSA) and optionally a PKCS#11 interface + + // VerifyWithPublicKey verifies a signature using a public key with asymmetric algorithms (e.g., RSA, ECDSA). + // It optionally supports PKCS#11 hardware tokens for key storage. + // It returns true if the signature is valid, false otherwise, and any error encountered during the verification process. VerifyWithPublicKey(message []byte, signature []byte, publicKey interface{}) (bool, error) - // ---PKCS#11 Operations--- + // --- PKCS#11 Operations --- - // InitializeToken initializes PKCS#11 token in the specified slot + // InitializeToken initializes a PKCS#11 token in the specified hardware slot. + // It returns any error encountered during the initialization. InitializeToken(slot string) error - // AddKeyToToken adds key to the PKCS#11 token + + // AddKeyToToken adds a cryptographic key to a PKCS#11 token. + // It returns any error encountered during the addition of the key. AddKeyToToken() error - // DeleteKeyFromToken deletes key from PKCS#11 token by type and label + + // DeleteKeyFromToken deletes a cryptographic key from a PKCS#11 token by its type and label. + // It returns any error encountered during the deletion of the key. DeleteKeyFromToken(objectType, objectLabel string) error } diff --git a/internal/domain/keys/model.go b/internal/domain/keys/model.go index 6b5c6d3..03a4433 100644 --- a/internal/domain/keys/model.go +++ b/internal/domain/keys/model.go @@ -7,8 +7,8 @@ import ( "github.com/go-playground/validator/v10" ) -// CryptographicKey represents the encryption key entity -type CryptographicKey struct { +// CryptoKeyMeta represents the encryption key entity +type CryptoKeyMeta struct { ID string `gorm:"primaryKey" validate:"required,uuid4"` // ID is required and must be a valid UUID Type string `validate:"required,oneof=AES RSA ECDSA"` // Type is required and must be one of the listed types CreatedAt time.Time `validate:"required"` // CreatedAt is required @@ -16,8 +16,8 @@ type CryptographicKey struct { UserID string `gorm:"index" validate:"required,uuid4"` // UserID is required and must be a valid UUID } -// Validate for validating CryptographicKey struct -func (k *CryptographicKey) Validate() error { +// Validate for validating CryptoKeyMeta struct +func (k *CryptoKeyMeta) Validate() error { // Initialize the validator validate := validator.New() diff --git a/internal/domain/keys/query.go b/internal/domain/keys/query.go new file mode 100644 index 0000000..eb4067e --- /dev/null +++ b/internal/domain/keys/query.go @@ -0,0 +1,51 @@ +package keys + +import ( + "fmt" + "time" + + "github.com/go-playground/validator/v10" +) + +// CryptoKeyQuery represents the parameters used to query encryption keys. +type CryptoKeyQuery struct { + Type string `validate:"omitempty,oneof=AES RSA ECDSA"` // Type is optional but if provided, must be one of the listed types (AES, RSA, ECDSA) + CreatedAt time.Time `validate:"omitempty,gtefield=CreatedAt"` // CreatedAt is optional, but can be used for filtering + ExpiresAt time.Time `validate:"omitempty,gtefield=CreatedAt"` // ExpiresAt is optional, but can be used for filtering + + // Pagination properties + Limit int `validate:"omitempty,min=1"` // Limit is optional but if provided, should be at least 1 + Offset int `validate:"omitempty,min=0"` // Offset is optional but should be 0 or greater for pagination + + // Sorting properties + SortBy string `validate:"omitempty,oneof=ID Type CreatedAt ExpiresAt"` // SortBy is optional but can be one of the fields to sort by + SortOrder string `validate:"omitempty,oneof=asc desc"` // SortOrder is optional, default is ascending ('asc'), can also be 'desc' +} + +// New function to create a CryptoKeyQuery with default values +func NewCryptoKeyQuery() *CryptoKeyQuery { + return &CryptoKeyQuery{ + Limit: 10, // Default limit to 10 results per page + Offset: 0, // Default offset to 0 for pagination + SortBy: "CreatedAt", // Default sort by CreatedAt + SortOrder: "asc", // Default sort order ascending + } +} + +// Validate for validating CryptoKeyQuery struct +func (k *CryptoKeyQuery) Validate() error { + // Initialize the validator + validate := validator.New() + + // Validate the struct + err := validate.Struct(k) + if err != nil { + // If validation fails, return a formatted error + var validationErrors []string + for _, err := range err.(validator.ValidationErrors) { + validationErrors = append(validationErrors, fmt.Sprintf("Field: %s, Tag: %s", err.Field(), err.Tag())) + } + return fmt.Errorf("Validation failed: %v", validationErrors) + } + return nil // Return nil if validation passes +} diff --git a/internal/infrastructure/connector/az_blob.go b/internal/infrastructure/connector/az_blob.go index 5e77cba..9e66af4 100644 --- a/internal/infrastructure/connector/az_blob.go +++ b/internal/infrastructure/connector/az_blob.go @@ -17,7 +17,7 @@ import ( // AzureBlobConnector is an interface for interacting with Azure Blob storage type AzureBlobConnector interface { // Upload uploads multiple files to Azure Blob Storage and returns their metadata. - Upload(filePaths []string) ([]*blobs.Blob, error) + Upload(filePaths []string) ([]*blobs.BlobMeta, error) // Download retrieves a blob's content by its ID and name, and returns the data as a stream. Download(blobId, blobName string) (*bytes.Buffer, error) // Delete deletes a blob from Azure Blob Storage by its ID and Name, and returns any error encountered. @@ -50,8 +50,8 @@ func NewAzureBlobConnector(connectionString string, containerName string) (*Azur } // Upload uploads multiple files to Azure Blob Storage and returns their metadata. -func (abc *AzureBlobConnectorImpl) Upload(filePaths []string) ([]*blobs.Blob, error) { - var uploadedBlobs []*blobs.Blob +func (abc *AzureBlobConnectorImpl) Upload(filePaths []string) ([]*blobs.BlobMeta, error) { + var uploadedBlobs []*blobs.BlobMeta blobID := uuid.New().String() // Iterate through all file paths and upload each file @@ -87,7 +87,7 @@ func (abc *AzureBlobConnectorImpl) Upload(filePaths []string) ([]*blobs.Blob, er fileExt := filepath.Ext(fileInfo.Name()) // Gets the file extension (e.g. ".txt", ".jpg") // Create a Blob object for metadata (Fill in missing fields) - blob := &blobs.Blob{ + blob := &blobs.BlobMeta{ ID: blobID, Name: fileInfo.Name(), Size: fileInfo.Size(), @@ -118,7 +118,7 @@ func (abc *AzureBlobConnectorImpl) Upload(filePaths []string) ([]*blobs.Blob, er } // rollbackUploadedBlobs deletes the blobs that were uploaded successfully before the error occurred -func (abc *AzureBlobConnectorImpl) rollbackUploadedBlobs(blobs []*blobs.Blob) { +func (abc *AzureBlobConnectorImpl) rollbackUploadedBlobs(blobs []*blobs.BlobMeta) { for _, blob := range blobs { err := abc.Delete(blob.ID, blob.Name) if err != nil { diff --git a/internal/persistence/repository/blob_repository.go b/internal/persistence/repository/blob_repository.go index 76e55b0..0ad1a1c 100644 --- a/internal/persistence/repository/blob_repository.go +++ b/internal/persistence/repository/blob_repository.go @@ -9,9 +9,9 @@ import ( // BlobRepository defines the interface for Blob-related operations type BlobRepository interface { - Create(blob *blobs.Blob) error - GetById(blobID string) (*blobs.Blob, error) - UpdateById(blob *blobs.Blob) error + Create(blob *blobs.BlobMeta) error + GetById(blobID string) (*blobs.BlobMeta, error) + UpdateById(blob *blobs.BlobMeta) error DeleteById(blobID string) error } @@ -21,7 +21,7 @@ type BlobRepositoryImpl struct { } // Create adds a new Blob to the database -func (r *BlobRepositoryImpl) Create(blob *blobs.Blob) error { +func (r *BlobRepositoryImpl) Create(blob *blobs.BlobMeta) error { // Validate the Blob before saving if err := blob.Validate(); err != nil { return fmt.Errorf("validation error: %v", err) @@ -35,8 +35,8 @@ func (r *BlobRepositoryImpl) Create(blob *blobs.Blob) error { } // GetById retrieves a Blob by its ID from the database -func (r *BlobRepositoryImpl) GetById(blobID string) (*blobs.Blob, error) { - var blob blobs.Blob +func (r *BlobRepositoryImpl) GetById(blobID string) (*blobs.BlobMeta, error) { + var blob blobs.BlobMeta if err := r.DB.Where("id = ?", blobID).First(&blob).Error; err != nil { if err == gorm.ErrRecordNotFound { return nil, fmt.Errorf("blob with ID %s not found", blobID) @@ -47,7 +47,7 @@ func (r *BlobRepositoryImpl) GetById(blobID string) (*blobs.Blob, error) { } // UpdateById updates an existing Blob in the database -func (r *BlobRepositoryImpl) UpdateById(blob *blobs.Blob) error { +func (r *BlobRepositoryImpl) UpdateById(blob *blobs.BlobMeta) error { // Validate the Blob before updating if err := blob.Validate(); err != nil { return fmt.Errorf("validation error: %v", err) @@ -62,7 +62,7 @@ func (r *BlobRepositoryImpl) UpdateById(blob *blobs.Blob) error { // DeleteById removes a Blob from the database by its ID func (r *BlobRepositoryImpl) DeleteById(blobID string) error { - if err := r.DB.Where("id = ?", blobID).Delete(&blobs.Blob{}).Error; err != nil { + if err := r.DB.Where("id = ?", blobID).Delete(&blobs.BlobMeta{}).Error; err != nil { return fmt.Errorf("failed to delete blob: %w", err) } return nil diff --git a/internal/persistence/repository/key_repository.go b/internal/persistence/repository/key_repository.go index ad85254..41a25e3 100644 --- a/internal/persistence/repository/key_repository.go +++ b/internal/persistence/repository/key_repository.go @@ -7,22 +7,22 @@ import ( "gorm.io/gorm" ) -// CryptographicKeyRepository defines the interface for CryptographicKey-related operations -type CryptographicKeyRepository interface { - Create(key *keys.CryptographicKey) error - GetByID(keyID string) (*keys.CryptographicKey, error) - UpdateByID(key *keys.CryptographicKey) error +// CryptoKeyRepository defines the interface for CryptoKey-related operations +type CryptoKeyRepository interface { + Create(key *keys.CryptoKeyMeta) error + GetByID(keyID string) (*keys.CryptoKeyMeta, error) + UpdateByID(key *keys.CryptoKeyMeta) error DeleteByID(keyID string) error } -// CryptographicKeyRepositoryImpl is the implementation of the CryptographicKeyRepository interface -type CryptographicKeyRepositoryImpl struct { +// CryptoKeyRepositoryImpl is the implementation of the CryptoKeyRepository interface +type CryptoKeyRepositoryImpl struct { DB *gorm.DB } -// Create adds a new CryptographicKey to the database -func (r *CryptographicKeyRepositoryImpl) Create(key *keys.CryptographicKey) error { - // Validate the CryptographicKey before saving +// Create adds a new CryptoKey to the database +func (r *CryptoKeyRepositoryImpl) Create(key *keys.CryptoKeyMeta) error { + // Validate the CryptoKey before saving if err := key.Validate(); err != nil { return fmt.Errorf("validation error: %v", err) } @@ -34,9 +34,9 @@ func (r *CryptographicKeyRepositoryImpl) Create(key *keys.CryptographicKey) erro return nil } -// GetByID retrieves a CryptographicKey by its ID from the database -func (r *CryptographicKeyRepositoryImpl) GetByID(keyID string) (*keys.CryptographicKey, error) { - var key keys.CryptographicKey +// GetByID retrieves a CryptoKey by its ID from the database +func (r *CryptoKeyRepositoryImpl) GetByID(keyID string) (*keys.CryptoKeyMeta, error) { + var key keys.CryptoKeyMeta if err := r.DB.Where("id = ?", keyID).First(&key).Error; err != nil { if err == gorm.ErrRecordNotFound { return nil, fmt.Errorf("cryptographic key with ID %s not found", keyID) @@ -46,9 +46,9 @@ func (r *CryptographicKeyRepositoryImpl) GetByID(keyID string) (*keys.Cryptograp return &key, nil } -// UpdateByID updates an existing CryptographicKey in the database -func (r *CryptographicKeyRepositoryImpl) UpdateByID(key *keys.CryptographicKey) error { - // Validate the CryptographicKey before updating +// UpdateByID updates an existing CryptoKey in the database +func (r *CryptoKeyRepositoryImpl) UpdateByID(key *keys.CryptoKeyMeta) error { + // Validate the CryptoKey before updating if err := key.Validate(); err != nil { return fmt.Errorf("validation error: %v", err) } @@ -60,9 +60,9 @@ func (r *CryptographicKeyRepositoryImpl) UpdateByID(key *keys.CryptographicKey) return nil } -// DeleteByID removes a CryptographicKey from the database by its ID -func (r *CryptographicKeyRepositoryImpl) DeleteByID(keyID string) error { - if err := r.DB.Where("id = ?", keyID).Delete(&keys.CryptographicKey{}).Error; err != nil { +// DeleteByID removes a CryptoKey from the database by its ID +func (r *CryptoKeyRepositoryImpl) DeleteByID(keyID string) error { + if err := r.DB.Where("id = ?", keyID).Delete(&keys.CryptoKeyMeta{}).Error; err != nil { return fmt.Errorf("failed to delete cryptographic key: %w", err) } return nil diff --git a/test/integration/persistence/blob_repository_in_memory_test.go b/test/integration/persistence/blob_repository_in_memory_test.go index cbb5b05..85a82e3 100644 --- a/test/integration/persistence/blob_repository_in_memory_test.go +++ b/test/integration/persistence/blob_repository_in_memory_test.go @@ -17,8 +17,8 @@ func TestBlobRepository_Create(t *testing.T) { ctx := setupTestDB(t) defer teardownTestDB(t, ctx) - // Create a valid CryptographicKey object - cryptographicKey := keys.CryptographicKey{ + // Create a valid CryptoKey object + cryptographicKey := keys.CryptoKeyMeta{ ID: uuid.New().String(), // Generate valid UUID for ID Type: "AES", // Example key type CreatedAt: time.Now(), // Valid CreatedAt time @@ -27,17 +27,17 @@ func TestBlobRepository_Create(t *testing.T) { } // Create a test Blob object with valid UUIDs and required fields - blob := &blobs.Blob{ - ID: uuid.New().String(), // Generate valid UUID - UploadTime: time.Now(), - UserID: uuid.New().String(), // Generate valid UUID for UserID - Name: "test-blob", - Size: 1024, - Type: "text", - IsEncrypted: false, - IsSigned: true, - CryptographicKey: cryptographicKey, // Set the CryptographicKey - KeyID: cryptographicKey.ID, // Ensure ID is set + blob := &blobs.BlobMeta{ + ID: uuid.New().String(), // Generate valid UUID + UploadTime: time.Now(), + UserID: uuid.New().String(), // Generate valid UUID for UserID + Name: "test-blob", + Size: 1024, + Type: "text", + IsEncrypted: false, + IsSigned: true, + CryptoKey: cryptographicKey, // Set the CryptoKey + KeyID: cryptographicKey.ID, // Ensure ID is set } // Call the Create method @@ -45,7 +45,7 @@ func TestBlobRepository_Create(t *testing.T) { assert.NoError(t, err, "Create should not return an error") // Verify the blob is created and exists in DB - var createdBlob blobs.Blob + var createdBlob blobs.BlobMeta err = ctx.DB.First(&createdBlob, "id = ?", blob.ID).Error assert.NoError(t, err, "Failed to find created blob") assert.Equal(t, blob.ID, createdBlob.ID, "ID should match") @@ -58,8 +58,8 @@ func TestBlobRepository_GetById(t *testing.T) { ctx := setupTestDB(t) defer teardownTestDB(t, ctx) - // Create a valid CryptographicKey object - cryptographicKey := keys.CryptographicKey{ + // Create a valid CryptoKey object + cryptographicKey := keys.CryptoKeyMeta{ ID: uuid.New().String(), // Generate valid UUID for ID Type: "AES", // Example key type CreatedAt: time.Now(), // Valid CreatedAt time @@ -68,17 +68,17 @@ func TestBlobRepository_GetById(t *testing.T) { } // Create a test Blob object with valid UUIDs and required fields - blob := &blobs.Blob{ - ID: uuid.New().String(), // Generate valid UUID - UploadTime: time.Now(), - UserID: cryptographicKey.UserID, // Link to valid UserID from CryptographicKey - Name: "test-blob", - Size: 1024, - Type: "text", - IsEncrypted: false, - IsSigned: true, - CryptographicKey: cryptographicKey, // Set the CryptographicKey - KeyID: cryptographicKey.ID, // Ensure ID is set + blob := &blobs.BlobMeta{ + ID: uuid.New().String(), // Generate valid UUID + UploadTime: time.Now(), + UserID: cryptographicKey.UserID, // Link to valid UserID from CryptoKey + Name: "test-blob", + Size: 1024, + Type: "text", + IsEncrypted: false, + IsSigned: true, + CryptoKey: cryptographicKey, // Set the CryptoKey + KeyID: cryptographicKey.ID, // Ensure ID is set } // Create the blob in DB @@ -98,8 +98,8 @@ func TestBlobRepository_UpdateById(t *testing.T) { ctx := setupTestDB(t) defer teardownTestDB(t, ctx) - // Create a valid CryptographicKey object - cryptographicKey := keys.CryptographicKey{ + // Create a valid CryptoKey object + cryptographicKey := keys.CryptoKeyMeta{ ID: uuid.New().String(), // Generate valid UUID for ID Type: "AES", // Example key type CreatedAt: time.Now(), // Valid CreatedAt time @@ -108,17 +108,17 @@ func TestBlobRepository_UpdateById(t *testing.T) { } // Create a test Blob object with valid UUIDs and required fields - blob := &blobs.Blob{ - ID: uuid.New().String(), // Generate valid UUID - UploadTime: time.Now(), - UserID: uuid.New().String(), // Generate valid UUID for UserID - Name: "test-blob", - Size: 1024, - Type: "text", - IsEncrypted: false, - IsSigned: true, - CryptographicKey: cryptographicKey, // Set the CryptographicKey - KeyID: cryptographicKey.ID, // Ensure ID is set + blob := &blobs.BlobMeta{ + ID: uuid.New().String(), // Generate valid UUID + UploadTime: time.Now(), + UserID: uuid.New().String(), // Generate valid UUID for UserID + Name: "test-blob", + Size: 1024, + Type: "text", + IsEncrypted: false, + IsSigned: true, + CryptoKey: cryptographicKey, // Set the CryptoKey + KeyID: cryptographicKey.ID, // Ensure ID is set } // Create the blob in DB @@ -131,7 +131,7 @@ func TestBlobRepository_UpdateById(t *testing.T) { assert.NoError(t, err) // Verify the blob is updated - var updatedBlob blobs.Blob + var updatedBlob blobs.BlobMeta err = ctx.DB.First(&updatedBlob, "id = ?", blob.ID).Error assert.NoError(t, err) assert.Equal(t, "updated-blob-name", updatedBlob.Name, "Name should be updated") @@ -143,8 +143,8 @@ func TestBlobRepository_DeleteById(t *testing.T) { ctx := setupTestDB(t) defer teardownTestDB(t, ctx) - // Create a valid CryptographicKey object - cryptographicKey := keys.CryptographicKey{ + // Create a valid CryptoKey object + cryptographicKey := keys.CryptoKeyMeta{ ID: uuid.New().String(), // Generate valid UUID for ID Type: "AES", // Example key type CreatedAt: time.Now(), // Valid CreatedAt time @@ -153,17 +153,17 @@ func TestBlobRepository_DeleteById(t *testing.T) { } // Create a test Blob object with valid UUIDs and required fields - blob := &blobs.Blob{ - ID: uuid.New().String(), // Generate valid UUID - UploadTime: time.Now(), - UserID: uuid.New().String(), // Generate valid UUID for UserID - Name: "test-blob", - Size: 1024, - Type: "text", - IsEncrypted: false, - IsSigned: true, - CryptographicKey: cryptographicKey, // Set the CryptographicKey - KeyID: cryptographicKey.ID, // Ensure ID is set + blob := &blobs.BlobMeta{ + ID: uuid.New().String(), // Generate valid UUID + UploadTime: time.Now(), + UserID: uuid.New().String(), // Generate valid UUID for UserID + Name: "test-blob", + Size: 1024, + Type: "text", + IsEncrypted: false, + IsSigned: true, + CryptoKey: cryptographicKey, // Set the CryptoKey + KeyID: cryptographicKey.ID, // Ensure ID is set } // Create the blob in DB @@ -175,7 +175,7 @@ func TestBlobRepository_DeleteById(t *testing.T) { assert.NoError(t, err) // Verify the blob is deleted - var deletedBlob blobs.Blob + var deletedBlob blobs.BlobMeta err = ctx.DB.First(&deletedBlob, "id = ?", blob.ID).Error assert.Error(t, err, "Blob should be deleted") assert.Equal(t, gorm.ErrRecordNotFound, err, "Error should be 'record not found'") diff --git a/test/integration/persistence/key_repository_in_memory_test.go b/test/integration/persistence/key_repository_in_memory_test.go index 0f88f95..7705240 100644 --- a/test/integration/persistence/key_repository_in_memory_test.go +++ b/test/integration/persistence/key_repository_in_memory_test.go @@ -10,14 +10,14 @@ import ( "gorm.io/gorm" ) -// TestCryptographicKeyRepository_Create tests the Create method of CryptographicKeyRepositoryImpl -func TestCryptographicKeyRepository_Create(t *testing.T) { +// TestCryptoKeyRepository_Create tests the Create method of CryptoKeyRepositoryImpl +func TestCryptoKeyRepository_Create(t *testing.T) { // Set up test context ctx := setupTestDB(t) defer teardownTestDB(t, ctx) - // Create a valid CryptographicKey object - cryptographicKey := &keys.CryptographicKey{ + // Create a valid CryptoKey object + cryptographicKey := &keys.CryptoKeyMeta{ ID: uuid.New().String(), // Generate valid UUID for ID Type: "AES", // Example key type CreatedAt: time.Now(), // Valid CreatedAt time @@ -30,21 +30,21 @@ func TestCryptographicKeyRepository_Create(t *testing.T) { assert.NoError(t, err, "Create should not return an error") // Verify the cryptographic key is created and exists in DB - var createdKey keys.CryptographicKey + var createdKey keys.CryptoKeyMeta err = ctx.DB.First(&createdKey, "id = ?", cryptographicKey.ID).Error assert.NoError(t, err, "Failed to find created cryptographic key") assert.Equal(t, cryptographicKey.ID, createdKey.ID, "ID should match") assert.Equal(t, cryptographicKey.Type, createdKey.Type, "Type should match") } -// TestCryptographicKeyRepository_GetByID tests the GetByID method of CryptographicKeyRepositoryImpl -func TestCryptographicKeyRepository_GetByID(t *testing.T) { +// TestCryptoKeyRepository_GetByID tests the GetByID method of CryptoKeyRepositoryImpl +func TestCryptoKeyRepository_GetByID(t *testing.T) { // Set up test context ctx := setupTestDB(t) defer teardownTestDB(t, ctx) - // Create a valid CryptographicKey object - cryptographicKey := &keys.CryptographicKey{ + // Create a valid CryptoKey object + cryptographicKey := &keys.CryptoKeyMeta{ ID: uuid.New().String(), // Generate valid UUID for ID Type: "RSA", // Example key type CreatedAt: time.Now(), // Valid CreatedAt time @@ -63,14 +63,14 @@ func TestCryptographicKeyRepository_GetByID(t *testing.T) { assert.Equal(t, cryptographicKey.ID, fetchedKey.ID, "ID should match") } -// TestCryptographicKeyRepository_UpdateByID tests the UpdateByID method of CryptographicKeyRepositoryImpl -func TestCryptographicKeyRepository_UpdateByID(t *testing.T) { +// TestCryptoKeyRepository_UpdateByID tests the UpdateByID method of CryptoKeyRepositoryImpl +func TestCryptoKeyRepository_UpdateByID(t *testing.T) { // Set up test context ctx := setupTestDB(t) defer teardownTestDB(t, ctx) - // Create a valid CryptographicKey object - cryptographicKey := &keys.CryptographicKey{ + // Create a valid CryptoKey object + cryptographicKey := &keys.CryptoKeyMeta{ ID: uuid.New().String(), // Generate valid UUID for ID Type: "AES", // Example key type CreatedAt: time.Now(), // Valid CreatedAt time @@ -88,20 +88,20 @@ func TestCryptographicKeyRepository_UpdateByID(t *testing.T) { assert.NoError(t, err, "UpdateByID should not return an error") // Verify the cryptographic key is updated - var updatedKey keys.CryptographicKey + var updatedKey keys.CryptoKeyMeta err = ctx.DB.First(&updatedKey, "id = ?", cryptographicKey.ID).Error assert.NoError(t, err, "Failed to find updated cryptographic key") assert.Equal(t, "ECDSA", updatedKey.Type, "Type should be updated") } -// TestCryptographicKeyRepository_DeleteByID tests the DeleteByID method of CryptographicKeyRepositoryImpl -func TestCryptographicKeyRepository_DeleteByID(t *testing.T) { +// TestCryptoKeyRepository_DeleteByID tests the DeleteByID method of CryptoKeyRepositoryImpl +func TestCryptoKeyRepository_DeleteByID(t *testing.T) { // Set up test context ctx := setupTestDB(t) defer teardownTestDB(t, ctx) - // Create a valid CryptographicKey object - cryptographicKey := &keys.CryptographicKey{ + // Create a valid CryptoKey object + cryptographicKey := &keys.CryptoKeyMeta{ ID: uuid.New().String(), // Generate valid UUID for ID Type: "AES", // Example key type CreatedAt: time.Now(), // Valid CreatedAt time @@ -118,7 +118,7 @@ func TestCryptographicKeyRepository_DeleteByID(t *testing.T) { assert.NoError(t, err, "DeleteByID should not return an error") // Verify the cryptographic key is deleted - var deletedKey keys.CryptographicKey + var deletedKey keys.CryptoKeyMeta err = ctx.DB.First(&deletedKey, "id = ?", cryptographicKey.ID).Error assert.Error(t, err, "Cryptographic key should be deleted") assert.Equal(t, gorm.ErrRecordNotFound, err, "Error should be 'record not found'") diff --git a/test/integration/persistence/test_context.go b/test/integration/persistence/test_context.go index 9427674..05b9443 100644 --- a/test/integration/persistence/test_context.go +++ b/test/integration/persistence/test_context.go @@ -15,7 +15,7 @@ import ( type TestContext struct { DB *gorm.DB BlobRepo *repository.BlobRepositoryImpl - CryptoKeyRepo *repository.CryptographicKeyRepositoryImpl + CryptoKeyRepo *repository.CryptoKeyRepositoryImpl } // Setup function to initialize the test DB and repositories @@ -27,15 +27,15 @@ func setupTestDB(t *testing.T) *TestContext { t.Fatalf("Failed to setup DB: %v", err) } - // Migrate the schema for Blob and CryptographicKey - err = db.AutoMigrate(&blobs.Blob{}, &keys.CryptographicKey{}) + // Migrate the schema for Blob and CryptoKey + err = db.AutoMigrate(&blobs.BlobMeta{}, &keys.CryptoKeyMeta{}) if err != nil { t.Fatalf("Failed to migrate schema: %v", err) } // Initialize the repositories with the DB instance blobRepo := &repository.BlobRepositoryImpl{DB: db} - cryptoKeyRepo := &repository.CryptographicKeyRepositoryImpl{DB: db} + cryptoKeyRepo := &repository.CryptoKeyRepositoryImpl{DB: db} // Return the test context that holds the DB and repositories return &TestContext{ diff --git a/test/unit/domain/blobs/blob_test.go b/test/unit/domain/blobs/blob_test.go index 8473d45..530681c 100644 --- a/test/unit/domain/blobs/blob_test.go +++ b/test/unit/domain/blobs/blob_test.go @@ -12,16 +12,16 @@ import ( // BlobValidationTests struct encapsulates the test data and methods for blob validation type BlobValidationTests struct { - // TestData can be used for holding the Blob and CryptographicKey data - validBlob blobs.Blob - invalidBlob blobs.Blob - invalidBlob2 blobs.Blob + // TestData can be used for holding the Blob and CryptoKey data + validBlob blobs.BlobMeta + invalidBlob blobs.BlobMeta + invalidBlob2 blobs.BlobMeta } // NewBlobValidationTests is a constructor to create a new instance of BlobValidationTests func NewBlobValidationTests() *BlobValidationTests { // Create valid and invalid test data - validBlob := blobs.Blob{ + validBlob := blobs.BlobMeta{ ID: uuid.New().String(), UploadTime: time.Now(), UserID: uuid.New().String(), @@ -32,11 +32,11 @@ func NewBlobValidationTests() *BlobValidationTests { HashAlgorithm: "SHA256", IsEncrypted: true, IsSigned: false, - CryptographicKey: keys.CryptographicKey{ID: uuid.New().String(), Type: "AES", CreatedAt: time.Now(), ExpiresAt: time.Now().Add(24 * time.Hour), UserID: uuid.New().String()}, + CryptoKey: keys.CryptoKeyMeta{ID: uuid.New().String(), Type: "AES", CreatedAt: time.Now(), ExpiresAt: time.Now().Add(24 * time.Hour), UserID: uuid.New().String()}, KeyID: uuid.New().String(), } - invalidBlob := blobs.Blob{ + invalidBlob := blobs.BlobMeta{ ID: "", // Invalid empty ID UploadTime: time.Now(), UserID: "invalid-uuid", // Invalid UserID @@ -47,11 +47,11 @@ func NewBlobValidationTests() *BlobValidationTests { HashAlgorithm: "SHA256", IsEncrypted: true, IsSigned: false, - CryptographicKey: keys.CryptographicKey{ID: uuid.New().String(), Type: "AES", CreatedAt: time.Now(), ExpiresAt: time.Now().Add(24 * time.Hour), UserID: uuid.New().String()}, + CryptoKey: keys.CryptoKeyMeta{ID: uuid.New().String(), Type: "AES", CreatedAt: time.Now(), ExpiresAt: time.Now().Add(24 * time.Hour), UserID: uuid.New().String()}, KeyID: uuid.New().String(), } - invalidBlob2 := blobs.Blob{ + invalidBlob2 := blobs.BlobMeta{ ID: uuid.New().String(), UploadTime: time.Now(), UserID: uuid.New().String(), @@ -62,7 +62,7 @@ func NewBlobValidationTests() *BlobValidationTests { HashAlgorithm: "SHA256", IsEncrypted: true, IsSigned: false, - CryptographicKey: keys.CryptographicKey{ID: uuid.New().String(), Type: "AES", CreatedAt: time.Now(), ExpiresAt: time.Now().Add(24 * time.Hour), UserID: uuid.New().String()}, + CryptoKey: keys.CryptoKeyMeta{ID: uuid.New().String(), Type: "AES", CreatedAt: time.Now(), ExpiresAt: time.Now().Add(24 * time.Hour), UserID: uuid.New().String()}, KeyID: uuid.New().String(), } diff --git a/test/unit/domain/keys/key_test.go b/test/unit/domain/keys/key_test.go index e4ee699..4fa85e2 100644 --- a/test/unit/domain/keys/key_test.go +++ b/test/unit/domain/keys/key_test.go @@ -9,10 +9,10 @@ import ( "github.com/stretchr/testify/assert" ) -// TestCryptographicKeyValidation tests the Validator method for CryptographicKey -func TestCryptographicKeyValidation(t *testing.T) { - // Valid CryptographicKey - validKey := keys.CryptographicKey{ +// TestCryptoKeyValidation tests the Validator method for CryptoKey +func TestCryptoKeyValidation(t *testing.T) { + // Valid CryptoKey + validKey := keys.CryptoKeyMeta{ ID: uuid.New().String(), // Valid UUID Type: "AES", // Valid Type CreatedAt: time.Now(), @@ -20,12 +20,12 @@ func TestCryptographicKeyValidation(t *testing.T) { UserID: uuid.New().String(), // Valid UserID } - // Validate the valid CryptographicKey + // Validate the valid CryptoKey err := validKey.Validate() - assert.Nil(t, err, "Expected no validation errors for valid CryptographicKey") + assert.Nil(t, err, "Expected no validation errors for valid CryptoKey") - // Invalid CryptographicKey (empty ID, invalid Type, expired) - invalidKey := keys.CryptographicKey{ + // Invalid CryptoKey (empty ID, invalid Type, expired) + invalidKey := keys.CryptoKeyMeta{ ID: "", // Invalid empty ID Type: "InvalidType", // Invalid Type CreatedAt: time.Now(), @@ -33,18 +33,18 @@ func TestCryptographicKeyValidation(t *testing.T) { UserID: "invalid-user-id", // Invalid UserID } - // Validate the invalid CryptographicKey + // Validate the invalid CryptoKey err = invalidKey.Validate() - assert.NotNil(t, err, "Expected validation errors for invalid CryptographicKey") + assert.NotNil(t, err, "Expected validation errors for invalid CryptoKey") assert.Contains(t, err.Error(), "Field: ID, Tag: required") assert.Contains(t, err.Error(), "Field: Type, Tag: oneof") assert.Contains(t, err.Error(), "Field: ExpiresAt, Tag: gtefield") } -// TestCryptographicKeyValidations tests the validation edge cases for CryptographicKey -func TestCryptographicKeyValidations(t *testing.T) { +// TestCryptoKeyValidations tests the validation edge cases for CryptoKey +func TestCryptoKeyValidations(t *testing.T) { // Test missing UserID (should fail) - invalidKey := keys.CryptographicKey{ + invalidKey := keys.CryptoKeyMeta{ ID: uuid.New().String(), // Valid UUID Type: "AES", // Valid Type CreatedAt: time.Now(),