2022-06-30 14:54:58 -07:00
|
|
|
package input_test
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/jtom38/newsbot/collector/database"
|
2022-06-30 14:54:58 -07:00
|
|
|
"github.com/jtom38/newsbot/collector/services/input"
|
2022-05-15 21:48:23 -07:00
|
|
|
)
|
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
var TwitchSourceRecord = database.Source {
|
|
|
|
ID: uuid.New(),
|
2022-05-15 21:48:23 -07:00
|
|
|
Name: "nintendo",
|
|
|
|
Source: "Twitch",
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
var TwitchInvalidRecord = database.Source {
|
|
|
|
ID: uuid.New(),
|
2022-05-15 21:48:23 -07:00
|
|
|
Name: "EvilNintendo",
|
|
|
|
Source: "Twitch",
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwitchLogin(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchSourceRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reach out and confirms that the API returns posts made by the user.
|
|
|
|
func TestTwitchReturnsUserPosts(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchSourceRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := tc.GetUserDetails()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
posts, err := tc.GetPosts(user)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if len(posts) == 0 {
|
|
|
|
t.Error("expected videos but got none")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwitchReturnsNothingDueToInvalidUserName(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchInvalidRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := tc.GetUserDetails()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
posts, err := tc.GetPosts(user)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if len(posts) != 0 {
|
|
|
|
t.Error("expected videos but got none")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwitchReturnsVideoAuthor(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchSourceRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := tc.GetUserDetails()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
posts, err := tc.GetPosts(user)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if posts[0].UserName == "" {
|
|
|
|
t.Error("uable to parse username")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwitchReturnsThumbnail(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {t.Error(err) }
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchSourceRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
user, err := tc.GetUserDetails()
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
posts, err := tc.GetPosts(user)
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
value, err := tc.ExtractThumbnail(posts[0])
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
if value == "" { t.Error("uable to parse username") }
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwitchReturnsPubDate(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil { t.Error(err) }
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchSourceRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
user, err := tc.GetUserDetails()
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
posts, err := tc.GetPosts(user)
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
date, err := tc.ExtractPubDate(posts[0])
|
|
|
|
log.Println(date)
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwitchReturnsDescription(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchSourceRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := tc.GetUserDetails()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
posts, err := tc.GetPosts(user)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tc.ExtractDescription(posts[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwitchReturnsAuthorImage(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {t.Error(err) }
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchSourceRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
user, err := tc.GetUserDetails()
|
|
|
|
if err != nil {t.Error(err) }
|
|
|
|
|
|
|
|
_, err = tc.ExtractAuthorImage(user)
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwitchReturnsTags(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchSourceRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := tc.GetUserDetails()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
posts, err := tc.GetPosts(user)
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
_, err = tc.ExtractTags(posts[0], user)
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwitchReturnsTitle(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchSourceRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := tc.GetUserDetails()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
posts, err := tc.GetPosts(user)
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
res, err := tc.ExtractTitle(posts[0])
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
if res == "" { t.Error("expected a filled string but got nil")}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwitchReturnsUrl(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil { t.Error(err) }
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchSourceRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
user, err := tc.GetUserDetails()
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
posts, err := tc.GetPosts(user)
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
res, err := tc.ExtractUrl(posts[0])
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
if res == "" { t.Error("expected a filled string but got nil")}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwitchGetContent(t *testing.T) {
|
2022-06-30 14:54:58 -07:00
|
|
|
tc, err := input.NewTwitchClient()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil { t.Error(err) }
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(TwitchSourceRecord)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
|
|
|
err = tc.Login()
|
|
|
|
if err != nil { t.Error(err) }
|
|
|
|
|
|
|
|
posts, err := tc.GetContent()
|
|
|
|
if err != nil {t.Error(err) }
|
|
|
|
if len(posts) == 0 { t.Error("posts came back with 0 posts") }
|
|
|
|
if len(posts) != 20 { t.Error("expected 20 posts") }
|
|
|
|
}
|