2022-06-30 14:54:58 -07:00
|
|
|
package output_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2024-05-26 07:52:29 -07:00
|
|
|
//"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/entity"
|
2024-04-23 07:15:38 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/services/output"
|
2022-06-30 14:54:58 -07:00
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
var (
|
2024-05-26 07:52:29 -07:00
|
|
|
article entity.ArticleEntity = entity.ArticleEntity{
|
|
|
|
ID: 999,
|
|
|
|
SourceID: 1,
|
2022-12-04 08:49:17 -08:00
|
|
|
Tags: "unit, testing",
|
|
|
|
Title: "Demo",
|
|
|
|
Url: "https://github.com/jtom38/newsbot.collector.api",
|
2022-07-12 15:28:31 -07:00
|
|
|
Description: "Hello World",
|
|
|
|
}
|
|
|
|
blank string = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiscordMessageContainsTitle(t *testing.T) {
|
|
|
|
d := output.NewDiscordWebHookMessage(article)
|
|
|
|
msg, err := d.GeneratePayload()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-12-04 08:49:17 -08:00
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
for _, i := range *msg.Embeds {
|
|
|
|
if i.Title == &blank {
|
|
|
|
t.Error("title missing")
|
|
|
|
}
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
func TestDiscordMessageContainsDescription(t *testing.T) {
|
|
|
|
d := output.NewDiscordWebHookMessage(article)
|
|
|
|
msg, err := d.GeneratePayload()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-12-04 08:49:17 -08:00
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
for _, i := range *msg.Embeds {
|
|
|
|
if i.Description == &blank {
|
|
|
|
t.Error("description missing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiscordMessageFooter(t *testing.T) {
|
|
|
|
d := output.NewDiscordWebHookMessage(article)
|
|
|
|
msg, err := d.GeneratePayload()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
for _, i := range *msg.Embeds {
|
|
|
|
blank := ""
|
|
|
|
if i.Footer.Value == &blank {
|
|
|
|
t.Error("missing footer vlue")
|
|
|
|
}
|
|
|
|
if i.Footer.IconUrl == &blank {
|
|
|
|
t.Error("missing footer url")
|
2022-12-04 08:49:17 -08:00
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
func TestDiscordMessageFields(t *testing.T) {
|
|
|
|
header := "Link"
|
|
|
|
d := output.NewDiscordWebHookMessage(article)
|
|
|
|
msg, err := d.GeneratePayload()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
for _, embed := range *msg.Embeds {
|
|
|
|
for _, field := range embed.Fields {
|
2022-12-04 08:49:17 -08:00
|
|
|
var fName string
|
2022-07-12 15:28:31 -07:00
|
|
|
if field.Name != nil {
|
|
|
|
fName = *field.Name
|
|
|
|
} else {
|
|
|
|
t.Error("missing link field value")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fName != header {
|
|
|
|
t.Error("missing link field key")
|
|
|
|
}
|
|
|
|
|
|
|
|
var fValue string
|
|
|
|
if field.Value != nil {
|
|
|
|
fValue = *field.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
if fValue == blank {
|
|
|
|
t.Error("missing link field value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test requires a env value to be present to work
|
|
|
|
func TestDiscordMessagePost(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
_, err := os.Open(".env")
|
|
|
|
if err != nil {
|
2022-07-12 15:28:31 -07:00
|
|
|
t.Error(err)
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
2022-12-04 08:49:17 -08:00
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
err = godotenv.Load()
|
|
|
|
if err != nil {
|
2022-07-12 15:28:31 -07:00
|
|
|
t.Error(err)
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
res := os.Getenv("TESTS_DISCORD_WEBHOOK")
|
|
|
|
if res == "" {
|
2022-07-12 15:28:31 -07:00
|
|
|
t.Error("TESTS_DISCORD_WEBHOOK is missing")
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
endpoints := strings.Split(res, " ")
|
2022-06-30 14:54:58 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
d := output.NewDiscordWebHookMessage(article)
|
|
|
|
msg, err := d.GeneratePayload()
|
2022-06-30 14:54:58 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2022-12-04 08:49:17 -08:00
|
|
|
err = d.SendPayload(msg, endpoints[0])
|
2022-06-30 14:54:58 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-12-04 08:49:17 -08:00
|
|
|
}
|