Skip to content

Commit

Permalink
fix documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jamcunha committed Jun 28, 2023
1 parent 3179ffd commit 413af20
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.bind.annotation.RestController
import pt.up.fe.ni.website.backend.dto.auth.PassRecoveryDto
import org.springframework.web.multipart.MultipartFile
import pt.up.fe.ni.website.backend.dto.auth.ChangePasswordDto
import pt.up.fe.ni.website.backend.dto.auth.PassRecoveryDto
import pt.up.fe.ni.website.backend.dto.entity.account.CreateAccountDto
import pt.up.fe.ni.website.backend.dto.entity.account.UpdateAccountDto
import pt.up.fe.ni.website.backend.model.Account
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package pt.up.fe.ni.website.backend.service

import java.time.Instant
import java.util.UUID
import org.springframework.data.repository.findByIdOrNull
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.oauth2.jwt.JwtDecoder
import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException
import org.springframework.stereotype.Service
import pt.up.fe.ni.website.backend.dto.auth.PassRecoveryDto
import pt.up.fe.ni.website.backend.model.Account
import pt.up.fe.ni.website.backend.repository.AccountRepository
import java.time.Instant
import org.springframework.web.multipart.MultipartFile
import pt.up.fe.ni.website.backend.dto.auth.ChangePasswordDto
import pt.up.fe.ni.website.backend.dto.auth.PassRecoveryDto
import pt.up.fe.ni.website.backend.dto.entity.account.CreateAccountDto
import pt.up.fe.ni.website.backend.dto.entity.account.UpdateAccountDto
import pt.up.fe.ni.website.backend.model.Account
import pt.up.fe.ni.website.backend.repository.AccountRepository
import pt.up.fe.ni.website.backend.service.upload.FileUploader
import pt.up.fe.ni.website.backend.utils.extensions.filenameExtension

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import pt.up.fe.ni.website.backend.utils.annotations.NestedTest
import pt.up.fe.ni.website.backend.utils.documentation.payloadschemas.model.PayloadAccount
import pt.up.fe.ni.website.backend.utils.documentation.utils.DocumentedJSONField
import pt.up.fe.ni.website.backend.utils.documentation.utils.MockMVCExtension.Companion.andDocument
import pt.up.fe.ni.website.backend.utils.documentation.utils.MockMVCExtension.Companion.andDocumentCustomRequestSchema
import pt.up.fe.ni.website.backend.utils.documentation.utils.MockMVCExtension.Companion.andDocumentCustomRequestSchemaEmptyResponse
import pt.up.fe.ni.website.backend.utils.documentation.utils.MockMVCExtension.Companion.andDocumentCustomRequestSchemaErrorResponse
import pt.up.fe.ni.website.backend.utils.documentation.utils.MockMVCExtension.Companion.andDocumentEmptyObjectResponse
Expand Down Expand Up @@ -915,7 +916,7 @@ class AccountControllerTest @Autowired constructor(
private val passwordRecoveryPayload = PayloadSchema(
"password-recover",
mutableListOf(
DocumentedJSONField("password", "The new password.", JsonFieldType.STRING),
DocumentedJSONField("password", "The new password.", JsonFieldType.STRING)
)
)

Expand All @@ -928,20 +929,21 @@ class AccountControllerTest @Autowired constructor(
fun `should update the password`() {
mockMvc.perform(post("/auth/recoverPassword/${testAccount.id}"))
.andReturn().response.let { authResponse ->
val recoveryLink = objectMapper.readTree(authResponse.contentAsString)["recovery_url"].asText()
.removePrefix("localhost:8080")
mockMvc.perform(put(recoveryLink)
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
mapOf(
"password" to newPassword
val recoveryToken = objectMapper.readTree(authResponse.contentAsString)["recovery_url"].asText()
.removePrefix("localhost:8080/accounts/recoverPassword/")
mockMvc.perform(
put("/accounts/recoverPassword/{recoveryToken}", recoveryToken)
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
mapOf(
"password" to newPassword
)
)
)
)
).andExpectAll(
status().isOk()
).andDocumentCustomRequestSchemaEmptyResponse(
).andDocumentCustomRequestSchema(
documentation,
passwordRecoveryPayload,
"Recover password",
Expand All @@ -954,15 +956,16 @@ class AccountControllerTest @Autowired constructor(

@Test
fun `should fail when token is invalid`() {
mockMvc.perform(put("/accounts/recoverPassword/invalid_token")
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
mapOf(
"password" to newPassword
mockMvc.perform(
put("/accounts/recoverPassword/invalid_token")
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
mapOf(
"password" to newPassword
)
)
)
)
).andExpectAll(
status().isUnauthorized(),
jsonPath("$.errors.length()").value(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class AuthControllerTest @Autowired constructor(
status().isNotFound(),
jsonPath("$.errors.length()").value(1),
jsonPath("$.errors[0].message").value("account not found with id 1234")
).andDocument(
).andDocumentErrorResponse(
documentation,
"Recover password",
"This endpoint operation allows the recovery of the password of an account, " +
Expand All @@ -213,7 +213,13 @@ class AuthControllerTest @Autowired constructor(
.andExpectAll(
status().isOk(),
jsonPath("$.recovery_url").exists()
).andDocumentErrorResponse(documentation)
).andDocument(
documentation,
"Recover password",
"This endpoint operation allows the recovery of the password of an account, " +
"sending an email with a link to reset the password.",
documentRequestPayload = false
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class PayloadRecoverPassword : ModelDocumentation(
"Id of the account",
JsonFieldType.NUMBER,
isInResponse = false
),
DocumentedJSONField(
"recovery_url",
"URL to recover the password",
JsonFieldType.STRING,
isInRequest = false
)
)
)
Expand Down

0 comments on commit 413af20

Please sign in to comment.