# -*- coding: utf-8 -*-
# 测试用例
# 作者: 陈磊
# 时间: 2019-11-27


from __future__ import division

import pytest


from WorkCase.APP import AppBase
from WorkCase.API import ApiBase
from WorkData.Argus.alarm_rule import DataAlarmRule
from WorkData.Argus.businesses import DataBusinesses
from WorkData.Argus.experience import DataExperience
from WorkData.Argus.hosts import DataHosts
from WorkData.Argus.hosts_items import DataHostsItems
from WorkData.Argus.members import DataMembers
from WorkData.Argus.problem import DataProblem
from WorkData.Argus.tag import DataTag
from WorkUtils.UtilsLog import UtilsLog
from WorkUtils.UtilsDataBase import UtilsDataBase
from WorkUtils.UtilsResponse import UtilsResponse
from WorkApi.APP.Experience.Problem.experience_problem_list import ExperienceProblemList

from WorkCase import CaseBase

import allure


@allure.feature("测试模块：experience.problem.list")
class TestExperienceProblemList(object):
    log = UtilsLog()
    env = CaseBase().app_environment
    host = env["host"]
    db_url = env["db_url"]
    db_port = env["db_port"]
    db_user = env["db_user"]
    db_pw = env["db_pw"]
    db_base = env["db_base"]

    base_env = CaseBase().environment
    base_host = base_env["host"]

    login_name = "SS测试用户名"
    base_id = 10
    base_password = "123456"

    base_type_agent = 126
    base_type_snmp = 127
    base_business_id_1 = "12"
    base_business_id_2 = "13"

    base_ip = "172.16.3.170"
    base_port = 161
    base_title = "SS 自动化专用 经验手册 标题"
    base_content = "SS 自动化专用 经验手册 内容"
    base_description = "SS 自动化专用 经验手册 描述"

    base_name = "SS 自动化专用 经验手册相关"
    base_eventid = 91000000

    session = UtilsDataBase().conn_mysql(db_url=db_url, db_port=db_port, db_base=db_base, db_user=db_user, db_pw=db_pw)

    @classmethod
    def setup_class(cls):
        cls.log.debug("开始执行测试套件.......")

        DataAlarmRule().delete_like_name(session=cls.session, name=cls.base_name)
        DataBusinesses().delete_like_name(session=cls.session, name=cls.base_name)

        sql = DataHosts().select_all_like_host_name(session=cls.session, host_name=cls.base_name)
        for x, y in enumerate(sql):
            DataHostsItems().delete_host_id(session=cls.session, host_id=y.hostid)
        DataProblem().delete_like_name(session=cls.session, name=cls.base_name)
        DataHosts().delete_like_host_name(session=cls.session, host_name=cls.base_name)
        DataTag().delete_like_tag(session=cls.session, tag=cls.base_name)
        DataExperience().delete_like_title(session=cls.session, title=cls.base_title)

    @classmethod
    def teardown_class(cls):
        cls.log.debug("结束执行测试套件.......")

    def setup_method(self):
        self.log.debug("测试用例执行开始...")

        self.app = AppBase(host=self.host)
        self.app.update_members_del_flag(session=self.db_session(), members_id=self.base_id, value=CaseBase().del_flag["OFF"])
        self.app.update_members_status(session=self.db_session(), members_id=self.base_id, value=CaseBase().status["ON"])
        self.token = self.app.get_base_token(login_name=self.login_name, base_password=self.base_password)

    def teardown_method(self):
        self.log.debug("测试用例执行结束...")
        self.update_members_del_flag(members_id=self.base_id, value=CaseBase().del_flag["OFF"])
        self.update_members_status(members_id=self.base_id, value=CaseBase().status["ON"])

    @allure.step("查询表：hosts_items")
    def select_hosts_items(self, host_id=None):
        session = self.db_session()
        sql = DataHostsItems().select_all_from_allKeys(session=session, host_id=host_id)
        return sql

    @allure.step("创建测试数据")
    def case_create(self, num):
        self.name = self.base_name + num
        self.eventid = self.base_eventid + int(num)

        session = self.db_session()
        self.alarm_ruleid = DataAlarmRule().insert_one(session=session, name=self.name, status=1, severity=1,
                                                       manual_close=0, enable_time_default=1, create_time=1)
        session = self.db_session()
        self.businessid = DataBusinesses().insert_one(session=session, name=self.name, _type=1)

        self.hostid = self.app.case_create_host(
            name=self.name, host_type=self.base_type_snmp, iplist=self.base_ip, port=self.base_port,
            businessIds=self.base_business_id_1 + "," + self.base_business_id_2
        )

        sql = self.select_hosts_items(host_id=self.hostid)
        itemids = []
        for x, y in enumerate(sql):
            itemids.append(y.id)
        self.itemid = itemids[0]
        session = self.db_session()
        DataProblem().insert_one(session=session, eventid=self.eventid, source=0, _object=0, objectid=0, clock=0, ns=0,
                                 r_clock=0, r_ns=0, acknowledged=0, acknowledged_time=0, severity=0,
                                 status=0, hostid=self.hostid, name=self.name)

    @allure.step("连接数据库：Argus")
    def db_session(self):
        session = UtilsDataBase().conn_mysql(db_url=self.db_url, db_port=self.db_port, db_base=self.db_base, db_user=self.db_user, db_pw=self.db_pw)
        return session

    @allure.title("experience.problem.list：token未传")
    @allure.story("查询告警下拉框：token未传")
    @allure.severity("blocker")
    @pytest.mark.skip(reason="此接口未加入token验证")
    def test_case_01(self):
        response = self.app.experience_problem_list()
        self.app.check_code(response=response, code=2001)
        self.app.check_msg(response=response, msg="无token，请重新登录")

    @allure.title("experience.problem.list：token的用户已删除")
    @allure.story("查询告警下拉框：token的用户已删除")
    @allure.severity("blocker")
    @pytest.mark.skip(reason="此接口未加入token验证")
    def test_case_02(self):
        self.app.update_members_del_flag(session=self.db_session(), members_id=self.base_id, value=CaseBase().del_flag["ON"])
        response = self.app.experience_problem_list(token=self.token)
        self.app.check_code(response=response, code=2004)
        self.app.check_msg(response=response, msg="用户不存在")

    @allure.title("experience.problem.list：token的用户已禁用")
    @allure.story("查询告警下拉框：token的用户已禁用")
    @allure.severity("blocker")
    @pytest.mark.skip(reason="此接口未加入token验证")
    def test_case_03(self):
        self.app.update_members_status(session=self.db_session(), members_id=self.base_id, value=CaseBase().status["OFF"])
        response = self.app.experience_problem_list(token=self.token)
        self.app.check_code(response=response, code=2003)
        self.app.check_msg(response=response, msg="账户已被禁用")

    @allure.title("experience.problem.list：problemName成功查询")
    @allure.story("查询告警下拉框：problemName成功查询")
    @allure.severity("blocker")
    def test_case_04(self):
        title = self.base_title + "04"
        content = self.base_content + "04"
        description = self.base_description + "04"
        self.case_create(num="04")
        self.app.case_create_experience(title=title, content=content, description=description, host_types=[self.base_type_agent])

        response = self.app.experience_problem_list(token=self.token, problemName="SS")
        self.app.check_code(response=response, code=0)
        self.app.check_keyValue(_json=UtilsResponse().get_data(response=response), _key="name", _value="SS", isIn=1)


if __name__ == "__main__":
    from WorkUtils.UtilsPyTest import UtilsPyTest
    from WorkUtils.UtilsCmd import UtilsCmd

    import os

    # 执行自动化测试用例
    case_info = os.path.split(__file__)
    case = UtilsCmd().pytest_cmd()
    r = UtilsPyTest(case=case, case_info=case_info)
    r.run_main()

    # a = TestExperienceProblemList()
    # a.setup_class()
    # a.setup_method()
    # a.test_case_04()
