From c3bc18fda52304c4e97473a546a92caa5b7fe1e5 Mon Sep 17 00:00:00 2001 From: Tibor Bossanyi Date: Tue, 11 Apr 2023 17:55:23 +0200 Subject: [PATCH] API 1.2.2.1 update password --- .../api/controller/CustomerController.kt | 13 ++++++- .../com/aitrainer/api/test/CustomerTests.kt | 34 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/aitrainer/api/controller/CustomerController.kt b/src/main/kotlin/com/aitrainer/api/controller/CustomerController.kt index 13c688d..1e155a8 100644 --- a/src/main/kotlin/com/aitrainer/api/controller/CustomerController.kt +++ b/src/main/kotlin/com/aitrainer/api/controller/CustomerController.kt @@ -17,6 +17,7 @@ import java.time.LocalDateTime import java.time.format.DateTimeFormatter import java.util.* import javax.validation.Valid +import java.util.Base64 @RestController @@ -42,7 +43,6 @@ class CustomerController ( private val customerRepository: CustomerRepository) { @Secured @GetMapping("/customers/{id}") - @CrossOrigin(origins = ["http://localhost:48102"]) fun getCustomerById(@PathVariable(value = "id") customerId: Long): ResponseEntity { val customer: Customer? = customerRepository.findById(customerId).orElse(null) 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)) } + @Secured + @PostMapping("customers/update_password/{id}") + fun updateCustomerPasswordById(@PathVariable(value = "id") customerId: Long, @Valid @RequestBody passwordB64: String) + : ResponseEntity { + 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 @PostMapping("customers/deactivate/{id}") fun deactivateCustomer(@PathVariable(value = "id") customerId: Long): ResponseEntity { diff --git a/src/test/kotlin/com/aitrainer/api/test/CustomerTests.kt b/src/test/kotlin/com/aitrainer/api/test/CustomerTests.kt index b76dad7..d9004ed 100644 --- a/src/test/kotlin/com/aitrainer/api/test/CustomerTests.kt +++ b/src/test/kotlin/com/aitrainer/api/test/CustomerTests.kt @@ -27,6 +27,7 @@ import java.time.format.DateTimeFormatter import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertTrue +import java.util.Base64 @SpringBootTest @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -386,6 +387,39 @@ class CustomerTests { 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 { return Gson().toJson(obj) }