Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamcunha committed Feb 28, 2023
1 parent 36812a3 commit c067a58
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.get
import org.springframework.test.web.servlet.post
import org.springframework.test.web.servlet.put
import pt.up.fe.ni.website.backend.model.Account
import pt.up.fe.ni.website.backend.model.CustomWebsite
import pt.up.fe.ni.website.backend.repository.AccountRepository
Expand Down Expand Up @@ -419,6 +420,52 @@ class AccountControllerTest @Autowired constructor(
}
}

@NestedTest
@DisplayName("PUT /recoverPassword/{recoveryToken}")
inner class RecoverPassword {
private val newPassword = "new-password"

@BeforeEach
fun setup() {
repository.save(testAccount)
}

@Test
fun `should update the password`() {
mockMvc.post("/auth/recoverPassword/${testAccount.id}")
.andReturn().response.let { authResponse ->
val recoveryLink = objectMapper.readTree(authResponse.contentAsString)["recovery_url"].asText()
.removePrefix("localhost:8080")
mockMvc.put(recoveryLink) {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(
mapOf(
"password" to newPassword
)
)
}.andExpect {
status { isOk() }
}
}
}

@Test
fun `should fail when token is invalid`() {
mockMvc.put("/accounts/recoverPassword/invalid_token") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(
mapOf(
"password" to newPassword
)
)
}.andExpect {
status { isUnauthorized() }
jsonPath("$.errors.length()") { value(1) }
jsonPath("$.errors[0].message") { value("invalid password recovery token") }
}
}
}

fun Date?.toJson(): String {
val quotedDate = objectMapper.writeValueAsString(this)
// objectMapper adds quotes to the date, so remove them
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ class AuthControllerTest @Autowired constructor(
}
}

@NestedTest
@DisplayName("POST /auth/recoverPassword/{id}")
inner class RecoverPassword {
@BeforeEach
fun setup() {
repository.save(testAccount)
}

@Test
fun `should fail if id is not found`() {
mockMvc.post("/auth/recoverPassword/1234")
.andExpect {
status { isNotFound() }
jsonPath("$.errors.length()") { value(1) }
jsonPath("$.errors[0].message") { value("account not found with id 1234") }
}
}

@Test
fun `should return password recovery link`() {
mockMvc.post("/auth/recoverPassword/${testAccount.id}")
.andExpect {
status { isOk() }
jsonPath("$.recovery_url") { exists() }
}
}
}

@NestedTest
@DisplayName("GET /auth/check")
inner class CheckToken {
Expand Down

0 comments on commit c067a58

Please sign in to comment.