|
| 1 | +/* |
| 2 | + * Janssen Project software is available under the Apache License (2004). See http://www.apache.org/licenses/ for full text. |
| 3 | + * |
| 4 | + * Copyright (c) 2026, Janssen Project |
| 5 | + */ |
| 6 | + |
| 7 | +package io.jans.fido2.service.readiness; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 11 | +import static org.mockito.Mockito.when; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 18 | +import org.mockito.InjectMocks; |
| 19 | +import org.mockito.Mock; |
| 20 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.databind.JsonNode; |
| 23 | + |
| 24 | +import io.jans.fido2.model.conf.AndroidApp; |
| 25 | +import io.jans.fido2.model.conf.AppConfiguration; |
| 26 | +import io.jans.fido2.model.conf.Fido2Configuration; |
| 27 | +import io.jans.fido2.model.conf.IosApp; |
| 28 | +import io.jans.fido2.model.conf.RequestedParty; |
| 29 | +import io.jans.fido2.service.readiness.NativeReadinessService.NotFound; |
| 30 | + |
| 31 | +@ExtendWith(MockitoExtension.class) |
| 32 | +class AssociationFileExporterTest { |
| 33 | + |
| 34 | + private static final String FP = "AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99"; |
| 35 | + |
| 36 | + @Mock |
| 37 | + private AppConfiguration appConfiguration; |
| 38 | + |
| 39 | + @InjectMocks |
| 40 | + private AssociationFileExporter exporter; |
| 41 | + |
| 42 | + private RequestedParty party; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void setUp() { |
| 46 | + party = new RequestedParty(); |
| 47 | + party.setId("example.com"); |
| 48 | + Fido2Configuration config = new Fido2Configuration(); |
| 49 | + config.setRequestedParties(List.of(party)); |
| 50 | + when(appConfiguration.getFido2Configuration()).thenReturn(config); |
| 51 | + } |
| 52 | + |
| 53 | + private static AndroidApp android(String pkg, String... fingerprints) { |
| 54 | + AndroidApp app = new AndroidApp(); |
| 55 | + app.setPackageName(pkg); |
| 56 | + app.setSha256CertFingerprints(List.of(fingerprints)); |
| 57 | + return app; |
| 58 | + } |
| 59 | + |
| 60 | + private static IosApp ios(String team, String bundle) { |
| 61 | + IosApp app = new IosApp(); |
| 62 | + app.setTeamId(team); |
| 63 | + app.setBundleId(bundle); |
| 64 | + return app; |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void assetLinks_isTheBuilderOutput_forTheConfiguredAndroidApps() { |
| 69 | + party.setAndroidApps(List.of(android("com.example.app", FP.toLowerCase()))); |
| 70 | + |
| 71 | + JsonNode file = exporter.assetLinks("example.com"); |
| 72 | + |
| 73 | + assertEquals(ExpectedAssociationFileBuilder.assetLinks(party), file); |
| 74 | + assertEquals("com.example.app", file.get(0).path("target").path("package_name").asText()); |
| 75 | + assertEquals(FP, file.get(0).path("target").path("sha256_cert_fingerprints").get(0).asText()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void appleAppSiteAssociation_listsTeamIdDotBundleId() { |
| 80 | + party.setIosApps(List.of(ios("TEAM123456", "com.example.app"))); |
| 81 | + |
| 82 | + JsonNode file = exporter.appleAppSiteAssociation("example.com"); |
| 83 | + |
| 84 | + assertEquals("TEAM123456.com.example.app", file.path("webcredentials").path("apps").get(0).asText()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void unknownRp_isNotFound() { |
| 89 | + assertThrows(NotFound.class, () -> exporter.assetLinks("other.example")); |
| 90 | + assertThrows(NotFound.class, () -> exporter.appleAppSiteAssociation(null)); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void rpWithoutAppsOfThePlatform_hasNothingToExport() { |
| 95 | + party.setAndroidApps(List.of(android("com.example.app", FP))); |
| 96 | + party.setIosApps(List.of(ios("TEAM123456", " "))); |
| 97 | + |
| 98 | + assertThrows(NotFound.class, () -> exporter.appleAppSiteAssociation("example.com")); |
| 99 | + |
| 100 | + party.setAndroidApps(null); |
| 101 | + assertThrows(NotFound.class, () -> exporter.assetLinks("example.com")); |
| 102 | + } |
| 103 | +} |
0 commit comments