2020-06-02 13:37:38 +03:00
|
|
|
defmodule Plausible.Workers.FetchTweetsTest do
|
2020-01-16 14:39:47 +03:00
|
|
|
use Plausible.DataCase
|
2020-06-02 13:37:38 +03:00
|
|
|
alias Plausible.Workers.FetchTweets
|
2020-06-05 16:14:17 +03:00
|
|
|
import Double
|
|
|
|
|
|
|
|
test "fetches Twitter referrals from the last day" do
|
2020-06-08 10:35:13 +03:00
|
|
|
twitter_mock = stub(Plausible.Twitter.Api, :search, fn _link -> [] end)
|
2020-06-05 16:14:17 +03:00
|
|
|
FetchTweets.perform(nil, nil, twitter_mock)
|
|
|
|
|
|
|
|
assert_receive({Plausible.Twitter.Api, :search, ["t.co/a-link"]})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fetches Twitter referrals from 5-6 days ago" do
|
2020-06-08 10:35:13 +03:00
|
|
|
twitter_mock = stub(Plausible.Twitter.Api, :search, fn _link -> [] end)
|
2020-06-05 16:14:17 +03:00
|
|
|
FetchTweets.perform(nil, nil, twitter_mock)
|
|
|
|
|
|
|
|
assert_receive({Plausible.Twitter.Api, :search, ["t.co/b-link"]})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "stores twitter results" do
|
|
|
|
tweet = %{
|
|
|
|
"full_text" => "a Tweet body",
|
|
|
|
"id_str" => "the_tweet_id",
|
|
|
|
"created_at" => "Mon May 06 20:01:29 +0000 2019",
|
|
|
|
"user" => %{
|
|
|
|
"screen_name" => "twitter_author",
|
|
|
|
"name" => "Twitter Author",
|
|
|
|
"profile_image_url_https" => "https://image.com"
|
|
|
|
},
|
|
|
|
"entities" => %{
|
|
|
|
"user_mentions" => [],
|
|
|
|
"urls" => []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
twitter_mock =
|
|
|
|
stub(Plausible.Twitter.Api, :search, fn
|
|
|
|
"t.co/a-link" -> [tweet]
|
|
|
|
_link -> []
|
|
|
|
end)
|
|
|
|
|
2020-06-05 16:14:17 +03:00
|
|
|
FetchTweets.perform(nil, nil, twitter_mock)
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
[found_tweet] = Repo.all(from(t in Plausible.Twitter.Tweet))
|
2020-06-05 16:14:17 +03:00
|
|
|
assert found_tweet.tweet_id == "the_tweet_id"
|
|
|
|
assert found_tweet.text == "a Tweet body"
|
|
|
|
assert found_tweet.author_handle == "twitter_author"
|
|
|
|
assert found_tweet.author_name == "Twitter Author"
|
|
|
|
assert found_tweet.author_image == "https://image.com"
|
|
|
|
assert found_tweet.created == ~N[2019-05-06 20:01:29]
|
|
|
|
end
|
2020-01-16 14:39:47 +03:00
|
|
|
|
|
|
|
describe "processing tweet entities" do
|
|
|
|
test "inlines links to the body" do
|
|
|
|
tweet = %{
|
|
|
|
"full_text" => "asd https://t.co/somelink",
|
|
|
|
"entities" => %{
|
|
|
|
"user_mentions" => [],
|
2020-06-08 10:35:13 +03:00
|
|
|
"urls" => [
|
|
|
|
%{
|
|
|
|
"display_url" => "plausible.io",
|
|
|
|
"indices" => [4, 17],
|
|
|
|
"url" => "https://t.co/somelink"
|
|
|
|
}
|
|
|
|
]
|
2020-01-16 14:39:47 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2020-01-16 14:39:47 +03:00
|
|
|
body = FetchTweets.html_body(tweet)
|
|
|
|
|
|
|
|
assert body == "asd <a href=\"https://t.co/somelink\" target=\"_blank\">plausible.io</a>"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "inlines user mentions to the body" do
|
|
|
|
tweet = %{
|
|
|
|
"full_text" => "asd @hello",
|
|
|
|
"entities" => %{
|
2020-06-08 10:35:13 +03:00
|
|
|
"user_mentions" => [
|
|
|
|
%{
|
|
|
|
"screen_name" => "hello",
|
|
|
|
"id_str" => "123123"
|
|
|
|
}
|
|
|
|
],
|
2020-01-16 14:39:47 +03:00
|
|
|
"urls" => []
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2020-01-16 14:39:47 +03:00
|
|
|
body = FetchTweets.html_body(tweet)
|
|
|
|
|
|
|
|
assert body == "asd <a href=\"https://twitter.com/hello\" target=\"_blank\">@hello</a>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|