Skip to content

Commit

Permalink
Adding a data class for "telegram users" + Kolin Spring test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
orchestr7 committed Oct 21, 2024
1 parent 2730c65 commit bfc65d7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,31 @@ class UserController(
) {
@GetMapping("/get")
fun getResults(
@RequestParam authDate: Long,
@RequestParam firstName: String,
@RequestParam lastName: String,
@RequestParam hash: String,
@RequestParam id: Long,
@RequestParam photoUrl: String,
@RequestParam username: String,
userDataFromTelegram: UserDataFromTelegram,
): ResponseEntity<Any> {
val user = UserDataFromTelegram(
authDate = authDate,
firstName = firstName,
lastName = lastName,
hash = hash,
id = id,
photoUrl = photoUrl,
username = username,
)
println("Received a request to get results from $user. Converted to map: ${user.convertToMap()}")

if(!telegramAuthService.isValidHash(user.convertToMap(), user.hash)) {
println("Validation unsuccessful for ${user.username}")
println("Received a request to get results from $userDataFromTelegram. Converted to map: ${userDataFromTelegram.convertToMap()}")

if(!telegramAuthService.isValidHash(userDataFromTelegram.convertToMap(), userDataFromTelegram.hash)) {
println("Validation unsuccessful for ${userDataFromTelegram.username}")
return ResponseEntity(HttpStatus.FORBIDDEN)
}

println("Validation successful for ${user.username}")
println("Validation successful for ${userDataFromTelegram.username}")

val responseUser = userService.findOrCreateUser(user)
val responseUser = userService.findOrCreateUser(userDataFromTelegram)
return ResponseEntity.status(HttpStatus.OK).body(responseUser.toDTO())
}

@GetMapping("/update")
fun submitAnswer(
@RequestParam authDate: Long,
@RequestParam firstName: String,
@RequestParam lastName: String,
@RequestParam hash: String,
@RequestParam id: Long,
@RequestParam photoUrl: String,
@RequestParam username: String,
@RequestParam isNextRound: Boolean
userDataFromTelegram: UserDataFromTelegram,
): ResponseEntity<Any> {
val user = UserDataFromTelegram(
authDate = authDate,
firstName = firstName,
lastName = lastName,
hash = hash,
id = id,
photoUrl = photoUrl,
username = username,
)
println("Received a request to get results from $user. Converted to map: ${user.convertToMap()}")

if(!telegramAuthService.isValidHash(user.convertToMap(), user.hash)) {
println("Received a request to get results from $userDataFromTelegram. " +
"Converted to map: ${userDataFromTelegram.convertToMap()}")

if(!telegramAuthService.isValidHash(userDataFromTelegram.convertToMap(), userDataFromTelegram.hash)) {
return ResponseEntity(HttpStatus.FORBIDDEN)
}

Expand Down
45 changes: 35 additions & 10 deletions backend/src/test/kotlin/ru/posidata/backend/UserControllerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,41 @@ package ru.posidata.backend

import ru.posidata.backend.controller.UserController

import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.springframework.http.HttpStatus
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import ru.posidata.backend.service.TelegramAuthService
import ru.posidata.common.ResourceType
import org.junit.jupiter.api.Assertions.*
import org.mockito.Mockito.verifyNoInteractions
import ru.posidata.backend.service.UserService
import kotlin.test.Test

class UserControllerTest {
@WebMvcTest(controllers = [UserController::class], excludeAutoConfiguration = [SecurityAutoConfiguration::class])
class MyControllerTest {

}
@Autowired
private lateinit var mockMvc: MockMvc

@MockBean
private lateinit var userService: UserService

@MockBean
private lateinit var telegramAuthService: TelegramAuthService

@Test
fun `test user controller`() {
mockMvc.perform(
get("/api/get")
.param("authDate", "1630454400000")
.param("firstName", "Andrey")
.param("lastName", "Kuleshov")
.param("hash", "abc123hash")
.param("id", "12345")
.param("photoUrl", "https://example.com/photo.jpg")
.param("username", "akuleshov7")
)
.andExpect(status().isForbidden)
}
}

0 comments on commit bfc65d7

Please sign in to comment.