刘光辉
5 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package jnpf.elnService.template.impl;
 
import jnpf.elnService.template.ElnTemplateAnnotationAccessService;
import jnpf.permission.model.authorize.UserPermissionProbe;
import org.junit.Test;
 
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicInteger;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
 
public class ElnTemplateAnnotationAccessServiceImplTest {
 
    @Test
    public void editableTemplateAndGrantedMenuAllowAnnotation() {
        AtomicReference<UserPermissionProbe> captured = new AtomicReference<>();
        ElnTemplateAnnotationAccessService service = new ElnTemplateAnnotationAccessServiceImpl(
                (tenantId, templateId, fileId) -> true,
                probe -> {
                    captured.set(probe);
                    return new UserPermissionProbe.Result(true, "ALLOWED");
                });
 
        boolean allowed = service.canAnnotate("tenant-a", "user-1", "template-1", "file-1");
 
        assertTrue(allowed);
        assertEquals("tenant-a", captured.get().getTenantId());
        assertEquals("user-1", captured.get().getUserId());
        assertEquals("LIMS", captured.get().getSystemCode());
        assertEquals("dianziJiluMubanGuanli", captured.get().getModuleCode());
        assertEquals("Web", captured.get().getModuleCategory());
        assertEquals("", captured.get().getButtonCode());
    }
 
    @Test
    public void nonEditableTemplateRejectsWithoutPermissionProbe() {
        AtomicInteger permissionCalls = new AtomicInteger();
        ElnTemplateAnnotationAccessService service = new ElnTemplateAnnotationAccessServiceImpl(
                (tenantId, templateId, fileId) -> false,
                probe -> {
                    permissionCalls.incrementAndGet();
                    return new UserPermissionProbe.Result(true, "ALLOWED");
                });
 
        assertFalse(service.canAnnotate("tenant-a", "user-1", "template-1", "file-1"));
        assertEquals(0, permissionCalls.get());
    }
 
    @Test
    public void deniedMenuPermissionRejectsAnnotation() {
        ElnTemplateAnnotationAccessService service = new ElnTemplateAnnotationAccessServiceImpl(
                (tenantId, templateId, fileId) -> true,
                probe -> new UserPermissionProbe.Result(false, "MODULE_DENIED"));
 
        assertFalse(service.canAnnotate("tenant-a", "user-1", "template-1", "file-1"));
    }
 
    @Test
    public void dependencyFailureRejectsAnnotation() {
        ElnTemplateAnnotationAccessService service = new ElnTemplateAnnotationAccessServiceImpl(
                (tenantId, templateId, fileId) -> true,
                probe -> { throw new IllegalStateException("permission unavailable"); });
 
        assertFalse(service.canAnnotate("tenant-a", "user-1", "template-1", "file-1"));
    }
}