-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
268 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...in/kotlin/com/faforever/userservice/ui/view/exception/InvalidRegistrationExceptionView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.faforever.userservice.ui.view.exception | ||
|
||
import com.faforever.userservice.backend.registration.InvalidRegistrationException | ||
import com.faforever.userservice.ui.component.FafLogo | ||
import com.faforever.userservice.ui.layout.CardLayout | ||
import com.faforever.userservice.ui.layout.CompactVerticalLayout | ||
import com.vaadin.flow.component.html.H2 | ||
import com.vaadin.flow.component.html.Span | ||
import com.vaadin.flow.component.orderedlayout.FlexComponent | ||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout | ||
import com.vaadin.flow.router.BeforeEnterEvent | ||
import com.vaadin.flow.router.ErrorParameter | ||
import com.vaadin.flow.router.HasErrorParameter | ||
import com.vaadin.flow.router.ParentLayout | ||
import jakarta.servlet.http.HttpServletResponse | ||
|
||
@Suppress("unused") | ||
@ParentLayout(CardLayout::class) | ||
class InvalidRegistrationExceptionView : CompactVerticalLayout(), HasErrorParameter<InvalidRegistrationException> { | ||
|
||
private val errorLayout = HorizontalLayout() | ||
private val errorMessage = Span() | ||
|
||
init { | ||
val formHeader = HorizontalLayout() | ||
|
||
val formHeaderLeft = FafLogo() | ||
val formHeaderRight = H2(getTranslation("title.technicalError")) | ||
formHeader.add(formHeaderLeft, formHeaderRight) | ||
formHeader.alignItems = FlexComponent.Alignment.CENTER | ||
formHeader.justifyContentMode = FlexComponent.JustifyContentMode.CENTER | ||
formHeader.setId("form-header") | ||
formHeader.setWidthFull() | ||
|
||
add(formHeader) | ||
|
||
errorMessage.text = getTranslation("register.technicalError") | ||
|
||
errorLayout.setWidthFull() | ||
errorLayout.addClassNames("error", "error-info") | ||
errorLayout.add(errorMessage) | ||
errorLayout.alignItems = FlexComponent.Alignment.CENTER | ||
errorLayout.setVerticalComponentAlignment(FlexComponent.Alignment.CENTER) | ||
|
||
add(errorLayout) | ||
} | ||
|
||
override fun setErrorParameter(event: BeforeEnterEvent?, parameter: ErrorParameter<InvalidRegistrationException>?): Int { | ||
return HttpServletResponse.SC_INTERNAL_SERVER_ERROR | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/test/kotlin/com/faforever/userservice/backend/registration/RegistrationServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.faforever.userservice.backend.registration | ||
|
||
import com.faforever.userservice.backend.domain.DomainBlacklistRepository | ||
import com.faforever.userservice.backend.domain.IpAddress | ||
import com.faforever.userservice.backend.domain.NameRecordRepository | ||
import com.faforever.userservice.backend.domain.User | ||
import com.faforever.userservice.backend.domain.UserRepository | ||
import com.faforever.userservice.backend.security.FafTokenService | ||
import com.faforever.userservice.config.FafProperties | ||
import io.quarkus.mailer.MockMailbox | ||
import io.quarkus.test.junit.QuarkusTest | ||
import io.quarkus.test.junit.mockito.InjectMock | ||
import jakarta.inject.Inject | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.hasSize | ||
import org.hamcrest.Matchers.`is` | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import org.mockito.ArgumentMatchers.anyString | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
|
||
|
||
@QuarkusTest | ||
class RegistrationServiceTest { | ||
|
||
companion object { | ||
private const val username = "someUsername" | ||
private const val email = "[email protected]" | ||
private const val password = "somePassword" | ||
private val ipAddress = IpAddress("127.0.0.1") | ||
|
||
private val user = User(1, username, password, email, null) | ||
} | ||
|
||
@Inject | ||
private lateinit var registrationService: RegistrationService | ||
|
||
@Inject | ||
private lateinit var mailbox: MockMailbox | ||
|
||
@Inject | ||
private lateinit var fafProperties: FafProperties | ||
|
||
@InjectMock | ||
private lateinit var userRepository: UserRepository | ||
|
||
@InjectMock | ||
private lateinit var nameRecordRepository: NameRecordRepository | ||
|
||
@InjectMock | ||
private lateinit var domainBlacklistRepository: DomainBlacklistRepository | ||
|
||
@InjectMock | ||
private lateinit var fafTokenService: FafTokenService | ||
|
||
@BeforeEach | ||
fun setup() { | ||
mailbox.clear() | ||
} | ||
|
||
@Test | ||
fun registerSuccess() { | ||
registrationService.register(username, email) | ||
|
||
val sent = mailbox.getMailMessagesSentTo(email) | ||
assertThat(sent, hasSize(1)) | ||
val actual = sent[0] | ||
assertThat(actual.subject, `is`(fafProperties.account().registration().subject())) | ||
} | ||
|
||
@Test | ||
fun registerUsernameTaken() { | ||
whenever(userRepository.existsByUsername(anyString())).thenReturn(true) | ||
|
||
assertThrows<IllegalArgumentException> { registrationService.register(username, email) } | ||
} | ||
|
||
@Test | ||
fun registerUsernameReserved() { | ||
whenever(nameRecordRepository.existsByPreviousNameAndChangeTimeAfter(anyString(), any())).thenReturn(true) | ||
|
||
assertThrows<IllegalArgumentException> { registrationService.register(username, email) } | ||
} | ||
|
||
@Test | ||
fun registerEmailTaken() { | ||
whenever(userRepository.existsByEmail(anyString())).thenReturn(true) | ||
|
||
assertThrows<IllegalArgumentException> { registrationService.register(username, email) } | ||
} | ||
|
||
@Test | ||
fun registerEmailBlacklisted() { | ||
whenever(domainBlacklistRepository.existsByDomain(anyString())).thenReturn(true) | ||
|
||
assertThrows<IllegalArgumentException> { registrationService.register(username, email) } | ||
} | ||
|
||
@Test | ||
fun activateSuccess() { | ||
registrationService.activate(RegisteredUser(username, email), ipAddress, password) | ||
|
||
verify(userRepository).persist(any<User>()) | ||
} | ||
|
||
@Test | ||
fun activateUsernameTaken() { | ||
whenever(userRepository.existsByUsername(anyString())).thenReturn(true) | ||
|
||
assertThrows<IllegalArgumentException> { registrationService.activate(RegisteredUser(username, email), ipAddress, password) } | ||
} | ||
|
||
@Test | ||
fun activateUsernameReserved() { | ||
whenever(nameRecordRepository.existsByPreviousNameAndChangeTimeAfter(anyString(), any())).thenReturn(true) | ||
|
||
assertThrows<IllegalArgumentException> { registrationService.activate(RegisteredUser(username, email), ipAddress, password) } | ||
} | ||
|
||
@Test | ||
fun activateEmailTaken() { | ||
whenever(userRepository.existsByEmail(anyString())).thenReturn(true) | ||
|
||
assertThrows<IllegalArgumentException> { registrationService.activate(RegisteredUser(username, email), ipAddress, password) } | ||
} | ||
|
||
@Test | ||
fun activateEmailBlacklisted() { | ||
whenever(domainBlacklistRepository.existsByDomain(anyString())).thenReturn(true) | ||
|
||
assertThrows<IllegalArgumentException> { registrationService.activate(RegisteredUser(username, email), ipAddress, password) } | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/test/kotlin/com/faforever/userservice/backend/security/FAFTokenServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.faforever.userservice.backend.security | ||
|
||
import io.quarkus.test.junit.QuarkusTest | ||
import jakarta.inject.Inject | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.hasEntry | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import java.time.Duration | ||
|
||
@QuarkusTest | ||
class FAFTokenServiceTest { | ||
|
||
@Inject | ||
private lateinit var fafTokenService: FafTokenService | ||
|
||
@Test | ||
fun testTokenCreationAndParsing() { | ||
val attributes = mapOf("username" to "test", "email" to "[email protected]") | ||
val token = fafTokenService.createToken(FafTokenType.REGISTRATION, Duration.ofSeconds(60), attributes) | ||
val decodedAttributes = fafTokenService.getTokenClaims(FafTokenType.REGISTRATION, token) | ||
assertThat(decodedAttributes, hasEntry("username", "test")) | ||
assertThat(decodedAttributes, hasEntry("email", "[email protected]")) | ||
} | ||
|
||
@Test | ||
fun testTokenFailsWithWrongType() { | ||
val attributes = mapOf("username" to "test", "email" to "[email protected]") | ||
val token = fafTokenService.createToken(FafTokenType.REGISTRATION, Duration.ofSeconds(60), attributes) | ||
assertThrows<IllegalArgumentException> { | ||
fafTokenService.getTokenClaims( | ||
FafTokenType.LINK_TO_STEAM, | ||
token | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun testTokenFailsExpired() { | ||
val attributes = mapOf("username" to "test", "email" to "[email protected]") | ||
val token = fafTokenService.createToken(FafTokenType.REGISTRATION, Duration.ofSeconds(-60), attributes) | ||
assertThrows<IllegalArgumentException> { | ||
fafTokenService.getTokenClaims( | ||
FafTokenType.REGISTRATION, | ||
token | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun testTokenFailsCustomAction() { | ||
val attributes = mapOf("username" to "test", "email" to "[email protected]", "action" to "bad_action") | ||
assertThrows<IllegalArgumentException> { | ||
fafTokenService.createToken(FafTokenType.REGISTRATION, Duration.ofSeconds(60), attributes) | ||
} | ||
} | ||
|
||
} |