Example #1
0
async def test_api_mark_unread(sqlite_db):
    test_notification = {
        'json_data': json.dumps({
            'author': 'testuser1336',
            'weight': 100,
            'item': {
                'author': 'testuser1337',
                'permlink': 'test-post-1',
                'summary': 'A test post',
                'category': 'test1',
                'depth': 0
            }
        }),
        'to_username': '******',
        'from_username': '******',
        'notify_type': 'vote',
        'read': True
    }
    API = api_server.YoAPIServer()
    _ = sqlite_db.create_wwwpoll_notification(**test_notification)
    assert _ is True
    notification = sqlite_db.get_wwwpoll_notifications()[0]
    assert notification['read'] is True
    result = await API.api_mark_unread(ids=[notification['nid']],
                                       context=dict(yo_db=sqlite_db))
    assert result == [True]
    notification = sqlite_db.get_wwwpoll_notifications()[0]
    assert notification['read'] is False
Example #2
0
async def test_api_get_set_transports(sqlite_db):
    """Test get and set transports backed by sqlite with simple non-default transports"""
    API = api_server.YoAPIServer()

    simple_transports_obj = {
        'username': '******',
        'transports': {
            'email': {
                'notification_types': ['vote', 'comment'],
                'sub_data': '*****@*****.**'
            },
            'wwwpoll': {
                'notification_types': ['mention', 'post_reply'],
                'sub_data': {
                    'stuff': 'not here by default'
                }
            }
        }
    }

    resp = await API.api_set_transports(
        username='******',
        transports=simple_transports_obj['transports'],
        context=dict(yo_db=sqlite_db))
    assert resp

    resp = await API.api_get_transports(
        username='******', context=dict(yo_db=sqlite_db))
    assert resp == simple_transports_obj['transports']
Example #3
0
async def test_follow_flow(sqlite_db):
    """Tests follow events get through to a transport
    """
    mock_follow_op = {
        'trx_id':
        str(uuid.uuid4()),
        'op': ('custom_json', {
            'required_auths': (),
            'required_posting_auths': ['testfollower'],
            'id':
            'follow',
            'json':
            json.dumps(('follow', {
                'follower': 'testfollower',
                'following': 'testfollowed',
                'what': ('blog')
            }))
        })
    }

    # boilerplate stuff
    yo_db = sqlite_db
    yo_app = MockApp(yo_db)
    sender = notification_sender.YoNotificationSender(db=yo_db, yo_app=yo_app)
    mock_tx = MockTransport()
    sender.configured_transports = {}
    sender.configured_transports['mock'] = mock_tx
    API = api_server.YoAPIServer()
    follower = blockchain_follower.YoBlockchainFollower(db=yo_db,
                                                        yo_app=yo_app)

    # configure testupvoted and testupvoter users to use mock transport for follows
    transports_obj = {
        'mock': {
            'notification_types': ['follow'],
            'sub_data': ''
        }
    }
    await API.api_set_transports(username='******',
                                 transports=transports_obj,
                                 context=dict(yo_db=sqlite_db))
    await API.api_set_transports(username='******',
                                 transports=transports_obj,
                                 context=dict(yo_db=sqlite_db))

    # handle the mock follow op
    await follower.notify(mock_follow_op)

    # since we don't run stuff in the background in test suite, manually invoke the notification sender
    await sender.api_trigger_notifications()

    # test it got through to our mock transport for testupvoted only
    assert 'testfollowed' in mock_tx.received_by_user.keys()
    assert not ('testfollower' in mock_tx.received_by_user.keys())
Example #4
0
async def test_basic_ratelimit(sqlite_db):
    """Tests basic ratelimit functionality
    """

    # boilerplate stuff
    yo_db = sqlite_db
    yo_app = MockApp(yo_db)
    sender = notification_sender.YoNotificationSender(db=yo_db, yo_app=yo_app)
    mock_tx = MockTransport()
    sender.configured_transports = {}
    sender.configured_transports['mock'] = mock_tx
    API = api_server.YoAPIServer()
    follower = blockchain_follower.YoBlockchainFollower(db=yo_db,
                                                        yo_app=yo_app)

    # configure testupvoted and testupvoter users to use mock transport for votes
    transports_obj = {'mock': {'notification_types': ['vote'], 'sub_data': ''}}
    await API.api_set_transports(username='******',
                                 transports=transports_obj,
                                 context=dict(yo_db=sqlite_db))
    await API.api_set_transports(username='******',
                                 transports=transports_obj,
                                 context=dict(yo_db=sqlite_db))

    # send a single vote op
    vote_op = gen_vote_op()
    await follower.notify(vote_op)
    await sender.api_trigger_notifications()

    # ensure single vote op got through (should be priority LOW)
    assert 'testupvoted' in mock_tx.received_by_user.keys()
    assert mock_tx.rxcount == 1

    # immediately attempt to send a second vote op, this one should fail
    vote_op = gen_vote_op()
    await follower.notify(vote_op)
    await sender.api_trigger_notifications()

    # ensure there is still only one vote op sent
    assert mock_tx.rxcount == 1

    # check that the ratelimit fails for a fresh vote op
    vote_op = gen_vote_op()
    vote_op[
        'priority_level'] = Priority.LOW  # some silly hacks as we've got a raw blockchain op here
    vote_op['to_username'] = '******'
    assert not ratelimits.check_ratelimit(yo_db, vote_op)

    # check the ratelimit succeeds when override flag is set
    assert ratelimits.check_ratelimit(yo_db, vote_op, override=True)
Example #5
0
async def test_vote_flow(sqlite_db):
    """Tests vote events get through to a transport
    """
    mock_vote_op = {
        'trx_id':
        str(uuid.uuid4()),
        'op': ('vote', {
            'permlink': 'test-post',
            'author': 'testupvoted',
            'voter': 'testupvoter',
            'weight': 10000
        })
    }

    # boilerplate stuff
    yo_db = sqlite_db
    yo_app = MockApp(yo_db)
    sender = notification_sender.YoNotificationSender(db=yo_db, yo_app=yo_app)
    mock_tx = MockTransport()
    sender.configured_transports = {}
    sender.configured_transports['mock'] = mock_tx
    API = api_server.YoAPIServer()
    follower = blockchain_follower.YoBlockchainFollower(db=yo_db,
                                                        yo_app=yo_app)

    # configure testupvoted and testupvoter users to use mock transport for votes
    transports_obj = {'mock': {'notification_types': ['vote'], 'sub_data': ''}}
    await API.api_set_transports(username='******',
                                 transports=transports_obj,
                                 context=dict(yo_db=sqlite_db))
    await API.api_set_transports(username='******',
                                 transports=transports_obj,
                                 context=dict(yo_db=sqlite_db))

    # handle the mock vote op
    await follower.notify(mock_vote_op)

    # since we don't run stuff in the background in test suite, manually invoke the notification sender
    await sender.api_trigger_notifications()

    print(mock_tx.received_by_user.items())

    # test it got through to our mock transport for testupvoted only
    assert 'testupvoted' in mock_tx.received_by_user.keys()
    assert not ('testupvoter' in mock_tx.received_by_user.keys())
Example #6
0
async def test_api_get_notifications(sqlite_db):
    """Basic test of get_notifications backed by sqlite3"""
    vote_data = {
        'author': 'testuser1337',
        'weight': 100,
        'item': {
            'author': 'testuser1337',
            'permlink': 'test-post-1',
            'summary': 'A test post',
            'category': 'test',
            'depth': 0
        }
    }
    test_data = {
        'json_data': json.dumps(vote_data),
        'to_username': '******',
        'from_username': '******',
        'notify_type': 'vote',
        'trx_id': '123abc'
    }
    API = api_server.YoAPIServer()
    yo_db = sqlite_db
    retval = yo_db.create_notification(**test_data)
    assert retval is True

    result = await API.api_get_notifications(
        username='******', context=dict(yo_db=sqlite_db))

    assert len(result) == 1
    result = result[0]

    assert result['notify_type'] == 'vote'
    assert result['to_username'] == 'testuser1337'
    assert result['from_username'] == 'testuser1336'
    assert json.loads(result['json_data']) == vote_data
    assert isinstance(result['created'], datetime)

    # notifications only columns
    assert result['trx_id'] == '123abc'
Example #7
0
async def test_account_update_flow(sqlite_db):
    """Tests account_update events get through to a transport
    """

    # TODO - different types of account_update

    mock_update_op = {
        'trx_id':
        str(uuid.uuid4()),
        'op': ('account_update', {
            'account':
            'testuser',
            'active': {
                'account_auths': [],
                'key_auths':
                [['STM7qPrQjAfQjsU3QXcXW7vutB6b4hEtT6UjZCYUNTCLuke9becT2', 1]],
                'weight_threshold':
                1
            },
            'owner': {
                'account_auths': [],
                'key_auths':
                [['STM7qPrQjAfQjsU3QXcXW7vutB6b4hEtT6UjZCYUNTCLuke9becT2', 1]],
                'weight_threshold':
                1
            },
            'posting': {
                'account_auths': [],
                'key_auths':
                [['STM7qPrQjAfQjsU3QXcXW7vutB6b4hEtT6UjZCYUNTCLuke9becT2', 1]],
                'weight_threshold':
                1
            },
            'memo_key':
            'STM8S9siuc6wBQztU2qNSuftcZRew96mdpaJpVRWjbMHTvkMDLMH7',
            'json_metadata':
            json.dumps({
                "profile": {
                    "profile_image": "https://example.com/test.jpg",
                    "name": "Test User"
                }
            })
        })
    }

    # boilerplate stuff
    yo_db = sqlite_db
    yo_app = MockApp(yo_db)
    sender = notification_sender.YoNotificationSender(db=yo_db, yo_app=yo_app)
    mock_tx = MockTransport()
    sender.configured_transports = {}
    sender.configured_transports['mock'] = mock_tx
    API = api_server.YoAPIServer()
    follower = blockchain_follower.YoBlockchainFollower(db=yo_db,
                                                        yo_app=yo_app)

    # configure testuser to use mock transport for account updates
    transports_obj = {
        'mock': {
            'notification_types': ['account_update'],
            'sub_data': ''
        }
    }
    await API.api_set_transports(username='******',
                                 transports=transports_obj,
                                 context=dict(yo_db=sqlite_db))

    # handle the mock follow op
    await follower.notify(mock_update_op)

    # since we don't run stuff in the background in test suite, manually invoke the notification sender
    await sender.api_trigger_notifications()

    # test it got through to our mock transport for testupvoted only
    assert 'testuser' in mock_tx.received_by_user.keys()