Exemple #1
0
def test_PageView_load_fm_db(client, view_name, locale, elements):
    '''clean up content table'''
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    [_content.delete_fm_db() for _content in ContentModel.find()]

    _user_id = randrange(128)
    _view_name = view_name()
    _locale = locale()
    _elements = elements()
    _page_view = PageView(view_name=_view_name,
                          locale=_locale,
                          elements=_elements)
    _page_view.save_to_db(user_id=_user_id)
    '''success'''
    _page_view_fm_db = PageView.load_fm_db(ids={
        'view_id': _view_name,
        'locale_id': _locale
    })
    assert _page_view_fm_db.view_name == _page_view.view_name
    assert _page_view_fm_db.locale == _page_view.locale
    assert len(_page_view_fm_db.elements) == len(_page_view.elements)
    for i, element in enumerate(_page_view.elements):
        _type = type(element)
        assert isinstance(_page_view_fm_db.elements[i], _type)
        assert _page_view_fm_db.elements[i].name == element.name
        if isinstance(element, ContentElementsBlock):
            assert len(_page_view_fm_db.elements[i].elements)\
                == len(element.elements)
    '''fails'''
    _wrong = 'wrong'
    _fm_db = PageView.load_fm_db({'view_id': _wrong, 'locale_id': locale})
    assert _fm_db is None
Exemple #2
0
def test_PageView_remove_vals(client, view_name, locale, simple_element,
                              block_element, elements):
    '''clean up tables'''
    [_content.delete_fm_db() for _content in ContentModel.find()]
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    _view_name = view_name()
    _locale = locale()
    _elements = elements()
    _page_view = PageView(view_name=_view_name,
                          locale=_locale,
                          elements=_elements)
    _length = len(_page_view.elements)
    _remove_position = randrange(_length)
    _element_in_page = dumps(_page_view.get_element_vals(_remove_position),
                             sort_keys=True)
    _removed_element = dumps(_page_view.remove_vals(_remove_position),
                             sort_keys=True)
    assert len(_page_view.elements) == _length - 1
    assert _removed_element == _element_in_page
    _last_element = _page_view.get_element_vals(_length - 2)
    assert _last_element.get('index') == len(_page_view.elements) - 1
    _page_view.save_to_db(user_id=randrange(128))
Exemple #3
0
def test_PageView_save_to_db(client, view_name, locale, elements):
    '''clean up content table'''
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    [_content.delete_fm_db() for _content in ContentModel.find()]
    '''success'''
    _user_id = randrange(128)
    _view_name = view_name()
    _locale = locale()
    _elements = elements()
    _page_view = PageView(view_name=_view_name,
                          locale=_locale,
                          elements=_elements)
    _page_view.save_to_db(user_id=_user_id)
    # print('\ntest_page_view:\n test_PageView_save_to_db')
    # [print(
    #     '  _elements ->', element.upper_index) for element in _elements]
    for element in _page_view.elements:
        # print('  elements ->', element.__dict__)
        _identity = '_'.join([str(element.upper_index).zfill(2), element.type])
        if isinstance(element, ContentElementsBlock):
            _identity = '_'.join([_identity, element.subtype])
        # print('  _identity ->', _identity)
        element_fm_db = element.load_fm_db(identity=_identity,
                                           view_id=_view_name,
                                           locale_id=_locale)
        assert element.upper_index == element_fm_db.upper_index
        assert element.type == element_fm_db.type
        if isinstance(element, ContentElementsBlock):
            assert element.subtype == element_fm_db.subtype
            for j, item in enumerate(element.elements):
                assert item.value == element_fm_db.elements[j].value
                # print('  from_db ->', item.value)
                # print('  from_db ->', element_fm_db.elements[j].value)
        elif isinstance(element, ContentElementsSimple):
            assert element.element.value == element_fm_db.element.value
    '''failure'''
Exemple #4
0
def test_PageView_move_element(client, view_name, locale, simple_element,
                               block_element, elements):
    '''it works I don't like to test success movements'''
    '''clean up tables'''
    [_content.delete_fm_db() for _content in ContentModel.find()]
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    _view_name = view_name()
    _locale = locale()
    _ids = {'view_id': _view_name, 'locale_id': _locale}
    _elements = elements()
    _page_view = PageView(view_name=_view_name,
                          locale=_locale,
                          elements=_elements)
    _page_view.save_to_db(user_id=1)
    _length = len(_page_view.elements)
    '''success'''
    '''moving up'''
    # _index = 1
    _index = randrange(1, _length)
    _direction = 'up'
    _moving_element = _page_view.get_element_vals(_index)
    _moving_element.pop('index')
    _moving_element_json = dumps(_moving_element, sort_keys=True)

    _shifting_element = _page_view.get_element_vals(_index - 1)
    _shifting_element.pop('index')
    _shifting_element_json = dumps(_shifting_element, sort_keys=True)

    _page_view.move_element(_index, _direction)
    _page_view.save_to_db(user_id=2)
    _loaded_view = PageView.load_fm_db(ids=_ids)

    _moved_element = _loaded_view.get_element_vals(_index - 1)
    _moved_element.pop('index')
    _moved_element_json = dumps(_moved_element, sort_keys=True)

    _shifted_element = _loaded_view.get_element_vals(_index)
    _shifted_element.pop('index')
    _shifted_element_json = dumps(_shifted_element, sort_keys=True)
    assert _moved_element_json == _moving_element_json
    assert _shifted_element_json == _shifting_element_json
    # print('\ntest_page_view:\n test_PageView_move_element')
    # [print(dumps(element.serialize_to_content, indent=4))
    #  for element in _loaded_view.elements]
    '''moving down'''
    _index = randrange(0, _length - 1)
    _direction = 'down'
    _moving_element = _page_view.get_element_vals(_index)
    _moving_element.pop('index')
    _moving_element_json = dumps(_moving_element, sort_keys=True)

    _shifting_element = _page_view.get_element_vals(_index + 1)
    _shifting_element.pop('index')
    _shifting_element_json = dumps(_shifting_element, sort_keys=True)

    _page_view.move_element(_index, _direction)

    _moved_element = _page_view.get_element_vals(_index + 1)
    _moved_element.pop('index')
    _moved_element_json = dumps(_moved_element, sort_keys=True)

    _shifted_element = _page_view.get_element_vals(_index)
    _shifted_element.pop('index')
    _shifted_element_json = dumps(_shifted_element, sort_keys=True)
    assert _moved_element_json == _moving_element_json
    assert _shifted_element_json == _shifting_element_json
    '''fails'''
    '''wrong combination index - direction'''
    _direction = 'up'
    _index = 0
    with pytest.raises(WrongIndexError) as e_info:
        _page_view.move_element(_index, _direction)
    assert str(e_info.value).find('-1') != -1

    _direction = 'down'
    _index = _length - 1
    with pytest.raises(WrongIndexError) as e_info:
        _page_view.move_element(_index, _direction)
    assert str(e_info.value).find('4') != -1
    '''wrong direction'''
    _direction = 'f**k'
    with pytest.raises(WrongDirection) as e_info:
        _page_view.move_element(_index, _direction)
    assert str(e_info.value).find(_direction) != -1
def test_UpperLevelHandling_patch(client, create_test_content, user_instance,
                                  access_token, view_name, locale, elements,
                                  lng, test_word_00, test_word_01,
                                  test_word_02, test_word_03, test_word_04,
                                  test_word_05):
    '''clean up tables'''
    [_content.delete_fm_db() for _content in ContentModel.find()]
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    '''create and save new contents in tables'''
    _view_name = view_name()
    _locale = lng
    _elements = elements()
    _page_view = PageView(view_name=_view_name,
                          locale=_locale,
                          elements=_elements)
    _page_view.save_to_db(user_id=randrange(128))
    # _index = 1
    '''Create user and admin'''
    _user = user_instance({'role_id': 'user'})
    _user.save_to_db()
    _admin = user_instance({'role_id': 'admin'})
    _admin.save_to_db()
    '''create tokens'''
    _admin_access_token = access_token(_admin)
    _user_access_token = access_token(_user)
    '''creating headers'''
    _admin_headers = {
        'Authorization': f'Bearer {_admin_access_token}',
        'Content-Type': 'application/json',
        'Accept-Language': _locale
    }
    _user_headers = {
        'Authorization': f'Bearer {_user_access_token}',
        'Content-Type': 'application/json',
        'Accept-Language': _locale
    }
    '''success'''
    '''remove random element'''
    _index = randrange(len(_elements) - 1)
    _identity = str(_index).zfill(2)
    _json = {'view_id': _view_name, 'identity': _identity}
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_admin_headers)
    assert _resp.status_code == 200
    assert _resp.json.get('message').find(test_word_00) != -1
    '''failues'''
    '''No tokens'''
    _no_token_header = {
        k: v
        for (k, v) in _admin_headers.items() if k != 'Authorization'
    }
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_no_token_header)
    assert _resp.status_code == 401
    assert _resp.json.get('description').find(test_word_01) != -1
    assert _resp.json.get('error') == 'authorization_required'
    '''Non admin'''
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_user_headers)
    assert _resp.status_code == 401
    assert _resp.json.get('message').find(test_word_02) != -1
    '''No block identity'''
    _json_no_id = {k: v for (k, v) in _json.items() if k != 'identity'}
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json_no_id,
                         headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload') == "'identity'"
    '''wrong identity'''
    '''first part is not integer'''
    _wrong_identity = 'f**k'
    _json = {'view_id': _view_name, 'identity': _wrong_identity}
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json == (
        f"invalid literal for int() with base 10: '{_wrong_identity}'")
    '''index out of range'''
    _wrong_identity = '-1'
    _json = {'view_id': _view_name, 'identity': _wrong_identity}
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload').find(_wrong_identity) != -1

    _wrong_identity = str(len(_page_view.elements))
    _json = {'view_id': _view_name, 'identity': _wrong_identity}
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload').find(_wrong_identity) != -1
def test_UpperLevelHandling_post(client, create_test_content, user_instance,
                                 access_token, view_name, locale, elements,
                                 lng, test_word, test_word_01, test_word_02,
                                 test_word_03, test_word_04):
    '''clean up tables'''
    [_content.delete_fm_db() for _content in ContentModel.find()]
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    '''create and save new contents in tables'''
    _view_name = view_name()
    _locale = lng
    _elements = elements()
    _page_view = PageView(view_name=_view_name,
                          locale=_locale,
                          elements=_elements)
    _page_view.save_to_db(user_id=randrange(128))
    # _index = 1
    '''Create user and admin'''
    _user = user_instance({'role_id': 'user'})
    _user.save_to_db()
    _admin = user_instance({'role_id': 'admin'})
    _admin.save_to_db()
    '''create tokens'''
    _admin_access_token = access_token(_admin)
    _user_access_token = access_token(_user)
    '''creating headers'''
    _admin_headers = {
        'Authorization': f'Bearer {_admin_access_token}',
        'Content-Type': 'application/json',
        'Accept-Language': _locale
    }
    _user_headers = {
        'Authorization': f'Bearer {_user_access_token}',
        'Content-Type': 'application/json',
        'Accept-Language': _locale
    }
    '''success'''
    _direction = 'up'
    _index = randrange(1, len(_elements))
    _index_key = str(_index).zfill(2)
    _info_json = _elements[_index].serialize_to_structure
    _identity = '_'.join(
        [list(_info_json.keys())[0],
         _info_json.get(_index_key).get('type')])
    if isinstance(_page_view.elements[_index], ContentElementsBlock):
        _identity = '_'.join(
            [_identity, _info_json.get(_index_key).get('subtype')])
    _json = {
        'view_id': _view_name,
        'identity': _identity,
        'direction': _direction
    }
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json,
                        headers=_admin_headers)
    assert _resp.status_code == 200
    assert _resp.json.get('message').find(test_word) != -1
    '''down'''
    _direction = 'down'
    _index = randrange(len(_elements) - 1)
    _info_json = _elements[_index].serialize_to_structure
    _identity = '_'.join([
        list(_info_json.keys())[0],
        _info_json.get(str(_index).zfill(2)).get('type')
    ])
    if isinstance(_page_view.elements[_index], ContentElementsBlock):
        _identity = '_'.join(
            [_identity,
             _info_json.get(str(_index).zfill(2)).get('subtype')])
    _json = {
        'view_id': _view_name,
        'identity': _identity,
        'direction': _direction
    }
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json,
                        headers=_admin_headers)
    assert _resp.status_code == 200
    assert _resp.json.get('message').find(test_word) != -1
    '''failues'''
    '''No tokens'''
    _no_token_header = {
        k: v
        for (k, v) in _admin_headers.items() if k != 'Authorization'
    }
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json,
                        headers=_no_token_header)
    assert _resp.status_code == 401
    assert _resp.json.get('description').find(test_word_01) != -1
    '''Non admin'''
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json,
                        headers=_user_headers)
    assert _resp.status_code == 401
    assert _resp.json.get('message').find(test_word_02) != -1
    '''No block identity'''
    _json_no_id = {k: v for (k, v) in _json.items() if k != 'identity'}
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json_no_id,
                        headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload') == "'identity'"
    '''wrong direction - index combination'''
    _index = 0
    _direction = 'up'
    _index_key = str(_index).zfill(2)
    _info_json = _elements[_index].serialize_to_structure
    _identity = '_'.join([_index_key, _info_json.get(_index_key).get('type')])
    if isinstance(_page_view.elements[_index], ContentElementsBlock):
        _identity = '_'.join(
            [_identity, _info_json.get(_index_key).get('subtype')])
    _json_up = {
        'view_id': _view_name,
        'identity': _identity,
        'direction': _direction
    }
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json_up,
                        headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload').find(str(_index - 1)) != -1

    _index = len(_elements) - 1
    _direction = 'down'
    _index_key = str(_index).zfill(2)
    _info_json = _elements[_index].serialize_to_structure
    _identity = '_'.join([_index_key, _info_json.get(_index_key).get('type')])
    if isinstance(_page_view.elements[_index], ContentElementsBlock):
        _identity = '_'.join(
            [_identity, _info_json.get(_index_key).get('subtype')])
    _json_down = {
        'view_id': _view_name,
        'identity': _identity,
        'direction': _direction
    }
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json_down,
                        headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload').find(str(_index + 1)) != -1
    '''generate not found error'''
    _wrong_view_id = 'wrong'
    _wrong_view_id_json = {**_json, 'view_id': _wrong_view_id}
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_wrong_view_id_json,
                        headers=_admin_headers)
    assert _resp.status_code == 404
    assert _resp.json.get('message').find(test_word_04) != -1
    assert _resp.json.get('message').find(_wrong_view_id) != -1
    '''clean up users'''
    if _user is not None:
        _user.delete_fm_db()
    if _admin is not None:
        _admin.delete_fm_db()