feat(notifications): admin bell API in mobile-api
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
d5384c6ac0
commit
66e2e488d9
6 files changed
+269
-1
No files matched your search
+41
@@ -0,0 +1,41 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.mobile.api.service.AdminNotificationService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
// add-lead-notifications: every signed-in staff user has the bell; no module, and no user id from the request.
|
||||
class AdminNotificationControllerTest {
|
||||
|
||||
@Test
|
||||
void everyEndpointNeedsASignedInStaffUserOnly() {
|
||||
for (Method m : AdminNotificationController.class.getDeclaredMethods()) {
|
||||
if (!java.lang.reflect.Modifier.isPublic(m.getModifiers())) continue;
|
||||
assertThat(m.isAnnotationPresent(AppAuth.class)).as(m.getName()).isTrue();
|
||||
assertThat(m.getAnnotation(AdminModule.class).value()).as(m.getName()).containsExactly(AdminModule.ANY_ADMIN);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void passesThrough() {
|
||||
AdminNotificationService service = mock(AdminNotificationService.class);
|
||||
AdminNotificationController c = new AdminNotificationController(service);
|
||||
when(service.list(2, 20)).thenReturn(Map.of("total", 0));
|
||||
when(service.unreadCount()).thenReturn(Map.of("count", 1));
|
||||
assertThat(c.list(2, 20).getBody()).containsEntry("total", 0);
|
||||
assertThat(c.unreadCount().getBody()).containsEntry("count", 1);
|
||||
assertThat(c.markRead(5L).getStatusCode().value()).isEqualTo(204);
|
||||
verify(service).markRead(5L);
|
||||
c.markAllRead();
|
||||
verify(service).markAllRead();
|
||||
}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
// add-lead-notifications: the bell's admin API always acts for the signed-in user, never one named in the request.
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
class AdminNotificationServiceTest {
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private AdminAuthenticationService auth;
|
||||
private AdminNotificationService service;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rest = mock(RestTemplateCustomUtil.class);
|
||||
auth = mock(AdminAuthenticationService.class);
|
||||
service = new AdminNotificationService(rest, auth);
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> "http://gw" + i.getArgument(0));
|
||||
http = mockStatic(HttpReqRespUtils.class);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
|
||||
User u = new User();
|
||||
u.setId(11L);
|
||||
u.setCompanyId(2L);
|
||||
when(auth.loadUserByToken("tok")).thenReturn(u);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() {
|
||||
http.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void listsTheCallersOwnNotifications() {
|
||||
Map<String, Object> page = Map.of("items", List.of(), "total", 0);
|
||||
when(rest.getForEntity(eq("http://gw/notification/inbox/11/1/20"), any(ParameterizedTypeReference.class)))
|
||||
.thenReturn(ResponseEntity.ok(page));
|
||||
assertThat(service.list(1, 20)).isEqualTo(page);
|
||||
}
|
||||
|
||||
@Test
|
||||
void capsThePageSize() {
|
||||
when(rest.getForEntity(eq("http://gw/notification/inbox/11/0/50"), any(ParameterizedTypeReference.class)))
|
||||
.thenReturn(ResponseEntity.ok(Map.of("items", List.of(), "total", 0)));
|
||||
service.list(-3, 500);
|
||||
verify(rest).getForEntity(eq("http://gw/notification/inbox/11/0/50"), any(ParameterizedTypeReference.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void countsTheCallersUnread() {
|
||||
when(rest.getForEntity(eq("http://gw/notification/unreadCount/11"), any(ParameterizedTypeReference.class)))
|
||||
.thenReturn(ResponseEntity.ok(Map.of("count", 3)));
|
||||
assertThat(service.unreadCount()).containsEntry("count", 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void marksReadForTheCaller() {
|
||||
service.markRead(5L);
|
||||
verify(rest).postForEntity(eq("http://gw/notification/markRead/11/5"), eq(Void.class), any());
|
||||
when(rest.postForEntity(eq("http://gw/notification/markAllRead/11"), any(ParameterizedTypeReference.class), any()))
|
||||
.thenReturn(ResponseEntity.ok(Map.of("count", 2)));
|
||||
assertThat(service.markAllRead()).containsEntry("count", 2);
|
||||
verify(rest).postForEntity(eq("http://gw/notification/markAllRead/11"), any(ParameterizedTypeReference.class), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherUsersNotificationIsNotFound() {
|
||||
when(rest.postForEntity(eq("http://gw/notification/markRead/11/6"), eq(Void.class), any()))
|
||||
.thenThrow(HttpClientErrorException.create(HttpStatus.NOT_FOUND, "nf", null, null, null));
|
||||
assertThatThrownBy(() -> service.markRead(6L))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getStatusCode().value()).isEqualTo(404));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notSignedInIsRefused() {
|
||||
when(auth.loadUserByToken("tok")).thenReturn(null);
|
||||
assertThatThrownBy(() -> service.unreadCount())
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getStatusCode().value()).isEqualTo(401));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user