Пример #1
0
def test_add_feed(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({"subscriptions"})
    sub_repo = SubscriptionRepo()
    assert sub_repo.sub_list == []
    # Create example rss feed
    rf = RssSource("http://spangle.org.uk/hallo/test_rss.xml", "feed title")
    sub = Subscription(test_server, test_channel, rf, timedelta(days=1), None, None)
    sub_repo.add_sub(sub)
    assert len(sub_repo.sub_list) == 1
    assert sub_repo.sub_list[0] == sub
Пример #2
0
 def test_sub_classes_added_to_factory(self):
     """
     Test tht all source classes which are implemented are added to SubscriptionFactory
     """
     module_obj = importlib.import_module("hallo.modules.subscriptions")
     # Loop through module, searching for Subscriptions subclasses.
     for function_tuple in inspect.getmembers(module_obj, inspect.isclass):
         function_class = function_tuple[1]
         # Only look at subclasses of Source
         if not inherits_from(function_class, "Source"):
             continue
         # Only look at implemented classes.
         sub_repo = SubscriptionRepo()
         # noinspection PyBroadException
         try:
             function_class.from_input(
                 "hello",
                 self.test_user,
                 sub_repo,
             )
         except NotImplementedError:
             continue
         except Exception:
             pass
         # Check it's in SubscriptionFactory
         assert function_class.__name__ in [
             sub_class.__name__ for sub_class in SubscriptionFactory.sub_sources
         ]
Пример #3
0
def test_from_input(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({"subscriptions"})
    sub_repo = SubscriptionRepo()

    rf = E621Source.from_input("cabinet", test_user, sub_repo)

    assert rf.search == "cabinet"
Пример #4
0
def test_from_input(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({"subscriptions"})
    sub_repo = SubscriptionRepo()

    rf = RssSource.from_input(TEST_RSS, test_user, sub_repo)

    assert rf.url == TEST_RSS
    assert rf.feed_title == "Example rss feed"
Пример #5
0
 def get_source_create_arguments(self) -> Dict[Type[Source], str]:
     sub_repo = SubscriptionRepo()
     fa_key = FAKey(self.test_user, self.cookie_a, self.cookie_b)
     fa_commons = sub_repo.get_common_config_by_type(hallo.modules.subscriptions.common_fa_key.FAKeysCommon)
     fa_commons.add_key(fa_key)
     sub_evts = dict()
     sub_evts[E621Source] = "cabinet"
     sub_evts[E621TaggingSource] = "cabinet tags=door"
     sub_evts[RssSource] = "http://spangle.org.uk/hallo/test_rss.xml"
     sub_evts[FANotesSource] = ""
     sub_evts[FAFavsSource] = "zephyr42"
     sub_evts[FAUserWatchersSource] = "zephyr42"
     sub_evts[FAWatchersSource] = ""
     sub_evts[FAFavNotificationsSource] = ""
     sub_evts[FACommentNotificationsSource] = ""
     sub_evts[RedditSource] = "r/deer"
     # sub_evts[TwitterSource] = "telegram"
     return sub_evts
Пример #6
0
def test_remove_feed(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({"subscriptions"})
    # Setup a feed list
    sub_repo = SubscriptionRepo()
    rf1 = RssSource(
        "http://spangle.org.uk/hallo/test_rss.xml?1", "title1"
    )
    sub1 = Subscription(test_server, test_channel, rf1, timedelta(days=1), None, None)
    sub_repo.add_sub(sub1)
    rf2 = RssSource(
        "http://spangle.org.uk/hallo/test_rss.xml?2", "title2"
    )
    sub2 = Subscription(test_server, test_channel, rf2, timedelta(days=1), None, None)
    sub_repo.add_sub(sub2)
    assert len(sub_repo.sub_list) == 2
    # Remove an item from the feed list
    sub_repo.remove_sub(sub1)
    assert len(sub_repo.sub_list) == 1
    assert sub_repo.sub_list[0] == sub2
Пример #7
0
def test_json(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({"subscriptions"})
    sub_repo = SubscriptionRepo()
    rf = RssSource(TEST_RSS)
    rf.last_keys = ["item3", "item2", "item1"]

    rf_json = rf.to_json()

    assert "type" in rf_json
    assert "last_keys" in rf_json
    assert "url" in rf_json
    assert "title" in rf_json

    rf2 = RssSource.from_json(rf_json, test_channel, sub_repo)

    assert rf2.url == TEST_RSS
    assert rf2.feed_title == rf.feed_title
    assert rf2.last_keys == rf.last_keys
Пример #8
0
def test_json(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({"subscriptions"})
    sub_repo = SubscriptionRepo()
    test_e621_search = "cabinet"
    # Create example source
    rf = E621Source(test_e621_search)
    rf.last_keys = [1234, 2345, 3456]
    # Save to json and load up new E621Sub
    rf_json = rf.to_json()

    assert "type" in rf_json
    assert "last_keys" in rf_json
    assert "search" in rf_json
    assert rf_json["type"] == E621Source.type_name

    rf2 = E621Source.from_json(rf_json, test_channel, sub_repo)

    assert rf2.search == test_e621_search
    assert rf2.last_keys == rf.last_keys
Пример #9
0
def test_get_feeds_by_title(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({"subscriptions"})
    serv1 = ServerMock(hallo)
    serv1.name = "test_serv1"
    serv2 = ServerMock(hallo)
    serv2.name = "test_serv2"
    chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1")
    user2 = serv1.get_user_by_address("test_user2", "test_user2")
    chan3 = serv2.get_channel_by_address("test_chan3".lower(), "test_chan3")
    # Setup a feed list
    sub_repo = SubscriptionRepo()
    rf1 = RssSource(
        "http://spangle.org.uk/hallo/test_feed.xml?1",
        feed_title="test_feed1",
    )
    sub1 = Subscription(serv1, chan1, rf1, timedelta(days=1), None, None)
    sub_repo.add_sub(sub1)
    rf2 = RssSource(
        "http://spangle.org.uk/hallo/test_feed.xml?2",
        feed_title="test_feed2",
    )
    sub2 = Subscription(serv1, user2, rf2, timedelta(days=1), None, None)
    sub_repo.add_sub(sub2)
    rf3 = RssSource(
        "http://spangle.org.uk/hallo/test_feed.xml?3",
        feed_title="test_feed3",
    )
    sub3 = Subscription(serv2, chan3, rf3, timedelta(days=1), None, None)
    sub_repo.add_sub(sub3)
    rf4 = RssSource(
        "http://spangle.org.uk/hallo/test_feed.xml?4",
        feed_title="test_feed4",
    )
    sub4 = Subscription(serv2, chan3, rf4, timedelta(days=1), None, None)
    sub_repo.add_sub(sub4)
    rf5 = RssSource(
        "http://spangle.org.uk/hallo/test_feed.xml?5",
        feed_title="test_feed3",
    )
    sub5 = Subscription(serv2, chan3, rf5, timedelta(days=1), None, None)
    sub_repo.add_sub(sub5)
    # Check function
    feed_list = sub_repo.get_subs_by_name("test_feed3", chan3)
    assert len(feed_list) == 2
    assert sub3 in feed_list
    assert sub5 in feed_list
Пример #10
0
def test_json(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({"subscriptions"})
    # Setup a feed list
    sub_repo = SubscriptionRepo()
    rf1 = RssSource(
        "http://spangle.org.uk/hallo/test_rss.xml?1",
        feed_title="test_feed1",
    )
    sub1 = Subscription(test_server, test_channel, rf1, timedelta(days=1), None, None)
    sub_repo.add_sub(sub1)
    rf2 = RssSource(
        "http://spangle.org.uk/hallo/test_rss.xml?2",
        feed_title="test_feed2",
    )
    sub2 = Subscription(test_server, test_user, rf2, timedelta(days=1), None, None)
    sub_repo.add_sub(sub2)
    rf3 = RssSource(
        "http://spangle.org.uk/hallo/test_rss.xml?3",
        feed_title="test_feed3",
    )
    sub3 = Subscription(test_server, test_channel, rf3, timedelta(hours=1), None, None)
    sub_repo.add_sub(sub3)
    # Save to JSON and load
    try:
        try:
            os.rename("store/subscriptions.json", "store/subscriptions.json.tmp")
        except OSError:
            pass
        sub_repo.save_json()
        new_rfl = SubscriptionRepo.load_json(hallo)
        assert len(new_rfl.sub_list) == 3
    finally:
        try:
            os.remove("store/subscriptions.json")
        except OSError:
            pass
        try:
            os.rename("store/subscriptions.json.tmp", "store/subscriptions.json")
        except OSError:
            pass
Пример #11
0
def test_init():
    rfl = SubscriptionRepo()
    assert rfl.sub_list == []
Пример #12
0
 def test_run_all(self):
     # Set up test servers and channels
     serv1 = ServerMock(self.hallo)
     serv1.name = "test_serv1"
     chan1 = serv1.get_channel_by_address("test_chan1".lower(),
                                          "test_chan1")
     chan2 = serv1.get_channel_by_address("test_chan2".lower(),
                                          "test_chan2")
     serv2 = ServerMock(self.hallo)
     serv2.name = "test_serv2"
     chan3 = serv2.get_channel_by_address("test_chan1".lower(),
                                          "test_chan1")
     try:
         self.hallo.add_server(serv1)
         self.hallo.add_server(serv2)
         # Set up rss feeds
         sub_repo = SubscriptionRepo()
         rf1 = E621Source("cabinet")
         sub1 = Subscription(serv1, chan1, rf1, timedelta(days=1), None,
                             None)
         sub_repo.add_sub(sub1)
         rf2 = E621Source("clefable")
         sub2 = Subscription(serv1, chan2, rf2, timedelta(days=1), None,
                             None)
         sub_repo.add_sub(sub2)
         rf3 = E621Source("fez")
         sub3 = Subscription(serv2, chan3, rf3, timedelta(days=1), None,
                             None)
         sub_repo.add_sub(sub3)
         # Splice this rss feed list into the function dispatcher's rss check object
         e621_sub_check = self.function_dispatcher.get_function_by_name(
             "check subscription")
         e621_sub_obj = self.function_dispatcher.get_function_object(
             e621_sub_check)  # type: SubscriptionCheck
         e621_sub_obj.subscription_repo = sub_repo
         # Test running all feed updates
         self.function_dispatcher.dispatch(
             EventMessage(self.server, self.test_chan, self.test_user,
                          "e621 sub check all"))
         # Check original calling channel data
         serv0_data = self.server.get_send_data(1, self.test_chan,
                                                EventMessage)
         assert (
             "subscription updates were found"
             in serv0_data[0].text.lower()), "Actual message: {}".format(
                 serv0_data[0].text)
         # Check test server 1 data
         serv1_data = serv1.get_send_data(100)
         chan1_count = 0
         chan2_count = 0
         for data_line in serv1_data:
             if data_line.channel == chan1:
                 chan1_count += 1
             if data_line.channel == chan2:
                 chan2_count += 1
         assert chan1_count == 50
         assert chan2_count == 50
         # Check test server 2 data
         serv2.get_send_data(50, chan3, EventMessage)
         # Test running with no new updates.
         self.function_dispatcher.dispatch(
             EventMessage(self.server, self.test_chan, self.test_user,
                          "e621 sub check all"))
         data = self.server.get_send_data(1, self.test_chan, EventMessage)
         assert "no updates" in data[
             0].text, "No further updates should be found."
     finally:
         self.hallo.remove_server(serv2)
         self.hallo.remove_server(serv1)
Пример #13
0
 def test_run_passive(self):
     # Set up test servers and channels
     serv1 = ServerMock(self.hallo)
     serv1.name = "test_serv1"
     chan1 = serv1.get_channel_by_address("test_chan1".lower(),
                                          "test_chan1")
     chan2 = serv1.get_channel_by_address("test_chan2".lower(),
                                          "test_chan2")
     serv2 = ServerMock(self.hallo)
     serv2.name = "test_serv2"
     chan3 = serv2.get_channel_by_address("test_chan1".lower(),
                                          "test_chan1")
     try:
         self.hallo.add_server(serv1)
         self.hallo.add_server(serv2)
         # Set up rss feeds
         sub_repo = SubscriptionRepo()
         rf1 = E621Source("cabinet")
         sub1 = Subscription(serv1, chan1, rf1, timedelta(days=1), None,
                             None)
         sub_repo.add_sub(sub1)
         rf2 = E621Source("clefable")
         sub2 = Subscription(serv1, chan2, rf2, timedelta(days=1), None,
                             None)
         sub_repo.add_sub(sub2)
         rf3 = E621Source("fez")
         sub3 = Subscription(serv2, chan3, rf3, timedelta(days=1), None,
                             None)
         sub_repo.add_sub(sub3)
         # Splice this rss feed list into the function dispatcher's rss check object
         rss_check_class = self.function_dispatcher.get_function_by_name(
             "check subscription")
         rss_check_obj = self.function_dispatcher.get_function_object(
             rss_check_class)  # type: SubscriptionCheck
         rss_check_obj.subscription_repo = sub_repo
         # Test passive feed updates
         self.function_dispatcher.dispatch_passive(EventMinute())
         # Check test server 1 data
         serv1_data = serv1.get_send_data(100)
         chan1_count = 0
         chan2_count = 0
         for data_line in serv1_data:
             if data_line.channel == chan1:
                 chan1_count += 1
             if data_line.channel == chan2:
                 chan2_count += 1
         assert chan1_count == 50
         assert chan2_count == 50
         # Check test server 2 data
         serv2.get_send_data(50, chan3, EventMessage)
         # Test that no updates are found the second run
         rf1.last_check = None
         rf2.last_check = None
         rf3.last_check = None
         self.function_dispatcher.dispatch_passive(EventMinute())
         serv1.get_send_data(0)
         serv2.get_send_data(0)
         # Test that no feeds are checked before timeout, set urls to none and see if anything explodes.
         self.failed = False
         rf1.check_feed = self.do_not_call
         rf2.check_feed = self.do_not_call
         rf3.check_feed = self.do_not_call
         self.function_dispatcher.dispatch_passive(EventMinute())
         serv1.get_send_data(0)
         serv2.get_send_data(0)
         assert (not self.failed
                 ), "check_feed() should not have been called on any feed."
     finally:
         self.hallo.remove_server(serv2)
         self.hallo.remove_server(serv1)
Пример #14
0
 def test_run_by_search(self):
     # Set up test servers and channels
     serv1 = ServerMock(self.hallo)
     serv1.name = "test_serv1"
     chan1 = serv1.get_channel_by_address("test_chan1".lower(),
                                          "test_chan1")
     chan2 = serv1.get_channel_by_address("test_chan2".lower(),
                                          "test_chan2")
     user1 = serv1.get_user_by_address("test_user1", "test_user1")
     serv2 = ServerMock(self.hallo)
     serv2.name = "test_serv2"
     chan3 = serv2.get_channel_by_address("test_chan1".lower(),
                                          "test_chan1")
     try:
         self.hallo.add_server(serv1)
         self.hallo.add_server(serv2)
         # Set up rss feeds
         sub_repo = SubscriptionRepo()
         rf1 = E621Source("cabinet")
         sub1 = Subscription(serv1, chan1, rf1, timedelta(days=1), None,
                             None)
         sub_repo.add_sub(sub1)
         rf2 = E621Source("clefable")
         sub2 = Subscription(serv1, chan2, rf2, timedelta(days=1), None,
                             None)
         sub_repo.add_sub(sub2)
         rf3 = E621Source("fez")
         sub3 = Subscription(serv2, chan3, rf3, timedelta(days=1), None,
                             None)
         sub_repo.add_sub(sub3)
         # Splice this rss feed list into the function dispatcher's rss check object
         rss_check_class = self.function_dispatcher.get_function_by_name(
             "check subscription")
         rss_check_obj = self.function_dispatcher.get_function_object(
             rss_check_class)  # type: SubscriptionCheck
         rss_check_obj.subscription_repo = sub_repo
         # Invalid title
         self.function_dispatcher.dispatch(
             EventMessage(
                 self.server,
                 self.test_chan,
                 self.test_user,
                 "e621 sub check Not a valid search",
             ))
         data = self.server.get_send_data(1, self.test_chan, EventMessage)
         assert "error" in data[0].text.lower()
         # Correct title but wrong channel
         self.function_dispatcher.dispatch(
             EventMessage(serv1, chan1, user1, "e621 sub check clefable"))
         data = serv1.get_send_data(1, chan1, EventMessage)
         assert "error" in data[0].text.lower()
         # Correct title check update
         self.function_dispatcher.dispatch(
             EventMessage(serv1, chan2, user1, "e621 sub check clefable"))
         data = serv1.get_send_data(51, chan2, EventMessage)
         has_photo_id = 0
         for x in range(50):
             assert "update on" in data[x].text.lower()
             if hasattr(data[x],
                        "photo_id") and data[x].photo_id is not None:
                 has_photo_id += 1
             assert "clefable" in data[x].text
         assert has_photo_id > 40, "Almost all subscription updates should have a photo"
         assert ("subscription updates were found"
                 in data[50].text.lower()), "Actual message: {}".format(
                     data[0].text)
         # No updates
         self.function_dispatcher.dispatch(
             EventMessage(serv1, chan2, user1, "e621 sub check clefable"))
         data = serv1.get_send_data(1, chan2, EventMessage)
         assert "no updates" in data[
             0].text, "No further updates should be found."
     finally:
         self.hallo.remove_server(serv2)
         self.hallo.remove_server(serv1)