def test_delete_a_product(request, db_connect, get_random_product_id):
    """

    :param request:
    :param db_connect:
    :param get_random_product_id:
    :return:
    """

    # get random product id
    prod_id = get_random_product_id

    # getting product by id
    response = request.delete(f'products/{prod_id}?force=true')

    status_code = response[0]
    response_body = response[1]

    # verifying status code
    assert status_code == 200, f"Response code is not 200. Response is {response_body['message']}"

    # validate json schema
    assert_valid_schema(response_body, 'product.json')

    # get product data from DB
    query = f"""
        SELECT * FROM {db_connect.db}.wp_posts WHERE id={prod_id}
        """

    # executing select statement
    qresp = db_connect.select(query)

    assert not qresp, 'DB select result is not empty, product is not deleted from the DB'
Example #2
0
def test_create_a_product(get_product_json, request, db_connect):
    """
    http://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product

    Create a product
    This API helps you to create a new product.

    HTTP request
    POST /wp-json/wc/v2/products
    """

    # getting new product json
    product = get_product_json

    # posting new product
    response = request.post('products', product)

    status_code = response[0]
    response_body = response[1]
    resp_id = response_body['id']

    # validate json schema
    assert_valid_schema(response_body, 'product.json')

    # checking the response status
    assert status_code == 201, f"Response code is not 201. Response is {response_body['message']}"

    # verifying response
    mapped_response = map_response(product, response_body)
    assert product == mapped_response, 'Requested data doesn\'t match with response data'

    # get product data from DB
    query = f"""
    SELECT p.post_title, p.post_type, pm.meta_value 
    FROM {db_connect.db}.wp_posts as p 
    JOIN {db_connect.db}.wp_postmeta as pm 
    ON p.id=pm.post_id 
    WHERE p.id={resp_id} 
    AND pm.meta_key='_regular_price'
    """

    # executing select statement
    qresp = db_connect.select(query)

    db_name = qresp[0][0]
    db_price = qresp[0][2]

    req_name = mapped_response['name']
    req_price = mapped_response['regular_price']

    # verifying data from db and response match
    assert db_name == req_name, 'name in db doesn\'t match with name in request'
    assert db_price == req_price, 'price in db doesn\'t match with price in request'
Example #3
0
def test_retrieve_a_product(get_random_product_id, request, db_connect):
    """
    http://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-product

    Retrieve a product
    This API lets you retrieve and view a specific product by ID.

    HTTP request
    GET /wp-json/wc/v2/products/<id>

    """

    # get random product id
    prod_id = get_random_product_id

    # getting product by id
    response = request.get(f'products/{prod_id}')

    status_code = response[0]
    response_body = response[1]

    # verifying status code
    assert status_code == 200, f"Response code is not 201. Response is {response_body['message']}"

    # validate json schema
    assert_valid_schema(response_body, 'product.json')

    resp_name = response_body['name'].lower()
    resp_status = response_body['status'].lower()

    # get product data from DB
    query = f"""
        SELECT post_name, post_status
        FROM {db_connect.db}.wp_posts where id={prod_id}
        """

    # executing select statement
    qresp = db_connect.select(query)

    db_name = qresp[0][0]
    db_status = qresp[0][1]

    # verifying data from db and response match
    assert resp_name == db_name, 'Response name and db name doesn\'t match'
    assert resp_status == db_status, 'Response status and db status doesn\'t match'
Example #4
0
def test_update_customer(get_random_customer_id, get_customer_update_json,
                         request, db_connect):
    """
    hhttp://woocommerce.github.io/woocommerce-rest-api-docs/#update-a-customer

    Update a customer
    This API lets you make changes to a customer.

    HTTP request
    PUT /wp-json/wc/v2/customers/<id>
    """

    # getting new customer json
    customer = get_customer_update_json

    # getting random customer id
    c_id = get_random_customer_id

    # posting new customer
    response = request.put(f'customers/{c_id}', customer)

    status_code = response[0]
    response_body = response[1]
    response_url = response[2]
    print(response_url)

    # checking the response status
    assert status_code == 200, f"Response code is not 200. Response is {response_body['message']}"

    # validate json schema
    assert_valid_schema(response_body, 'customer.json')

    resp_id = response_body['id']

    # mapping response to the request structure
    mapped_response = map_response(customer, response_body)

    # verifying response
    assert mapped_response == customer, 'Requested data doesn\'t match with response data'

    # verifying data in DB
    fields = (
        'first_name',
        'billing_first_name',
        'shipping_first_name',
    )

    query = f'select meta_key, meta_value ' \
            f'from {db_connect.db}.wp_usermeta ' \
            f'where user_id={c_id} and meta_key in {fields}'

    # executing select statement
    qresp = db_connect.select(query)

    first_name = qresp[0][1]
    billing_first_name = qresp[1][1]
    shipping_first_name = qresp[2][1]

    # verifying data from db and response match
    assert first_name == customer['first_name'], \
        'First name in request and db doesn\'t match'

    assert billing_first_name == customer['billing']['first_name'], \
        'Billing first name in request and db doesn\'t match'

    assert shipping_first_name == customer['shipping']['first_name'], \
        'shipping first name in request and db doesn\'t match'
def test_create_customer(get_customer_json, request, db_connect):
    """
    http://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-customer

    Create a customer
    This API helps you to create a new customer.

    HTTP request
    POST /wp-json/wc/v2/customers
    """

    # getting new customer json
    customer = get_customer_json

    # posting new customer
    response = request.post('customers', customer)

    status_code = response[0]
    response_body = response[1]
    response_url = response[2]

    # checking the response status
    assert status_code == 201, 'Response code is not 201. Response is {}'.format(response_body['message'])

    # validate json schema
    assert_valid_schema(response_body, 'customer.json')

    resp_id = response_body['id']

    # mapping response to the request structure
    mapped_response = map_response(customer, response_body)

    # verifying response
    assert mapped_response == customer, 'Requested data doesn\'t match with response data'

    # verifying data in DB
    fields = ('nickname',
              'first_name',
              'last_name',
              'billing_first_name',
              'billing_last_name',
              'billing_address_1',
              'billing_city',
              'billing_state',
              'billing_postcode',
              'billing_country',
              'billing_email',
              'billing_phone',
              'shipping_first_name',
              'shipping_last_name',
              'shipping_address_1',
              'shipping_city',
              'shipping_state',
              'shipping_postcode',
              'shipping_country'
              )

    query = f"""
            select meta_key, meta_value 
            from {db_connect.db}.wp_usermeta 
            where user_id={resp_id} 
            and meta_key in {fields}
            """

    # executing select statement
    qresp = db_connect.select(query)

    nickname = qresp[0][1]
    first_name = qresp[1][1]
    last_name = qresp[2][1]
    billing_first_name = qresp[3][1]
    billing_last_name = qresp[4][1]
    billing_address_1 = qresp[5][1]
    billing_city = qresp[6][1]
    billing_state = qresp[7][1]
    billing_postcode = qresp[8][1]
    billing_country = qresp[9][1]
    billing_email = qresp[10][1]
    billing_phone = qresp[11][1]
    shipping_first_name = qresp[12][1]
    shipping_last_name = qresp[13][1]
    shipping_address_1 = qresp[14][1]
    shipping_city = qresp[15][1]
    shipping_state = qresp[16][1]
    shipping_postcode = qresp[17][1]
    shipping_country = qresp[18][1]

    # verifying data from db and response match
    assert nickname == customer['username'], 'Username in request and db doesn\'t match'
    assert first_name == customer['first_name'], 'First name in request and db doesn\'t match'
    assert last_name == customer['last_name'], 'Last name in request and db doesn\'t match'
    assert billing_first_name == customer['billing']['first_name'], 'Billing first name in request and db doesn\'t match'
    assert billing_last_name == customer['billing']['last_name'], 'Billing last name in request and db doesn\'t match'
    assert billing_address_1 == customer['billing']['address_1'], 'billing address in request and db doesn\'t match'
    assert billing_city == customer['billing']['city'], 'billing city in request and db doesn\'t match'
    assert billing_state == customer['billing']['state'], 'billing state in request and db doesn\'t match'
    assert billing_postcode == customer['billing']['postcode'], 'billing postcode in request and db doesn\'t match'
    assert billing_country == customer['billing']['country'], 'billing country in request and db doesn\'t match'
    assert billing_email == customer['billing']['email'], 'billing email in request and db doesn\'t match'
    assert billing_phone == customer['billing']['phone'], 'billing phone in request and db doesn\'t match'
    assert shipping_first_name == customer['shipping']['first_name'], 'shipping first name in request and db doesn\'t match'
    assert shipping_last_name == customer['shipping']['last_name'], 'shilling last name in request and db doesn\'t match'
    assert shipping_address_1 == customer['shipping']['address_1'], 'shipping address in request and db doesn\'t match'
    assert shipping_city == customer['shipping']['city'], 'shipping city in request and db doesn\'t match'
    assert shipping_state == customer['shipping']['state'], 'shipping state in request and db doesn\'t match'
    assert shipping_postcode == customer['shipping']['postcode'], 'shipping postcode in request and db doesn\'t match'
    assert shipping_country == customer['shipping']['country'], 'shipping country in request and db doesn\'t match'