def test_list_paginate(db_conn, session, notices_table): """ Expect to paginate lists of notices. """ for i in range(0, 25): Notice.insert({ 'user_id': 'abcd1234', 'kind': 'create_proposal', 'data': { 'user_name': '', 'proposal_name': '', 'entity_kind': '', 'entity_name': '', } }) request = {'cookies': {'session_id': session}, 'params': {}} 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
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): Notice.insert({ 'user_id': 'abcd1234', 'kind': 'create_proposal', 'data': { 'user_name': '', 'proposal_name': '', 'entity_kind': '', 'entity_name': '', } }) request = {'cookies': {'session_id': session}, 'params': {}} code, response = routes.notice.list_notices_route(request) assert code == 200 assert len(response['notices']) == 10 assert 'user_id' in response['notices'][0]
def test_create(db_conn, notices_table): """ Expect to create a notice. """ notice, errors = Notice.insert({ 'user_id': 'abcd1234', 'kind': 'create_proposal', 'tags': ['test'] }) 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']
def send_notices(entity_id, entity_kind, notice_kind, notice_data): """ When an event occurs, send notices outs. """ user_ids = Follow.get_user_ids_by_entity(entity_id=entity_id, entity_kind=entity_kind) for user_id in user_ids: notice, errors = Notice.insert({ 'user_id': user_id, 'kind': notice_kind, 'data': notice_data, }) if errors: raise Exception(errors)
def test_mark_as_read(db_conn, notices_table): """ Expect to mark a notice as read. """ notice, errors = Notice.insert({ 'user_id': 'abcd1234', 'kind': 'create_proposal', 'tags': ['test'] }) assert notice['read'] is False notice.mark_as_read() assert notice['read'] is True record = notices_table.filter({'user_id': 'abcd1234'}).run(db_conn) record = list(record)[0] assert record['read'] is True
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 = Notice.insert({ 'read': 1234, 'tags': 'test' }) 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.'
def test_mark_no_user(db_conn, notices_table): """ Expect to error on not logged in when marking as read. """ notice, errors = Notice.insert({ 'user_id': 'abcd1234', 'kind': 'create_proposal', 'data': { 'user_name': '', 'proposal_name': '', 'entity_kind': '', 'entity_name': '', } }) nid = notice['id'] request = {'params': {'read': True}} 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
def test_mark_not_owned(db_conn, session, notices_table): """ Expect to error when not own notice when marking as read. """ notice, errors = Notice.insert({ 'user_id': '1234abcd', 'kind': 'create_proposal', 'data': { 'user_name': '', 'proposal_name': '', 'entity_kind': '', 'entity_name': '', } }) nid = notice['id'] request = {'cookies': {'session_id': session}, 'params': {'read': True}} 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
def test_mark(db_conn, session, notices_table): """ Expect to mark a notice as read. """ notice, errors = Notice.insert({ 'user_id': 'abcd1234', 'kind': 'create_proposal', 'data': { 'user_name': '', 'proposal_name': '', 'entity_kind': '', 'entity_name': '', } }) nid = notice['id'] request = {'cookies': {'session_id': session}, 'params': {'read': True}} 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