Skip to content

Commit

Permalink
PRSD-NONE: Sets up Playwright (#3)
Browse files Browse the repository at this point in the history
* PRSD-NONE: Adds Playwright dependency

* PRSD-NONE: Adds integration test set-up

* PRSD-NONE: Replaces Selenium integration tests with Playwright ones

* PRSD-NONE: Bypasses Spring Security for integration tests

* PRSD-NONE: Re-enables method security in controller tests
  • Loading branch information
isobel-softwire authored Oct 10, 2024
1 parent 1be241f commit 310e433
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 125 deletions.
7 changes: 6 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ dependencies {
testImplementation("org.springframework.boot:spring-boot-testcontainers")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testImplementation("net.sourceforge.htmlunit:htmlunit")
testImplementation("org.seleniumhq.selenium:htmlunit-driver")
testImplementation("org.testcontainers:junit-jupiter")
testImplementation("org.testcontainers:postgresql")
testImplementation("org.springframework.security:spring-security-test")
testImplementation("com.microsoft.playwright:playwright:1.47.0")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

Expand Down Expand Up @@ -94,6 +94,11 @@ tasks.withType<KotlinCompile> {
dependsOn("copyBuiltAssets")
}

tasks.register<JavaExec>("playwright") {
classpath(sourceSets["test"].runtimeClasspath)
mainClass.set("com.microsoft.playwright.CLI")
}

buildscript {
repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package uk.gov.communities.prsdb.webapp.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import org.springframework.security.config.Customizer
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity
import org.springframework.security.config.annotation.web.builders.HttpSecurity
Expand All @@ -17,7 +18,6 @@ import uk.gov.communities.prsdb.webapp.services.UserRolesService
import kotlin.collections.HashSet

@Configuration
@EnableMethodSecurity
class CustomSecurityConfig {
@Bean
fun filterChain(http: HttpSecurity): SecurityFilterChain {
Expand Down Expand Up @@ -58,3 +58,8 @@ class CustomSecurityConfig {
}
}
}

@Configuration
@EnableMethodSecurity
@Profile("!INTEGRATION_TEST")
class EnableMethodSecurityConfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uk.gov.communities.prsdb.webapp
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
import org.springframework.context.annotation.Bean
import org.testcontainers.containers.GenericContainer
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.utility.DockerImageName

Expand All @@ -11,4 +12,8 @@ class TestcontainersConfiguration {
@Bean
@ServiceConnection
fun postgresContainer(): PostgreSQLContainer<*> = PostgreSQLContainer(DockerImageName.parse("postgres:latest"))

@Bean
@ServiceConnection(name = "redis")
fun redisContainer(): GenericContainer<*> = GenericContainer(DockerImageName.parse("redis:latest")).withExposedPorts(6379)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.context.WebApplicationContext
import uk.gov.communities.prsdb.webapp.config.CustomSecurityConfig
import uk.gov.communities.prsdb.webapp.config.EnableMethodSecurityConfig
import uk.gov.communities.prsdb.webapp.services.UserRolesService

@Import(CustomSecurityConfig::class)
@Import(CustomSecurityConfig::class, EnableMethodSecurityConfig::class)
abstract class ControllerTest(
private val context: WebApplicationContext,
) {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.communities.prsdb.webapp.integration

import com.microsoft.playwright.Page
import com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat
import com.microsoft.playwright.options.AriaRole
import org.junit.jupiter.api.Test

class CheckHomePageTests : IntegrationTest() {
@Test
fun `check a home for rent page renders`(page: Page) {
page.navigate("http://localhost:$port/check")
assertThat(page.getByRole(AriaRole.HEADING)).containsText("Check a home to rent")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.communities.prsdb.webapp.integration

import com.microsoft.playwright.Page
import com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat
import com.microsoft.playwright.options.AriaRole
import org.junit.jupiter.api.Test

class ErrorPageTests : IntegrationTest() {
@Test
fun `500 page renders when error controller path called`(page: Page) {
page.navigate("http://localhost:$port/error")
assertThat(page.getByRole(AriaRole.HEADING)).containsText("Sorry, there is a problem with the service")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package uk.gov.communities.prsdb.webapp.integration

import com.microsoft.playwright.junit.UsePlaywright
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.boot.test.web.server.LocalServerPort
import org.springframework.context.annotation.Import
import org.springframework.security.oauth2.client.endpoint.DefaultAuthorizationCodeTokenResponseClient
import org.springframework.security.oauth2.client.registration.ClientRegistration
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository
import org.springframework.security.oauth2.jwt.JwtDecoderFactory
import org.springframework.security.web.SecurityFilterChain
import org.springframework.test.context.ActiveProfiles
import uk.gov.communities.prsdb.webapp.TestcontainersConfiguration
import uk.gov.communities.prsdb.webapp.config.OneLoginConfig

@Import(TestcontainersConfiguration::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@UsePlaywright
@ActiveProfiles("INTEGRATION_TEST")
abstract class IntegrationTest {
@LocalServerPort
val port: Int = 0

@MockBean
lateinit var mockClientRegistrationRepository: ClientRegistrationRepository

@MockBean
lateinit var mockDefaultAuthorizationCodeTokenResponseClient: DefaultAuthorizationCodeTokenResponseClient

@MockBean
lateinit var jwtDecoderFactory: JwtDecoderFactory<ClientRegistration?>

@MockBean
lateinit var oneLoginConfig: OneLoginConfig

@MockBean
lateinit var securityFilterChain: SecurityFilterChain
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.communities.prsdb.webapp.integration

import com.microsoft.playwright.Page
import com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat
import com.microsoft.playwright.options.AriaRole
import org.junit.jupiter.api.Test

class RegisterHomePageTests : IntegrationTest() {
@Test
fun `register a home to rent page renders`(page: Page) {
page.navigate("http://localhost:$port/registration")
assertThat(page.getByRole(AriaRole.HEADING)).containsText("Register a home to rent")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.communities.prsdb.webapp.integration

import com.microsoft.playwright.Page
import com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat
import com.microsoft.playwright.options.AriaRole
import org.junit.jupiter.api.Test

class SearchRegisterPageTests : IntegrationTest() {
@Test
fun `search for private rented sector information page renders`(page: Page) {
page.navigate("http://localhost:$port/search")
assertThat(page.getByRole(AriaRole.HEADING)).containsText("Search for Private Rented Sector information")
}
}

0 comments on commit 310e433

Please sign in to comment.