Skip to content

Commit

Permalink
feat: add email requests limit test
Browse files Browse the repository at this point in the history
  • Loading branch information
huglx committed Jul 29, 2024
1 parent d69574a commit 2e662e9
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import io.tolgee.constants.Caches
import io.tolgee.fixtures.andIsOk
import io.tolgee.fixtures.andIsRateLimited
import io.tolgee.fixtures.andIsUnauthorized
import io.tolgee.security.ratelimit.RateLimitedException
import io.tolgee.testing.AuthorizedControllerTest
import io.tolgee.testing.ContextRecreatingTest
import jakarta.servlet.http.HttpServletRequest
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.mockito.Mockito
import org.mockito.kotlin.whenever
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest

Expand All @@ -20,6 +25,8 @@ import org.springframework.boot.test.context.SpringBootTest
"tolgee.rate-limits.ip-request-window=10000",
"tolgee.rate-limits.user-request-limit=15",
"tolgee.rate-limits.user-request-window=10000",
"tolgee.rate-limits.email-verification-request-limit=5",
"tolgee.rate-limits.email-verification-request-window=10000",
],
)
class RateLimitsTest : AuthorizedControllerTest() {
Expand All @@ -28,6 +35,20 @@ class RateLimitsTest : AuthorizedControllerTest() {
cacheManager.getCache(Caches.RATE_LIMITS)?.clear()
}

@Test
fun `email verification request limit works`() {
val createUser = dbPopulator.createUserIfNotExists(initialUsername)
val mockedRequest = Mockito.mock<HttpServletRequest>()
whenever(mockedRequest.remoteAddr).thenAnswer { "0.0.0.0" }

(0..4).forEach { _ ->
emailVerificationService.resendEmailVerification(createUser, mockedRequest, newEmail = "newEmail@gmail.com")
}
assertThrows<RateLimitedException> {
emailVerificationService.resendEmailVerification(createUser, mockedRequest, newEmail = "newEmail@gmail.com")
}
}

@Test
fun `ip request limit works`() {
(1..10).forEach { _ ->
Expand Down

0 comments on commit 2e662e9

Please sign in to comment.