-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HAI-1526 Send hanke invitation emails
Include the email sending feature to creation of new hanke users. Note: this commit does not include sending of application invitations. It will be implemented on another commit.
- Loading branch information
Showing
8 changed files
with
201 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,18 +163,6 @@ class EmailSenderServiceITest : DatabaseTest() { | |
contains("""<a href="http://localhost:3001/${data.invitationToken}">""") | ||
} | ||
} | ||
|
||
@Test | ||
fun `sendHankeInvitationEmail handles input without inviter name`() { | ||
val data = hankeInvitationData(inviterName = null) | ||
|
||
emailSenderService.sendHankeInvitationEmail(data) | ||
|
||
val email = greenMail.firstReceivedMessage() | ||
val (textBody, htmlBody) = getBodiesFromHybridEmail(email) | ||
assertThat(textBody).contains("Asioija ${data.inviterEmail}") | ||
assertThat(htmlBody).contains("Asioija ${data.inviterEmail}") | ||
} | ||
} | ||
|
||
@Nested | ||
|
@@ -229,18 +217,6 @@ class EmailSenderServiceITest : DatabaseTest() { | |
contains("""<a href="http://localhost:3001">""") | ||
} | ||
} | ||
|
||
@Test | ||
fun `sendApplicationInvitationEmail handles input without inviter name`() { | ||
val data = applicationInvitationData(inviterName = null) | ||
|
||
emailSenderService.sendApplicationInvitationEmail(data) | ||
|
||
val email = greenMail.firstReceivedMessage() | ||
val (textBody, htmlBody) = getBodiesFromHybridEmail(email) | ||
assertThat(textBody).contains("Asioija ${data.inviterEmail} on tehnyt") | ||
assertThat(htmlBody).contains("Asioija ${data.inviterEmail} on tehnyt") | ||
} | ||
} | ||
|
||
/** Returns a (text body, HTML body) pair. */ | ||
|
@@ -270,7 +246,7 @@ class EmailSenderServiceITest : DatabaseTest() { | |
return Pair(bodies[0], bodies[1]) | ||
} | ||
|
||
private fun hankeInvitationData(inviterName: String? = DEFAULT_INVITER_NAME) = | ||
private fun hankeInvitationData(inviterName: String = DEFAULT_INVITER_NAME) = | ||
HankeInvitationData( | ||
inviterName = inviterName, | ||
inviterEmail = "[email protected]", | ||
|
@@ -280,7 +256,7 @@ class EmailSenderServiceITest : DatabaseTest() { | |
invitationToken = "MgtzRbcPsvoKQamnaSxCnmW7", | ||
) | ||
|
||
private fun applicationInvitationData(inviterName: String? = DEFAULT_INVITER_NAME) = | ||
private fun applicationInvitationData(inviterName: String = DEFAULT_INVITER_NAME) = | ||
ApplicationInvitationData( | ||
inviterName = inviterName, | ||
inviterEmail = "[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,26 +13,40 @@ import assertk.assertions.hasSize | |
import assertk.assertions.isEmpty | ||
import assertk.assertions.isEqualTo | ||
import assertk.assertions.isIn | ||
import assertk.assertions.isNotEmpty | ||
import assertk.assertions.isNotNull | ||
import assertk.assertions.isNull | ||
import assertk.assertions.isTrue | ||
import assertk.assertions.matches | ||
import assertk.assertions.messageContains | ||
import com.fasterxml.jackson.databind.util.ClassUtil.hasClass | ||
import com.ninjasquad.springmockk.MockkBean | ||
import fi.hel.haitaton.hanke.DatabaseTest | ||
import fi.hel.haitaton.hanke.email.EmailSenderService | ||
import fi.hel.haitaton.hanke.email.HankeInvitationData | ||
import fi.hel.haitaton.hanke.factory.AlluDataFactory | ||
import fi.hel.haitaton.hanke.factory.AlluDataFactory.Companion.defaultApplicationName | ||
import fi.hel.haitaton.hanke.factory.AlluDataFactory.Companion.teppoEmail | ||
import fi.hel.haitaton.hanke.factory.AlluDataFactory.Companion.withContacts | ||
import fi.hel.haitaton.hanke.factory.HankeFactory | ||
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withGeneratedOmistaja | ||
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withYhteystiedot | ||
import fi.hel.haitaton.hanke.factory.HankeYhteystietoFactory | ||
import fi.hel.haitaton.hanke.factory.TEPPO_TESTI | ||
import fi.hel.haitaton.hanke.logging.AuditLogRepository | ||
import fi.hel.haitaton.hanke.logging.ObjectType | ||
import fi.hel.haitaton.hanke.logging.Operation | ||
import fi.hel.haitaton.hanke.logging.UserRole | ||
import fi.hel.haitaton.hanke.test.Asserts.isRecent | ||
import fi.hel.haitaton.hanke.toChangeLogJsonString | ||
import io.mockk.checkUnnecessaryStub | ||
import io.mockk.clearAllMocks | ||
import io.mockk.confirmVerified | ||
import io.mockk.justRun | ||
import io.mockk.verify | ||
import java.time.OffsetDateTime | ||
import java.util.UUID | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
|
@@ -51,6 +65,7 @@ const val kayttajaTunnistePattern = "[a-zA-z0-9]{24}" | |
class HankeKayttajaServiceITest : DatabaseTest() { | ||
|
||
@Autowired private lateinit var hankeKayttajaService: HankeKayttajaService | ||
@Autowired private lateinit var permissionService: PermissionService | ||
|
||
@Autowired private lateinit var hankeFactory: HankeFactory | ||
|
||
|
@@ -59,7 +74,18 @@ class HankeKayttajaServiceITest : DatabaseTest() { | |
@Autowired private lateinit var permissionRepository: PermissionRepository | ||
@Autowired private lateinit var auditLogRepository: AuditLogRepository | ||
|
||
@Autowired private lateinit var permissionService: PermissionService | ||
@MockkBean private lateinit var emailSenderService: EmailSenderService | ||
|
||
@BeforeEach | ||
fun setup() { | ||
clearAllMocks() | ||
} | ||
|
||
@AfterEach | ||
fun tearDown() { | ||
checkUnnecessaryStub() | ||
confirmVerified(emailSenderService) | ||
} | ||
|
||
@Nested | ||
inner class GetKayttajatByHankeId { | ||
|
@@ -95,6 +121,61 @@ class HankeKayttajaServiceITest : DatabaseTest() { | |
} | ||
} | ||
|
||
@Nested | ||
inner class GetKayttajaByCurrentUser { | ||
|
||
@Test | ||
fun `When user exists should return current hanke user`() { | ||
val hankeWithApplications = hankeFactory.saveGenerated(userId = USERNAME) | ||
|
||
val result: HankeKayttajaDto? = | ||
hankeKayttajaService.getKayttajaByCurrentUser( | ||
hankeId = hankeWithApplications.hanke.id!! | ||
) | ||
|
||
assertThat(result).isNotNull() | ||
with(result!!) { | ||
assertThat(id).isNotNull() | ||
assertThat(sahkoposti).isEqualTo(teppoEmail) | ||
assertThat(nimi).isEqualTo(TEPPO_TESTI) | ||
assertThat(kayttooikeustaso).isEqualTo(Kayttooikeustaso.KAIKKI_OIKEUDET) | ||
assertThat(tunnistautunut).isTrue() | ||
} | ||
} | ||
|
||
@Test | ||
fun `When no hanke should return null`() { | ||
val result: HankeKayttajaDto? = | ||
hankeKayttajaService.getKayttajaByCurrentUser(hankeId = 123) | ||
|
||
assertThat(result).isNull() | ||
} | ||
|
||
@Test | ||
fun `When no related permission should return null`() { | ||
val hanke = hankeFactory.save() | ||
permissionRepository.deleteAll() | ||
|
||
val result: HankeKayttajaDto? = | ||
hankeKayttajaService.getKayttajaByCurrentUser(hankeId = hanke.id!!) | ||
|
||
assertThat(result).isNull() | ||
} | ||
|
||
@Test | ||
fun `When no kayttaja should return null`() { | ||
val hankeWithApplications = hankeFactory.saveGenerated(userId = USERNAME) | ||
val hankeId = hankeWithApplications.hanke.id!! | ||
val jou = hankeKayttajaService.getKayttajaByCurrentUser(hankeId)!! | ||
hankeKayttajaRepository.deleteById(jou.id) | ||
|
||
val result: HankeKayttajaDto? = | ||
hankeKayttajaService.getKayttajaByCurrentUser(hankeId = hankeId) | ||
|
||
assertThat(result).isNull() | ||
} | ||
} | ||
|
||
@Nested | ||
inner class AddHankeFounder { | ||
private val perustaja = HankeFactory.defaultPerustaja | ||
|
@@ -371,6 +452,27 @@ class HankeKayttajaServiceITest : DatabaseTest() { | |
"[email protected]", | ||
) | ||
} | ||
|
||
@Test | ||
fun `Sends emails for new hanke users`() { | ||
val hanke = hankeFactory.saveGenerated(userId = USERNAME).hanke | ||
val hankeWithYhteystiedot = hanke.withYhteystiedot() // 4 sub contacts | ||
val capturedEmails = mutableListOf<HankeInvitationData>() | ||
justRun { emailSenderService.sendHankeInvitationEmail(capture(capturedEmails)) } | ||
|
||
hankeKayttajaService.saveNewTokensFromHanke(hankeWithYhteystiedot) | ||
|
||
verify(exactly = 4) { emailSenderService.sendHankeInvitationEmail(any()) } | ||
assertThat(capturedEmails).each { inv -> | ||
inv.transform { it.inviterName }.isEqualTo(TEPPO_TESTI) | ||
inv.transform { it.inviterEmail }.isEqualTo(teppoEmail) | ||
inv.transform { it.recipientEmail } | ||
.isIn("yhteys-email1", "yhteys-email2", "yhteys-email3", "yhteys-email4") | ||
inv.transform { it.hankeTunnus }.isEqualTo(hanke.hankeTunnus!!) | ||
inv.transform { it.hankeNimi }.isEqualTo(defaultApplicationName) | ||
inv.transform { it.invitationToken }.isNotEmpty() | ||
} | ||
} | ||
} | ||
|
||
@Nested | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.