def test_ContentElementsSimple_delete_fm_db(client, value): '''clean up structure and content tables''' # [_structure.delete_fm_db() for _structure in StructureModel.find()] [_content.delete_fm_db() for _content in ContentModel.find()] '''init''' _view_id = choice(global_constants.get_VIEWS_PKS) _locale_id = choice(global_constants.get_PKS) _upper_index = randrange(100) _type = 'header' _name = 'name' _value = value('db value') _element = value('element value') _identity = '_'.join([str(_upper_index).zfill(2), _type]) '''creating record in db''' _record = content_schema.load({ 'identity': _identity, 'view_id': _view_id, 'locale_id': _locale_id, 'title': _value.get('title'), 'content': _value.get('content') }, session=dbs_global.session) _record.save_to_db() '''test it exists''' _found_db_instance = ContentModel.find_by_identity_view_locale( identity=_identity, view_id=_view_id, locale_id=_locale_id ) assert _found_db_instance.identity\ == _identity assert _found_db_instance.view_id == _view_id assert _found_db_instance.locale_id == _locale_id assert _found_db_instance.title == _value.get('title') assert _found_db_instance.content == _value.get('content') '''create element instance with same PKs and delete record with the element''' _element_instance = ContentElementsSimple( upper_index=_upper_index, type=_type, name=_name, element=_element) _element_instance.delete_fm_db(view_id=_view_id, locale_id=_locale_id) '''check there is nothing in db''' _found_db_instance = ContentModel.find_by_identity_view_locale( identity=_identity, view_id=_view_id, locale_id=_locale_id) assert _found_db_instance is None '''try to delete the instance once more''' with pytest.raises(RecordNotFoundError) as e_info: _element_instance.delete_fm_db( view_id=_view_id, locale_id=_locale_id) # assert str(e_info.value)\ # == (f"Record with identity '{_identity}', view id '{_view_id}' " # f"and locale id '{_locale_id}' has not been found.") assert str(e_info.value).find(_identity) != -1 assert str(e_info.value).find(_view_id) != -1 assert str(e_info.value).find(_locale_id) != -1
def test_contents_post_success( client, content_api_resp, user_instance, access_token, lng, test_word, test_word_01 ): '''Create instance and get proper confirmation''' _user = user_instance(values={'role_id': 'admin'}) _user.save_to_db() # to have user with this id _access_token = access_token(_user) # create content instance to confirm it works _headers = {'Authorization': f'Bearer {_access_token}', 'Content-Type': 'application/json', 'Accept-Language': lng} _lng_key = {'locale_id': lng} resp = content_api_resp(values=_lng_key, headers=_headers) assert resp.status_code == 201 assert 'message' in resp.json.keys() assert 'payload' in resp.json.keys() assert resp.json.get('message').find(test_word) != -1 assert resp.json.get('payload').get('locale_id') == lng # print('\ntest_content_post, code ->', resp.status_code) # print('test_content_post, json ->', resp.json.get('payload')) '''clean up''' '''user''' _user.delete_fm_db() '''content''' _search_json = {k: v for (k, v) in resp.json.get('payload').items() if k in ['identity', 'view_id', 'locale_id']} _contents = ContentModel.find_by_identity_view_locale(**_search_json) if _contents: _contents.delete_fm_db()
def test_contents_post_wrong_fk(client, content_api_resp, lng, test_word, test_word_01, user_instance, access_token): ''' Operation with foreign key that are not in appropriate tables. ''' _user = user_instance(values={'role_id': 'admin'}) _user.save_to_db() # to have user with this id _access_token = access_token(_user) '''create content instance to confirm it works''' _headers = {'Authorization': f'Bearer {_access_token}', 'Content-Type': 'application/json', 'Accept-Language': lng} resp = content_api_resp(headers=_headers) _search_json = {k: v for (k, v) in resp.json.get('payload').items() if k in ['identity', 'view_id', 'locale_id']} assert resp.status_code == 201 assert 'message' in resp.json.keys() assert 'payload' in resp.json.keys() assert isinstance(resp.json.get('payload'), Dict) assert resp.json.get('message').find(test_word) != -1 assert resp.json.get('payload').get('locale_id') == lng _create_json = {k: v for(k, v) in resp.json.get('payload').items() if k not in ['created', 'view']} '''Try to create instance breaching db's constrains:''' '''wrong locale_id''' _wrong_creating_json = _create_json.copy() # _wrong_creating_json['locale_id'] = 'not' _headers = {**_headers, 'Accept-Language': 'wrong'} resp = client.post(url_for('contents_bp.content'), json=_wrong_creating_json, headers=_headers) assert resp.status_code == 500 assert 'payload' in resp.json.keys() assert isinstance(resp.json['payload'], str) '''wrong view_id''' _wrong_creating_json = _create_json.copy() _headers = {**_headers, 'Accept-Language': lng} _wrong_creating_json['view_id'] = 'not' resp = client.post(url_for('contents_bp.content'), json=_wrong_creating_json, headers=_headers) assert resp.status_code == 500 assert 'payload' in resp.json.keys() assert isinstance(resp.json['payload'], str) assert resp.json['message'].find(test_word_01) != -1 assert resp.json['payload'].find('foreign key constraint fails') != -1 '''Clean up user table''' _user.delete_fm_db() '''clean up contents''' _contents = ContentModel.find_by_identity_view_locale(**_search_json) if _contents: _contents.delete_fm_db()
def test_contents_post_already_exists( client, content_api_resp, lng, test_word, user_instance, access_token ): _user = user_instance(values={'role_id': 'admin'}) # user admin _user.save_to_db() # to have user with this id _access_token = access_token(_user) _headers = {'Authorization': f'Bearer {_access_token}', 'Content-Type': 'application/json', 'Accept-Language': lng} '''Create one instance''' resp = content_api_resp(values={'locale_id': lng}, headers=_headers) _search_json = {k: v for (k, v) in resp.json.get('payload').items() if k in ['identity', 'view_id', 'locale_id']} assert resp.status_code == 201 assert 'message' in resp.json.keys() assert 'payload' in resp.json.keys() assert isinstance(resp.json.get('payload'), Dict) assert resp.json.get('message').find(test_word) != -1 '''Try to save other instance with same keys''' _create_json = {k: v for(k, v) in resp.json.get('payload').items() if k not in ['created', 'view', 'locale']} resp = client.post(url_for('contents_bp.content'), json=_create_json, headers=_headers) assert resp.status_code == 400 assert 'message' in resp.json.keys() assert 'payload' not in resp.json.keys() assert resp.json.get('message').find(test_word) != -1 '''clean up''' '''user''' _user.delete_fm_db() '''contents''' # print(_search_json) _contents = ContentModel.find_by_identity_view_locale(**_search_json) if _contents: _contents.delete_fm_db()
def test_ContentElementsSimple_save_to_db_content_structure( client, value): '''clean up structure and content tables''' [_structure.delete_fm_db() for _structure in StructureModel.find()] [_content.delete_fm_db() for _content in ContentModel.find()] '''init''' _view_id = choice(global_constants.get_VIEWS_PKS) _locale_id = choice(global_constants.get_PKS) _upper_index = randrange(100) _user_id = randrange(64) _type = 'header' _name = 'name' # _value = value('db value') _element = value('element value') '''creating element for testing''' _content_element = ContentElementsSimple( upper_index=_upper_index, type=_type, name=_name, element=_element) _content_element.save_to_db_content( view_id=_view_id, locale_id=_locale_id, user_id=_user_id, save_structure=True) '''testing useing load''' _loaded_instance = ContentElementsSimple.load_fm_db( identity={'_'.join([str(_upper_index).zfill(2), _type])}, view_id=_view_id, locale_id=_locale_id, load_name=True) '''test loaded element''' assert _loaded_instance.upper_index == _upper_index assert _loaded_instance.type == _type assert _loaded_instance.name == _name assert _loaded_instance.element.value == _element '''testing records in tables''' _found_db_instance = ContentModel.find_by_identity_view_locale( identity='_'.join([str(_upper_index).zfill(2), _type]), view_id=_view_id, locale_id=_locale_id ) assert _found_db_instance.identity\ == '_'.join([str(_upper_index).zfill(2), _type]) assert _found_db_instance.title == _element.get('title') assert _found_db_instance.content == _element.get('content') '''update db record with element instance, same PKs''' _element = value(marker='new value') _name = f'new {_name}' # _upper_index = randrange(50, 100) _user_id = randrange(64, 128) _new_instance = ContentElementsSimple( upper_index=_upper_index, type=_type, name=_name, element=_element) _new_instance.save_to_db_content( view_id=_view_id, locale_id=_locale_id, user_id=_user_id, save_structure=True) _found_identity = ContentElementsSimple.load_fm_db( identity='_'.join([str(_upper_index).zfill(2), _type]), view_id=_view_id, locale_id=_locale_id, load_name=True ) assert _found_identity.upper_index == _upper_index assert _found_identity.type == _type # assert _found_identity.name == _name assert _found_identity.element.value == _element
def test_content_delete(client, content_api_resp, sessions, user_instance, lng, test_word, access_token): # lng = 'en' '''Create data with normal set of keys:''' _user = user_instance(values={'role_id': 'admin'}) # user not admin _user.save_to_db() # to have user with this id _access_token = access_token(_user) _headers = {'Authorization': f'Bearer {_access_token}', 'Content-Type': 'application/json', 'Accept-Language': lng} resp = content_api_resp(values={'locale_id': lng}, headers=_headers) _search_json = {k: v for (k, v) in resp.json.get('payload').items() if k in ['identity', 'view_id', 'locale_id']} _key_json = {k: v for (k, v) in resp.json.get('payload').items() if k in ['identity', 'view_id']} # if k in ['identity', 'view_id', 'locale_id']} '''Insure content is exist in db:''' _uuid = uuid4() sessions.setter(str(_uuid)) _tech_token = create_access_token(_uuid, expires_delta=False) params = _key_json.copy() # params = {k: v for (k, v) in _key_json.items() if k not in ['locale_id']} _get_headers = {**_headers, 'Authorization': f'Bearer {_tech_token}'} resp = client.get(url_for('contents_bp.content', **params), headers=_get_headers) assert resp.status_code == 200 assert 'payload' in resp.json.keys() assert isinstance(resp.json['payload'], Dict) assert resp.json.get('message').find(test_word) != -1 '''Try to find view with wrong value (404):''' for key in _key_json.keys(): _wrong_key_value = _key_json.copy() _wrong_key_value[key] += '_wrong' headers = {**_headers, 'Accept-Language': lng} resp = client.delete(url_for('contents_bp.content', **_wrong_key_value), headers=headers) assert resp.status_code == 404 assert 'message' in resp.json.keys() assert 'payload' not in resp.json.keys() assert resp.json.get('message').find(test_word) != -1 headers = {**_headers, 'Accept-Language': 'wrong'} # print('\ntest_content_delete, params ->', params, '\theaders ->', headers) resp = client.delete(url_for('contents_bp.content', **_key_json), headers=headers) assert resp.status_code == 404 assert 'message' in resp.json.keys() assert 'payload' not in resp.json.keys() # assert resp.json.get('message').find(test_word) != -1 '''delete view instance from db:''' resp = client.delete(url_for('contents_bp.content', **_key_json), headers=_headers) assert resp.status_code == 200 assert 'message' in resp.json.keys() assert 'payload' not in resp.json.keys() assert resp.json.get('message').find(test_word) != -1 '''try to delete already deleted instance''' resp = client.delete(url_for('contents_bp.content', **_key_json), headers=_headers) assert resp.status_code == 404 assert 'message' in resp.json.keys() assert 'payload' not in resp.json.keys() assert resp.json.get('message').find(test_word) != -1 '''Insure view is deleted in db:''' resp = client.get(url_for('contents_bp.content', **_key_json), headers=_get_headers) assert resp.status_code == 404 assert 'message' in resp.json.keys() assert 'payload' not in resp.json.keys() assert resp.json.get('message').find(test_word) != -1 # print('\ntest_content_delete, resp ->', resp.status_code) # print('test_content_delete, resp ->', resp.json) '''Clean up user table''' _user.delete_fm_db() '''clean up contents''' _contents = ContentModel.find_by_identity_view_locale(**_search_json) if _contents: _contents.delete_fm_db()
def test_content_put(client, content_api_resp, lng, test_word, test_word_01, user_instance, access_token): _admin = user_instance(values={'role_id': 'admin'}) _admin.save_to_db() # to have user with this id _access_token = access_token(_admin) _headers = {'Authorization': f'Bearer {_access_token}', 'Content-Type': 'application/json', 'Accept-Language': lng} '''Create data with normal set of keys:''' _lng_key = {'locale_id': lng} resp = content_api_resp(values=_lng_key, headers=_headers) _search_json = {k: v for (k, v) in resp.json.get('payload').items() if k in ['identity', 'view_id', 'locale_id']} _original_json = {k: v for(k, v) in resp.json.get('payload').items() if k not in [ 'locale', 'created', 'updated', 'view']} _key_json = {key: value for (key, value) in _original_json.items() if key in [ 'identity', 'view_id']} _value_json = {key: value for (key, value) in _original_json.items() if key not in [ 'identity', 'view_id', 'locale_id', 'user_id']} assert resp.status_code == 201 assert 'message' in resp.json.keys() assert 'payload' in resp.json.keys() assert resp.json.get('message').find(test_word) != -1 assert resp.json.get('payload').get('locale_id') == lng '''Find and update data with normal set of keys:''' for key in _value_json.keys(): # print(key) _corrected_data = _original_json.copy() # if isinstance(_corrected_data.get(key), int): # print('\nfunctional, contents, put key ->', key) # _corrected_data[key] = 0 # elif isinstance(_corrected_data.get(key), str): _corrected_data[key] += ' - corrected' resp = client.put(url_for('contents_bp.content'), json=_corrected_data, headers=_headers) assert resp.status_code == 200 assert 'payload' in resp.json.keys() assert resp.json['payload'][key] == _corrected_data[key] assert resp.json.get('message').find(test_word) != -1 assert resp.json.get('payload').get('locale_id') == lng # print('\nfunctional, contents, put code ->', resp.status_code) # print('functional, contents, put json ->', resp.json.get('payload').get(key)) '''Try to find view with wrong value (404):''' for key in _key_json.keys(): _wrong_key_value = _key_json.copy() _wrong_key_value[key] += '_wrong' resp = client.put(url_for('contents_bp.content'), json=_wrong_key_value, headers=_headers) assert resp.status_code == 404 assert 'payload' not in resp.json.keys() assert isinstance(resp.json, Dict) assert resp.json.get('message').find(test_word) != -1 assert resp.json.get('message').find(lng) != -1 resp = client.put(url_for('contents_bp.content'), json=_key_json, headers=({**_headers, 'Accept-Language': 'wrong'})) assert resp.status_code == 404 assert 'payload' not in resp.json.keys() assert isinstance(resp.json, Dict) # assert resp.json.get('message').find(test_word) != -1 # assert resp.json.get('message').find(lng) != -1 # print('\nfunctional, contents, put code ->', resp.status_code) # print('functional, contents, put json ->', resp.json) '''Clean up user table''' _admin.delete_fm_db() '''clean up contents''' _contents = ContentModel.find_by_identity_view_locale(**_search_json) if _contents: _contents.delete_fm_db()
def test_contents_get(client, content_api_resp, lng, test_word, test_word_01, sessions, user_instance, access_token): '''user admin to create content instance''' _user = user_instance(values={'role_id': 'admin'}) _user.save_to_db() _access_token = access_token(_user) '''Create instance''' _headers = {'Authorization': f'Bearer {_access_token}', 'Content-Type': 'application/json', 'Accept-Language': lng} _lng_key = {'locale_id': lng} resp = content_api_resp(values=_lng_key, headers=_headers) _search_json = {k: v for (k, v) in resp.json.get('payload').items() if k in ['identity', 'view_id', 'locale_id']} assert resp.status_code == 201 assert 'message' in resp.json.keys() assert 'payload' in resp.json.keys() assert resp.json.get('message').find(test_word) != -1 assert resp.json.get('payload').get('locale_id') == lng '''Find data with normal set of keys having tech_token and sessions:''' _uuid = uuid4() sessions.setter(str(_uuid)) _tech_token = create_access_token(_uuid, expires_delta=False) _original_json = resp.json.get('payload').copy() _key_json = {key: value for (key, value) in _original_json.items() if key in ['view_id', 'identity']} _get_headers = {**_headers, 'Authorization': f'Bearer {_tech_token}'} resp = client.get(url_for('contents_bp.content', **_key_json), headers=_get_headers) '''Check found data has been identical:''' assert resp.status_code == 200 assert 'message' in resp.json.keys() assert 'payload' in resp.json.keys() assert resp.json.get('message').find(test_word) != -1 for key in _original_json.keys(): assert _original_json.get(key) == resp.json.get('payload').get(key) '''Try to find data with wrong value (404):''' for key in _key_json.keys(): _wrong_value_json = _key_json.copy() _wrong_value_json[key] += '_wrong' resp = client.get(url_for('contents_bp.content', **_wrong_value_json), headers=_get_headers) assert resp.status_code == 404 assert 'payload' not in resp.json.keys() assert 'message' in resp.json.keys() assert resp.json.get('message').find(test_word) != -1 assert resp.json.get('message').find(lng) != -1 resp = client.get( url_for('contents_bp.content', **_key_json), headers={**_get_headers, 'Accept-Language': 'wrong'}) assert resp.status_code == 404 assert 'payload' not in resp.json.keys() assert 'message' in resp.json.keys() # print('\ntest_content_get, code ->', resp.status_code) # print('test_content_get, json ->', resp.json) '''Clean up user table''' _user.delete_fm_db() '''clean up contents''' _contents = ContentModel.find_by_identity_view_locale(**_search_json) if _contents: _contents.delete_fm_db()