Skip to content

Commit

Permalink
chore: Make tests faster
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCizmar committed Oct 11, 2024
1 parent c4daa3d commit 88a2a5f
Show file tree
Hide file tree
Showing 110 changed files with 540 additions and 149 deletions.
4 changes: 4 additions & 0 deletions backend/app/src/test/kotlin/io/tolgee/AuthTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import io.tolgee.model.Project
import io.tolgee.security.authentication.JwtService
import io.tolgee.security.thirdParty.GithubOAuthDelegate.GithubEmailResponse
import io.tolgee.testing.AbstractControllerTest
import io.tolgee.testing.ContextRecreatingTest
import io.tolgee.util.GitHubAuthUtil
import io.tolgee.util.GoogleAuthUtil
import io.tolgee.util.OAuth2AuthUtil
Expand All @@ -33,6 +34,7 @@ import org.springframework.web.client.RestTemplate
import java.util.*

@Transactional
@ContextRecreatingTest
class AuthTest : AbstractControllerTest() {
@Autowired
private val publicController: PublicController? = null
Expand Down Expand Up @@ -65,6 +67,7 @@ class AuthTest : AbstractControllerTest() {
}

@Test
@CleanDbBeforeMethod
fun generatesTokenForValidUser() {
val response = doAuthentication(initialUsername, initialPassword)
println(response.andReturn().response.contentAsString)
Expand All @@ -81,6 +84,7 @@ class AuthTest : AbstractControllerTest() {
}

@Test
@CleanDbBeforeMethod
fun userWithTokenHasAccess() {
val response =
doAuthentication(initialUsername, initialPassword)
Expand Down
3 changes: 2 additions & 1 deletion backend/app/src/test/kotlin/io/tolgee/PatAuthTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PatAuthTest : AbstractControllerTest() {
add("X-API-Key", "tgpat_${pat.token}")
},
).andIsOk.andAssertThatJson {
node("username").isEqualTo("franta")
node("username").isString.contains("franta")
}

val lastUsedAt = patService.get(pat.id).lastUsedAt
Expand Down Expand Up @@ -94,6 +94,7 @@ class PatAuthTest : AbstractControllerTest() {
private fun createUserWithPat(expiresAt: Date? = Date(Date().time + 10000)): Pat {
var pat: Pat? = null
testDataService.saveTestData {
makeUsernamesUnique = true
addUserAccount {
username = "franta"
}.build {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.tolgee.commandLineRunners.StartupImportCommandLineRunner
import io.tolgee.configuration.tolgee.ImportProperties
import io.tolgee.configuration.tolgee.TolgeeProperties
import io.tolgee.development.Base
import io.tolgee.testing.ContextRecreatingTest
import io.tolgee.testing.assert
import io.tolgee.testing.assertions.Assertions.assertThat
import org.junit.jupiter.api.BeforeAll
Expand All @@ -20,6 +21,7 @@ import org.springframework.core.io.Resource

@Suppress("LateinitVarOverridesLateinitVar")
@CleanDbBeforeClass
@ContextRecreatingTest
class StartupImportCommandLineRunnerTest : AbstractSpringTest() {
private lateinit var base: Base

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import io.tolgee.development.testDataBuilder.data.TranslationsTestData
import io.tolgee.fixtures.andIsOk
import io.tolgee.fixtures.retry
import io.tolgee.fixtures.waitForNotThrowing
import io.tolgee.testing.ContextRecreatingTest
import io.tolgee.testing.annotations.ProjectJWTAuthTestMethod
import io.tolgee.testing.assert
import org.junit.jupiter.api.BeforeEach
Expand All @@ -36,6 +37,7 @@ import javax.sql.DataSource
"tolgee.rate-limits.authentication-limits = false",
],
)
@ContextRecreatingTest
class StreamingBodyDatabasePoolHealthTest : ProjectAuthControllerTest("/v2/projects/") {
lateinit var testData: TranslationsTestData

Expand All @@ -45,6 +47,7 @@ class StreamingBodyDatabasePoolHealthTest : ProjectAuthControllerTest("/v2/proje
@BeforeEach
fun setupData() {
testData = TranslationsTestData()
testData.root.makeUsernamesUnique = true
testDataService.saveTestData(testData.root)

userAccount = testData.user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.tolgee.fixtures.node
import io.tolgee.fixtures.waitFor
import io.tolgee.fixtures.waitForNotThrowing
import io.tolgee.model.enums.TranslationState
import io.tolgee.testing.ContextRecreatingTest
import io.tolgee.testing.annotations.ProjectJWTAuthTestMethod
import io.tolgee.testing.assert
import net.javacrumbs.jsonunit.assertj.JsonAssert
Expand All @@ -32,6 +33,7 @@ import org.springframework.test.web.servlet.ResultActions
import java.math.BigDecimal
import java.util.function.Consumer

@ContextRecreatingTest
class ActivityLogTest : ProjectAuthControllerTest("/v2/projects/") {
private lateinit var testData: BaseTestData

Expand All @@ -55,6 +57,7 @@ class ActivityLogTest : ProjectAuthControllerTest("/v2/projects/") {
name = "key"
}
}
testData.root.makeUsernamesUnique = true
testDataService.saveTestData(testData.root)
projectSupplier = { testData.projectBuilder.self }
userAccount = testData.user
Expand Down Expand Up @@ -97,6 +100,7 @@ class ActivityLogTest : ProjectAuthControllerTest("/v2/projects/") {
fun `it returns params for batch job activity`() {
val testData = BatchJobsTestData()
val keys = testData.addTranslationOperationData(10)
testData.root.makeUsernamesUnique = true
testDataService.saveTestData(testData.root)
userAccount = testData.user
this.projectSupplier = { testData.projectBuilder.self }
Expand Down Expand Up @@ -171,7 +175,7 @@ class ActivityLogTest : ProjectAuthControllerTest("/v2/projects/") {
text = "t"
state = TranslationState.REVIEWED
}.self

testData.root.makeUsernamesUnique = true
testDataService.saveTestData(testData.root)
userAccount = testData.user
projectSupplier =
Expand Down Expand Up @@ -201,7 +205,7 @@ class ActivityLogTest : ProjectAuthControllerTest("/v2/projects/") {

private fun JsonAssert.isValidAuthor() {
node("id").isNumber
node("username").isEqualTo("test_username")
node("username").isString.contains("test_username")
node("name").isEqualTo("Franta")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.tolgee.api.v2.controllers

import io.tolgee.CleanDbBeforeMethod
import io.tolgee.development.testDataBuilder.data.AdministrationTestData
import io.tolgee.fixtures.andAssertThatJson
import io.tolgee.fixtures.andGetContentAsString
Expand All @@ -24,11 +25,13 @@ class AdministrationControllerTest : AuthorizedControllerTest() {
@BeforeEach
fun createData() {
testData = AdministrationTestData()
testData.root.makeUsernamesUnique = true
testDataService.saveTestData(testData.root)
userAccount = testData.admin
}

@Test
@CleanDbBeforeMethod
fun `returns organizations`() {
performAuthGet("/v2/administration/organizations").andPrettyPrint.andIsOk.andAssertThatJson {
node("_embedded.organizations") {
Expand All @@ -41,6 +44,7 @@ class AdministrationControllerTest : AuthorizedControllerTest() {
}

@Test
@CleanDbBeforeMethod
fun `returns users`() {
performAuthGet("/v2/administration/users").andPrettyPrint.andAssertThatJson {
node("_embedded.users") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AnnouncementControllerTest : AuthorizedControllerTest() {
@BeforeEach
fun createData() {
testData = ProjectsTestData()
testData.root.makeUsernamesUnique = true
testDataService.saveTestData(testData.root)
userAccount = testData.user
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.tolgee.model.enums.Scope
import io.tolgee.testing.AuthorizedControllerTest
import io.tolgee.testing.assert
import io.tolgee.testing.assertions.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
Expand All @@ -31,10 +32,16 @@ class ApiKeyControllerTest : AuthorizedControllerTest() {
@BeforeEach
fun createData() {
testData = ApiKeysTestData()
testData.root.makeUsernamesUnique = true
testDataService.saveTestData(testData.root)
userAccount = testData.user
}

@AfterEach
fun cleanup() {
testDataService.cleanApiKeys(testData.root)
}

@Test
fun `creates API key (without description and expiration)`() {
performAuthPost(
Expand All @@ -45,7 +52,7 @@ class ApiKeyControllerTest : AuthorizedControllerTest() {
),
).andIsOk.andPrettyPrint.andAssertThatJson {
node("key").isString.hasSizeGreaterThan(10)
node("username").isEqualTo("test_username")
node("username").isString.contains("test_username")
node("userFullName").isEqualTo("")
node("projectId").isNumber.isGreaterThan(BigDecimal(0))
node("id").isValidId
Expand All @@ -64,7 +71,7 @@ class ApiKeyControllerTest : AuthorizedControllerTest() {
),
).andIsOk.andPrettyPrint.andAssertThatJson {
node("key").isString.hasSizeGreaterThan(10)
node("username").isEqualTo("test_username")
node("username").isString.contains("test_username")
node("userFullName").isEqualTo("")
node("projectId").isNumber.isGreaterThan(BigDecimal(0))
node("id").isValidId
Expand Down Expand Up @@ -131,7 +138,7 @@ class ApiKeyControllerTest : AuthorizedControllerTest() {
node("[0]") {
node("key").isAbsent()
node("description").isEqualTo("test_......ta_39")
node("username").isEqualTo("franta")
node("username").isString.contains("franta")
node("userFullName").isEqualTo("Franta Dobrota")
node("projectId").isNumber.isGreaterThan(BigDecimal(0))
node("id").isValidId
Expand All @@ -151,7 +158,7 @@ class ApiKeyControllerTest : AuthorizedControllerTest() {
isArray.hasSize(1)
node("[0]") {
node("key").isAbsent()
node("username").isEqualTo("franta")
node("username").isString.contains("franta")
node("userFullName").isEqualTo("Franta Dobrota")
node("projectId").isNumber.isGreaterThan(BigDecimal(0))
node("id").isValidId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package io.tolgee.api.v2.controllers

import io.tolgee.constants.Message
import io.tolgee.development.testDataBuilder.data.AutoTranslateTestData
import io.tolgee.fixtures.*
import io.tolgee.fixtures.MachineTranslationTest
import io.tolgee.fixtures.andHasErrorMessage
import io.tolgee.fixtures.andIsBadRequest
import io.tolgee.fixtures.andIsForbidden
import io.tolgee.fixtures.andIsOk
import io.tolgee.model.enums.Scope
import io.tolgee.testing.ContextRecreatingTest
import io.tolgee.testing.annotations.ProjectApiKeyAuthTestMethod
import io.tolgee.testing.annotations.ProjectJWTAuthTestMethod
import io.tolgee.testing.assertions.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

@ContextRecreatingTest
class AutoTranslationControllerTest : MachineTranslationTest() {
companion object {
private const val INITIAL_BUCKET_CREDITS = 150000L
Expand All @@ -20,6 +26,7 @@ class AutoTranslationControllerTest : MachineTranslationTest() {
@BeforeEach
fun setup() {
testData = AutoTranslateTestData()
testData.root.makeUsernamesUnique = true
testData.disableAutoTranslating()
this.projectSupplier = { testData.project }
initMachineTranslationMocks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BigMetaControllerTest : ProjectAuthControllerTest("/v2/projects/"), Loggin
@BeforeEach
fun setup() {
testData = BigMetaTestData()
testData.root.makeUsernamesUnique = true
}

private fun saveTestDataAndPrepare() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class BusinessEventControllerTest : ProjectAuthControllerTest("/v2/projects/") {
@BeforeEach
fun setup() {
testData = BaseTestData()
testData.root.makeUsernamesUnique = true
testDataService.saveTestData(testData.root)
projectSupplier = { testData.projectBuilder.self }
userAccount = testData.user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class NamespaceControllerTest : ProjectAuthControllerTest("/v2/projects/") {
@BeforeEach
fun createData() {
testData = NamespacesTestData()
testData.root.makeUsernamesUnique = true
testDataService.saveTestData(testData.root)
userAccount = testData.user
projectSupplier = { testData.projectBuilder.self }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PatControllerTest : AuthorizedControllerTest() {
@BeforeEach
fun createData() {
testData = PatTestData()
testData.root.makeUsernamesUnique = true
testDataService.saveTestData(testData.root)
userAccount = testData.user
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PreferredOrganizationControllerTest : AuthorizedControllerTest() {
@BeforeEach
fun setup() {
testData = OrganizationTestData()
testData.root.makeUsernamesUnique = true
tolgeeProperties.authentication.userCanCreateOrganizations = true
testDataService.saveTestData(testData.root)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ProjectActivityControllerTest : ProjectAuthControllerTest("/v2/projects/")
@BeforeEach
fun setup() {
testData = ImportTestData()
testData.root.makeUsernamesUnique = true
testData.addManyTranslations()
testData.setAllResolved()
testData.setAllOverride()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ProjectStatsControllerTest : ProjectAuthControllerTest("/v2/projects/") {
fun setup() {
mockDate("2022-03-20")
testData = TranslationsTestData()
testData.root.makeUsernamesUnique = true
testData.addTranslationsWithStates()
testDataService.saveTestData(testData.root)
userAccount = testData.user
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.tolgee.api.v2.controllers

import io.tolgee.CleanDbBeforeClass
import io.tolgee.CleanDbBeforeMethod
import io.tolgee.dtos.request.GenerateSlugDto
import io.tolgee.fixtures.andAssertThatJson
import io.tolgee.fixtures.andIsOk
Expand All @@ -8,6 +10,7 @@ import io.tolgee.model.Project
import io.tolgee.testing.AuthorizedControllerTest
import org.junit.jupiter.api.Test

@CleanDbBeforeClass
class SlugControllerTest : AuthorizedControllerTest() {
@Test
fun testValidateOrganizationSlug() {
Expand Down Expand Up @@ -42,6 +45,7 @@ class SlugControllerTest : AuthorizedControllerTest() {
}

@Test
@CleanDbBeforeMethod
fun testGenerateOrganizationSlug() {
performAuthPost("/v2/slug/generate-organization", GenerateSlugDto("Hello world"))
.andIsOk.andAssertThatJson {
Expand Down
Loading

0 comments on commit 88a2a5f

Please sign in to comment.