.
This commit is contained in:
@@ -64,6 +64,11 @@ class JwtServiceTest {
|
||||
assertThrows(JwtException.class, () -> jwtService.parseAndValidate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseAndValidateShouldThrowForMalformedToken() {
|
||||
assertThrows(JwtException.class, () -> jwtService.parseAndValidate("not-a-jwt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructorWithoutConfiguredSecretShouldUseDefaultSigningKey() {
|
||||
JwtService serviceWithDefaultKey = new JwtService("");
|
||||
@@ -76,6 +81,7 @@ class JwtServiceTest {
|
||||
void extractTokenFromHeaderShouldHandleBearerPrefixBlankAndRawToken() {
|
||||
assertNull(jwtService.extractTokenFromHeader(null));
|
||||
assertNull(jwtService.extractTokenFromHeader(" "));
|
||||
assertEquals("", jwtService.extractTokenFromHeader("Bearer "));
|
||||
assertEquals("sample-token", jwtService.extractTokenFromHeader("Bearer sample-token"));
|
||||
assertEquals("raw-token", jwtService.extractTokenFromHeader("raw-token"));
|
||||
}
|
||||
@@ -114,6 +120,17 @@ class JwtServiceTest {
|
||||
assertNull(jwtService.extractUserIdFromToken("not-a-jwt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractUserIdFromTokenShouldReturnNullForInvalidSignature() {
|
||||
String token = createToken(
|
||||
Base64.getEncoder().encodeToString("another-valid-secret-key-1234567".getBytes(StandardCharsets.UTF_8)),
|
||||
"qa@example.com",
|
||||
Instant.now().plusSeconds(3600),
|
||||
Map.of("uid", 123L));
|
||||
|
||||
assertNull(jwtService.extractUserIdFromToken(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractUserIdFromHeaderShouldExtractFromBearerToken() {
|
||||
String token = createToken(TEST_SECRET, "qa@example.com", Instant.now().plusSeconds(3600), Map.of("uid", 501L));
|
||||
@@ -131,6 +148,7 @@ class JwtServiceTest {
|
||||
@Test
|
||||
void extractEmailFromTokenShouldReturnNullForInvalidToken() {
|
||||
assertNull(jwtService.extractEmailFromToken("broken-token"));
|
||||
assertNull(jwtService.extractEmailFromToken(" "));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -350,8 +350,8 @@ class PostingTaskServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeTaskShouldFailWhenCredentialsAreMissing() {
|
||||
PostingTask task = task("task-1", "user-1", "facebook", "pending");
|
||||
void executeTaskShouldFailWhenCredentialsAreMissingForNonFacebookPlatforms() {
|
||||
PostingTask task = task("task-1", "user-1", "linkedin", "pending");
|
||||
List<String> savedStatuses = new ArrayList<>();
|
||||
|
||||
when(taskRepository.findById("task-1")).thenReturn(Optional.of(task));
|
||||
@@ -360,13 +360,14 @@ class PostingTaskServiceTest {
|
||||
savedStatuses.add(savedTask.getStatus());
|
||||
return savedTask;
|
||||
});
|
||||
when(credentialsService.getCredentials("user-1", "facebook")).thenReturn(null);
|
||||
when(credentialsService.getCredentials("user-1", "linkedin")).thenReturn(null);
|
||||
|
||||
postingTaskService.executeTask("task-1");
|
||||
|
||||
assertEquals(List.of("processing", "failed"), savedStatuses);
|
||||
assertEquals("failed", task.getStatus());
|
||||
assertEquals("Credentials not found for platform: facebook", task.getErrorMessage());
|
||||
assertEquals("Credentials not found for platform: linkedin", task.getErrorMessage());
|
||||
verifyNoInteractions(linkedInPostingService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -383,6 +384,47 @@ class PostingTaskServiceTest {
|
||||
assertEquals("Platform not supported: instagram", task.getErrorMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeTaskShouldCompleteWhenFacebookReturnsNullPostId() throws Exception {
|
||||
PostingTask task = task("task-1", "user-1", "facebook", "pending");
|
||||
task.setPostText("Silent post");
|
||||
task.setHashtags(List.of("#silent"));
|
||||
|
||||
when(taskRepository.findById("task-1")).thenReturn(Optional.of(task));
|
||||
when(taskRepository.save(any(PostingTask.class))).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(facebookPostingService.postToPage("Silent post", List.of("#silent"))).thenReturn(null);
|
||||
|
||||
postingTaskService.executeTask("task-1");
|
||||
|
||||
assertEquals("completed", task.getStatus());
|
||||
assertNull(task.getErrorMessage());
|
||||
verify(facebookPostingService).postToPage("Silent post", List.of("#silent"));
|
||||
verify(facebookPostingService, never()).sendPrivateMessage(anyString(), anyString());
|
||||
verify(facebookPostingService, never()).sendPrivateImageMessage(anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeTaskShouldFailWhenTelegramPublishingTimesOut() {
|
||||
PostingTask task = task("task-1", "user-1", "telegram", "pending");
|
||||
task.setPostText("Timeout post");
|
||||
task.setHashtags(List.of("#timeout"));
|
||||
|
||||
when(taskRepository.findById("task-1")).thenReturn(Optional.of(task));
|
||||
when(taskRepository.save(any(PostingTask.class))).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(credentialsService.getCredentials("user-1", "telegram")).thenReturn("{\"botToken\":\"t\",\"chatId\":\"1\"}");
|
||||
when(telegramPostingService.postToTelegram(
|
||||
"{\"botToken\":\"t\",\"chatId\":\"1\"}",
|
||||
"Timeout post",
|
||||
List.of("#timeout")))
|
||||
.thenThrow(new RuntimeException("Network timeout"));
|
||||
|
||||
postingTaskService.executeTask("task-1");
|
||||
|
||||
assertEquals("failed", task.getStatus());
|
||||
assertEquals("Network timeout", task.getErrorMessage());
|
||||
assertNotNull(task.getExecutedAt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeTaskShouldHandleFacebookExpiredToken() throws Exception {
|
||||
PostingTask task = task("task-1", "user-1", "facebook", "pending");
|
||||
|
||||
Reference in New Issue
Block a user