Bläddra i källkod

fix domain note unit test

Daniel Bohry 1 vecka sedan
förälder
incheckning
bef8c3f16b

+ 3 - 3
src/main/java/com/lhamacorp/knotes/api/NoteController.java

@@ -17,7 +17,9 @@ import java.util.List;
 import static com.lhamacorp.knotes.context.UserContextHolder.isAuthenticated;
 import static com.lhamacorp.knotes.domain.EncryptionMode.PRIVATE;
 import static com.lhamacorp.knotes.domain.EncryptionMode.PUBLIC;
-import static org.springframework.http.HttpStatus.*;
+import static com.lhamacorp.knotes.domain.Note.ANONYMOUS;
+import static org.springframework.http.HttpStatus.FORBIDDEN;
+import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
 import static org.springframework.http.ResponseEntity.badRequest;
 import static org.springframework.http.ResponseEntity.ok;
 
@@ -28,8 +30,6 @@ public class NoteController {
 
     private final NoteService service;
 
-    private static final String ANONYMOUS = "1";
-
     public NoteController(NoteService service) {
         this.service = service;
     }

+ 11 - 4
src/main/java/com/lhamacorp/knotes/domain/Note.java

@@ -24,22 +24,29 @@ public record Note(
         @Field("salt") Binary encryptionSalt,
         Boolean requiresPassword
 ) {
+
+    public static final String ANONYMOUS = "1";
+
     public Note(String id, String content, String createdBy, Instant createdAt, Instant modifiedAt) {
         this(id, content, createdBy, createdAt, modifiedAt, PUBLIC, null);
     }
 
     public Note(String id, String content, String createdBy, Instant createdAt, Instant modifiedAt, EncryptionMode encryptionMode, String password) {
-        EncryptionMode finalMode = encryptionMode != null ? encryptionMode : PUBLIC;
+        EncryptionMode mode = encryptionMode != null ? encryptionMode : PUBLIC;
+
+        if (createdBy == null || createdBy.equals(ANONYMOUS)) {
+            mode = PUBLIC;
+        }
 
         byte[] salt = null;
-        if (finalMode != PUBLIC) {
+        if (mode != PUBLIC) {
             salt = EncryptionUtils.generateSalt();
         }
 
-        Binary processedContent = processContent(content, finalMode, createdBy, password, salt);
+        Binary processedContent = processContent(content, mode, createdBy, password, salt);
         Binary storedSalt = salt != null ? new Binary(salt) : null;
 
-        this(id, processedContent, createdBy, createdAt, modifiedAt, finalMode, storedSalt, finalMode == EncryptionMode.PASSWORD_SHARED);
+        this(id, processedContent, createdBy, createdAt, modifiedAt, mode, storedSalt, mode == EncryptionMode.PASSWORD_SHARED);
     }
 
     public String content() {

+ 2 - 10
src/test/java/com/lhamacorp/knotes/domain/NoteTest.java

@@ -3,15 +3,13 @@ package com.lhamacorp.knotes.domain;
 import com.lhamacorp.knotes.exception.DecryptionException;
 import com.lhamacorp.knotes.exception.UnauthorizedException;
 import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Disabled;
-import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
 
 import java.time.Instant;
 
 import static org.junit.jupiter.api.Assertions.*;
 
-@Disabled
 @DisplayName("Note Domain Model Tests")
 class NoteTest {
 
@@ -22,12 +20,9 @@ class NoteTest {
 
     @BeforeEach
     void setUp() {
-        // Set up test key for encryption tests
         System.setProperty("knotes.encryption.key", TEST_KEY);
     }
 
-    // ===== EXISTING TESTS (Backward Compatibility) =====
-
     @Test
     @DisplayName("Constructor with string content should compress and store (backward compatibility)")
     void constructor_withStringContent_shouldCompressAndStore() {
@@ -87,8 +82,6 @@ class NoteTest {
         assertEquals(originalContent, retrievedContent);
     }
 
-    // ===== NEW ENCRYPTION TESTS =====
-
     @Test
     @DisplayName("Should create PUBLIC content with no encryption (default)")
     void constructor_withPublicMode_shouldNotEncrypt() {
@@ -133,7 +126,6 @@ class NoteTest {
 
         // Verify content is actually encrypted (compressed data should be different from original)
         assertNotNull(note.compressedData());
-        // The encrypted data will be longer than just compressed data due to IV + auth tag
     }
 
     @Test
@@ -269,7 +261,7 @@ class NoteTest {
 
         // Create a corrupted content - copy the encrypted data but remove the salt (simulating corruption)
         Note corruptedNote = new Note(id, validNote.compressedData(), TEST_USER_ID, now, now,
-                                      EncryptionMode.PRIVATE, null, false);
+                EncryptionMode.PRIVATE, null, false);
 
         // When & Then
         DecryptionException exception = assertThrows(DecryptionException.class, () -> {

+ 0 - 2
src/test/java/com/lhamacorp/knotes/service/NoteServiceTest.java

@@ -53,8 +53,6 @@ class NoteServiceTest {
         testCreatedAt = Instant.parse("2024-01-01T10:00:00Z");
         testModifiedAt = Instant.parse("2024-01-01T11:00:00Z");
         testNote = new Note(testId, testContent, testUserId, testCreatedAt, testModifiedAt);
-
-        // Set up UserContext for all tests
         testUserContext = new UserContext(testUserId, "testuser", List.of("USER"));
         UserContextHolder.set(testUserContext);
     }