API 1.2.2.1 update password

This commit is contained in:
Tibor Bossanyi 2023-04-11 17:55:23 +02:00
parent 73d189a357
commit c3bc18fda5
2 changed files with 46 additions and 1 deletions

View File

@ -17,6 +17,7 @@ import java.time.LocalDateTime
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
import java.util.* import java.util.*
import javax.validation.Valid import javax.validation.Valid
import java.util.Base64
@RestController @RestController
@ -42,7 +43,6 @@ class CustomerController ( private val customerRepository: CustomerRepository) {
@Secured @Secured
@GetMapping("/customers/{id}") @GetMapping("/customers/{id}")
@CrossOrigin(origins = ["http://localhost:48102"])
fun getCustomerById(@PathVariable(value = "id") customerId: Long): ResponseEntity<Customer> { fun getCustomerById(@PathVariable(value = "id") customerId: Long): ResponseEntity<Customer> {
val customer: Customer? = customerRepository.findById(customerId).orElse(null) val customer: Customer? = customerRepository.findById(customerId).orElse(null)
return if (customer == null) ResponseEntity.notFound().build() else ResponseEntity.ok().body(customer) return if (customer == null) ResponseEntity.notFound().build() else ResponseEntity.ok().body(customer)
@ -90,6 +90,17 @@ class CustomerController ( private val customerRepository: CustomerRepository) {
return ResponseEntity.ok().body(customerRepository.save(returnCustomer)) return ResponseEntity.ok().body(customerRepository.save(returnCustomer))
} }
@Secured
@PostMapping("customers/update_password/{id}")
fun updateCustomerPasswordById(@PathVariable(value = "id") customerId: Long, @Valid @RequestBody passwordB64: String)
: ResponseEntity<Customer> {
val returnCustomer: Customer = customerRepository.findById(customerId).orElse(null)
?: return ResponseEntity.notFound().build()
returnCustomer.password = serviceBeans!!.passwordEncoder().encode(String(Base64.getDecoder().decode(passwordB64)))
return ResponseEntity.ok().body(customerRepository.save(returnCustomer))
}
@Secured @Secured
@PostMapping("customers/deactivate/{id}") @PostMapping("customers/deactivate/{id}")
fun deactivateCustomer(@PathVariable(value = "id") customerId: Long): ResponseEntity<Customer> { fun deactivateCustomer(@PathVariable(value = "id") customerId: Long): ResponseEntity<Customer> {

View File

@ -27,6 +27,7 @@ import java.time.format.DateTimeFormatter
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
import kotlin.test.assertTrue import kotlin.test.assertTrue
import java.util.Base64
@SpringBootTest @SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ -386,6 +387,39 @@ class CustomerTests {
assertEquals(customerId, 103) assertEquals(customerId, 103)
} }
@Test
fun `update password`() {
authToken = Tokenizer.getToken()
val id = 103
var password = Base64.getEncoder().encodeToString("andio20091".toByteArray())
val mvcResult: MvcResult = mockMvc.perform(
MockMvcRequestBuilders.post("/api/customers/update_password/$id")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(password)
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.jsonPath("$.name").value("Bos"))
.andExpect(MockMvcResultMatchers.jsonPath("$.firstname").value("Kakadu"))
.andExpect(MockMvcResultMatchers.jsonPath("$.birthYear").value(1972))
.andReturn()
val customerId = JSONObject(mvcResult.response.contentAsString).getInt("customerId")
println("MockCustomer Id $customerId")
assertEquals(customerId, 103)
password = Base64.getEncoder().encodeToString("andio2009".toByteArray())
mockMvc.perform(
MockMvcRequestBuilders.post("/api/customers/update_password/$id")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(password)
)
.andExpect(MockMvcResultMatchers.status().isOk)
}
private fun toJson(obj: Any): String { private fun toJson(obj: Any): String {
return Gson().toJson(obj) return Gson().toJson(obj)
} }