def test_post(self, mock_post, mock_service):
     api_path = "/api/xxx/"
     data = {
         "key": "value",
     }
     from django.conf import settings
     settings.CONSUL_CLIENT_ADDR = {
         "HOST": "127.0.0.1",
         "PORT": 8500
     }
     settings.SERVICE_CONF = SERVICE_CONF
     res = requests_client.post(SERVICE_CONF, api_path, data=data)
     self.assertEqual(res.json(), {'key1': 'value1'})
예제 #2
0
 def base_send_task(self,
                    exchange,
                    routing_key,
                    message_code,
                    args=[],
                    kwargs={},
                    delay=False,
                    delay_time=0):
     # {
     #     "code": "new_task",
     #     "args": [1,2,3],
     #     "kwargs": {"key": "value"},
     #     "exchange": "default",
     #     "routing_key": "default",
     #     "delivery_mode": "persistent",
     #     "delay": False,
     #     "delay_time": 1
     # }
     data = {
         "code": message_code,
         "exchange": exchange,
         "args": args,
         "kwargs": kwargs,
         "routing_key": routing_key,
         "delivery_mode": "persistent",
         "delay": delay,
         "delay_time": delay_time
     }
     parent_options = os.environ.get("SPARROW_TASK_PARENT_OPTIONS")
     if parent_options:
         # parent_options = parent_options.replace("'",'"')
         try:
             data['parent_options'] = json.loads(parent_options)
         except:
             pass
         # os.environ.pop("SPARROW_TASK_PARENT_OPTIONS")
     result = requests_client.post(self.sc_message_sender_svc,
                                   api_path=self.sc_message_sender_api,
                                   json=data)
     if result.status_code == 200:
         try:
             res = result.json()
             task_id = res.get('task_id')
             return task_id
         except:
             raise Exception(result.text)
     else:
         raise Exception(result.text)
예제 #3
0
def get_user_token(user_id):
    """
    get user token
    :param user_id:
    
    configmap:
        SC_MANAGE_SVC
        SC_MANAGE_API
    """
    user_token_key = get_hash_key(key_type="user", user_id=user_id)
    service_conf = get_settings_value("SERVICE_CONF")
    sc_manage_svc = get_cm_value("SC_MANAGE_SVC")
    sc_manage_api = get_cm_value("SC_MANAGE_API")
    try:
        if os.environ.get("SC_SKIP_TOKEN_CACHE", "").title() != "True":
            cache_user_token = cache.get(user_token_key)
            if cache_user_token:
                return cache_user_token
        # 跳过缓存或者缓存数据空
        data = {
            "name": service_conf["NAME"],
            "secret": service_conf["SECRET"],
            "uid": user_id
        }
        res = requests_client.post(service_address=sc_manage_svc,
                                   api_path=sc_manage_api,
                                   json=data)
        if res.status_code < 200 or res.status_code >= 300:
            raise Exception(
                f"请求app_manage服务出错,状态码:{res.status_code},数据:{res.text}")
        cache.set(user_token_key,
                  res.text,
                  timeout=res.json().get("expires_in", 7200))
        return res.text
    except Exception as ex:
        logger.error(
            f"sparrowcloud/get_user_token error, no token available in cache and app_manage_error,\
                        'message:{ex.__str__()}, return static token")
        return json.dumps({
            "iss": "sparrow_cloud",
            "uid": user_id,
            "type": "user"
        })
예제 #4
0
 def test_not_acl(self, mock_post):
     service_address = settings.SERVICE_ADDRESS
     res = requests_client.post(service_address, API_PATH, data=DATA)
     self.assertEqual(res.json(), {'key1': 'value1'})