小程序接口


import json
import os

import requests
import unittest
ROOT = os.path.dirname(__file__)
PLAN_ROOT = os.path.join(ROOT, "plans")
true = True
false = False
null = None

URL = "https://newrc.xkool.org"

class MiniApp():
    def __init__(self):
        self.url = URL
        self.post_headers = {"content-type": "application/json", "authorization": "Basic eGtvb2w6eGtvb2xAMDQxNQ=="}
        self.get_headers = {"authorization": "Basic eGtvb2w6eGtvb2xAMDQxNQ=="}
        self.project_list = []
        self.session = requests.Session()
        self.plan_ids = []
        self.video_ids = []
    def login(self):
        url = "{}/api/user_center/v2/dynamic_login".format(self.url)
        data = {"phone_number": "133xxxxxxxx", "dynamic_code": 666666, "login_terminal": 1}
        req = self.session.post(url, headers=self.post_headers, json=data)
        respoce_json = req.json()
        # print(respoce_json)
        for team in respoce_json["legal_entities"]:
            if team["name"] == "xkool_开发商权限_测试":
                self.get_headers["xauthorization"] = team["token"]
                self.post_headers["xauthorization"] = team["token"]
        self.legal_entity_id = "k2kg"
        self.token = "2oy1"

    def main(self):
        """首页查看"""
        # path = "/v1/app/home_page_activities"
        url = "{}/api/v1/project_center/projects".format(self.url)
        params = {"page": 1, "per_page": -1, "sort_type": 1, "share_type": 3}
        req = self.session.get(url, headers=self.get_headers)
        print(req.json()["data"])
        if req.status_code == 200:
            return True
        else:
            return False
        # req_json = req.json()
        # print(req_json)
        # return True
    def invitee_user_count(self):
        """获取当前预览总量"""
        url = "{}/api/user_center/v1/invitee/count".format(self.url)
        req = self.session.get(url, headers=self.get_headers)
        print(req.json()["data"])
        if req.status_code == 200:
            return True
        else:
            return False

    def invitee_user_list(self):
        """获取当前用户邀请列表"""
        url = "{}/api/user_center/v1/invitees".format(self.url)
        params = {"page": 1, "perPage": -1}
        req = self.session.get(url, headers=self.get_headers)
        print(req.json()["data"])
        if req.status_code == 200:
            return True
        else:
            return False
    def get_user_unique_code(self):
        """获取用户唯一邀请码"""
        url = "{}/api/user_center/v1/user_invitation/unique_code".format(self.url)
        params = {"page": 1, "perPage": -1}
        req = self.session.get(url, headers=self.get_headers)
        print(req.json()["data"])
        if req.status_code == 200:
            return True
        else:
            return False

    def get_project_list(self):
        """获取项目列表"""
        url = "{}/api/v1/wx_mini_app/project_center/projects".format(self.url)
        req = self.session.get(url, headers=self.get_headers)
        # print(req.json())
        for project in req.json()["data"]["projects"]:
            print(project)
            if project["source_type"] == "new_design" and project["plan_id_list"]:
                self.plan_ids = project["plan_id_list"]
                break
        if req.status_code == 200:
            return True
        else:
            return False

    def get_share_project(self):
        """进入方案列表"""
        data = {"expire_secs":-1,"has_password":False,"share_mode":"view_only","plan_ids":self.plan_ids}
        url = "{}/api/gh_backend/v2/plans/share_uuid".format(self.url)
        req = self.session.post(url,json=data, headers=self.post_headers)
        if req.status_code == 200 and self.plan_ids:
            return True
        else:
            return False

    def video_list(self):
        """视频列表"""
        url = "{}/api/operation/v1/videos?app_type=mini_app".format(self.url)
        req = self.session.get(url, headers=self.get_headers)
        video_data = req.json()["data"][0]
        # print(video_data)
        for children in video_data["children"]:
            for videos in children["videos"]:
                self.video_ids.append(videos["id"])
        return self.video_ids
    def get_video_data(self):
        for video_id in self.video_ids:
            url = "{}/api/operation/v1/videos/{}".format(self.url,video_id)
            print(url)
            req = self.session.get(url, headers=self.get_headers)
            if req.status_code != 200:
                 return False
        return True

class TestMiniApp(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.miniapp = MiniApp()
        cls.miniapp.login()

    @classmethod
    def tearDownClass(cls):
        print ("this teardownclass() method only called once too.\n")

    def setUp(self):
        print ("do something before test : prepare environment.\n")

    def tearDown(self):
        print ("do something after test : clean up.\n")

    def test_00001_main(self):
        """Test method main"""
        result = self.miniapp.main()
        self.assertTrue(result)

    def test_00002_invitee_user_count(self):
        """Test method invitee_user_count"""
        result = self.miniapp.invitee_user_count()
        self.assertTrue(result)

    def test_00003_invitee_user_list(self):
        """Test method invitee_user_list"""
        result = self.miniapp.invitee_user_list()
        self.assertTrue(result)

    def test_00004_get_user_unique_code(self):
        """Test method get_user_unique_code"""
        result = self.miniapp.get_user_unique_code()
        self.assertTrue(result)

    def test_00005_get_project_list(self):
        """Test method get_project_list"""
        result = self.miniapp.get_project_list()
        self.assertTrue(result)

    def test_00006_get_share_project(self):
        """Test method get_share_project"""
        result = self.miniapp.get_share_project()
        self.assertTrue(result)

    def test_00007_video_list(self):
        """Test method video_list"""
        result = self.miniapp.video_list()
        self.assertTrue(result)

    def test_00008_get_video_data(self):
        """Test method get_video_data"""
        result = self.miniapp.get_video_data()
        self.assertTrue(result)

if __name__ == "__main__":
    unittest.main()