|
| 1 | +package org.keycloak.gh.bot.security.command; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.BeforeEach; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.keycloak.gh.bot.security.email.MailSender; |
| 6 | +import org.kohsuke.github.GHEventPayload; |
| 7 | +import org.kohsuke.github.GHIssue; |
| 8 | +import org.kohsuke.github.GHIssueComment; |
| 9 | +import org.kohsuke.github.GHIssueCommentQueryBuilder; |
| 10 | +import org.kohsuke.github.GHRepository; |
| 11 | +import org.kohsuke.github.PagedIterable; |
| 12 | +import org.kohsuke.github.PagedIterator; |
| 13 | +import org.kohsuke.github.ReactionContent; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.lang.reflect.Field; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import static org.mockito.ArgumentMatchers.*; |
| 20 | +import static org.mockito.Mockito.*; |
| 21 | + |
| 22 | +class MailingListCommandTest { |
| 23 | + |
| 24 | + private MailSender mailSender; |
| 25 | + private MailingListCommand command; |
| 26 | + private GHEventPayload.IssueComment payload; |
| 27 | + private GHIssueComment comment; |
| 28 | + private GHIssue issue; |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + @SuppressWarnings("unchecked") |
| 32 | + void setUp() throws Exception { |
| 33 | + mailSender = mock(MailSender.class); |
| 34 | + command = new MailingListCommand(); |
| 35 | + |
| 36 | + setField(command, "mailSender", mailSender); |
| 37 | + setField(command, "targetGroup", "keycloak-security@googlegroups.com"); |
| 38 | + |
| 39 | + payload = mock(GHEventPayload.IssueComment.class); |
| 40 | + comment = mock(GHIssueComment.class); |
| 41 | + issue = mock(GHIssue.class); |
| 42 | + |
| 43 | + GHRepository repository = mock(GHRepository.class); |
| 44 | + when(repository.getFullName()).thenReturn("keycloak/keycloak-private"); |
| 45 | + when(payload.getRepository()).thenReturn(repository); |
| 46 | + when(payload.getComment()).thenReturn(comment); |
| 47 | + when(payload.getIssue()).thenReturn(issue); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void execute_sendsReplyAndReactsWithThumbsUp() throws Exception { |
| 52 | + when(comment.getBody()).thenReturn("@security reply\n\nThis is my reply to the mailing list"); |
| 53 | + setupIssueComments("**Gmail-Thread-ID:** abc123def"); |
| 54 | + when(mailSender.sendReply("abc123def", "This is my reply to the mailing list", "keycloak-security@googlegroups.com")) |
| 55 | + .thenReturn(true); |
| 56 | + |
| 57 | + command.run(payload); |
| 58 | + |
| 59 | + verify(mailSender).sendReply("abc123def", "This is my reply to the mailing list", "keycloak-security@googlegroups.com"); |
| 60 | + verify(comment).createReaction(ReactionContent.PLUS_ONE); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void execute_reactsWithThumbsDownWhenSendFails() throws Exception { |
| 65 | + when(comment.getBody()).thenReturn("@security reply\n\nFailed reply"); |
| 66 | + setupIssueComments("**Gmail-Thread-ID:** abc123def"); |
| 67 | + when(mailSender.sendReply("abc123def", "Failed reply", "keycloak-security@googlegroups.com")) |
| 68 | + .thenReturn(false); |
| 69 | + |
| 70 | + command.run(payload); |
| 71 | + |
| 72 | + verify(comment).createReaction(ReactionContent.MINUS_ONE); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void execute_reactsWithThumbsDownWhenNoThreadIdFound() throws Exception { |
| 77 | + when(comment.getBody()).thenReturn("@security reply\n\nOrphan reply"); |
| 78 | + setupIssueComments("Just a regular comment with no thread ID"); |
| 79 | + |
| 80 | + command.run(payload); |
| 81 | + |
| 82 | + verify(comment).createReaction(ReactionContent.MINUS_ONE); |
| 83 | + verify(mailSender, never()).sendReply(anyString(), anyString(), anyString()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void execute_reactsWithThumbsDownOnInvalidCommandSignature() throws Exception { |
| 88 | + when(comment.getBody()).thenReturn("@security reply extra-stuff\n\nBody text"); |
| 89 | + |
| 90 | + command.run(payload); |
| 91 | + |
| 92 | + verify(comment).createReaction(ReactionContent.MINUS_ONE); |
| 93 | + verify(mailSender, never()).sendReply(anyString(), anyString(), anyString()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void execute_reactsWithThumbsDownWhenNoBody() throws Exception { |
| 98 | + when(comment.getBody()).thenReturn("@security reply"); |
| 99 | + |
| 100 | + command.run(payload); |
| 101 | + |
| 102 | + verify(comment).createReaction(ReactionContent.MINUS_ONE); |
| 103 | + verify(mailSender, never()).sendReply(anyString(), anyString(), anyString()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void execute_findsThreadIdAcrossMultipleComments() throws Exception { |
| 108 | + when(comment.getBody()).thenReturn("@security reply\n\nMulti-comment reply"); |
| 109 | + setupIssueComments( |
| 110 | + "First comment without thread ID", |
| 111 | + "**Gmail-Thread-ID:** deadbeef42\nSubject: CVE report\nFrom: reporter@example.com" |
| 112 | + ); |
| 113 | + when(mailSender.sendReply("deadbeef42", "Multi-comment reply", "keycloak-security@googlegroups.com")) |
| 114 | + .thenReturn(true); |
| 115 | + |
| 116 | + command.run(payload); |
| 117 | + |
| 118 | + verify(mailSender).sendReply("deadbeef42", "Multi-comment reply", "keycloak-security@googlegroups.com"); |
| 119 | + verify(comment).createReaction(ReactionContent.PLUS_ONE); |
| 120 | + } |
| 121 | + |
| 122 | + @SuppressWarnings("unchecked") |
| 123 | + private void setupIssueComments(String... commentBodies) throws IOException { |
| 124 | + GHIssueCommentQueryBuilder queryBuilder = mock(GHIssueCommentQueryBuilder.class); |
| 125 | + when(issue.queryComments()).thenReturn(queryBuilder); |
| 126 | + |
| 127 | + List<GHIssueComment> comments = new java.util.ArrayList<>(); |
| 128 | + for (String body : commentBodies) { |
| 129 | + GHIssueComment c = mock(GHIssueComment.class); |
| 130 | + when(c.getBody()).thenReturn(body); |
| 131 | + comments.add(c); |
| 132 | + } |
| 133 | + |
| 134 | + PagedIterable<GHIssueComment> pagedIterable = mock(PagedIterable.class); |
| 135 | + when(queryBuilder.list()).thenReturn(pagedIterable); |
| 136 | + |
| 137 | + PagedIterator<GHIssueComment> pagedIterator = mock(PagedIterator.class); |
| 138 | + final int[] index = {0}; |
| 139 | + when(pagedIterator.hasNext()).thenAnswer(inv -> index[0] < comments.size()); |
| 140 | + when(pagedIterator.next()).thenAnswer(inv -> comments.get(index[0]++)); |
| 141 | + when(pagedIterable.iterator()).thenReturn(pagedIterator); |
| 142 | + } |
| 143 | + |
| 144 | + private static void setField(Object target, String fieldName, Object value) throws Exception { |
| 145 | + Class<?> clazz = target.getClass(); |
| 146 | + while (clazz != null) { |
| 147 | + try { |
| 148 | + Field field = clazz.getDeclaredField(fieldName); |
| 149 | + field.setAccessible(true); |
| 150 | + field.set(target, value); |
| 151 | + return; |
| 152 | + } catch (NoSuchFieldException e) { |
| 153 | + clazz = clazz.getSuperclass(); |
| 154 | + } |
| 155 | + } |
| 156 | + throw new NoSuchFieldException(fieldName); |
| 157 | + } |
| 158 | +} |
0 commit comments