def test_update_asset_category_with_two_wrong_input_controls( #pylint: disable=R0201,C0103 self, client, init_db, auth_header): #pylint: disable=W0613 """ Test to Update an asset category without choices with two wrong input controls """ asset_category = AssetCategory(name="TestLaptop b") asset_category.save() data = json.dumps(asset_category_with_two_wrong_input_control) response = client.patch( f'{api_v1_base_url}/asset-categories/{asset_category.id}', headers=auth_header, data=data) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 400 assert response_json["status"] == "error" assert response_json["message"] == "An error occurred" assert response_json["errors"]["0"]["inputControl"][0] == \ serialization_errors["input_control"].format( input_controls="'dropdown', 'checkbox', 'radio button', 'textarea', 'text'") assert response_json["errors"]["1"]["inputControl"][0] == \ serialization_errors["input_control"].format( input_controls="'dropdown', 'checkbox', 'radio button', 'textarea', 'text'")
def test_update_asset_category_with_attribute( #pylint: disable=R0201,C0103 self, client, init_db, #pylint: disable=W0613 auth_header): """ Test to Update an asset category with an attribute """ asset_category = AssetCategory(name="TestLaptop") attribute_data = valid_asset_category_data["customAttributes"][0] attribute_data["_key"] = attribute_data["key"] attribute_data["is_required"] = attribute_data["isRequired"] attribute_data["input_control"] = attribute_data["inputControl"] attribute_data.__delitem__("key") attribute_data.__delitem__("isRequired") attribute_data.__delitem__("inputControl") attribute = Attribute(**attribute_data) asset_category.attributes.append(attribute) asset_category.save() attribute_data["id"] = attribute.id data = json.dumps({"attributes": [attribute_data]}) response = client.patch( f'{api_v1_base_url}/asset-categories/{asset_category.id}', headers=auth_header, data=data) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 200 assert response_json["status"] == "success" assert len(response_json["data"]["customAttributes"]) > 0
def test_get_asset_category_attributes_with_valid_id( self, init_db, client, auth_header): """Should return attributes of an asset category""" asset_category = AssetCategory(name='HD Screen') asset_category.save() choices = ['red', 'blue'] attributes = Attribute(label='color', _key='color', is_required=False, input_control='text-area', choices=','.join(choices), asset_category_id=asset_category.id) attributes.save() response = client.get( f'{api_base_url_v1}/asset-categories/{asset_category.id}/' f'attributes', headers=auth_header) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 200 assert response_json['status'] == 'success' assert response_json['data']['name'] == 'HD Screen' assert type(response_json['data']['customAttributes']) == list assert len(response_json['data']['customAttributes']) == 1 assert response_json['data']['customAttributes'][0]['label'] == 'color' assert response_json['data']['customAttributes'][0][ 'inputControl'] == 'text-area' assert response_json['data']['customAttributes'][0][ 'choices'] == choices assert response_json['data']['customAttributes'][0][ 'isRequired'] == False
def test_delete_asset_category(self, client, init_db, auth_header): #pylint: disable=R0201,W0613,C0103 """ Tests that a single asset category can be deleted """ asset_category = AssetCategory(name="TestLaptop4") asset_category.save() response = client.delete( f'{api_v1_base_url}/asset-categories/{asset_category.id}', headers=auth_header) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 200 assert response_json["status"] == "success"
def test_get_one_asset_category(self, client, init_db, auth_header): #pylint: disable=R0201,W0613 """ Tests that a single asset category can be retrieved """ asset_category = AssetCategory(name="TestLaptop1") asset_category.save() response = client.get( f'{api_v1_base_url}/asset-categories/{asset_category.id}', headers=auth_header) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 200 assert response_json["status"] == "success" assert response_json["data"]["name"] == "TestLaptop1" assert type(response_json["data"]["customAttributes"]) == list
def test_delete_asset_category_invalid_id( #pylint: disable=R0201,C0103 self, client, init_db, #pylint: disable=W0613 auth_header): """ Tests that 400 is returned when id is invalid """ asset_category = AssetCategory(name="TestLaptop5") asset_category.save() response = client.delete(f'{api_v1_base_url}/asset-categories/LX@', headers=auth_header) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 400 assert response_json["status"] == "error" assert response_json["message"] == "Invalid id in parameter"
def test_get_one_asset_category_not_found( #pylint: disable=R0201,C0103 self, client, init_db, #pylint: disable=W0613 auth_header): """ Tests that 404 is returned for an asset category that does not exist """ asset_category = AssetCategory(name="TestLaptop2") asset_category.save() response = client.get(f'{api_v1_base_url}/asset-categories/-L2', headers=auth_header) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 404 assert response_json["status"] == "error" assert response_json["message"] == "Asset category not found"
def test_update_asset_category(self, client, init_db, auth_header): #pylint: disable=R0201,W0613 """ Test to Update an asset category without attribute """ asset_category = AssetCategory(name="TestLaptop") asset_category.save() data = json.dumps(valid_asset_category_data_without_attributes) response = client.patch( f'{api_v1_base_url}/asset-categories/{asset_category.id}', headers=auth_header, data=data) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 200 assert response_json["status"] == "success" assert isinstance(response_json["data"]["customAttributes"], list)
def test_get_asset_category_assets(self, client, init_db, auth_header): #pylint: disable=R0201,W0613,C0103 """ Should return lists of assets that belongs to an asset category """ asset_category = AssetCategory(name="Apple Tv 2") asset = Asset(tag="abc", serial="def") asset_category.assets.append(asset) asset_category.save() asset_category_id = asset_category.id response = client.get( f'{api_v1_base_url}/asset-categories/{asset_category_id}/assets', headers=auth_header) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 200 assert response_json["status"] == "success" assert len(response_json["data"]) != 0 assert response_json["message"] == serialization_errors[ "asset_category_assets"].format(asset_category.name)
def test_update_asset_category_without_choices( #pylint: disable=R0201,C0103 self, client, init_db, #pylint: disable=W0613,C0103 auth_header): """ Test to Update an asset category without choices when multichoice input controls are selected """ asset_category = AssetCategory(name="TestLaptop A") asset_category.save() data = json.dumps(asset_category_data_without_choices) response = client.patch( f'{api_v1_base_url}/asset-categories/{asset_category.id}', headers=auth_header, data=data) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 400 assert response_json["status"] == "error" assert response_json["errors"]["1"]["choices"][ 0] == serialization_errors['choices_required'] # pylint: disable=line-too-long
def test_export_asset_category_as_csv_success(self, init_db, client, auth_header): """ Should return csv data file with asset categories names and the assets count in each category """ asset_category = AssetCategory(name='HD Screen') asset_category.save() attributes = Attribute(label='color', _key='color', is_required=False, input_control='text-area', choices='multiple choices', asset_category_id=asset_category.id) attributes.save() response = client.get(f'{api_base_url_v1}/asset-categories/export', headers=auth_header) assert response.status_code == 200 assert response.headers['Content-Type'] == 'text/csv' assert b'name,assets_count\r\nHD Screen,0\r\n' in response.data
def test_get_asset_category_with_no_assets( #pylint: disable=R0201,C0103 self, client, init_db, #pylint: disable=W0613 auth_header): """ Should return an empty list if no assets belong to an asset category """ asset_category = AssetCategory(name="Apple Tv 1") asset_category.save() asset_category_id = asset_category.id response = client.get( f'{api_v1_base_url}/asset-categories/{asset_category_id}/assets', headers=auth_header) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 200 assert response_json["status"] == "success" assert len(response_json["data"]) == 0 assert response_json["message"] == serialization_errors[ "asset_category_assets"].format(asset_category.name)
def validate_attribute_exists(attribute_id): """ Validates if attribute is related to an asset category that its id matches the id provided in the url """ try: asset_category_id = request.url.split('/').pop() if is_valid_id(attribute_id): attribute = AssetCategory.get( asset_category_id).attributes.filter_by( id=attribute_id).first() if not attribute: raise Exception('attribute not found') else: raise Exception('missing value') except: raise MarshError(serialization_errors['attribute_not_related'].format( attribute_id=attribute_id, asset_category_id=asset_category_id))
def test_asset_categories_list_endpoint_args(self, client, auth_header): #pylint: disable=R0201,W0613,C0103,C0111 asset_category = AssetCategory(name="Laptop6") asset_category2 = AssetCategory(name="Chairs") asset_category.save() asset_category2.save() response = client.get( f'{api_v1_base_url}/asset-categories?where=name,like,chairs', headers=auth_header) response_json = json.loads(response.data.decode(CHARSET)) assert response.status_code == 200 assert response_json["status"] == "success" assert isinstance(response_json["data"], list) assert len(response_json["data"]) is 1 assert response_json["data"][0]["name"] == "Chairs" invalid_response = client.get( f'{api_v1_base_url}/asset-categories?where=name,like.chairs', headers=auth_header) invalid_response_json = json.loads( invalid_response.data.decode(CHARSET)) assert invalid_response.status_code == 400 assert invalid_response_json["status"] == "error" asset_category.delete() asset_category2.delete()
def create_asset_category(self, data): """Return asset category object after successful loading of data""" result = AssetCategory.query.filter_by(name=data['name']).first() if not result: return AssetCategory(**data) raise ValidationError({'message': 'Asset Category already exist'}, 409)