Commit fc943a12 authored by Andreas Muttscheller's avatar Andreas Muttscheller

Add simple IT for SlackClient

parent d2f2c9e5
......@@ -28,6 +28,7 @@ lazy val root = (project in file("."))
envVars in IntegrationTest := Map(
"SERVERLESS_SERVICE" -> (awsServiceName in IntegrationTest).value,
"SERVERLESS_STAGE" -> (awsStage in IntegrationTest).value,
"SLACK_API_URL" -> "http://localhost:1080/slack/",
),
libraryDependencies ++= Seq(
......@@ -70,5 +71,6 @@ lazy val root = (project in file("."))
// Test
"org.scalatest" %% "scalatest" % "3.0.5" % "test,it",
"org.mock-server" % "mockserver-netty" % "5.5.1" % "it",
)
)
\ No newline at end of file
package de.codecentric.amuttsch.bahndelayinfo
import ch.qos.logback.classic.{Level, LoggerContext}
import de.codecentric.amuttsch.bahndelayinfo.slackbot.SlackClient
import org.json4s._
import org.json4s.native.JsonMethods._
import org.mockserver.integration.ClientAndServer
import org.mockserver.model.HttpRequest
import org.mockserver.model.HttpRequest._
import org.mockserver.model.HttpResponse._
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, FunSpec}
import org.slf4j.LoggerFactory
class SlackClientTest extends FunSpec with BeforeAndAfterEach with BeforeAndAfterAll {
implicit val formats: Formats = DefaultFormats
var cas: ClientAndServer = _
val context: LoggerContext = LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext]
// Suppress logging
context.getLogger("org.mockserver.mock").setLevel(Level.valueOf("WARN"))
override def beforeAll(): Unit = {
cas = new ClientAndServer(1080)
}
override def afterAll(): Unit = {
cas.close()
}
override def afterEach(): Unit = {
cas.reset()
}
describe("SlackClient") {
it("should send messages successfully to the API") {
var receivedRequest: HttpRequest = null
cas.when(request()
.withHeader("Authorization", "Bearer foo")
.withPath("/slack/chat.postMessage")
).respond(request => {
receivedRequest = request
response
.withStatusCode(200)
.withBody("{\"ok\": true}")
})
assert(SlackClient("foo").postMessage("ch", "text"))
val body = parse(receivedRequest.getBodyAsString)
assert((body \ "channel").extract[String] == "ch")
assert((body \ "text").extract[String] == "text")
}
}
}
package de.codecentric.amuttsch.bahndelayinfo.slackbot
import com.softwaremill.sttp._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization.write
import org.json4s.{DefaultFormats, Formats}
import org.json4s.{DefaultFormats, Formats, _}
import scala.util.Properties
class SlackClient(token: String) {
private implicit val backend: SttpBackend[Id, Nothing] = HttpURLConnectionBackend()
private implicit val jsonFormats: Formats = DefaultFormats
val slackApiBaseUri = "https://slack.com/api/"
val slackApiBaseUri: String = Properties.envOrElse("SLACK_API_URL", "https://slack.com/api/")
def postMessage(
channel: String,
text: String,
attachments: Option[Seq[SlackAttachment]] = None
): Unit = {
): Boolean = {
postMessage(
SlackApiMessage(
channel,
......@@ -26,8 +29,8 @@ class SlackClient(token: String) {
def postMessage(
message: SlackApiMessage
): Unit = {
sttp
): Boolean = {
val response = sttp
.post(uri"${slackApiBaseUri}chat.postMessage")
.headers(Map(
"Content-type" -> "application/json",
......@@ -35,6 +38,13 @@ class SlackClient(token: String) {
))
.body(write(message))
.send()
response.body match {
case Left(_) =>
false
case Right(body) =>
(parse(body) \ "ok").extractOrElse[Boolean](false)
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment