Skip to content

Commit

Permalink
Extend test for OicServerManualConfigurationTest
Browse files Browse the repository at this point in the history
  • Loading branch information
eva-mueller-coremedia committed Dec 25, 2024
1 parent 405dbfb commit 01a48ec
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package org.jenkinsci.plugins.oic;

import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.openid.connect.sdk.op.OIDCProviderMetadata;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.util.FormValidation;
import java.io.IOException;
import java.net.URISyntaxException;
import jenkins.security.FIPS140;
import org.hamcrest.Matcher;
import org.jenkinsci.plugins.oic.OicServerManualConfiguration.DescriptorImpl;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.WithoutJenkins;
import org.mockito.MockedStatic;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.jvnet.hudson.test.JenkinsMatchers.hasKind;
import static org.mockito.Mockito.mockStatic;

public class OicServerManualConfigurationTest {

Expand Down Expand Up @@ -91,6 +102,31 @@ public void doCheckEndSessionEndpoint() throws IOException {
assertThat(descriptor.doCheckEndSessionUrl("http://localhost.jwks"), hasKind(FormValidation.Kind.OK));
}

@Test
@WithoutJenkins
public void testProviderMetadataWithFips() throws Descriptor.FormException {
OicServerManualConfiguration config = new OicServerManualConfiguration("issuer", "t-url", "a-url");
try (MockedStatic<FIPS140> fips140Mock = mockStatic(FIPS140.class)) {
JWSAlgorithm.Family ed = JWSAlgorithm.Family.ED;
JWSAlgorithm arbitraryEdAlgorithm = (JWSAlgorithm) ed.toArray()[0];

fips140Mock.when(FIPS140::useCompliantAlgorithms).thenReturn(true);
OIDCProviderMetadata data = config.toProviderMetadata();
assertFalse(data.getIDTokenJWSAlgs().contains(arbitraryEdAlgorithm));

fips140Mock.when(FIPS140::useCompliantAlgorithms).thenReturn(false);
data = config.toProviderMetadata();
assertTrue(data.getIDTokenJWSAlgs().contains(arbitraryEdAlgorithm));
}
}

@Test
@WithoutJenkins
public void testProviderMetadataWithInvalidURI() throws Descriptor.FormException, URISyntaxException {
OicServerManualConfiguration config = new OicServerManualConfiguration("issuer", "t-url", "inv%alid");
assertThrows(IllegalStateException.class, () -> config.toProviderMetadata());
}

private static DescriptorImpl getDescriptor() {
return (DescriptorImpl) jenkinsRule.jenkins.getDescriptor(OicServerManualConfiguration.class);
}
Expand Down

0 comments on commit 01a48ec

Please sign in to comment.