feat: whatsapp admin api in mobile-api
- /api/admin/whatsapp: conversations, thread, send, read, assign, start, templates and sync, media, unread count, assignees. - Caller context from the token: company through AdminCompanyScope (fail closed), admin for BO_WHATSAPP_ADMIN or super admin. - Exactly-one validation for send, assign and start; path ids must look like uuids before they reach the service. - Service reason codes come back with their status and a message; an unreachable service is 502. - Owner and assignee names filled from the system service. - Audit: send (conversation and template name, never text), assign (owner before and after), start, sync; read markers skipped. - Media streams with nosniff and a sandbox CSP; anything but image, audio or video downloads. - Disabling a user or changing their roles tells the service to free their chats (best effort). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
0d5c3f351a
commit
13b692748c
8 files changed
+1030
-1
No files matched your search
+155
@@ -0,0 +1,155 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.mobile.api.dto.whatsapp.AssignRequest;
|
||||
import com.solvway.mobile.api.dto.whatsapp.SendMessageRequest;
|
||||
import com.solvway.mobile.api.dto.whatsapp.UnreadCount;
|
||||
import com.solvway.mobile.api.service.AdminWhatsAppService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
import com.solvway.mobile.api.useraccess.Audited;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AdminWhatsAppControllerTest {
|
||||
|
||||
@Mock private AdminWhatsAppService whatsapp;
|
||||
@InjectMocks private AdminWhatsAppController controller;
|
||||
private MockMvc mvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mvc = MockMvcBuilders.standaloneSetup(controller).build();
|
||||
}
|
||||
|
||||
private static List<String> modules(Method m) {
|
||||
AdminModule a = m.getAnnotation(AdminModule.class);
|
||||
if (a == null) a = AdminWhatsAppController.class.getAnnotation(AdminModule.class);
|
||||
return Arrays.asList(a.value());
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyHandlerIsAuthenticatedAndGuardedByTheWhatsappModules() {
|
||||
List<Method> handlers = Arrays.stream(AdminWhatsAppController.class.getDeclaredMethods())
|
||||
.filter(m -> Modifier.isPublic(m.getModifiers()) && !m.isSynthetic()).toList();
|
||||
assertThat(handlers).hasSize(11);
|
||||
for (Method m : handlers) {
|
||||
assertThat(m.isAnnotationPresent(AppAuth.class)).as(m.getName() + " @AppAuth").isTrue();
|
||||
if (List.of("assign", "syncTemplates", "assignees").contains(m.getName())) {
|
||||
assertThat(modules(m)).as(m.getName()).containsExactly("BO_WHATSAPP_ADMIN");
|
||||
} else {
|
||||
assertThat(modules(m)).as(m.getName()).containsExactlyInAnyOrder("BO_WHATSAPP", "BO_WHATSAPP_ADMIN");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void readMarkersAreNotAuditedAndWritesNameTheirAction() throws Exception {
|
||||
assertThat(AdminWhatsAppController.class.getMethod("read", String.class, Long.class).getAnnotation(Audited.class).skip()).isTrue();
|
||||
assertThat(AdminWhatsAppController.class.getMethod("send", String.class, SendMessageRequest.class, Long.class)
|
||||
.getAnnotation(Audited.class).action()).isEqualTo("WHATSAPP_SEND");
|
||||
assertThat(AdminWhatsAppController.class.getMethod("assign", String.class, AssignRequest.class, Long.class)
|
||||
.getAnnotation(Audited.class).action()).isEqualTo("WHATSAPP_ASSIGN");
|
||||
assertThat(AdminWhatsAppController.class.getMethod("syncTemplates", Long.class)
|
||||
.getAnnotation(Audited.class).action()).isEqualTo("WHATSAPP_TEMPLATE_SYNC");
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendBindsTheBodyAndPath() throws Exception {
|
||||
mvc.perform(post("/api/admin/whatsapp/conversations/c1/messages").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"template\":\"expo_followup\",\"language\":\"en\",\"params\":[\"A\",\"B\"]}"))
|
||||
.andExpect(status().isOk());
|
||||
verify(whatsapp).send("c1", null, new SendMessageRequest(null, "expo_followup", "en", List.of("A", "B")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readAnswers204() throws Exception {
|
||||
mvc.perform(post("/api/admin/whatsapp/conversations/c1/read")).andExpect(status().isNoContent());
|
||||
verify(whatsapp).read("c1", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unreadCountAndListPassTheirParameters() throws Exception {
|
||||
when(whatsapp.unreadCount(null)).thenReturn(new UnreadCount(3));
|
||||
mvc.perform(get("/api/admin/whatsapp/unread-count")).andExpect(status().isOk()).andExpect(content().json("{\"count\":3}"));
|
||||
mvc.perform(get("/api/admin/whatsapp/conversations").param("tab", "unassigned").param("q", "ais").param("companyId", "2"))
|
||||
.andExpect(status().isOk());
|
||||
verify(whatsapp).list(2L, "unassigned", "ais", 0, 30);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mediaStreamsWithTheTypeAndNoCaching() throws Exception {
|
||||
doAnswer(i -> {
|
||||
AdminWhatsAppService.MediaOutput out = i.getArgument(2);
|
||||
out.open("image/jpeg", 3).write(new byte[]{1, 2, 3});
|
||||
return null;
|
||||
}).when(whatsapp).media(eq("m1"), isNull(), any());
|
||||
|
||||
mvc.perform(get("/api/admin/whatsapp/media/m1"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(header().string("Content-Type", "image/jpeg"))
|
||||
.andExpect(header().string("Cache-Control", "private, no-store"))
|
||||
.andExpect(content().bytes(new byte[]{1, 2, 3}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mediaASenderTypedAsHtmlIsADownloadThatCannotRun() throws Exception {
|
||||
doAnswer(i -> {
|
||||
AdminWhatsAppService.MediaOutput out = i.getArgument(2);
|
||||
out.open("text/html", 1).write(new byte[]{1});
|
||||
return null;
|
||||
}).when(whatsapp).media(eq("m2"), isNull(), any());
|
||||
|
||||
mvc.perform(get("/api/admin/whatsapp/media/m2"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(header().string("X-Content-Type-Options", "nosniff"))
|
||||
.andExpect(header().string("Content-Security-Policy", "sandbox"))
|
||||
.andExpect(header().string("Content-Disposition", "attachment"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mediaImagesStayInline() throws Exception {
|
||||
doAnswer(i -> {
|
||||
AdminWhatsAppService.MediaOutput out = i.getArgument(2);
|
||||
out.open("image/png", 1).write(new byte[]{1});
|
||||
return null;
|
||||
}).when(whatsapp).media(eq("m3"), isNull(), any());
|
||||
|
||||
mvc.perform(get("/api/admin/whatsapp/media/m3"))
|
||||
.andExpect(header().doesNotExist("Content-Disposition"))
|
||||
.andExpect(header().string("X-Content-Type-Options", "nosniff"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void assigneesComeFromTheService() throws Exception {
|
||||
when(whatsapp.assignees(null)).thenReturn(List.of(Map.of("id", 21, "name", "Siti")));
|
||||
mvc.perform(get("/api/admin/whatsapp/assignees")).andExpect(status().isOk())
|
||||
.andExpect(content().json("[{\"id\":21,\"name\":\"Siti\"}]", true));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user