Skip to content

Commit

Permalink
Refactored account test mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoRosendo committed Jul 31, 2023
1 parent 630c569 commit 5dab80d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package pt.up.fe.ni.website.backend.controller

import pt.up.fe.ni.website.backend.model.constants.AccountConstants as Constants
import com.epages.restdocs.apispec.ResourceDocumentation.parameterWithName
import com.fasterxml.jackson.databind.ObjectMapper
import java.util.Calendar
import java.util.Date
import java.util.UUID
import org.junit.jupiter.api.AfterEach
import java.util.*
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
Expand All @@ -29,7 +28,6 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import pt.up.fe.ni.website.backend.config.upload.UploadConfigProperties
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.model.constants.AccountConstants as Constants
import pt.up.fe.ni.website.backend.repository.AccountRepository
import pt.up.fe.ni.website.backend.utils.TestUtils
import pt.up.fe.ni.website.backend.utils.ValidationTester
Expand Down Expand Up @@ -169,9 +167,17 @@ class AccountControllerTest @Autowired constructor(
@NestedTest
@DisplayName("POST /accounts/new")
inner class CreateAccount {
@AfterEach
fun clearAccounts() {
repository.deleteAll()
private val uuid: UUID = UUID.randomUUID()
private val mockedSettings = Mockito.mockStatic(UUID::class.java)

@BeforeAll
fun setupMocks() {
Mockito.`when`(UUID.randomUUID()).thenReturn(uuid)
}

@AfterAll
fun cleanup() {
mockedSettings.close()
}

@Test
Expand Down Expand Up @@ -238,10 +244,6 @@ class AccountControllerTest @Autowired constructor(

@Test
fun `should create the account with valid image`() {
val uuid: UUID = UUID.randomUUID()
val mockedSettings = Mockito.mockStatic(UUID::class.java)
Mockito.`when`(UUID.randomUUID()).thenReturn(uuid)

val expectedPhotoPath = "${uploadConfigProperties.staticServe}/profile/${testAccount.email}-$uuid.jpeg"

mockMvc.multipartBuilder("/accounts/new")
Expand All @@ -268,16 +270,10 @@ class AccountControllerTest @Autowired constructor(
// "This endpoint operation creates a new account.",
// documentRequestPayload = true
// )

mockedSettings.close()
}

@Test
fun `should fail to create account with invalid filename extension`() {
val uuid: UUID = UUID.randomUUID()
val mockedSettings = Mockito.mockStatic(UUID::class.java)
Mockito.`when`(UUID.randomUUID()).thenReturn(uuid)

mockMvc.multipartBuilder("/accounts/new")
.addPart("account", testAccount.toJson())
.addFile(filename = "photo.pdf")
Expand All @@ -290,16 +286,10 @@ class AccountControllerTest @Autowired constructor(
jsonPath("$.errors[0].param").value("createAccount.photo")
)
.andDocumentErrorResponse(documentation, hasRequestPayload = true)

mockedSettings.close()
}

@Test
fun `should fail to create account with invalid filename media type`() {
val uuid: UUID = UUID.randomUUID()
val mockedSettings = Mockito.mockStatic(UUID::class.java)
Mockito.`when`(UUID.randomUUID()).thenReturn(uuid)

mockMvc.multipartBuilder("/accounts/new")
.addPart("account", testAccount.toJson())
.addFile(contentType = MediaType.APPLICATION_PDF_VALUE)
Expand All @@ -311,8 +301,6 @@ class AccountControllerTest @Autowired constructor(
jsonPath("$.errors[0].message").value("invalid image type (png, jpg or jpeg)"),
jsonPath("$.errors[0].param").value("createAccount.photo")
)

mockedSettings.close()
}

@Test
Expand Down Expand Up @@ -714,12 +702,25 @@ class AccountControllerTest @Autowired constructor(
)
)

private val uuid: UUID = UUID.randomUUID()
private val mockedSettings = Mockito.mockStatic(UUID::class.java)

@BeforeEach
fun addAccounts() {
repository.save(testAccount)
repository.save(newAccount)
}

@BeforeAll
fun setupMocks() {
Mockito.`when`(UUID.randomUUID()).thenReturn(uuid)
}

@AfterAll
fun cleanup() {
mockedSettings.close()
}

private val parameters = listOf(parameterWithName("id").description("ID of the account to update"))

private val newName = "Test Account 2"
Expand Down Expand Up @@ -766,7 +767,7 @@ class AccountControllerTest @Autowired constructor(
// .andDocument(
// documentationNoPassword,
// "Update accounts",
// "Update a previously created account, with the exception of the password, using its ID (no image).",
// "Update a previously created account, except the password, using its ID (no image).",
// urlParameters = parameters,
// documentRequestPayload = true
// )
Expand All @@ -783,10 +784,6 @@ class AccountControllerTest @Autowired constructor(

@Test
fun `should update the account with valid image`() {
val uuid: UUID = UUID.randomUUID()
val mockedSettings = Mockito.mockStatic(UUID::class.java)
Mockito.`when`(UUID.randomUUID()).thenReturn(uuid)

val expectedPhotoPath = "${uploadConfigProperties.staticServe}/profile/$newEmail-$uuid.jpeg"

mockMvc.multipartBuilder("/accounts/${testAccount.id}")
Expand All @@ -811,7 +808,7 @@ class AccountControllerTest @Autowired constructor(
// .andDocument(
// documentationNoPassword,
// "Update accounts",
// "Update a previously created account, with the exception of the password, using its ID.",
// "Update a previously created account, except the password, using its ID.",
// urlParameters = parameters,
// documentRequestPayload = true
// )
Expand All @@ -825,20 +822,14 @@ class AccountControllerTest @Autowired constructor(
Assertions.assertEquals(newLinkedin, updatedAccount.linkedin)
Assertions.assertEquals(newWebsites[0].url, updatedAccount.websites[0].url)
Assertions.assertEquals(newWebsites[0].iconPath, updatedAccount.websites[0].iconPath)

mockedSettings.close()
}

@Test
fun `should fail to update account with invalid filename extension`() {
val uuid: UUID = UUID.randomUUID()
val mockedSettings = Mockito.mockStatic(UUID::class.java)
Mockito.`when`(UUID.randomUUID()).thenReturn(uuid)

mockMvc.multipartBuilder("/accounts/${testAccount.id}")
.asPutMethod()
.addPart("account", data)
.addFile(filename = "photo.pdf", contentType = MediaType.APPLICATION_PDF_VALUE)
.addFile(filename = "photo.pdf")
.perform()
.andExpectAll(
status().isBadRequest,
Expand All @@ -848,16 +839,10 @@ class AccountControllerTest @Autowired constructor(
jsonPath("$.errors[0].param").value("updateAccountById.photo")
)
.andDocumentErrorResponse(documentation, hasRequestPayload = true)

mockedSettings.close()
}

@Test
fun `should fail to update account with invalid filename media type`() {
val uuid: UUID = UUID.randomUUID()
val mockedSettings = Mockito.mockStatic(UUID::class.java)
Mockito.`when`(UUID.randomUUID()).thenReturn(uuid)

mockMvc.multipartBuilder("/accounts/${testAccount.id}")
.asPutMethod()
.addPart("account", data)
Expand All @@ -871,8 +856,6 @@ class AccountControllerTest @Autowired constructor(
jsonPath("$.errors[0].param").value("updateAccountById.photo")
)
.andDocumentErrorResponse(documentation, hasRequestPayload = true)

mockedSettings.close()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,13 @@ class GenerationControllerTest @Autowired constructor(
listOf(testAccount),
listOf(
buildTestPerActivityRole(
Project("NIJobs", "cool project", image = "cool-image.png", targetAudience = "students", github = "https://github.com/NIAEFEUP/nijobs-be")
Project(
"NIJobs",
"cool project",
image = "cool-image.png",
targetAudience = "students",
github = "https://github.com/NIAEFEUP/nijobs-be"
)
)
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AccountTest {
"NI Website",
"NI's website is where everything about NI is shown to the public",
image = "cool-image.jpg",
targetAudience = "Everyone",
targetAudience = "Everyone"

)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ class PayloadActivity {
"Information about the target audience",
JsonFieldType.STRING
),
DocumentedJSONField(
"slogan",
"Slogan of the activity",
JsonFieldType.STRING
),
DocumentedJSONField(
"github",
"Handle/link to the project's GitHub repository",
"Handle/link to the activity's GitHub repository",
JsonFieldType.STRING
),
DocumentedJSONField(
Expand Down

0 comments on commit 5dab80d

Please sign in to comment.