def log_in(self, session, name, password, role):
        if self.cookies[role]:
            session.headers.update({"Cookie": self.cookies[role]})
        else:
            session.headers.update(
                {"Content-Type": "application/x-www-form-urlencoded"})

            data = {
                "name": name,
                "pass": password,
                "form_id": target["user"]["login"]["form_id"]
            }

            with session.post("/",
                              name="/LOGIN",
                              data=data,
                              catch_response=True,
                              verify=False) as response:
                assert_status_code(response)
                if not session.cookies:
                    response.failure(
                        "No cookie retrieve from authorization response")
                else:
                    cookie_parts = session.cookies.items()[0]
                    cookie = cookie_parts[0] + '=' + cookie_parts[1]
                    self.cookies[role] = cookie
                    session.headers.update({"Cookie": f"{cookie}"})
                response.success()
                return None
    def authorize(self, session, email, password, role):
        if self.tokens[role]:
            return self.tokens[role]
        else:
            session.headers.update({"Content-Type": "application/json"})

            data = {"data": {"email": email, "password": password}}
            with session.post(target['authorization']['host'],
                              data=json.dumps(data),
                              name="/SIGN IN",
                              catch_response=True,
                              verify=False) as response:
                assert_status_code(response)
                rs_json = response.json()
            if not rs_json["data"]["accessToken"]:
                response.failure("No authorization token found in response")
            else:
                token = rs_json["data"]["accessToken"]
                self.tokens[role] = token
                cookie = response.cookies.get("token_signature")
                session.headers.update({
                    "Authorization": f"Bearer {token}",
                    "Cookie": f"token_signature={cookie}"
                })
            response.success()
            return None
def get_request_by_id(session, request_id):
    with session.get(f"/{target['requests']['host']}/requests/{request_id}",
                     catch_response=True,
                     verify=False,
                     name="/REQUESTS/[ID]") as response:
        assert_status_code(response)
        rs_json = response.json()
    return rs_json
Beispiel #4
0
 def send_post_request(self, url, request_name, filename):
     source_file = Path("data") / filename
     data = read_json(source_file)
     with self.session.post(url,
                            name=request_name,
                            data=data,
                            verify=False,
                            catch_response=True) as response:
         assert_status_code(response)
 def get_user_profile(self):
     with self.client.get(f"/{target['user-profile']['host']}",
                          catch_response=True,
                          verify=False,
                          name="/PROFILE") as response:
         assert_status_code(response)
         if response.text == "":
             response.failure("Page not loaded")
         else:
             response.success()
def send_post_request(session, url, request_name, filename, status_code=200):
    source_file = Path("data") / filename
    with open(source_file, "rb") as data:
        with session.post(url,
                          name=request_name,
                          data=data,
                          verify=False,
                          catch_response=True) as response:
            assert_status_code(response, status_code)
            if response.content:
                return response.json()
def import_csv_file(session, url, request_name, filename):
    session.headers.update({"Content-Type": None})
    content_type = "text/csv"
    file_to_open = Path("data") / filename
    with open(file_to_open, "rb") as file:
        files = {"file": (filename, file, content_type)}
        with session.post(url,
                          files=files,
                          catch_response=True,
                          name=request_name,
                          verify=False) as response:
            assert_status_code(response)
def create_request(session, filename, user_id, endpoint):
    source_file = Path("data") / filename
    with open(source_file, "rb") as data:
        with session.post(
                f"/{target['requests']['host']}/admin/{endpoint}/user/{user_id}",
                data=data,
                name=f"/{endpoint}",
                catch_response=True,
                verify=False) as response:
            assert_status_code(response)
            rs_json = response.json()
        return rs_json
Beispiel #9
0
 def send_get_request(self, url, request_name, filename=None):
     if filename:
         source_file = Path("data") / filename
         params = read_json(source_file)
     else:
         params = None
     with self.session.get(url,
                           name=request_name,
                           params=params,
                           verify=False,
                           catch_response=True) as response:
         assert_status_code(response)
 def get_transaction(self):
     tr_id = target["transactions"]["id"]
     with self.client.get(f"/{target['transactions']['host']}/{tr_id}",
                          name="/TRANSACTION",
                          catch_response=True,
                          verify=False) as response:
         assert_status_code(response)
         rs_json = response.json()
         if rs_json["data"]["id"] != tr_id:
             response.failure(
                 f"Incorrect transaction was given ({rs_json['data']['id']})"
             )
         else:
             response.success()
 def create_ca_request(self):
     filename = target["ca_request"]["filename"]
     endpoint = target["ca_request"]["endpoint"]
     request_type = target["ca_request"]["subject"]
     source_file = Path("data") / filename
     with open(source_file, "rb") as data:
         with self.client.post(
                 f"/{target['requests']['host']}/admin/{endpoint}",
                 data=data,
                 name=f"/{endpoint}",
                 catch_response=True,
                 verify=False) as response:
             assert_status_code(response)
             rs_json = response.json()
         request_id = rs_json["data"]["id"]
     requests_helper.verify_request_details(self.client, request_id,
                                            request_type)
def verify_request_details(session, request_id, request_type):
    with session.get(f"/{target['requests']['host']}/requests/{request_id}",
                     catch_response=True,
                     verify=False,
                     name="/REQUESTS/[ID]") as response:
        assert_status_code(response)
        rs_json = response.json()
        response_type = rs_json['data']['request']['request']['subject']
        response_id = rs_json['data']['request']['request']['id']
        if request_id != response_id:
            response.failure(
                f"Invalid response id ({response_id} instead of {request_id})")
        elif request_type != response_type:
            response.failure(
                f"Invalid response type ({response_type} instead of {request_type})"
            )
        else:
            response.success()