Implement Mailer
This commit is contained in:
@@ -18,6 +18,8 @@ class Config {
|
||||
var username: String = config.getString("db.username")
|
||||
var password: String = config.getString("db.password")
|
||||
val port: Int = config.getInt("db.port")
|
||||
|
||||
val sendGridKey: String = config.getString("mail.sendGrid.key")
|
||||
}
|
||||
|
||||
object JwtConfig {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package fr.dcproject
|
||||
|
||||
import fr.dcproject.messages.Mailer
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.migration.Migrations
|
||||
@@ -47,4 +48,6 @@ val Module = module {
|
||||
single { VoteCommentRepository(get()) }
|
||||
|
||||
single { Migrations(connection = get(), directory = config.sqlFiles) }
|
||||
|
||||
single { Mailer(config.sendGridKey) }
|
||||
}
|
||||
|
||||
34
src/main/kotlin/fr/dcproject/messages/Mailer.kt
Normal file
34
src/main/kotlin/fr/dcproject/messages/Mailer.kt
Normal file
@@ -0,0 +1,34 @@
|
||||
package fr.dcproject.messages
|
||||
|
||||
import com.sendgrid.Method
|
||||
import com.sendgrid.Request
|
||||
import com.sendgrid.SendGrid
|
||||
import com.sendgrid.helpers.mail.Mail
|
||||
import com.sendgrid.helpers.mail.objects.Content
|
||||
import com.sendgrid.helpers.mail.objects.Email
|
||||
import java.io.IOException
|
||||
|
||||
class Mailer (
|
||||
private val key: String
|
||||
) {
|
||||
fun sendEmail(from: String, to: String, content: String, subject: String): Boolean {
|
||||
val mail = Mail(
|
||||
Email(from),
|
||||
subject,
|
||||
Email(to),
|
||||
Content("text/plain", content)
|
||||
)
|
||||
|
||||
val sg = SendGrid(key)
|
||||
val request = Request()
|
||||
try {
|
||||
request.method = Method.POST
|
||||
request.endpoint = "mail/send"
|
||||
request.body = mail.build()
|
||||
val response = sg.api(request)
|
||||
return response.statusCode == 202
|
||||
} catch (ex: IOException) {
|
||||
throw ex
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,3 +19,9 @@ db {
|
||||
password = dc-project
|
||||
port = 5432
|
||||
}
|
||||
|
||||
mail {
|
||||
sendGrid {
|
||||
key = ${?SEND_GRID_KEY}
|
||||
}
|
||||
}
|
||||
|
||||
28
src/test/kotlin/MailerTest.kt
Normal file
28
src/test/kotlin/MailerTest.kt
Normal file
@@ -0,0 +1,28 @@
|
||||
import fr.dcproject.Env
|
||||
import fr.dcproject.messages.Mailer
|
||||
import fr.dcproject.module
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.server.testing.withTestApplication
|
||||
import io.ktor.util.KtorExperimentalAPI
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import org.koin.test.AutoCloseKoinTest
|
||||
import org.koin.test.KoinTest
|
||||
import org.koin.test.get
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
@KtorExperimentalAPI
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class MailerTest: KoinTest, AutoCloseKoinTest() {
|
||||
@Test
|
||||
fun `can be send an email`() {
|
||||
withTestApplication({ module(Env.TEST) }) {
|
||||
get<Mailer>().sendEmail(
|
||||
"reset-password@dc-project.fr",
|
||||
"fabrice.lecomte.be@gmail.com",
|
||||
"Email Work !",
|
||||
"Test"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
import fr.dcproject.Module
|
||||
import fr.dcproject.Env
|
||||
import fr.dcproject.entity.Article
|
||||
import fr.dcproject.entity.Constitution
|
||||
import fr.dcproject.module
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.server.testing.withTestApplication
|
||||
import io.ktor.util.KtorExperimentalAPI
|
||||
import org.amshove.kluent.`should equal`
|
||||
import org.amshove.kluent.shouldBe
|
||||
import org.junit.jupiter.api.BeforeAll
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koin.dsl.koinApplication
|
||||
import org.koin.test.AutoCloseKoinTest
|
||||
import org.koin.test.KoinTest
|
||||
import org.koin.test.get
|
||||
import fr.dcproject.repository.Article as RepositoryArticle
|
||||
@@ -19,15 +19,10 @@ import fr.dcproject.repository.Constitution as RepositoryConstitution
|
||||
@KtorExperimentalLocationsAPI
|
||||
@KtorExperimentalAPI
|
||||
@TestInstance(PER_CLASS)
|
||||
class RepositoryTest: KoinTest {
|
||||
@BeforeAll
|
||||
fun beforeAll() {
|
||||
startKoin { modules(Module) }
|
||||
}
|
||||
|
||||
class RepositoryTest: KoinTest, AutoCloseKoinTest() {
|
||||
@Test
|
||||
fun `test get repository`() {
|
||||
koinApplication {
|
||||
withTestApplication({ module(Env.TEST) }) {
|
||||
val repoArticle = get<RepositoryArticle>()
|
||||
(repoArticle is RepositoryArticle) shouldBe true
|
||||
repoArticle.entityName `should equal` Article::class
|
||||
|
||||
Reference in New Issue
Block a user