2022-06-30 14:54:58 -07:00
|
|
|
package output_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-07-12 15:28:31 -07:00
|
|
|
//"time"
|
2022-06-30 14:54:58 -07:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/jtom38/newsbot/collector/database"
|
|
|
|
"github.com/jtom38/newsbot/collector/services/output"
|
|
|
|
)
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
var (
|
|
|
|
article database.Article = database.Article{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Sourceid: uuid.New(),
|
|
|
|
Tags: "unit, testing",
|
|
|
|
Title: "Demo",
|
|
|
|
Url: "https://github.com/jtom38/newsbot.collector.api",
|
|
|
|
//Pubdate: time.Now(),
|
|
|
|
Videoheight: 0,
|
|
|
|
Videowidth: 0,
|
|
|
|
Description: "Hello World",
|
|
|
|
}
|
|
|
|
blank string = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiscordMessageContainsTitle(t *testing.T) {
|
|
|
|
d := output.NewDiscordWebHookMessage(article)
|
|
|
|
msg, err := d.GeneratePayload()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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 {
|
|
|
|
var fName string
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
err = d.SendPayload(msg, endpoints[0])
|
2022-06-30 14:54:58 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|