def test_sub_to_hub(ctx): """ test make_sub_request and make_unsub_request """ from httplib2 import Http from eventlet import sleep, spawn from melk.util.nonce import nonce_str import traceback from melkman.db import RemoteFeed from melkman.fetch.worker import run_feed_indexer from melkman.fetch.pubsubhubbub import WSGISubClient from melkman.fetch.pubsubhubbub import callback_url_for from melkman.fetch.pubsubhubbub import hubbub_sub from melkman.fetch.pubsubhubbub import hubbub_unsub from melkman.fetch.pubsubhubbub import psh_digest import logging logging.basicConfig(level=logging.WARN) w = WSGISubClient(ctx) client = spawn(w.run) indexer = spawn(run_feed_indexer, ctx) hub = FakeHub() hub_proc = spawn(hub.run) hub_url = 'http://localhost:%d/' % hub.port feed_url = 'http://example.org/feeds/99' rf = RemoteFeed.create_from_url(feed_url, ctx) rf.feed_info = {'links': [{'rel': 'self', 'href': feed_url}, {'rel': 'hub', 'href': hub_url}]} rf.save() cb = callback_url_for(feed_url, ctx) # subscribe to the hub assert not hub.is_verified(cb, feed_url) r, c = hubbub_sub(rf, ctx) assert r.status == 202, 'Expected 202, got %d' % r.status sleep(.5) assert hub.is_verified(cb, feed_url) secret = hub.secret_for(cb, feed_url) http = Http() # simulate hub posting to callback URL content = random_atom_feed(feed_url, 10, link=feed_url, hub_urls=[hub_url]) digest = 'sha1=%s' % psh_digest(content, secret) r, c = http.request(cb, 'POST', body=content, headers={'X-Hub-Signature': digest}) assert r.status == 200, 'Expected 200, got %d' % r.status sleep(0.5) # since we are subscribed, new items should be in the feed now rf = RemoteFeed.get_by_url(feed_url, ctx) assert len(rf.entries) == 10 for iid in melk_ids_in(content, feed_url): assert iid in rf.entries # unsubscribe from hub r, c = hubbub_unsub(rf, ctx) assert r.status == 202, 'Expected 202, got %d' % r.status sleep(.5) assert not hub.is_verified(cb, feed_url) # simulate another POST to the callback URL # this time it should fail (we are not subscribed) content = random_atom_feed(feed_url, 10, link=feed_url, hub_urls=[hub_url]) digest = "sha1=%s" % psh_digest(content, secret) r, c = http.request(cb, 'POST', body=content, headers={'X-Hub-Signature': digest}) assert r.status == 200, 'Expected 200, got %d' % r.status sleep(0.5) # items should be the same as before (not subscribed) rf = RemoteFeed.get_by_url(feed_url, ctx) assert len(rf.entries) == 10 for iid in melk_ids_in(content, feed_url): assert not iid in rf.entries client.kill() client.wait() indexer.kill() indexer.wait() hub_proc.kill() hub_proc.wait()
def test_push_index_digest(ctx): from melk.util.nonce import nonce_str from melkman.db.remotefeed import RemoteFeed from melkman.fetch import push_feed_index from melkman.fetch.worker import run_feed_indexer from eventlet import sleep, spawn from melkman.fetch.pubsubhubbub import psh_digest # start a feed indexer indexer = spawn(run_feed_indexer, ctx) url = 'http://www.example.com/feeds/2' rf = RemoteFeed.create_from_url(url, ctx) rf.hub_info.enabled = True rf.hub_info.subscribed = True rf.save() secret = nonce_str() content = random_atom_feed(url, 10) ids = melk_ids_in(content, url) correct_digest = 'sha1=%s' % psh_digest(content, secret) wrong_digest = 'wrong digest' # # no hub secret is specified on the feed # push_feed_index(url, content, ctx, digest=wrong_digest, from_hub=True) sleep(.5) rf = RemoteFeed.get_by_url(url, ctx) for iid in ids: assert iid not in rf.entries push_feed_index(url, content, ctx, digest=None, from_hub=True) sleep(.5) rf = RemoteFeed.get_by_url(url, ctx) for iid in ids: assert iid not in rf.entries # even the correct digest fails as no digest has been set push_feed_index(url, content, ctx, digest=correct_digest, from_hub=True) sleep(.5) rf = RemoteFeed.get_by_url(url, ctx) for iid in ids: assert iid not in rf.entries # # now set the hub secret # rf.hub_info.secret = secret rf.save() push_feed_index(url, content, ctx, digest=wrong_digest, from_hub=True) sleep(.5) rf = RemoteFeed.get_by_url(url, ctx) for iid in ids: assert iid not in rf.entries push_feed_index(url, content, ctx, digest=None, from_hub=True) sleep(.5) rf = RemoteFeed.get_by_url(url, ctx) for iid in ids: assert iid not in rf.entries # finally, the correct digest should work now... push_feed_index(url, content, ctx, digest=correct_digest, from_hub=True) sleep(.5) rf = RemoteFeed.get_by_url(url, ctx) for iid in ids: assert iid in rf.entries indexer.kill() indexer.wait()
def test_sub_push(ctx): from httplib2 import Http from eventlet import sleep, spawn from melk.util.nonce import nonce_str from melkman.db import RemoteFeed from melkman.fetch.worker import run_feed_indexer from melkman.fetch.pubsubhubbub import WSGISubClient, callback_url_for, psh_digest import logging logging.basicConfig(level=logging.WARN) w = WSGISubClient(ctx) client = spawn(w.run) indexer = spawn(run_feed_indexer, ctx) http = Http() url = 'http://example.org/feed/0' content = random_atom_feed(url, 10) secret = nonce_str() digest = 'sha1=%s' % psh_digest(content, secret) cb = callback_url_for(url, ctx) assert RemoteFeed.get_by_url(url, ctx) == None # try posting something that is not subscribed r, c = http.request(cb, 'POST', body=content, headers={'X-Hub-Signature': digest}) assert r.status == 200, 'Expected 200, got %d' % r.status sleep(1) # nothing should happen... assert RemoteFeed.get(url, ctx) == None # set up the feed, but don't subscribe rf = RemoteFeed.create_from_url(url, ctx) rf.save() r, c = http.request(cb, 'POST', body=content, headers={'X-Hub-Signature': digest}) assert r.status == 200, 'Expected 200, got %d' % r.status sleep(1) # nothing should happen... rf = RemoteFeed.get_by_url(url, ctx) assert len(rf.entries) == 0 # now set it up rf.hub_info.enabled = True rf.hub_info.subscribed = True rf.hub_info.secret = secret rf.save() # try with wrong digest... r, c = http.request(cb, 'POST', body=content, headers={'X-Hub-Signature': 'wrong'}) assert r.status == 200, 'Expected 200, got %d' % r.status sleep(0.5) # nothing should happen... rf = RemoteFeed.get_by_url(url, ctx) assert len(rf.entries) == 0 # try with no digest r, c = http.request(cb, 'POST', body=content) assert r.status == 200, 'Expected 200, got %d' % r.status sleep(0.5) # nothing should happen... rf = RemoteFeed.get_by_url(url, ctx) assert len(rf.entries) == 0 # finally, try with correct digest r, c = http.request(cb, 'POST', body=content, headers={'X-Hub-Signature': digest}) assert r.status == 200, 'Expected 200, got %d' % r.status sleep(0.5) # nothing should happen... rf = RemoteFeed.get_by_url(url, ctx) assert len(rf.entries) == 10 for iid in melk_ids_in(content, url): assert iid in rf.entries client.kill() client.wait() indexer.kill() indexer.wait()