Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(Auth): add tests for accessing authenticated BPDM API access #867

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions bpdm-common-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
Expand All @@ -92,22 +93,11 @@
<groupId>com.neovisionaries</groupId>
<artifactId>nv-i18n</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<!-- in kotlin, use mockk instead of mockito -->
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
<groupId>com.github.dasniko</groupId>
<artifactId>testcontainers-keycloak</artifactId>
<scope>compile</scope>
</dependency>



</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package org.eclipse.tractusx.bpdm.test.config

import org.eclipse.tractusx.bpdm.test.util.AuthAssertionHelper
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class AssertionConfiguration {

@Bean
fun authAssertionHelper(): AuthAssertionHelper {
return AuthAssertionHelper()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package org.eclipse.tractusx.bpdm.test.config

import org.eclipse.tractusx.bpdm.common.util.ClientConfigurationProperties
import org.eclipse.tractusx.bpdm.common.util.ConditionalOnBoundProperty
import org.eclipse.tractusx.bpdm.common.util.HasEnablingProperty
import org.eclipse.tractusx.bpdm.test.util.BpdmOAuth2ClientFactory
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository


@ConfigurationProperties(prefix = SelfClientConfigProperties.PREFIX)
data class SelfClientConfigProperties(
val securityEnabled: Boolean = false,
val oauth2ClientRegistration: String = "self-client"
) : HasEnablingProperty {
companion object {
const val PREFIX = "${ClientConfigurationProperties.PREFIX}.self"
}

override val enabled: Boolean
get() = securityEnabled
}

@Configuration
class SelfClientConfiguration{

@Bean
@ConditionalOnBoundProperty(SelfClientConfigProperties.PREFIX, SelfClientConfigProperties::class, true)
fun createAuth2ClientFactory(clientRegistrationRepository: ClientRegistrationRepository,
oAuth2AuthorizedClientService: OAuth2AuthorizedClientService
): BpdmOAuth2ClientFactory{
return BpdmOAuth2ClientFactory(clientRegistrationRepository, oAuth2AuthorizedClientService)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package org.eclipse.tractusx.bpdm.test.containers

import dasniko.testcontainers.keycloak.KeycloakContainer
import org.eclipse.tractusx.bpdm.test.config.SelfClientConfigProperties
import org.eclipse.tractusx.bpdm.test.containers.KeyCloakInitializer.Companion.keycloakContainer
import org.springframework.boot.test.util.TestPropertyValues
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.ConfigurableApplicationContext

class KeyCloakInitializer: ApplicationContextInitializer<ConfigurableApplicationContext>{
companion object{
val keycloakContainer: KeycloakContainer = KeycloakContainer("quay.io/keycloak/keycloak:22.0.5")
.withRealmImportFile("keycloak/CX-Central.json")

const val REALM = "CX-Central"
}

override fun initialize(applicationContext: ConfigurableApplicationContext) {
keycloakContainer.start()

TestPropertyValues.of(
"bpdm.security.auth-server-url=${keycloakContainer.authServerUrl.trimEnd('/')}",
"bpdm.security.realm=${REALM}"
).applyTo(applicationContext)
}
}

abstract class SelfClientInitializer: ApplicationContextInitializer<ConfigurableApplicationContext>{

abstract val clientId: String

override fun initialize(applicationContext: ConfigurableApplicationContext){
val realm = applicationContext.environment.getProperty("bpdm.security.realm")
val authServerUrl = applicationContext.environment.getProperty("bpdm.security.auth-server-url")

val client = keycloakContainer.keycloakAdminClient
val clientSecret = client.realm(realm).clients().findByClientId(clientId).first().secret

TestPropertyValues.of(
"${SelfClientConfigProperties.PREFIX}.security-enabled=true",
"${SelfClientConfigProperties.PREFIX}.oauth2-client-registration=self-client",
"spring.security.oauth2.client.provider.self-provider.issuer-uri=${authServerUrl}/realms/$realm",
"spring.security.oauth2.client.registration.self-client.authorization-grant-type=client_credentials",
"spring.security.oauth2.client.registration.self-client.client-id=$clientId",
"spring.security.oauth2.client.registration.self-client.client-secret=$clientSecret",
"spring.security.oauth2.client.registration.self-client.provider=self-provider",
).applyTo(applicationContext)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import org.eclipse.tractusx.bpdm.common.dto.GeoCoordinateDto
import org.eclipse.tractusx.bpdm.common.model.BusinessStateType
import org.eclipse.tractusx.bpdm.common.model.DeliveryServiceType
import org.eclipse.tractusx.bpdm.pool.api.model.*
import org.eclipse.tractusx.bpdm.pool.api.model.request.AddressPartnerCreateRequest
import org.eclipse.tractusx.bpdm.pool.api.model.request.LegalEntityPartnerCreateRequest
import org.eclipse.tractusx.bpdm.pool.api.model.request.SitePartnerCreateRequest
import org.eclipse.tractusx.bpdm.pool.api.model.request.*
import java.time.LocalDateTime
import java.time.ZoneOffset
import kotlin.random.Random
Expand All @@ -51,57 +49,83 @@ class BusinessPartnerRequestFactory(
): LegalEntityPartnerCreateRequest {
val longSeed = seed.hashCode().toLong()
val random = Random(longSeed)
val timeStamp = LocalDateTime.ofEpochSecond(random.nextLong(0, 365241780471), random.nextInt(0, 999999999), ZoneOffset.UTC)

return LegalEntityPartnerCreateRequest(
legalEntity = LegalEntityDto(
legalName = "Legal Name $seed",
legalShortName = "Legal Short Name $seed",
legalForm = availableLegalForms.random(random),
identifiers = listOf(availableLegalEntityIdentifiers.random(random), availableLegalEntityIdentifiers.random(random))
.mapIndexed { index, idKey -> LegalEntityIdentifierDto("$idKey Value $seed $index", idKey, "$idKey Issuing Body $seed") },
states = listOf(
LegalEntityStateDto(validFrom = timeStamp, validTo = timeStamp.plusDays(10), BusinessStateType.ACTIVE),
LegalEntityStateDto(validFrom = timeStamp.plusDays(10), validTo = null, BusinessStateType.INACTIVE),
),
confidenceCriteria = ConfidenceCriteriaDto(
sharedByOwner = true,
checkedByExternalDataSource = false,
numberOfSharingMembers = 1,
lastConfidenceCheckAt = timeStamp,
nextConfidenceCheckAt = timeStamp.plusDays(7),
confidenceLevel = 5
),
isCatenaXMemberData = isCatenaXMemberData
),
legalEntity = createLegalEntityDto(seed, random, isCatenaXMemberData),
legalAddress = createAddressDto(seed, random),
index = seed
)
}

fun createSiteRequest(seed: String, bpnlParent: String): SitePartnerCreateRequest {
fun createLegalEntityUpdateRequest(seed: String, bpnl: String): LegalEntityPartnerUpdateRequest {
val longSeed = seed.hashCode().toLong()
val random = Random(longSeed)

return LegalEntityPartnerUpdateRequest(
bpnl = bpnl,
legalEntity = createLegalEntityDto(seed, random),
legalAddress = createAddressDto(seed, random)
)
}

fun createLegalEntityDto(seed: String, random: Random = Random(seed.hashCode().toLong()), isCatenaXMemberData: Boolean = true): LegalEntityDto {
val timeStamp = LocalDateTime.ofEpochSecond(random.nextLong(0, 365241780471), random.nextInt(0, 999999999), ZoneOffset.UTC)

return LegalEntityDto(
legalName = "Legal Name $seed",
legalShortName = "Legal Short Name $seed",
legalForm = availableLegalForms.randomOrNull(random),
identifiers = listOf(availableLegalEntityIdentifiers.randomOrNull(random), availableLegalEntityIdentifiers.randomOrNull(random))
.mapNotNull{ it }
.mapIndexed { index, idKey -> LegalEntityIdentifierDto("$idKey Value $seed $index", idKey, "$idKey Issuing Body $seed") },
states = listOf(
LegalEntityStateDto(validFrom = timeStamp, validTo = timeStamp.plusDays(10), BusinessStateType.ACTIVE),
LegalEntityStateDto(validFrom = timeStamp.plusDays(10), validTo = null, BusinessStateType.INACTIVE),
),
confidenceCriteria = ConfidenceCriteriaDto(
sharedByOwner = true,
checkedByExternalDataSource = false,
numberOfSharingMembers = 1,
lastConfidenceCheckAt = timeStamp,
nextConfidenceCheckAt = timeStamp.plusDays(7),
confidenceLevel = 5
),
isCatenaXMemberData = isCatenaXMemberData
)
}

fun createSiteRequest(seed: String, bpnlParent: String): SitePartnerCreateRequest {
return SitePartnerCreateRequest(
bpnlParent = bpnlParent,
index = seed,
site = SiteDto(
name = "Site Name $seed",
states = listOf(
SiteStateDto(validFrom = timeStamp, validTo = timeStamp.plusDays(10), BusinessStateType.ACTIVE),
SiteStateDto(validFrom = timeStamp.plusDays(10), validTo = null, BusinessStateType.INACTIVE),
),
mainAddress = createAddressDto(seed, random),
confidenceCriteria = ConfidenceCriteriaDto(
sharedByOwner = true,
checkedByExternalDataSource = false,
numberOfSharingMembers = 2,
lastConfidenceCheckAt = timeStamp.plusDays(10),
nextConfidenceCheckAt = timeStamp.plusDays(20),
confidenceLevel = 4
)
site = createSiteDto(seed)
)
}

fun createSiteUpdateRequest(seed: String, bpns: String): SitePartnerUpdateRequest {
return SitePartnerUpdateRequest(
bpns = bpns,
site = createSiteDto(seed)
)
}

fun createSiteDto(seed: String, random: Random = Random(seed.hashCode().toLong())): SiteDto{
val timeStamp = LocalDateTime.ofEpochSecond(random.nextLong(0, 365241780471), random.nextInt(0, 999999999), ZoneOffset.UTC)

return SiteDto(
name = "Site Name $seed",
states = listOf(
SiteStateDto(validFrom = timeStamp, validTo = timeStamp.plusDays(10), BusinessStateType.ACTIVE),
SiteStateDto(validFrom = timeStamp.plusDays(10), validTo = null, BusinessStateType.INACTIVE),
),
mainAddress = createAddressDto(seed, random),
confidenceCriteria = ConfidenceCriteriaDto(
sharedByOwner = true,
checkedByExternalDataSource = false,
numberOfSharingMembers = 2,
lastConfidenceCheckAt = timeStamp.plusDays(10),
nextConfidenceCheckAt = timeStamp.plusDays(20),
confidenceLevel = 4
)
)
}
Expand All @@ -117,20 +141,25 @@ class BusinessPartnerRequestFactory(
)
}

private fun createAddressDto(seed: String, random: Random): LogisticAddressDto {
fun createAddressUpdateRequest(seed: String, bpna: String): AddressPartnerUpdateRequest {
return AddressPartnerUpdateRequest(bpna, createAddressDto(seed))
}

fun createAddressDto(seed: String, random: Random = Random(seed.hashCode().toLong())): LogisticAddressDto {
val timeStamp = LocalDateTime.ofEpochSecond(random.nextLong(0, 365241780471), random.nextInt(0, 999999999), ZoneOffset.UTC)
return LogisticAddressDto(
name = "Address Name $seed",
states = listOf(
AddressStateDto(validFrom = timeStamp, validTo = timeStamp.plusDays(10), BusinessStateType.ACTIVE),
AddressStateDto(validFrom = timeStamp.plusDays(10), validTo = null, BusinessStateType.INACTIVE),
),
identifiers = listOf(availableAddressIdentifiers.random(random), availableAddressIdentifiers.random(random))
identifiers = listOf(availableAddressIdentifiers.randomOrNull(random), availableAddressIdentifiers.randomOrNull(random))
.mapNotNull { it }
.mapIndexed { index, idKey -> AddressIdentifierDto("$idKey Value $seed $index", idKey) },
physicalPostalAddress = PhysicalPostalAddressDto(
geographicCoordinates = GeoCoordinateDto(longitude = random.nextFloat(), latitude = random.nextFloat(), altitude = random.nextFloat()),
country = CountryCode.entries.random(random),
administrativeAreaLevel1 = availableAdminAreas.random(random),
administrativeAreaLevel1 = availableAdminAreas.randomOrNull(random),
administrativeAreaLevel2 = "Admin Level 2 $seed",
administrativeAreaLevel3 = "Admin Level 3 $seed",
postalCode = "Postal Code $seed",
Expand All @@ -156,7 +185,7 @@ class BusinessPartnerRequestFactory(
alternativePostalAddress = AlternativePostalAddressDto(
geographicCoordinates = GeoCoordinateDto(longitude = random.nextFloat(), latitude = random.nextFloat(), altitude = random.nextFloat()),
country = CountryCode.entries.random(random),
administrativeAreaLevel1 = availableAdminAreas.random(random),
administrativeAreaLevel1 = availableAdminAreas.randomOrNull(random),
postalCode = "Postal Code $seed",
city = "City $seed",
deliveryServiceNumber = "Delivery Service Number $seed",
Expand Down
Loading
Loading