Esempio n. 1
0
class Project:

    def __init__(self):
        self.session = Session()
        self.cleaner = Cleaner()
        self.set_up_clean()
        self.json_schema = json_schema
        # atexit.register(self.tear_down_clean)

    def send(self, method, url, id_to_url, **kwargs):
        if method in ("GET", "DELETE"):
            json = {}
            request_response = self.json_schema.replace_on(url=url, method=method, data=json)
        else:
            request_response = self.json_schema.replace_on(url=url, method=method, data=kwargs['json'])
            kwargs['json'] = request_response['request']
        url = get_url(url, id_to_url)
        response = self.session.send_request(method, url, **kwargs)
        response.expected = request_response['schema']
        return response

    def post(self, url, json=None, id_to_url=None, **kwargs):
        response = self.send(method="POST", url=url, id_to_url=id_to_url, json=json, **kwargs)
        self.cleaner.add(get_url(url), response)
        return response

    def get(self, url, id_to_url=None, **kwargs):
        return self.send(method="GET", url=url, id_to_url=id_to_url, **kwargs)

    def put(self, url, json=None, id_to_url=None, **kwargs):
        return self.send(method="PUT", url=url, id_to_url=id_to_url, json=json, **kwargs)

    def delete(self, url, id_to_url=None, **kwargs):
        return self.send(method="DELETE", url=url, id_to_url=id_to_url, **kwargs)

    def set_up_clean(self):
        """
        Очищает тестовые данные с прошлого запуска
        :return:
        """
        with open(DATA_TO_CLEAN, "r") as f:
            for i in f.readlines():
                rid_url = make_tuple(i)
                self.session.send_request(method="DELETE", url=get_url(rid_url[0], rid_url[1]))


    def tear_down_clean(self):
        """
        Очищает все тестовые данные за данную сессию
        :return:
        """
        for i in self.cleaner.storage_copy:
            self.session.delete(url=i[0], id_to_url=i[1])
Esempio n. 2
0
 def test_delete_connector_with_unknown_id(self, connector):
     random_id = random_string()
     response = Session.delete(TestConnectors.url, id_to_url=random_id)
     expected_response = {
         'UNMAPPED_EXCEPTION':
         'SMCRequestException: Connector with id=%s not found' % random_id
     }
     print(response.json())
     assert (response.status_code, response.json()) == (500,
                                                        expected_response)
Esempio n. 3
0
 def test_add_unknown_connector_with_ROOT_group(self, clear_data):
     data = parse_request(
         "post_connectors", {
             "$name": random_string(),
             "$url": "http://" + random_string(),
             "$groupId": root_group_id()
         })
     response = Session.post(TestConnectors.url, data['request'])
     print(response.json())
     assert response.status_code == 200
     clear_data.append(response.json()['rid'])
     assert equal_schema(response.json(), data['schema'])
Esempio n. 4
0
 def test_add_connector_with_empty_url(self):
     data = parse_request("post_connectors", {
         "$name": random_string(),
         "$url": None,
         "$groupId": root_group_id()
     })
     response = Session.post(TestConnectors.url, data['request'])
     print(response.json())
     expected_result = {
         'SMC_VALIDATION_CONNECTOR_URL': "Connector url mustn't be empty"
     }
     assert (response.status_code, response.json()) == (400,
                                                        expected_result)
Esempio n. 5
0
 def test_edit_connector(self, connector, immutable_group_with_child):
     group_id = immutable_group_with_child['groupId']
     data = parse_request(
         "put_connectors", {
             "$rid": connector['rid'],
             "$name": random_string(),
             "$url": "http://" + random_string(),
             "$groupId": group_id
         })
     response = Session.put(TestConnectors.url, data['request'])
     print(response.json())
     assert response.status_code == 200
     assert equal_schema(response.json(), data['schema'])
Esempio n. 6
0
 def test_edit_connector_without_name(self, connector):
     data = parse_request(
         "put_connectors", {
             "$rid": connector['rid'],
             "$name": None,
             "$url": "http://" + random_string(),
             "$groupId": root_group_id()
         })
     response = Session.put(TestConnectors.url, data['request'])
     print(response.json())
     expected_response = {
         'SMC_VALIDATION_CONNECTOR_NAME': "Connector name mustn't be empty"
     }
     assert (response.status_code, response.json()) == (400,
                                                        expected_response)
Esempio n. 7
0
 def test_add_connector_without_group(self):
     data = parse_request(
         "post_connectors", {
             "$name": random_string(),
             "$url": "http://" + random_string(),
             "$groupId": None
         })
     response = Session.post(TestConnectors.url, data['request'])
     print(response.json())
     expected_result = {
         'SMC_VALIDATION_CONNECTOR_GROUP':
         "Connector group mustn't be empty"
     }
     assert (response.status_code, response.json()) == (400,
                                                        expected_result)
Esempio n. 8
0
 def test_add_connector_with_existing_URL(self, connector):
     existing_url = connector['url']
     data = parse_request(
         "post_connectors", {
             "$name": random_string(),
             "$url": existing_url,
             "$groupId": root_group_id()
         })
     response = Session.post(TestConnectors.url, data['request'])
     print(response.json())
     expected_result = {
         'SMC_VALIDATION_CONNECTOR_URL_UNIQUE':
         "Connector url must be unique"
     }
     assert (response.status_code, response.json()) == (400,
                                                        expected_result)
Esempio n. 9
0
 def test_edit_connector_with_random_rid(self):
     random_rid = random_string()
     data = parse_request(
         "put_connectors", {
             "$rid": random_rid,
             "$name": random_string(),
             "$url": "http://" + random_string(),
             "$groupId": root_group_id()
         })
     response = Session.put(TestConnectors.url, data['request'])
     print(response.json())
     expected_response = {
         'UNMAPPED_EXCEPTION':
         'SMCRequestException: Connector with rid=%s not found' % random_rid
     }
     assert (response.status_code, response.json()) == (500,
                                                        expected_response)
Esempio n. 10
0
 def test_get_connectors(self, connector):
     response = Session.get(TestConnectors.url)
     assert response.status_code == 200
     assert connector in response.json()
Esempio n. 11
0
 def test_delete_connector(self, connector):
     response = Session.delete(TestConnectors.url,
                               id_to_url=connector['rid'])
     print(response.json())
     assert (response.status_code, response.json()) == (200, connector)
Esempio n. 12
0
 def __init__(self):
     self.session = Session()
     self.cleaner = Cleaner()
     self.set_up_clean()
     self.json_schema = json_schema