-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: java 17 authentication * use http headers constant * add jdk17 to build configs
- Loading branch information
Showing
3 changed files
with
60 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import com.google.common.net.HttpHeaders; | ||
|
||
import hudson.ProxyConfiguration; | ||
import hudson.util.Secret; | ||
import jenkins.model.Jenkins; | ||
|
||
|
@@ -15,8 +16,9 @@ | |
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockserver.integration.ClientAndServer; | ||
import org.mockserver.model.NottableString; | ||
import org.mockserver.verify.VerificationTimes; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockserver.model.HttpRequest.request; | ||
import static org.mockserver.model.HttpResponse.response; | ||
import static org.mockserver.model.StringBody.exact; | ||
|
@@ -28,9 +30,6 @@ public class ZulipTest { | |
@Mock | ||
private Jenkins jenkins; | ||
|
||
@Mock | ||
private Secret secret; | ||
|
||
@BeforeClass | ||
public static void startMockServer() { | ||
mockServer = ClientAndServer.startClientAndServer(1080); | ||
|
@@ -42,62 +41,92 @@ public static void stopMockServer() { | |
} | ||
|
||
private MockedStatic<Jenkins> jenkinsStatic; | ||
private MockedStatic<Secret> secretStatic; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
|
||
jenkinsStatic = Mockito.mockStatic(Jenkins.class); | ||
jenkinsStatic.when(Jenkins::get).thenReturn(jenkins); | ||
|
||
secretStatic = Mockito.mockStatic(Secret.class); | ||
secretStatic.when(() -> Secret.toString(any(Secret.class))).thenReturn("secret"); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
jenkinsStatic.close(); | ||
secretStatic.close(); | ||
mockServer.reset(); | ||
jenkins.proxy = null; | ||
} | ||
|
||
@Test | ||
public void testSendStreamMessage() throws Exception { | ||
mockServer.reset(); | ||
mockServer.when(request().withPath("/api/v1/messages")).respond(response().withStatusCode(200)); | ||
Zulip zulip = new Zulip("http://localhost:1080", "[email protected]", secret); | ||
Zulip zulip = new Zulip("http://localhost:1080", "[email protected]", Secret.fromString("secret")); | ||
zulip.sendStreamMessage("testStreamůř", "testTopic", "testMessage"); | ||
mockServer.verify( | ||
request() | ||
.withMethod("POST") | ||
.withPath("/api/v1/messages") | ||
.withBody(exact( | ||
"api-key=secret&subject=testTopic&to=testStream%C5%AF%C5%99&type=stream&email=jenkins-bot%40zulip.com&content=testMessage")) | ||
.withHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded") | ||
.withHeader(HttpHeaders.AUTHORIZATION, "Basic amVua2lucy1ib3RAenVsaXAuY29tOnNlY3JldA==")); | ||
.withHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded")); | ||
} | ||
|
||
@Test | ||
public void testFailGracefullyOnError() { | ||
mockServer.reset(); | ||
mockServer.when(request().withPath("/api/v1/messages")).respond(response().withStatusCode(500)); | ||
Zulip zulip = new Zulip("http://localhost:1080", "[email protected]", secret); | ||
Zulip zulip = new Zulip("http://localhost:1080", "[email protected]", Secret.fromString("secret")); | ||
// Test that this does not throw exception | ||
zulip.sendStreamMessage("testStream", "testTopic", "testMessage"); | ||
} | ||
|
||
@Test | ||
public void testFailGracefullyWhenUnreachable() { | ||
Zulip zulip = new Zulip("http://localhost:1081", "[email protected]", secret); | ||
Zulip zulip = new Zulip("http://localhost:1081", "[email protected]", Secret.fromString("secret")); | ||
// Test that this does not throw exception | ||
zulip.sendStreamMessage("testStream", "testTopic", "testMessage"); | ||
} | ||
|
||
@Test | ||
public void testFailGracefullyUnknonwHost() { | ||
Zulip zulip = new Zulip("http://unreachable:1080", "[email protected]", secret); | ||
Zulip zulip = new Zulip("http://unreachable:1080", "[email protected]", Secret.fromString("secret")); | ||
// Test that this does not throw exception | ||
zulip.sendStreamMessage("testStream", "testTopic", "testMessage"); | ||
} | ||
|
||
@Test | ||
public void testSendsAuthorizationWhenRequested() { | ||
mockServer | ||
.when(request().withPath("/api/v1/messages").withHeader(NottableString.not(HttpHeaders.AUTHORIZATION))) | ||
.respond(response().withStatusCode(401).withHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic")); | ||
mockServer.when(request().withPath("/api/v1/messages")) | ||
.respond(response().withStatusCode(200)); | ||
|
||
Zulip zulip = new Zulip("http://localhost:1080", "[email protected]", Secret.fromString("secret")); | ||
zulip.sendStreamMessage("testStream", "testTopic", "testMessage"); | ||
|
||
mockServer.verify(request(), VerificationTimes.exactly(2)); | ||
mockServer.verify( | ||
request().withHeader(HttpHeaders.AUTHORIZATION, "Basic amVua2lucy1ib3RAenVsaXAuY29tOnNlY3JldA=="), | ||
VerificationTimes.once()); | ||
} | ||
|
||
@Test | ||
public void testSendsProxyAuthorizationWhenRequested() { | ||
jenkins.proxy = new ProxyConfiguration("localhost", 1080, "proxy-user", "proxy-password"); | ||
|
||
mockServer | ||
.when(request().withPath("/api/v1/messages") | ||
.withHeader(NottableString.not(HttpHeaders.PROXY_AUTHORIZATION))) | ||
.respond(response().withStatusCode(407).withHeader(HttpHeaders.PROXY_AUTHENTICATE, "Basic")); | ||
mockServer.when(request().withPath("/api/v1/messages")).respond(response().withStatusCode(200)); | ||
|
||
Zulip zulip = new Zulip("http://localhost:5000", "[email protected]", Secret.fromString("secret")); | ||
zulip.sendStreamMessage("testStream", "testTopic", "testMessage"); | ||
|
||
mockServer.verify(request(), VerificationTimes.exactly(2)); | ||
mockServer.verify( | ||
request().withHeader(HttpHeaders.PROXY_AUTHORIZATION, "Basic cHJveHktdXNlcjpwcm94eS1wYXNzd29yZA=="), | ||
VerificationTimes.once()); | ||
} | ||
|
||
} |