# NOTE: Generated By HttpRunner v3.1.4
# FROM: search_to_cart_buy_good.har
"""
@author :gujunhong

"""

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
import pytest
from httprunner import Parameters
from testcases.login_test.login_test import TestCaseLogin as Login
from datetime import datetime


class TestCaseSearchToCartBuyGood(HttpRunner):

    @pytest.mark.parametrize(
        'param',
        Parameters(
            {
                "phoneNumber-verifyCode-storeCode": "${parameterize(common.csv)}",
                'good_name': "${get_good_name()}",
            }
        ),
    )
    def test_start(self, param):
        super().test_start(param)

    config = (
        Config("领取优惠券")
        .base_url("${get_base_url()}")
        .variables(**{"storeCode": "${get_store_code()}"})
        .export(*["token", "sessionId"])
        .verify(False)
    )

    teststeps = [
        Step(RunTestCase("login").call(Login).export(*['sessionId', 'token', 'accountCode', 'personId', "name", "nickname", "username", "phone", "memberCode", "userCode", "account_id"])),

        Step(
            RunRequest("/mall/cmsTemplate/getTemplateByStoreCode")
            .get("/mall/cmsTemplate/getTemplateByStoreCode")
            .with_params(
                **{
                   "qp-storeCode-eq": "${get_store_code()}",
                    "qp-bg-eq":1,
                    "qp-platform-eq":2

                   }
            )
            .with_headers(
                **{
                    "x-tenant-id": "${get_tenantId()}",
                    "sso_sessionid": "$sessionId",

                }
            )
            .extract()
            .with_jmespath('body.data.cmsTemplatePageResDto.modules[1].urls[0].activityResDto.activityNo', "activityNo")
            .with_jmespath('body.data.cmsTemplatePageResDto.modules[1].urls[0].activityResDto.ticketCode', "ticketCode")
            .validate()

            .assert_equal("status_code", 200)
            .assert_equal('headers."Content-Type"', "application/json;charset=UTF-8")
            .assert_equal("body.code", "000000")
            .assert_equal("body.msg", "Success")
        ),
        #领取优惠券
        Step(
            RunRequest("/mall/item/receiveTicketList")
            .post("/mall/item/receiveTicketList")
            .with_params(
                **{"sessionId": "$sessionId"}
            )
            .with_headers(
                **{
                    "x-tenant-id": "${get_tenantId()}",
                    "sso_sessionid": "$sessionId",
                }
            )
            .with_json(
                    {
                        # "acquiredTime": "{}T06:39:39.391Z".format(datetime.now().strftime("%y-%m-%d")),
                        "memberCardCode": "$memberCode",
                        "memberLevel": "LV1",
                        "mobileNo": "$phone",
                        "ticketCode": "TK20200818000006",
                        "ticketNo": "1",
                        "ticketStatus": "2",
                        "userCode": "$accountCode",
                    }
                )
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal('headers."Content-Type"', "application/json;charset=UTF-8")
            .assert_equal("body.code", "000000")
            .assert_equal("body.msg", "Success")
        ),
        #查询优惠券列表
        Step(
            RunRequest("/mall/user/getMyTicketList")
                .get("/mall/user/getMyTicketList")
                .with_params(
                **{
                    "sessionId": "$sessionId",
                    "accountCode": "$accountCode",
                    "memberCardCode":"$memberCode",
                    "storeCode":"${get_store_code()}"

                   }
            )
                .with_headers(
                **{
                    "x-tenant-id": "${get_tenantId()}",
                    "sso_sessionid": "$sessionId",

                }
            )
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal('headers."Content-Type"', "application/json;charset=UTF-8")
            .assert_equal("body.code", "000000")
            .assert_equal("body.msg", "Success")
            # .assert_equal("body.data.unusedTicket[0].ticketCode", "$ticketCode")
        )
    ]

if __name__ == "__main__":
    TestCaseGetTicket().test_start()
