コード例 #1
0
class CassetteSelection(unittest.TestCase):
    def setUp(self) -> None:
        # disable internet access via sockets
        setattr(socket, "socket", guard)

    def reset(self):
        setattr(socket, "socket", original_socket)

    def tearDown(self) -> None:
        self.reset()

    def testGuard(self):
        # check if
        self.assertRaises(IOError, requests.get, "http://example.com")

    @replace_module_match(
        what="requests.sessions.Session.request",
        decorate=RequestResponseHandling.decorator(
            item_list=["method", "url"]),
    )
    def testWrite(self, cassette: Cassette):
        self.reset()
        response = requests.get("http://example.com")
        self.assertIn("This domain is for use", response.text)
        self.assertFalse(os.path.exists(cassette.storage_file))
        cassette.dump()
        self.assertTrue(os.path.exists(cassette.storage_file))
        os.remove(cassette.storage_file)

    @replace_module_match(
        what="requests.sessions.Session.request",
        decorate=RequestResponseHandling.decorator(
            item_list=["method", "url"]),
    )
    def testRead(self, cassette: Cassette):
        if cassette.mode == StorageMode.read:
            self.assertTrue(os.path.exists(cassette.storage_file))
        else:
            self.reset()
        response = requests.get("http://example.com")
        self.assertIn("This domain is for use", response.text)

    @replace_module_match(
        what="requests.sessions.Session.request",
        decorate=RequestResponseHandling.decorator(
            item_list=["method", "url"]),
    )
    @replace_module_match(what="math.sin",
                          decorate=Simple.decorator(item_list=[]))
    def testReadMultiple(self, cassette: Cassette):
        assert cassette
        if cassette.mode == StorageMode.write:
            self.reset()
            sin_output = math.sin(1.5)
        else:
            sin_output = math.sin(4)
        response = requests.get("http://example.com")
        self.assertIn("This domain is for use", response.text)
        self.assertAlmostEqual(0.9974949866040544, sin_output, delta=0.0005)
コード例 #2
0
    def testRawCall(self):
        """
        Test if is class is able to explicitly write and read request handling
        """
        keys = [self.domain]
        sess = RequestResponseHandling(store_keys=keys)
        response = self.requests.post(*keys)
        sess.write(response)

        response_after = sess.read()
        self.assertIsInstance(response_after, self.requests.models.Response)
        self.assertNotIn("Example Domain",
                         str(sess.persistent_storage.storage_object))
        self.assertIn("Example Domain", response_after.text)
コード例 #3
0
 def testExecuteWrapper(self):
     """
     test if it is able to use explicit decorator_all_keys for storing request handling
     :return:
     """
     response_before = RequestResponseHandling.execute_all_keys(
         self.requests.post, self.domain)
     PersistentObjectStorage().dump()
     PersistentObjectStorage().mode = StorageMode.read
     response_after = RequestResponseHandling.execute_all_keys(
         self.requests.post, self.domain)
     self.assertEqual(response_before.text, response_after.text)
     self.assertRaises(
         Exception,
         RequestResponseHandling.execute_all_keys,
         self.requests.post,
         self.domain,
     )
コード例 #4
0
 def testFunctionDecoratorNotFound(self):
     """
     Check if it fails with Exception in case request is not stored
     """
     self.requests.post = RequestResponseHandling.decorator_all_keys()(
         self.requests.post)
     self.cassette.dump()
     self.cassette.mode = StorageMode.read
     self.assertRaises(Exception,
                       self.requests.post,
                       self.domain,
                       data={"a": "b"})
コード例 #5
0
 def testFunctionDecoratorNotFound(self):
     """
     Check if it fails with Exception in case request is not stored
     """
     self.requests.post = RequestResponseHandling.decorator_all_keys(
         self.requests.post)
     PersistentObjectStorage().dump()
     PersistentObjectStorage()._is_write_mode = False
     self.assertRaises(Exception,
                       self.requests.post,
                       self.domain,
                       data={"a": "b"})
コード例 #6
0
    def testFunctionDecorator(self):
        """
        Test main purpose of the class, decorate post function and use it then
        """
        self.requests.post = RequestResponseHandling.decorator_all_keys(
            self.requests.post)
        response_before = self.requests.post(self.domain)
        PersistentObjectStorage().dump()
        PersistentObjectStorage()._is_write_mode = False

        response_after = self.requests.post(self.domain)
        self.assertEqual(response_before.text, response_after.text)
        self.assertRaises(Exception, self.requests.post, self.domain)
コード例 #7
0
    def testFunctionDecorator(self):
        """
        Test main purpose of the class, decorate post function and use it then
        """
        self.requests.post = RequestResponseHandling.decorator_all_keys()(
            self.requests.post)
        response_before = self.requests.post(self.domain)
        self.cassette.dump()
        self.cassette.mode = StorageMode.read

        response_after = self.requests.post(self.domain)
        self.assertEqual(response_before.text, response_after.text)
        self.assertRaises(Exception, self.requests.post, self.domain)
コード例 #8
0
    def testFunctionFilterUnknownHeaders(self):
        self.requests.post = RequestResponseHandling.decorator(
            item_list=["url"],
            response_headers_to_drop=["NotKnownHeader"],
        )(self.requests.post)
        self.requests.post(self.domain)
        self.requests.post("http://www.google.com", data={"a": "b"})
        self.cassette.dump()
        self.cassette.mode = StorageMode.read

        saved_item = self.cassette.storage_object["unittest.case"][
            "tests.test_request_response"]["requre.objects"][
                "requre.cassette"]["requests.api"]["post"][
                    "http://www.google.com"][0]

        self.assertIn("headers", saved_item["output"])
        self.assertNotIn("NotKnownHeader", saved_item["output"]["headers"])
コード例 #9
0
    def testFunctionCustomFieldsWrong(self):
        """
        Check exceptions if using partial keys storing
        """
        self.requests.post = RequestResponseHandling.decorator(
            item_list=[0, "data"])(self.requests.post)
        self.requests.post(self.domain, data={"a": "b"})
        response_2 = self.requests.post(self.domain, data={"c": "d"})
        self.cassette.dump()
        self.cassette.mode = StorageMode.read

        self.assertRaises(Exception,
                          self.requests.post,
                          self.domain,
                          data={"x": "y"})
        self.assertRaises(ItemNotInStorage, self.requests.post, self.domain)
        response_2_after = self.requests.post(self.domain, data={"c": "d"})
        self.assertEqual(response_2.text, response_2_after.text)
コード例 #10
0
    def testFunctionCustomFieldsWrong(self):
        """
        Check exceptions if using partial keys storing
        """
        self.requests.post = RequestResponseHandling.decorator(
            item_list=[0, "data"])(self.requests.post)
        self.requests.post(self.domain, data={"a": "b"})
        response_2 = self.requests.post(self.domain, data={"c": "d"})
        PersistentObjectStorage().dump()
        PersistentObjectStorage()._is_write_mode = False

        self.assertRaises(Exception,
                          self.requests.post,
                          self.domain,
                          data={"x": "y"})
        self.assertRaises(KeyError, self.requests.post, self.domain)
        response_2_after = self.requests.post(self.domain, data={"c": "d"})
        self.assertEqual(response_2.text, response_2_after.text)
コード例 #11
0
    def testFunctionCustomFields(self):
        """
        Test if it is able to use partial storing of args, kwargs
        prepare to avoid leak authentication to data
        """
        self.requests.post = RequestResponseHandling.decorator(item_list=[0])(
            self.requests.post)
        response_before = self.requests.post(self.domain)
        response_google_before = self.requests.post("http://www.google.com",
                                                    data={"a": "b"})
        self.cassette.dump()
        self.cassette.mode = StorageMode.read

        response_after = self.requests.post(self.domain)
        response_google_after = self.requests.post("http://www.google.com")
        self.assertEqual(response_before.text, response_after.text)
        self.assertEqual(response_google_before.text,
                         response_google_after.text)
        self.assertRaises(Exception, self.requests.post, self.domain)
コード例 #12
0
ファイル: test_E2E_ogr.py プロジェクト: jpopelka/requre
class GithubTests(unittest.TestCase):
    def _pr_comments_test(self):
        token = os.environ.get("GITHUB_TOKEN")
        cassette = Cassette()
        if cassette.mode == StorageMode.write and (not token):
            raise EnvironmentError(
                f"You are in Requre write mode, please set proper GITHUB_TOKEN"
                f" env variables {cassette.storage_file}")
        # possible to check before reading values because in other case values are removed
        # and in write mode is does have sense at the end
        if cassette.mode == StorageMode.read:
            self.assertIn(self.id(), cassette.storage_file)
            self.assertIn("LGTM", str(cassette.storage_object))
            self.assertTrue(
                cassette.storage_object["requests.sessions"]["send"]["GET"]
                ["https://api.github.com:443/repos/packit-service/ogr"])
        service = GithubService(token=token)
        ogr_project = service.get_project(namespace="packit-service",
                                          repo="ogr")
        pr_comments = ogr_project.get_pr_comments(9)
        assert pr_comments
        assert len(pr_comments) == 2

        assert pr_comments[0].body.endswith("fixed")
        assert pr_comments[1].body.startswith("LGTM")

    @replace_module_match(
        what="requests.sessions.Session.request",
        decorate=RequestResponseHandling.decorator(
            item_list=["method", "url"],
            map_function_to_item={"url": remove_password_from_url},
        ),
    )
    def test_pr_comments(self):
        self._pr_comments_test()

    @record_requests
    def test_pr_comments_record_requests_decorator(self):
        self._pr_comments_test()
コード例 #13
0
 def testFunctionCustomFieldsCheckKeys(self):
     self.requests.post = RequestResponseHandling.decorator(
         item_list=["url", "data"],
         map_function_to_item={"url":
                               lambda x: x[0:10]})(self.requests.post)
     self.requests.post(self.domain)
     self.requests.post("http://www.google.com", data={"a": "b"})
     self.cassette.dump()
     self.cassette.mode = StorageMode.read
     print(">>>", self.cassette.storage_object)
     self.assertIn(
         "https://ex",
         self.cassette.storage_object["unittest.case"]
         ["tests.test_request_response"]["requre.objects"]
         ["requre.cassette"]["requests.api"]["post"],
     )
     self.assertIn(
         "http://www",
         self.cassette.storage_object["unittest.case"]
         ["tests.test_request_response"]["requre.objects"]
         ["requre.cassette"]["requests.api"]["post"],
     )
コード例 #14
0
ファイル: ogr.py プロジェクト: sakalosj/requre
from requre.helpers.requests_response import RequestResponseHandling
from requre.import_system import ReplaceType

session_send = {
    "Session.send": [
        ReplaceType.DECORATOR,
        RequestResponseHandling.decorator(item_list=[]),
    ]
}

MODULE_LIST = [
    ("^requests$", {
        "who_name": "ogr"
    }),
    ("^requests$", {
        "who_name": "gitlab"
    }),
    ("^requests$", {
        "who_name": "github"
    }),
    ("^requests$", {
        "who_name": "ogr.services.pagure"
    }, session_send),
    ("^requests$", {
        "who_name": "gitlab"
    }, session_send),
    ("^requests$", {
        "who_name": "github.MainClass"
    }, session_send),
    ("^requests$", {
        "who_name": "github.Requester"
コード例 #15
0
ファイル: __init__.py プロジェクト: psss/tmt
import xmlrpc.client

import nitrate
from bugzilla._backendxmlrpc import _BugzillaXMLRPCTransport
from requests import sessions
from requre import cassette
from requre.cassette import StorageKeysInspectSimple
from requre.helpers.guess_object import Guess
from requre.helpers.requests_response import RequestResponseHandling

import tmt.export

nitrate.set_cache_level(nitrate.CACHE_NONE)

# decorate functions what communicates with nitrate
nitrate.xmlrpc_driver.GSSAPITransport.single_request = Guess.decorator_plain()(
    nitrate.xmlrpc_driver.GSSAPITransport.single_request)
nitrate.xmlrpc_driver.GSSAPITransport.single_request_with_cookies = Guess.decorator_plain(
)(nitrate.xmlrpc_driver.GSSAPITransport.single_request_with_cookies)

# decorate functions that communicate with bugzilla (xmlrpc)
_BugzillaXMLRPCTransport.single_request = Guess.decorator_plain()(
    _BugzillaXMLRPCTransport.single_request)
sessions.Session.send = RequestResponseHandling.decorator(item_list=[1])(
    sessions.Session.send)

tmt.export.check_git_url = Guess.decorator_plain()(tmt.export.check_git_url)

# use storage simple strategy to avoid use full stack info for keys
cassette.StorageKeysInspectDefault = StorageKeysInspectSimple
コード例 #16
0
from requre.helpers.files import StoreFiles
from requre.helpers.simple_object import Simple
from requre.helpers.git.pushinfo import PushInfoStorageList
from requre.helpers.requests_response import RequestResponseHandling
from requre.helpers.tempfile import TempFile
from requre.import_system import upgrade_import_system
from requre.helpers.git.fetchinfo import RemoteFetch

FILTERS = (
    upgrade_import_system().decorate(
        where="download_helper",
        what="DownloadHelper.request",
        who_name="lookaside_cache_helper",
        decorator=RequestResponseHandling.decorator_plain(),
    ).replace_module(where="^tempfile$",
                     who_name="^packit",
                     replacement=TempFile).decorate(
                         where="^packit$",
                         who_name="fedpkg",
                         what="utils.run_command_remote",
                         decorator=Simple.decorator_plain(),
                     ).decorate(
                         where="packit.fedpkg",
                         what="FedPKG.clone",
                         decorator=StoreFiles.where_arg_references(
                             key_position_params_dict={"target_path": 2}),
                     ).decorate(
                         where="git",
                         who_name="local_project",
                         what="remote.Remote.push",
                         decorator=PushInfoStorageList.decorator_plain(),
コード例 #17
0
import os
from requre.storage import PersistentObjectStorage
from requre.helpers.requests_response import RequestResponseHandling
import requests

requests.Session.send = RequestResponseHandling.decorator_plain(
    requests.Session.send)

PersistentObjectStorage().storage_file = "github.yaml"

import github

g = github.Github(os.getenv("TOKEN", "EMPTY"))
print("Count of your repos: ", len(list(g.get_user().get_repos())))

PersistentObjectStorage().dump()