Example #1
0
def test_list_paginate(db_conn, session, notices_table):
    """
    Expect to paginate lists of notices.
    """

    for i in range(0, 25):
        insert_notice({
            'user_id': 'abcd1234',
            'kind': 'create_proposal',
            'data': {
                'user_name': '',
                'proposal_name': '',
                'entity_kind': '',
                'entity_name': '',
            }
        }, db_conn)

    request = {
        'cookies': {'session_id': session},
        'params': {},
        'db_conn': db_conn,
    }
    code, response = routes.notice.list_notices_route(request)
    assert len(response['notices']) == 10
    request.update({'params': {'skip': 10}})
    code, response = routes.notice.list_notices_route(request)
    assert len(response['notices']) == 10
    request.update({'params': {'skip': 20}})
    code, response = routes.notice.list_notices_route(request)
    assert len(response['notices']) == 5
Example #2
0
def test_list(db_conn, session, notices_table):
    """
    Expect to get a list of 10 notices by user ID.
    """
    for i in range(0, 10):
        insert_notice({
            'user_id': 'abcd1234',
            'kind': 'create_proposal',
            'data': {
                'user_name': '',
                'proposal_name': '',
                'entity_kind': '',
                'entity_name': '',
            }
        }, db_conn)

    request = {
        'cookies': {'session_id': session},
        'params': {},
        'db_conn': db_conn,
    }
    code, response = routes.notice.list_notices_route(request)
    assert code == 200
    assert len(response['notices']) == 10
    assert 'user_id' in response['notices'][0]
Example #3
0
def test_mark(db_conn, session, notices_table):
    """
    Expect to mark a notice as read.
    """
    notice, errors = insert_notice({
        'user_id': 'abcd1234',
        'kind': 'create_proposal',
        'data': {
            'user_name': '',
            'proposal_name': '',
            'entity_kind': '',
            'entity_name': '',
        }
    }, db_conn)
    nid = notice['id']

    request = {
        'cookies': {'session_id': session},
        'params': {'read': True},
        'db_conn': db_conn
    }
    code, response = routes.notice.mark_notice_route(request, nid)
    assert code == 200
    assert response['notice']['read'] is True
    record = notices_table.get(nid).run(db_conn)
    assert record['read'] is True
Example #4
0
def test_mark_not_owned(db_conn, session, notices_table):
    """
    Expect to error when not own notice when marking as read.
    """
    notice, errors = insert_notice({
        'user_id': '1234abcd',
        'kind': 'create_proposal',
        'data': {
            'user_name': '',
            'proposal_name': '',
            'entity_kind': '',
            'entity_name': '',
        }
    }, db_conn)
    nid = notice['id']

    request = {
        'cookies': {'session_id': session},
        'params': {'read': True},
        'db_conn': db_conn,
    }
    code, response = routes.notice.mark_notice_route(request, nid)
    assert code == 403
    record = notices_table.get(nid).run(db_conn)
    assert record['read'] is False
Example #5
0
def test_create(db_conn, notices_table):
    """
    Expect to create a notice.
    """

    notice, errors = insert_notice({"user_id": "abcd1234", "kind": "create_proposal", "tags": ["test"]}, db_conn)
    record = notices_table.filter({"user_id": "abcd1234"}).run(db_conn)
    record = list(record)[0]
    assert len(errors) == 0
    assert record["user_id"] == "abcd1234"
    assert record["tags"] == ["test"]
Example #6
0
def test_mark_as_read(db_conn, notices_table):
    """
    Expect to mark a notice as read.
    """

    notice, errors = insert_notice({"user_id": "abcd1234", "kind": "create_proposal", "tags": ["test"]}, db_conn)
    assert notice["read"] is False
    notice, errors = mark_notice_as_read(notice, db_conn)
    assert len(errors) == 0
    assert notice["read"] is True
    record = notices_table.filter({"user_id": "abcd1234"}).run(db_conn)
    record = list(record)[0]
    assert record["read"] is True
Example #7
0
def test_validations(db_conn, notices_table):
    """
    Expect to only create valid notices.
    - Fail if no user_id
    - Fail if no kind
    - Fail if read not boolean.
    - Fail if tags not list.
    """
    notice, errors = insert_notice({"read": 1234, "tags": "test"}, db_conn)
    assert len(errors) == 4
    assert get_error(errors, "user_id")["message"] == "Required."
    assert get_error(errors, "kind")["message"] == "Required."
    assert get_error(errors, "read")["message"] == "Must be true or false."
    assert get_error(errors, "tags")["message"] == "Must be a list."
Example #8
0
def test_notice_body(db_conn, notices_table):
    """
    Expect to get the notice body.
    """

    notice, errors = insert_notice(
        {
            "user_id": "abcd1234",
            "kind": "create_proposal",
            "tags": ["test"],
            "data": {"user_name": "A", "proposal_name": "B", "entity_kind": "C", "entity_name": "D"},
        },
        db_conn,
    )
    body = get_notice_body(notice)
    assert body == "A created a new proposal, B, for C D."
Example #9
0
def send_notices(db_conn, entity_id, entity_kind, notice_kind, notice_data):
    """
    When an event occurs, send notices outs.
    """

    user_ids = Follow.get_user_ids_by_entity(db_conn,
                                             entity_id=entity_id,
                                             entity_kind=entity_kind)
    for user_id in user_ids:
        notice, errors = insert_notice({
            'user_id': user_id,
            'kind': notice_kind,
            'data': notice_data,
        }, db_conn)
        if errors:
            raise Exception(errors)
Example #10
0
def test_mark_no_user(db_conn, notices_table):
    """
    Expect to error on not logged in when marking as read.
    """
    notice, errors = insert_notice({
        'user_id': 'abcd1234',
        'kind': 'create_proposal',
        'data': {
            'user_name': '',
            'proposal_name': '',
            'entity_kind': '',
            'entity_name': '',
        }
    }, db_conn)
    nid = notice['id']

    request = {
        'params': {'read': True},
        'db_conn': db_conn
    }
    code, response = routes.notice.mark_notice_route(request, nid)
    assert code == 401
    record = notices_table.get(nid).run(db_conn)
    assert record['read'] is False