コード例 #1
0
    def test_delete_request_via_basic_auth(self, deletepatch, username_check_patch):

        # Make a new test instance with different authorization:
        inst = connector.HandleSystemConnector(
            username='******',
            password='******',
            handle_server_url='http://foo.com'
        )

        # Define the replacement for the patched DELETE method:
        mock_response_del = MockResponse(success=True)
        deletepatch.return_value = mock_response_del

        # Define replacement for the patched check for username existence:
        username_check_patch = mock.Mock()
        username_check_patch.response_value = True

        # Test variables
        handle = '123/456'

        # Run code to be tested
        inst.send_handle_delete_request(handle=handle)

        # Check if the DELETE request was sent exactly once:
        self.assertEqual(deletepatch.call_count, 1,
            'The method "requests.delete" was not called once, but '+str(deletepatch.call_count)+' times.')

        # Get the payload+headers passed to "requests.delete"
        headers = self.get_kw_attribute_from_mockresponse('headers',deletepatch)

        # Compare with expected payload:
        self.assertIn('Basic ', headers['Authorization'],
            'Authorization header not sent correctly: '+headers['Authorization'])
コード例 #2
0
    def test_instantiate_with_username_and_password_wrongpw(self, getpatch):

        # Define the replacement for the patched method:
        mock_response = MockResponse(success=True)
        getpatch.return_value = mock_response

        inst = EUDATHandleClient.instantiate_with_username_and_password(
            'http://someurl', '100:my/testhandle', 'passywordy')

        self.assertIsInstance(inst, EUDATHandleClient)
コード例 #3
0
    def test_instantiate_with_username_and_password_inexistentuser(
            self, getpatch):

        # Define the replacement for the patched method:
        mock_response = MockResponse(notfound=True)
        getpatch.return_value = mock_response

        with self.assertRaises(HandleNotFoundException):
            inst = EUDATHandleClient.instantiate_with_username_and_password(
                'http://someurl', '100:john/doe', 'passywordy')
コード例 #4
0
    def test_check_if_username_exists_it_doesnot(self, getpatch):
        """Test exception"""

        # Test variables
        testhandle = 'who/cares'
        
        # Define the replacement for the patched method:
        mock_response = MockResponse(notfound=True)
        getpatch.return_value = mock_response

        # Call method and check result:
        with self.assertRaises(HandleNotFoundException):
            self.inst.check_if_username_exists(testhandle)
コード例 #5
0
    def test_is_10320LOC_empty_handle_does_not_exist(self, getpatch):
        """Test exception"""

        # Test variables
        testhandle = 'who/cares'

        # Define the replacement for the patched method:
        mock_response = MockResponse(notfound=True)
        getpatch.return_value = mock_response

        # Call method and check result:
        with self.assertRaises(HandleNotFoundException):
            self.inst.is_10320LOC_empty(testhandle)
コード例 #6
0
    def test_retrieve_handle_record_json_genericerror(self, getpatch):
        """Test exception if retrieve_handle_record_json returns a strange HTTP code."""

        # Test variables:
        testhandle = 'dont/exist'

        # Define the replacement for the patched method:
        mock_response = MockResponse(status_code=99999)
        getpatch.return_value = mock_response

        # Call method and check result:
        with self.assertRaises(GenericHandleError):
            json_record = self.inst.retrieve_handle_record_json(testhandle)
コード例 #7
0
    def test_get_value_from_handle_when_handle_inexistent(self, getpatch):
        """Test error when retrieving a handle record, giving a None type."""

        # Test variables
        testhandle = 'who/cares'
        key = 'foo'

        # Define the replacement for the patched method:
        mock_response = MockResponse(notfound=True)
        getpatch.return_value = mock_response

        # Call method and check result:
        with self.assertRaises(HandleNotFoundException):
            self.inst.get_value_from_handle(testhandle, key=key)
コード例 #8
0
    def test_retrieve_handle_record_json_handle_empty(self, getpatch):
        """Test return value if handle is empty (retrieve_handle_record_json)."""

        # Test variables:
        testhandle = 'dont/exist'

        # Define the replacement for the patched method:
        mock_response = MockResponse(empty=True)
        getpatch.return_value = mock_response

        # Call method and check result:
        json_record = self.inst.retrieve_handle_record_json(testhandle)
        self.assertEquals(json_record['responseCode'], 200,
                          'Unexpected return value: ' + str(json_record))
コード例 #9
0
    def test_check_if_username_exists_inconsistent_info(self, getpatch):
        """Test exception when contradictory inputs are given."""
    
        # Test variables
        handlerecord_string = RECORD
        testhandle = 'who/cares'

        # Define the replacement for the patched method:
        mock_response = MockResponse(success=True, content=handlerecord_string)
        getpatch.return_value = mock_response

        # Call method and check result:
        with self.assertRaises(GenericHandleError):
            self.inst.check_if_username_exists(testhandle)
コード例 #10
0
    def test_retrieve_handle_record_when_handle_is_wrong(self, getpatch):
        """Test error when retrieving a nonexistent handle record."""

        # Test variable
        testhandle = 'who/cares'

        # Define the replacement for the patched method:
        mock_response = MockResponse(notfound=True)
        getpatch.return_value = mock_response

        # Call method and check result:
        hrec = self.inst.retrieve_handle_record(testhandle)
        self.assertIsNone(
            hrec, 'The handle record for a nonexistent handle should be None!')
コード例 #11
0
    def test_retrieve_handle_record_json_normal(self, getpatch):
        """Test if retrieve_handle_record_json returns the correct things.."""

        # Test variables:
        handlerecord = RECORD
        expected = json.loads(handlerecord)

        # Define the replacement for the patched method:
        mock_response = MockResponse(success=True, content=handlerecord)
        getpatch.return_value = mock_response

        # Call method and check result:
        received = self.inst.retrieve_handle_record_json(expected['handle'])
        self.assertEqual(received, expected,
                         'Unexpected return from handle retrieval.')
コード例 #12
0
    def test_get_request(self, getpatch):

        # Define the replacement for the patched GET method:
        mock_response_get = MockResponse()
        getpatch.return_value = mock_response_get

        # Test variables
        handle = '123/456'

        # Run code to be tested
        self.inst.send_handle_get_request(handle)

        # Check if the GET request was sent exactly once:
        self.assertEqual(getpatch.call_count, 1,
            'The method "requests.get" was not called once, but '+str(getpatch.call_count)+' times.')
コード例 #13
0
    def test_retrieve_handle_record_when_handle_is_wrong(self, getpatch):
        """Test error when retrieving a handle record with contradicting inputs."""

        # Test variable
        testhandle = 'something/else'
        handlerecord_string = RECORD
        handlerecord_json = json.loads(handlerecord_string)

        # Define the replacement for the patched method:
        mock_response = MockResponse(success=True, content=handlerecord_string)
        getpatch.return_value = mock_response

        # Call method and check result:
        with self.assertRaises(GenericHandleError):
            self.inst.retrieve_handle_record(testhandle)
コード例 #14
0
    def test_retrieve_handle_record_json_handle_does_not_exist(self, getpatch):
        """Test return value (None) if handle does not exist (retrieve_handle_record_json)."""

        # Test variables:
        testhandle = 'dont/exist'

        # Define the replacement for the patched method:
        mock_response = MockResponse(notfound=True)
        getpatch.return_value = mock_response

        # Call method and check result:
        json_record = self.inst.retrieve_handle_record_json(testhandle)
        self.assertIsNone(
            json_record,
            'The return value should be None if the handle does not exist, not: '
            + str(json_record))
コード例 #15
0
    def test_check_if_username_exists_normal(self, getpatch):
        """Test whether username exists."""

        # Test variables
        handlerecord_string = RECORD
        handlerecord_json = json.loads(handlerecord_string)
        testhandle = '100:'+handlerecord_json['handle']

        # Define the replacement for the patched method:
        mock_response = MockResponse(success=True, content=handlerecord_string)
        getpatch.return_value = mock_response

        # Call method and check result:
        res = self.inst.check_if_username_exists(testhandle)
        self.assertTrue(res,
            'The handle exists, so "check_if_username_exists" should return true!')
コード例 #16
0
    def test_instantiate_with_credentials_config(self, checkpatch):
        """Test instantiation of client: No exception if password wrong."""

        # Define the replacement for the patched method:
        mock_response = MockResponse(success=True)
        checkpatch.return_value = mock_response

        # Test variables
        credentials = MockCredentials(restapi='foobar')

        self.assertEqual(credentials.get_config()['REST_API_url_extension'],
                         'foobar', 'Config: ' + str(credentials.get_config()))

        # Run code to be tested
        # Create instance with credentials
        inst = EUDATHandleClient.instantiate_with_credentials(credentials)
        self.assertIsInstance(inst, EUDATHandleClient)
コード例 #17
0
    def test_instantiate_with_credentials_inexistentuser(self, getpatch):
        """Test instantiation of client: Exception if username does not exist."""

        # Define the replacement for the patched method:
        mock_response = MockResponse(notfound=True)
        getpatch.return_value = mock_response

        # Test variables
        testusername_inexistent = '100:john/doe'
        credentials = b2handle.clientcredentials.PIDClientCredentials(
            handle_server_url='some/url',
            username=testusername_inexistent,
            password='******')

        # Run code to be tested + check exception:
        # Create instance with credentials
        with self.assertRaises(HandleNotFoundException):
            inst = EUDATHandleClient.instantiate_with_credentials(credentials)
コード例 #18
0
    def test_instantiate_with_credentials(self, getpatch):
        """Test instantiation of client: No exception if password wrong."""

        # Define the replacement for the patched method:
        mock_response = MockResponse(success=True)
        getpatch.return_value = mock_response

        # Test variables
        credentials = b2handle.clientcredentials.PIDClientCredentials(
            handle_server_url='some/url',
            username='******',
            password='******')

        # Run code to be tested
        # Create instance with credentials
        inst = EUDATHandleClient.instantiate_with_credentials(credentials)

        # Check desired outcomes
        self.assertIsInstance(inst, EUDATHandleClient)
コード例 #19
0
    def test_is_url_contained_in_10320LOC_handle_does_not_exist(
            self, getpatch):
        """Test exception"""

        # Test variables
        testhandle = 'who/cares'
        one_url = 'http://bla'
        list_of_urls = ['http://bla', 'http://foo.foo']

        # Define the replacement for the patched method:
        mock_response = MockResponse(notfound=True)
        getpatch.return_value = mock_response

        # Call method and check result:
        with self.assertRaises(HandleNotFoundException):
            self.inst.is_URL_contained_in_10320LOC(testhandle, url=one_url)
        with self.assertRaises(HandleNotFoundException):
            self.inst.is_URL_contained_in_10320LOC(testhandle,
                                                   url=list_of_urls)
コード例 #20
0
    def test_retrieve_handle_record_when_handlerecord_is_None(self, getpatch):
        """Test error when retrieving a handle record, giving a None type."""

        # Test variable
        handlerecord_string = RECORD
        handlerecord_json = json.loads(handlerecord_string)
        testhandle = handlerecord_json['handle']
        givenrecord = None

        # Define the replacement for the patched method:
        mock_response = MockResponse(success=True, content=handlerecord_string)
        getpatch.return_value = mock_response

        # Call method and check result:
        dict_record = self.inst.retrieve_handle_record(testhandle, givenrecord)
        self.assertIn('TEST1', dict_record,
                      'Key "test1" not in handlerecord dictionary!')
        self.assertIn('TEST2', dict_record,
                      'Key "test2" not in handlerecord dictionary!')
        self.assertIn('TESTDUP', dict_record,
                      'Key "TESTDUP" not in handlerecord dictionary!')
        self.assertIn('HS_ADMIN', dict_record,
                      'Key "HS_ADMIN" not in handlerecord dictionary!')

        self.assertEqual(dict_record['TEST1'], 'val1',
                         'The value of "test1" is not "val1.')
        self.assertEqual(dict_record['TEST2'], 'val2',
                         'The value of "test2" is not "val2.')
        self.assertIn(
            dict_record['TESTDUP'], ("dup1", "dup2"),
            'The value of the duplicate key "TESTDUP" should be "dup1" or "dup2".'
        )
        self.assertIn(
            'permissions', dict_record['HS_ADMIN'],
            'The HS_ADMIN has no permissions: ' + dict_record['HS_ADMIN'])

        self.assertEqual(
            len(dict_record), 4,
            'The record should have a length of 5 (as the duplicate is ignored.'
        )
コード例 #21
0
    def test_delete_request_via_cert(self, deletepatch):

        # Define the replacement for the patched DELETE method:
        mock_response_del = MockResponse(success=True)
        deletepatch.return_value = mock_response_del

        # Test variables
        handle = '123/456'

        # Run code to be tested
        self.inst.send_handle_delete_request(handle=handle)

        # Check if the DELETE request was sent exactly once:
        self.assertEqual(deletepatch.call_count, 1,
            'The method "requests.delete" was not called once, but '+str(deletepatch.call_count)+' times.')

        # Get the payload+headers passed to "requests.delete"
        headers = self.get_kw_attribute_from_mockresponse('headers',deletepatch)

        # Compare with expected payload:
        self.assertEquals(headers['Authorization'], 'Handle clientCert="true"',
            'Authorization header not sent correctly: '+headers['Authorization'])
コード例 #22
0
    def test_delete_request_via_cert(self, deletepatch):

        # Define the replacement for the patched DELETE method:
        mock_response_del = MockResponse(success=True)
        deletepatch.return_value = mock_response_del

        # Test variables
        handle = '123/456'
        indices = [1,4,5]

        # Run code to be tested
        self.inst.send_handle_delete_request(handle=handle, indices=indices)

        # Check if the DELETE request was sent exactly once:
        self.assertEqual(deletepatch.call_count, 1,
            'The method "requests.delete" was not called once, but '+str(deletepatch.call_count)+' times.')

        # Get the url passed to "requests.delete"
        url = self.get_pos_attribute_from_mockresponse(0,deletepatch)

        # Compare with expected payload:
        self.assertIn('index=1', url, 'Index 1 missing')
        self.assertIn('index=4', url, 'Index 4 missing')
        self.assertIn('index=5', url, 'Index 5 missing')
コード例 #23
0
    def test_put_request(self, putpatch):

        # Define the replacement for the patched PUT method:
        mock_response_put = MockResponse(wascreated=True)
        putpatch.return_value = mock_response_put

        # Test variables
        handle = '123/456'
        list_of_entries = [{"index":2, "type":"XYZ", "data":"xyz"}]

        # Run code to be tested
        self.inst.send_handle_put_request(handle=handle, list_of_entries=list_of_entries)

        # Check if the PUT request was sent exactly once:
        self.assertEqual(putpatch.call_count, 1,
            'The method "requests.put" was not called once, but '+str(putpatch.call_count)+' times.')

        # Get the payload+headers passed to "requests.put"
        passed_payload = self.get_payload_from_mockresponse(putpatch)

        # Compare with expected payload:
        expected_payload = {"values": list_of_entries}
        self.assertEqual(passed_payload, expected_payload,
            failure_message(expected=expected_payload, passed=passed_payload, methodname='put_request'))
コード例 #24
0
    def test_instantiate_with_credentials_config_override(
            self, getpatch, checkpatch):
        """Test instantiation of client: We pass a config value in the credentials
        and also as an arg in the instantiation. We want the latter to override the
        first one.
        """

        # Define the replacement for the patched method:
        # We pretend the username exists!
        mock_response = MockResponse(success=True)
        checkpatch.return_value = mock_response

        # Define the replacement for the patched GET:
        cont = {
            "responseCode":
            1,
            "handle":
            "my/testhandle",
            "values": [{
                "index": 111,
                "type": "TEST1",
                "data": {
                    "format": "string",
                    "value": "val1"
                },
                "ttl": 86400,
                "timestamp": "2015-09-30T15:08:49Z"
            }, {
                "index": 2222,
                "type": "TEST2",
                "data": {
                    "format": "string",
                    "value": "val2"
                },
                "ttl": 86400,
                "timestamp": "2015-09-30T15:08:49Z"
            }, {
                "index": 333,
                "type": "TEST3",
                "data": {
                    "format": "string",
                    "value": "val3"
                },
                "ttl": 86400,
                "timestamp": "2015-09-30T15:08:49Z"
            }, {
                "index": 4,
                "type": "TEST4",
                "data": {
                    "format": "string",
                    "value": "val4"
                },
                "ttl": 86400,
                "timestamp": "2015-09-30T15:08:49Z"
            }]
        }
        mock_response = MockResponse(success=True, content=json.dumps(cont))
        getpatch.return_value = mock_response

        # Test variables
        # Passing mock credentials, give them the value "foobar", which
        # should be overridden!
        credentials = MockCredentials(restapi='foobar')
        self.assertEqual(credentials.get_config()['REST_API_url_extension'],
                         'foobar', 'Config: ' + str(credentials.get_config()))

        # Run code to be tested
        # Create instance with credentials. It gets the "REST_API_url_extention"
        # from both the credentials and as a param.
        inst = EUDATHandleClient.instantiate_with_credentials(
            credentials, REST_API_url_extension='bar/bar/bar')
        self.assertIsInstance(inst, EUDATHandleClient)

        # How to know now which one was used?
        # Call a read and check its url! Did it get foobar or barbarbar appended?
        inst.get_value_from_handle('my/testhandle', 'key')
        positional_args_passed = getpatch.call_args_list[
            len(getpatch.call_args_list) - 1][0]
        passed_url = positional_args_passed[0]

        # Compare with expected URL:
        self.assertIn('bar/bar/bar', passed_url,
                      'bar/bar/bar is not specified in the URL ' + passed_url)