Exemplo n.º 1
0
    def __init__(self, *args, **kwargs):

        REQUESTLOGGER.info("\nINIT of EUDATHandleClientWriteaccessTestCase")

        unittest.TestCase.__init__(self, *args, **kwargs)

        # Read resources from file:
        self.testvalues = RESOURCES_FILE

        # Test values that need to be given by user:
        self.handle = self.testvalues['handle_to_be_modified']
        self.newhandle = self.testvalues['handle_to_be_created']

        self.url = self.testvalues['handle_server_url_write']
        self.user = self.testvalues['user']
        self.password = self.testvalues['password']

        # Optional:
        self.https_verify = True
        if 'HTTPS_verify' in self.testvalues:
            self.https_verify = self.testvalues['HTTPS_verify']

        # Others
        self.prefix = self.handle.split('/')[0]
        self.inexistent_handle = self.prefix + '/07e1fbf3-2b72-430a-a035-8584d4eada41'
        self.randompassword = '******'
        self.headers = None
        self.connector = HandleSystemConnector(handle_server_url=self.url)
    def __init__(self, *args, **kwargs):

        REQUESTLOGGER.info("\nINIT of EUDATHandleClientWriteaccessTestCase")

        unittest.TestCase.__init__(self, *args, **kwargs)

        # Read resources from file:
        self.testvalues = json.load(open(RESOURCES_FILE))

        # Test values that need to be given by user:
        self.handle = self.testvalues['handle_to_be_modified']
        self.newhandle = self.testvalues['handle_to_be_created']
        
        self.url = self.testvalues['handle_server_url_write']
        self.user = self.testvalues['user']
        self.password = self.testvalues['password']

        # Optional:
        self.https_verify = True
        if 'HTTPS_verify' in self.testvalues:
            self.https_verify = self.testvalues['HTTPS_verify']

        # Others
        self.prefix = self.handle.split('/')[0]
        self.inexistent_handle = self.prefix+'/07e1fbf3-2b72-430a-a035-8584d4eada41'
        self.randompassword = '******'
        self.headers = None
        self.connector = HandleSystemConnector(handle_server_url=self.url)
Exemplo n.º 3
0
class EUDATHandleClientWriteaccessTestCase(unittest.TestCase):
    def __init__(self, *args, **kwargs):

        REQUESTLOGGER.info("\nINIT of EUDATHandleClientWriteaccessTestCase")

        unittest.TestCase.__init__(self, *args, **kwargs)

        # Read resources from file:
        self.testvalues = RESOURCES_FILE

        # Test values that need to be given by user:
        self.handle = self.testvalues['handle_to_be_modified']
        self.newhandle = self.testvalues['handle_to_be_created']

        self.url = self.testvalues['handle_server_url_write']
        self.user = self.testvalues['user']
        self.password = self.testvalues['password']

        # Optional:
        self.https_verify = True
        if 'HTTPS_verify' in self.testvalues:
            self.https_verify = self.testvalues['HTTPS_verify']

        # Others
        self.prefix = self.handle.split('/')[0]
        self.inexistent_handle = self.prefix + '/07e1fbf3-2b72-430a-a035-8584d4eada41'
        self.randompassword = '******'
        self.headers = None
        self.connector = HandleSystemConnector(handle_server_url=self.url)

    def setUp(self):

        REQUESTLOGGER.info("\n" + 60 * "*" +
                           "\nsetUp of EUDATHandleClientWriteaccessTestCase")

        self.inst = EUDATHandleClient.instantiate_with_username_and_password(
            self.url,
            self.user,
            self.password,
            HTTPS_verify=self.https_verify,
            handleowner=self.user)

        authstring = b2handle.utilhandle.create_authentication_string(
            self.user, self.password)
        self.headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Basic ' + authstring
        }

        list_of_all_entries = [
            {
                "index": 100,
                "type": "HS_ADMIN",
                "data": {
                    "format": "admin",
                    "value": {
                        "handle": "21.T14999/B2HANDLE_INTEGRATION_TESTS",
                        "index": 300,
                        "permissions": "011111110011"
                    }
                }
            },
            {
                "index": 111,
                "type": "TEST1",
                "data": {
                    "format": "string",
                    "value": "val1"
                }
            },
            {
                "index": 2222,
                "type": "TEST2",
                "data": {
                    "format": "string",
                    "value": "val2"
                }
            },
            {
                "index": 333,
                "type": "TEST3",
                "data": {
                    "format": "string",
                    "value": "val3"
                }
            },
            {
                "index": 4,
                "type": "TEST4",
                "data": {
                    "format": "string",
                    "value": "val4"
                }
            },
        ]

        testhandle = self.handle
        url = self.connector.make_handle_URL(testhandle)
        veri = self.https_verify
        head = self.headers
        data = json.dumps({'values': list_of_all_entries})
        resp = requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', self.handle, url, head, veri, resp)

    def tearDown(self):
        pass

    # modify_handle_value:

    def test_modify_handle_value_corrupted(self):
        """Test exception when trying to modify corrupted handle record."""
        log_new_case("test_modify_handle_value_corrupted")

        # Test variables
        testhandle = self.handle
        head = self.headers
        url = self.connector.make_handle_URL(testhandle)
        # Create corrupted record:
        list_of_all_entries = [{
            "index": 100,
            "type": "HS_ADMIN",
            "data": {
                "format": "admin",
                "value": {
                    "handle": "21.T14999/B2HANDLE_INTEGRATION_TESTS",
                    "index": 300,
                    "permissions": "011111110011"
                }
            }
        }, {
            "index": 111,
            "type": "TEST1",
            "data": {
                "format": "string",
                "value": "val1"
            }
        }, {
            "index": 2222,
            "type": "TEST2",
            "data": {
                "format": "string",
                "value": "val2"
            }
        }, {
            "index": 333,
            "type": "TEST2",
            "data": {
                "format": "string",
                "value": "val3"
            }
        }, {
            "index": 4,
            "type": "TEST4",
            "data": {
                "format": "string",
                "value": "val4"
            }
        }]
        data = json.dumps({'values': list_of_all_entries})
        veri = self.https_verify
        resp = requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', testhandle, url, head, veri, resp,
                                     data)

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(BrokenHandleRecordException):
            self.inst.modify_handle_value(testhandle,
                                          TEST4='new4',
                                          TEST2='new2',
                                          TEST3='new3')
        log_end_test_code()

    def test_modify_handle_value_one(self):
        """Test modifying one existing handle value."""
        log_new_case("test_modify_handle_value_one")

        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.modify_handle_value(testhandle, TEST4='newvalue')
        log_end_test_code()

        # Check desired effects on handle:
        # check if one was modified:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val = self.inst.get_value_from_handle(testhandle, 'TEST4', rec)
        self.assertEqual(val, 'newvalue', 'The value did not change.')

        # check if others still there:
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        val2 = self.inst.get_value_from_handle(testhandle, 'TEST2', rec)
        self.assertEqual(val1, 'val1',
                         'The value of "TEST1" should still be "val1".')
        self.assertEqual(val2, 'val2',
                         'The value of "TEST2" should still be "val2".')

    def test_modify_handle_value_several(self):
        """Test modifying several existing handle values."""
        log_new_case("test_modify_handle_value_several")

        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.modify_handle_value(testhandle,
                                      TEST4='new4',
                                      TEST2='new2',
                                      TEST3='new3')
        log_end_test_code()

        # Check desired effects on handle:
        # check if three values were modified:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val2 = self.inst.get_value_from_handle(testhandle, 'TEST2', rec)
        val3 = self.inst.get_value_from_handle(testhandle, 'TEST3', rec)
        val4 = self.inst.get_value_from_handle(testhandle, 'TEST4', rec)
        self.assertEqual(val2, 'new2',
                         'The value of "TEST2" was not changed to "new2".')
        self.assertEqual(val3, 'new3',
                         'The value of "TEST3" was not changed to "new3".')
        self.assertEqual(val4, 'new4',
                         'The value of "TEST4" was not changed to "new4".')

        # check if one value remained unchanged:
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        self.assertEqual(val1, 'val1',
                         'The value of "TEST1" should still be "val1".')

    def test_modify_handle_value_several_inexistent(self):
        """Test modifying several existing handle values, one of them inexistent."""
        log_new_case("test_modify_handle_value_several_inexistent")

        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.modify_handle_value(testhandle,
                                      TEST4='new4',
                                      TEST2='new2',
                                      TEST100='new100')
        log_end_test_code()

        # Check desired effects on handle:
        # check if three values were modified:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val2 = self.inst.get_value_from_handle(testhandle, 'TEST2', rec)
        val100 = self.inst.get_value_from_handle(testhandle, 'TEST100', rec)
        val4 = self.inst.get_value_from_handle(testhandle, 'TEST4', rec)
        self.assertEqual(
            val100, 'new100',
            'The value of "TEST100" was not created and set to "new100".')
        self.assertEqual(val2, 'new2',
                         'The value of "TEST2" was not changed to "new2".')
        self.assertEqual(val4, 'new4',
                         'The value of "TEST4" was not changed to "new4".')

        # check if one value remained unchanged:
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        self.assertEqual(val1, 'val1',
                         'The value of "TEST1" should still be "val1".')

    def test_modify_handle_value_without_authentication(self):
        """Test if exception when not authenticated."""
        log_new_case("test_modify_handle_value_without_authentication")

        # Test variables
        testhandle = self.handle
        inst_readonly = EUDATHandleClient(self.url,
                                          HTTPS_verify=self.https_verify)

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(HandleAuthenticationError):
            inst_readonly.modify_handle_value(testhandle, foo='bar')
        log_end_test_code()

    def test_modify_handle_value_HS_ADMIN(self):
        """Test exception when trying to modify HS_ADMIN."""
        log_new_case("test_modify_handle_value_HS_ADMIN")

        # Test variables
        testhandle = self.handle

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(IllegalOperationException):
            self.inst.modify_handle_value(testhandle,
                                          HS_ADMIN='please let me in!')
        log_end_test_code()

    # register_handle:

    def test_register_handle(self):
        """Test registering a new handle with various types of values."""
        log_new_case("test_register_handle")

        # Test variables
        testhandle = self.newhandle
        additional_URLs = ['http://bar.bar', 'http://foo.foo']

        # Run code to be tested:
        log_start_test_code()
        handle_returned = self.inst.register_handle(
            testhandle,
            location='http://foo.bar',
            checksum='123456',
            additional_URLs=additional_URLs,
            FOO='foo',
            BAR='bar')
        log_end_test_code()

        # Check desired effects on handle:
        # Check if content was written ok:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val1 = self.inst.get_value_from_handle(testhandle, 'BAR', rec)
        val2 = self.inst.get_value_from_handle(testhandle, 'FOO', rec)
        val3 = self.inst.get_value_from_handle(testhandle, 'URL', rec)
        val4 = self.inst.get_value_from_handle(testhandle, 'CHECKSUM', rec)
        contained1 = self.inst.is_URL_contained_in_10320LOC(
            testhandle, 'http://bar.bar', rec)
        contained2 = self.inst.is_URL_contained_in_10320LOC(
            testhandle, 'http://foo.foo', rec)

        self.assertEqual(
            handle_returned, testhandle,
            'The handle returned by the create-method was not the one passed to it.'
        )
        self.assertEqual(val1, 'bar', 'The value "bar" was not inserted.')
        self.assertEqual(val2, 'foo', 'The value "foo" was not inserted.')
        self.assertEqual(val3, 'http://foo.bar',
                         'The value "http://foo.bar" was not inserted.')
        self.assertEqual(val4, '123456',
                         'The value "123456" was not inserted.')
        self.assertTrue(contained1,
                        'A specified additional URL was not inserted.')
        self.assertTrue(contained2,
                        'A specified additional URL was not inserted.')

        # Delete again (and check if was deleted):
        handle = self.newhandle
        url = self.connector.make_handle_URL(self.newhandle)
        head = self.headers
        veri = self.https_verify
        resp = requests.delete(url, headers=head, verify=veri)
        log_request_response_to_file('DELETE', handle, url, head, veri, resp)
        rec = self.inst.retrieve_handle_record_json(self.newhandle)

        self.assertEqual(
            resp.status_code, 200,
            'Deleting did not return a HTTP 200 code, but: %s, %s' %
            (resp, resp.content))
        self.assertIsNone(rec, 'The deleted record should return None.')

    def test_register_handle_already_exists(self):
        """Test if overwrite=False prevents handle overwriting."""
        log_new_case("test_register_handle_already_exists")

        # Test variables
        testhandle = self.handle

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(HandleAlreadyExistsException):
            self.inst.register_handle(testhandle,
                                      'http://foo.foo',
                                      TEST1='I am just an illusion.')
        log_end_test_code()

        # Check if nothing was changed:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        self.assertEqual(
            val1, 'val1',
            'The handle should not be overwritten, thus this value should have stayed the same.'
        )

    def test_generate_and_register_handle(self):
        """Test generating and registering a new handle with various types of values."""
        log_new_case("test_generate_and_register_handle")

        # Test variables
        additional_URLs = ['http://bar.bar', 'http://foo.foo']
        prefix = self.prefix

        # Run code to be tested:
        log_start_test_code()
        handle_returned = self.inst.generate_and_register_handle(
            prefix=prefix,
            location='http://foo.bar',
            checksum='123456',
            additional_URLs=additional_URLs,
            FOO='foo',
            BAR='bar')
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(handle_returned)
        val1 = self.inst.get_value_from_handle(handle_returned, 'BAR', rec)
        val2 = self.inst.get_value_from_handle(handle_returned, 'FOO', rec)
        val3 = self.inst.get_value_from_handle(handle_returned, 'URL', rec)
        val4 = self.inst.get_value_from_handle(handle_returned, 'CHECKSUM',
                                               rec)
        contained1 = self.inst.is_URL_contained_in_10320LOC(
            handle_returned, 'http://bar.bar', rec)
        contained2 = self.inst.is_URL_contained_in_10320LOC(
            handle_returned, 'http://foo.foo', rec)

        self.assertEqual(val1, 'bar', 'The value "bar" was not inserted.')
        self.assertEqual(val2, 'foo', 'The value "foo" was not inserted.')
        self.assertEqual(val3, 'http://foo.bar',
                         'The value "http://foo.bar" was not inserted.')
        self.assertEqual(val4, '123456',
                         'The value "123456" was not inserted.')
        self.assertTrue(contained1,
                        'A specified additional URL was not inserted.')
        self.assertTrue(contained2,
                        'A specified additional URL was not inserted.')
        self.assertIn(
            prefix, handle_returned,
            'The returned handle does not contain the given prefix.')

        # Delete again (and check if was deleted):
        url = self.connector.make_handle_URL(handle_returned)
        head = self.headers
        veri = self.https_verify
        resp = requests.delete(url, headers=head, verify=veri)
        log_request_response_to_file('DELETE', handle_returned, url, head,
                                     veri, resp)
        rec = self.inst.retrieve_handle_record_json(handle_returned)

        self.assertEqual(resp.status_code, 200,
                         'Deleting did not return a HTTP 200 code.')
        self.assertIsNone(rec, 'The deleted record should return None.')

    # delete_handle_value:

    def test_delete_handle_value_one_entry(self):
        """Test deleting one entry from a record."""
        log_new_case("test_delete_handle_value_one_entry")

        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.delete_handle_value(testhandle, 'TEST1')
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        indices = self.inst.get_handlerecord_indices_for_key(
            'TEST1', rec['values'])
        self.assertIsNone(val,
                          'The value for the deleted entry should be None.')
        self.assertEqual(len(indices), 0,
                         'There should be no index for the deleted entry.')

    def test_delete_handle_value_several_occurrences(self):
        """Test trying to delete from a corrupted handle record."""
        log_new_case("test_delete_handle_value_several_occurrences")

        # Test variables
        testhandle = self.handle

        # Call the method to be tested:
        log_start_test_code()
        self.inst.delete_handle_value(testhandle, 'TEST2')
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST2', rec)
        indices1 = self.inst.get_handlerecord_indices_for_key(
            'TEST2', rec['values'])

        self.assertIsNone(val1,
                          'The value for the deleted entry should be None.')
        self.assertEqual(len(indices1), 0,
                         'There should be no index for the deleted entry.')

    def test_delete_handle_value_several_entries(self):
        """Test deleting several entries from a record."""
        log_new_case("test_delete_handle_value_several_entries")

        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.delete_handle_value(testhandle, ['TEST1', 'TEST2'])
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        val2 = self.inst.get_value_from_handle(testhandle, 'TEST2', rec)
        indices1 = self.inst.get_handlerecord_indices_for_key(
            'TEST1', rec['values'])
        indices2 = self.inst.get_handlerecord_indices_for_key(
            'TEST2', rec['values'])

        self.assertIsNone(val1,
                          'The value for the deleted entry should be None.')
        self.assertIsNone(val2,
                          'The value for the deleted entry should be None.')
        self.assertEqual(len(indices1), 0,
                         'There should be no index for the deleted entry.')
        self.assertEqual(len(indices2), 0,
                         'There should be no index for the deleted entry.')

    def test_delete_handle_value_inexistent_entry(self):
        """Test deleting one entry from a record."""
        log_new_case("test_delete_handle_value_inexistent_entry")

        # Test variables
        testhandle = self.handle
        key = 'TEST100'

        # Run code to be tested:
        log_start_test_code()
        self.inst.delete_handle_value(testhandle, key)
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val = self.inst.get_value_from_handle(testhandle, key, rec)
        indices = self.inst.get_handlerecord_indices_for_key(
            key, rec['values'])
        self.assertIsNone(val,
                          'The index for the deleted entry should be None.')
        self.assertEqual(len(indices), 0,
                         'There should be no index for the deleted entry.')

    def test_delete_handle_value_several_entries_one_nonexistent(self):
        """Test deleting several entries from a record, one of them does not exist."""
        log_new_case(
            "test_delete_handle_value_several_entries_one_nonexistent")

        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.delete_handle_value(testhandle, ['TEST1', 'TEST100'])
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        indices = self.inst.get_handlerecord_indices_for_key(
            'TEST1', rec['values'])
        self.assertIsNone(val,
                          'The index for the deleted entry should be None.')
        self.assertEqual(len(indices), 0,
                         'There should be no index for the deleted entry.')

    # delete handle:

    def test_delete_handle_normal(self):
        """Test deleting an entire record."""
        log_new_case("test_delete_handle_normal")

        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        resp = self.inst.delete_handle(testhandle)
        log_end_test_code()

        # Check desired effects on handle:
        # Check if handle really deleted:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        self.assertIsNone(
            rec, 'Record should be None after deletion, but is: ' + str(resp))

    def test_delete_handle_too_many_args(self):
        """Test deleting an entire record, but we pass more arguments to the method."""
        log_new_case("test_delete_handle_too_many_args")

        # Test variables
        testhandle = self.handle

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(TypeError):
            self.inst.delete_handle(testhandle, 'TEST1')
        log_end_test_code()

    def test_delete_handle_inexistent(self):
        """Test deleting an inexistent handle."""
        log_new_case("test_delete_handle_inexistent")

        # Test variables
        testhandle = self.inexistent_handle

        # Run code to be tested:
        log_start_test_code()
        resp = self.inst.delete_handle(self.inexistent_handle)
        log_end_test_code()

        # Check desired effects on handle:
        self.assertIsNone(
            resp,
            'Response (when deleting inexistent handle) should be None, but is: '
            + str(resp))
class EUDATHandleClientWriteaccessTestCase(unittest.TestCase):

    def __init__(self, *args, **kwargs):

        REQUESTLOGGER.info("\nINIT of EUDATHandleClientWriteaccessTestCase")

        unittest.TestCase.__init__(self, *args, **kwargs)

        # Read resources from file:
        self.testvalues = json.load(open(RESOURCES_FILE))

        # Test values that need to be given by user:
        self.handle = self.testvalues['handle_to_be_modified']
        self.newhandle = self.testvalues['handle_to_be_created']
        
        self.url = self.testvalues['handle_server_url_write']
        self.user = self.testvalues['user']
        self.password = self.testvalues['password']

        # Optional:
        self.https_verify = True
        if 'HTTPS_verify' in self.testvalues:
            self.https_verify = self.testvalues['HTTPS_verify']

        # Others
        self.prefix = self.handle.split('/')[0]
        self.inexistent_handle = self.prefix+'/07e1fbf3-2b72-430a-a035-8584d4eada41'
        self.randompassword = '******'
        self.headers = None
        self.connector = HandleSystemConnector(handle_server_url=self.url)

    def setUp(self):

        REQUESTLOGGER.info("\n"+60*"*"+"\nsetUp of EUDATHandleClientWriteaccessTestCase")

        self.inst = EUDATHandleClient.instantiate_with_username_and_password(
            self.url,
            self.user,
            self.password,
            HTTPS_verify=self.https_verify)

        authstring = b2handle.utilhandle.create_authentication_string(self.user, self.password)
        self.headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Basic '+authstring
        }

        list_of_all_entries = [
            {
                "index":111,
                "type": "TEST1",
                "data":{
                    "format":"string",
                    "value":"val1"
                }
            },
            {
                "index":2222,
                "type": "TEST2",
                "data":{
                    "format":"string",
                    "value":"val2"
                }
            },
            {
                "index":333,
                "type": "TEST3",
                "data":{
                    "format":"string",
                    "value":"val3"
                }
            },
            {
                "index":4,
                "type": "TEST4",
                "data":{
                    "format":"string",
                    "value":"val4"
                }
            },
        ]

        testhandle = self.handle
        url = self.connector.make_handle_URL(testhandle)
        veri = self.https_verify
        head = self.headers
        data = json.dumps({'values':list_of_all_entries})
        resp = requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', self.handle, url, head, veri, resp)

    def tearDown(self):
        pass

    # modify_handle_value:

    def test_modify_handle_value_corrupted(self):
        """Test exception when trying to modify corrupted handle record."""
        log_new_test_case("test_modify_handle_value_corrupted")

        # Test variables
        testhandle = self.handle
        head = self.headers
        url = self.connector.make_handle_URL(testhandle)
        # Create corrupted record:
        list_of_all_entries = [
            {
                "index":111,
                "type": "TEST1",
                "data":{
                    "format":"string",
                    "value":"val1"
                }
            },
            {
                "index":2222,
                "type": "TEST2",
                "data":{
                    "format":"string",
                    "value":"val2"
                }
            },
            {
                "index":333,
                "type": "TEST2",
                "data":{
                    "format":"string",
                    "value":"val3"
                }
            },
            {
                "index":4,
                "type": "TEST4",
                "data":{
                    "format":"string",
                    "value":"val4"
                }
            }
        ]
        data = json.dumps({'values':list_of_all_entries})
        veri = self.https_verify
        resp = requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', testhandle, url, head, veri, resp, data)

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(BrokenHandleRecordException):
            self.inst.modify_handle_value(testhandle,
                                          TEST4='new4',
                                          TEST2='new2',
                                          TEST3='new3')
        log_end_test_code()

    def test_modify_handle_value_one(self):
        """Test modifying one existing handle value."""
        log_new_test_case("test_modify_handle_value_one")

        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.modify_handle_value(testhandle, TEST4='newvalue')
        log_end_test_code()

        # Check desired effects on handle:
        # check if one was modified:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val = self.inst.get_value_from_handle(testhandle, 'TEST4', rec)
        self.assertEqual(val, 'newvalue',
            'The value did not change.')

        # check if others still there:
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        val2 = self.inst.get_value_from_handle(testhandle, 'TEST2', rec)
        self.assertEqual(val1, 'val1',
            'The value of "TEST1" should still be "val1".')
        self.assertEqual(val2, 'val2',
            'The value of "TEST2" should still be "val2".')

    def test_modify_handle_value_several(self):
        """Test modifying several existing handle values."""
        log_new_test_case("test_modify_handle_value_several")

        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.modify_handle_value(testhandle,
                                      TEST4='new4',
                                      TEST2='new2',
                                      TEST3='new3')
        log_end_test_code()

        # Check desired effects on handle:
        # check if three values were modified:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val2 = self.inst.get_value_from_handle(testhandle, 'TEST2', rec)
        val3 = self.inst.get_value_from_handle(testhandle, 'TEST3', rec)
        val4 = self.inst.get_value_from_handle(testhandle, 'TEST4', rec)
        self.assertEqual(val2, 'new2',
            'The value of "TEST2" was not changed to "new2".')
        self.assertEqual(val3, 'new3',
            'The value of "TEST3" was not changed to "new3".')
        self.assertEqual(val4, 'new4',
            'The value of "TEST4" was not changed to "new4".')

        # check if one value remained unchanged:
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        self.assertEqual(val1, 'val1',
            'The value of "TEST1" should still be "val1".')

    def test_modify_handle_value_several_inexistent(self):
        """Test modifying several existing handle values, one of them inexistent."""
        log_new_test_case("test_modify_handle_value_several_inexistent")
        
        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.modify_handle_value(testhandle,
                                      TEST4='new4',
                                      TEST2='new2',
                                      TEST100='new100')
        log_end_test_code()

        # Check desired effects on handle:
        # check if three values were modified:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val2   = self.inst.get_value_from_handle(testhandle, 'TEST2', rec)
        val100 = self.inst.get_value_from_handle(testhandle, 'TEST100', rec)
        val4   = self.inst.get_value_from_handle(testhandle, 'TEST4', rec)
        self.assertEqual(val100, 'new100',
            'The value of "TEST100" was not created and set to "new100".')
        self.assertEqual(val2, 'new2',
            'The value of "TEST2" was not changed to "new2".')
        self.assertEqual(val4, 'new4',
            'The value of "TEST4" was not changed to "new4".')

        # check if one value remained unchanged:
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        self.assertEqual(val1, 'val1',
            'The value of "TEST1" should still be "val1".')

    def test_modify_handle_value_without_authentication(self):
        """Test if exception when not authenticated."""
        log_new_test_case("test_modify_handle_value_without_authentication")

        # Test variables
        testhandle = self.handle
        inst_readonly = EUDATHandleClient(self.url, HTTPS_verify=self.https_verify)

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(HandleAuthenticationError):
            inst_readonly.modify_handle_value(testhandle, foo='bar')
        log_end_test_code()

    def test_modify_handle_value_HS_ADMIN(self):
        """Test exception when trying to modify HS_ADMIN."""
        log_new_test_case("test_modify_handle_value_HS_ADMIN")

        # Test variables
        testhandle = self.handle

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(IllegalOperationException):
            self.inst.modify_handle_value(testhandle, HS_ADMIN='please let me in!')
        log_end_test_code()

    # register_handle:

    def test_register_handle(self):
        """Test registering a new handle with various types of values."""
        log_new_test_case("test_register_handle")

        # Test variables
        testhandle = self.newhandle
        additional_URLs = ['http://bar.bar', 'http://foo.foo']

        # Run code to be tested:
        log_start_test_code()
        handle_returned = self.inst.register_handle(testhandle,
                                                    location='http://foo.bar',
                                                    checksum='123456',
                                                    additional_URLs=additional_URLs,
                                                    FOO='foo',
                                                    BAR='bar')
        log_end_test_code()

        # Check desired effects on handle:
        # Check if content was written ok:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val1 = self.inst.get_value_from_handle(testhandle, 'BAR', rec)
        val2 = self.inst.get_value_from_handle(testhandle, 'FOO', rec)
        val3 = self.inst.get_value_from_handle(testhandle, 'URL', rec)
        val4 = self.inst.get_value_from_handle(testhandle, 'CHECKSUM', rec)
        contained1 = self.inst.is_URL_contained_in_10320LOC(testhandle, 'http://bar.bar', rec)
        contained2 = self.inst.is_URL_contained_in_10320LOC(testhandle, 'http://foo.foo', rec)

        self.assertEqual(handle_returned, testhandle,
            'The handle returned by the create-method was not the one passed to it.')
        self.assertEqual(val1, 'bar',
            'The value "bar" was not inserted.')
        self.assertEqual(val2, 'foo',
            'The value "foo" was not inserted.')
        self.assertEqual(val3, 'http://foo.bar',
            'The value "http://foo.bar" was not inserted.')
        self.assertEqual(val4, '123456',
            'The value "123456" was not inserted.')
        self.assertTrue(contained1,
            'A specified additional URL was not inserted.')
        self.assertTrue(contained2,
            'A specified additional URL was not inserted.')

        # Delete again (and check if was deleted):
        handle = self.newhandle
        url = self.connector.make_handle_URL(self.newhandle)
        head = self.headers
        veri = self.https_verify
        resp = requests.delete(url, headers=head, verify=veri)
        log_request_response_to_file('DELETE', handle, url, head, veri, resp)
        rec = self.inst.retrieve_handle_record_json(self.newhandle)

        self.assertEqual(resp.status_code, 200,
            'Deleting did not return a HTTP 200 code.')
        self.assertIsNone(rec,
            'The deleted record should return None.')

    def test_register_handle_already_exists(self):
        """Test if overwrite=False prevents handle overwriting."""
        log_new_test_case("test_register_handle_already_exists")

        # Test variables
        testhandle = self.handle

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(HandleAlreadyExistsException):
            self.inst.register_handle(testhandle,
                                      'http://foo.foo',
                                      TEST1='I am just an illusion.')
        log_end_test_code()

        # Check if nothing was changed:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        self.assertEqual(val1, 'val1',
            'The handle should not be overwritten, thus this value should have stayed the same.')

    def test_generate_and_register_handle(self):
        """Test generating and registering a new handle with various types of values."""
        log_new_test_case("test_generate_and_register_handle")

        # Test variables
        additional_URLs = ['http://bar.bar', 'http://foo.foo']
        prefix = self.prefix

        # Run code to be tested:
        log_start_test_code()
        handle_returned = self.inst.generate_and_register_handle(prefix=prefix,
                                                    location='http://foo.bar',
                                                    checksum='123456',
                                                    additional_URLs=additional_URLs,
                                                    FOO='foo',
                                                    BAR='bar')
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(handle_returned)
        val1 = self.inst.get_value_from_handle(handle_returned, 'BAR', rec)
        val2 = self.inst.get_value_from_handle(handle_returned, 'FOO', rec)
        val3 = self.inst.get_value_from_handle(handle_returned, 'URL', rec)
        val4 = self.inst.get_value_from_handle(handle_returned, 'CHECKSUM', rec)
        contained1 = self.inst.is_URL_contained_in_10320LOC(handle_returned, 'http://bar.bar', rec)
        contained2 = self.inst.is_URL_contained_in_10320LOC(handle_returned, 'http://foo.foo', rec)

        self.assertEqual(val1, 'bar',
            'The value "bar" was not inserted.')
        self.assertEqual(val2, 'foo',
            'The value "foo" was not inserted.')
        self.assertEqual(val3, 'http://foo.bar',
            'The value "http://foo.bar" was not inserted.')
        self.assertEqual(val4, '123456',
            'The value "123456" was not inserted.')
        self.assertTrue(contained1,
            'A specified additional URL was not inserted.')
        self.assertTrue(contained2,
            'A specified additional URL was not inserted.')
        self.assertIn(prefix, handle_returned,
            'The returned handle does not contain the given prefix.')

        # Delete again (and check if was deleted):
        url = self.connector.make_handle_URL(handle_returned)
        head = self.headers
        veri = self.https_verify
        resp = requests.delete(url, headers=head, verify=veri)
        log_request_response_to_file('DELETE', handle_returned, url, head, veri, resp)
        rec = self.inst.retrieve_handle_record_json(handle_returned)

        self.assertEqual(resp.status_code, 200,
            'Deleting did not return a HTTP 200 code.')
        self.assertIsNone(rec,
            'The deleted record should return None.')

    # delete_handle_value:

    def test_delete_handle_value_one_entry(self):
        """Test deleting one entry from a record."""
        log_new_test_case("test_delete_handle_value_one_entry")

        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.delete_handle_value(testhandle, 'TEST1')
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        indices = self.inst.get_handlerecord_indices_for_key('TEST1', rec['values'])
        self.assertIsNone(val,
            'The value for the deleted entry should be None.')
        self.assertEqual(len(indices), 0,
            'There should be no index for the deleted entry.')

    def test_delete_handle_value_several_occurrences(self):
        """Test trying to delete from a corrupted handle record."""
        log_new_test_case("test_delete_handle_value_several_occurrences")
        
        # Test variables
        testhandle = self.handle

        # Call the method to be tested:
        log_start_test_code()
        self.inst.delete_handle_value(testhandle, 'TEST2')
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST2', rec)
        indices1 = self.inst.get_handlerecord_indices_for_key('TEST2', rec['values'])

        self.assertIsNone(val1,
            'The value for the deleted entry should be None.')
        self.assertEqual(len(indices1), 0,
            'There should be no index for the deleted entry.')

    def test_delete_handle_value_several_entries(self):
        """Test deleting several entries from a record."""
        log_new_test_case("test_delete_handle_value_several_entries")

        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.delete_handle_value(testhandle, ['TEST1', 'TEST2'])
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val1 = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        val2 = self.inst.get_value_from_handle(testhandle, 'TEST2', rec)
        indices1 = self.inst.get_handlerecord_indices_for_key('TEST1', rec['values'])
        indices2 = self.inst.get_handlerecord_indices_for_key('TEST2', rec['values'])

        self.assertIsNone(val1,
            'The value for the deleted entry should be None.')
        self.assertIsNone(val2,
            'The value for the deleted entry should be None.')
        self.assertEqual(len(indices1), 0,
            'There should be no index for the deleted entry.')
        self.assertEqual(len(indices2), 0,
            'There should be no index for the deleted entry.')

    def test_delete_handle_value_inexistent_entry(self):
        """Test deleting one entry from a record."""
        log_new_test_case("test_delete_handle_value_inexistent_entry")

        # Test variables
        testhandle = self.handle
        key = 'TEST100'

        # Run code to be tested:
        log_start_test_code()
        self.inst.delete_handle_value(testhandle, key)
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val = self.inst.get_value_from_handle(testhandle, key, rec)
        indices = self.inst.get_handlerecord_indices_for_key(key, rec['values'])
        self.assertIsNone(val,
            'The index for the deleted entry should be None.')
        self.assertEqual(len(indices), 0,
            'There should be no index for the deleted entry.')

    def test_delete_handle_value_several_entries_one_nonexistent(self):
        """Test deleting several entries from a record, one of them does not exist."""
        log_new_test_case("test_delete_handle_value_several_entries_one_nonexistent")


        # Test variables
        testhandle = self.handle

        # Run code to be tested:
        log_start_test_code()
        self.inst.delete_handle_value(testhandle, ['TEST1', 'TEST100'])
        log_end_test_code()

        # Check desired effects on handle:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val = self.inst.get_value_from_handle(testhandle, 'TEST1', rec)
        indices = self.inst.get_handlerecord_indices_for_key('TEST1', rec['values'])
        self.assertIsNone(val,
            'The index for the deleted entry should be None.')
        self.assertEqual(len(indices), 0,
            'There should be no index for the deleted entry.')

    # delete handle:

    def test_delete_handle_normal(self):
        """Test deleting an entire record."""
        log_new_test_case("test_delete_handle_normal")

        # Test variables
        testhandle = self.handle
        
        # Run code to be tested:
        log_start_test_code()
        resp = self.inst.delete_handle(testhandle)
        log_end_test_code()

        # Check desired effects on handle:
        # Check if handle really deleted:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        self.assertIsNone(rec,
            'Record should be None after deletion, but is: '+str(resp))

    def test_delete_handle_too_many_args(self):
        """Test deleting an entire record, but we pass more arguments to the method."""
        log_new_test_case("test_delete_handle_too_many_args")

        # Test variables
        testhandle = self.handle

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(TypeError):
            self.inst.delete_handle(testhandle, 'TEST1')
        log_end_test_code()

    def test_delete_handle_inexistent(self):
        """Test deleting an inexistent handle."""
        log_new_test_case("test_delete_handle_inexistent")

        # Test variables
        testhandle = self.inexistent_handle
        
        # Run code to be tested:
        log_start_test_code()
        resp = self.inst.delete_handle(self.inexistent_handle)
        log_end_test_code()

        # Check desired effects on handle:
        self.assertIsNone(resp,
            'Response (when deleting inexistent handle) should be None, but is: '+str(resp))
Exemplo n.º 5
0
    def test_init_cert_twofiles(self):

        inst = HandleSystemConnector(certificate_only=FILE_CERT,
                                     private_key=FILE_KEY,
                                     handle_server_url='http://foo.com')
        self.assertIsInstance(inst, HandleSystemConnector)
Exemplo n.º 6
0
    def test_init_cert_serverurl_missing(self):

        with self.assertRaises(TypeError):
            inst = HandleSystemConnector(FILE_BOTH)
Exemplo n.º 7
0
 def setUp(self):
     self.inst = HandleSystemConnector()
Exemplo n.º 8
0
    def test_init_cert_onefile(self):

        inst = HandleSystemConnector(certificate_and_key=FILE_BOTH,
                                     handle_server_url='http://foo.com')
        self.assertIsInstance(inst, HandleSystemConnector)
Exemplo n.º 9
0
    def test_init_cert_onefile_wrongpath(self):

        with self.assertRaises(CredentialsFormatError):
            inst = HandleSystemConnector(certificate_and_key=PATH_CRED +
                                         '/noexist.pem',
                                         handle_server_url='http://foo.com')
Exemplo n.º 10
0
class EUDATHandleConnectorNoaccessTestCase(unittest.TestCase):
    def setUp(self):
        self.inst = HandleSystemConnector()

    def tearDown(self):
        pass

    # make_handle_url

    def test_make_handle_url(self):

        url = self.inst.make_handle_URL('testhandle')
        self.assertIn('/api/handles/', url,
                      'No REST API path specified in URL: ' + url)
        self.assertIn('handle.net', url, 'handle.net missing in URL: ' + url)
        self.assertNotIn('index=', url, 'Index specified in URL: ' + url)
        #self.assertIn('overwrite=false', url,
        #    'overwrite=false is missing: '+url)

    def test_make_handle_url_with_indices(self):

        url = self.inst.make_handle_URL('testhandle', [2, 3, 5])
        self.assertIn('/api/handles/', url,
                      'No REST API path specified in URL: ' + url)
        self.assertIn('index=2', url, 'Index 2 specified in URL: ' + url)
        self.assertIn('index=3', url, 'Index 3 specified in URL: ' + url)
        self.assertIn('index=5', url, 'Index 5 specified in URL: ' + url)
        #self.assertIn('overwrite=false', url,
        #    'overwrite=false is missing: '+url)

    def test_make_handle_url_overwrite_true(self):

        url = self.inst.make_handle_URL('testhandle', overwrite=True)
        self.assertIn('/api/handles/', url,
                      'No REST API path specified in URL: ' + url)
        self.assertIn('overwrite=true', url,
                      'overwrite=true is missing: ' + url)

    def test_make_handle_url_overwrite_false(self):

        url = self.inst.make_handle_URL('testhandle', overwrite=False)
        self.assertIn('/api/handles/', url,
                      'No REST API path specified in URL: ' + url)
        self.assertIn('overwrite=false', url,
                      'overwrite=false is missing: ' + url)

    def test_make_handle_url_otherurl(self):

        other = 'http://foo.foo'
        url = self.inst.make_handle_URL('testhandle', other_url=other)
        self.assertNotIn(
            '/api/handles/', url,
            'REST API path should not be specified in URL: ' + url)
        self.assertIn(other, url, 'Other URL missing in URL: ' + url)
        self.assertNotIn('handle.net', url,
                         'handle.net should not be in URL: ' + url)
        self.assertNotIn('index=', url, 'Index specified in URL: ' + url)
        #self.assertIn('overwrite=false', url,
        #    'overwrite=false is missing: '+url)

    # Initiating:

    def test_init_cert_onefile(self):

        inst = HandleSystemConnector(certificate_and_key=FILE_BOTH,
                                     handle_server_url='http://foo.com')
        self.assertIsInstance(inst, HandleSystemConnector)

    def test_init_cert_twofiles(self):

        inst = HandleSystemConnector(certificate_only=FILE_CERT,
                                     private_key=FILE_KEY,
                                     handle_server_url='http://foo.com')
        self.assertIsInstance(inst, HandleSystemConnector)

    def test_init_cert_serverurl_missing(self):

        with self.assertRaises(TypeError):
            inst = HandleSystemConnector(FILE_BOTH)

    def test_init_privatekey_missing(self):

        inst = HandleSystemConnector(certificate_only=FILE_BOTH,
                                     handle_server_url='http://foo.com')
        self.assertIsInstance(inst, HandleSystemConnector)

    def test_init_certificate_missing(self):

        inst = HandleSystemConnector(handle_server_url='http://foo.com',
                                     private_key=FILE_KEY)
        self.assertIsInstance(inst, HandleSystemConnector)

    def test_init_cert_onefile_wrongpath(self):

        with self.assertRaises(CredentialsFormatError):
            inst = HandleSystemConnector(certificate_and_key=PATH_CRED +
                                         '/noexist.pem',
                                         handle_server_url='http://foo.com')
Exemplo n.º 11
0
    def test_init_certificate_missing(self):

        inst = HandleSystemConnector(handle_server_url='http://foo.com',
                                     private_key=FILE_KEY)
        self.assertIsInstance(inst, HandleSystemConnector)
Exemplo n.º 12
0
    def test_init_privatekey_missing(self):

        inst = HandleSystemConnector(certificate_only=FILE_BOTH,
                                     handle_server_url='http://foo.com')
        self.assertIsInstance(inst, HandleSystemConnector)
Exemplo n.º 13
0
class EUDATHandleClientWriteaccess10320LOCTestCase(unittest.TestCase):

    def __init__(self, *args, **kwargs):

        REQUESTLOGGER.info("\nINIT of EUDATHandleClientWriteaccess10320LOCTestCase")

        unittest.TestCase.__init__(self, *args, **kwargs)

        # Read resources from file:
        self.testvalues = RESOURCES_FILE

        # Test values that need to be given by user:
        self.url = self.testvalues['handle_server_url_write']
        self.user = self.testvalues['user']
        self.password = self.testvalues['password']
        self.handle = self.testvalues['handle_for_read_tests']
        self.handle_withloc = self.testvalues['handle_to_be_modified_with_10320LOC']
        self.handle_withoutloc = self.testvalues['handle_to_be_modified_without_10320LOC']

        # Optional:
        self.https_verify = True
        if 'HTTPS_verify' in self.testvalues:
            self.https_verify = self.testvalues['HTTPS_verify']

        # Others
        self.randompassword = '******'
        prefix = self.handle.split('/')[0]
        self.inexistent_handle = prefix+'/07e1fbf3-2b72-430a-a035-8584d4eada41'
        self.headers = None
        self.connector = HandleSystemConnector(handle_server_url = self.url)

    def setUp(self):

        REQUESTLOGGER.info("\n"+60*"*"+"\nsetUp of EUDATHandleClientWriteaccess10320LOCTestCase")

        self.inst = EUDATHandleClient.instantiate_with_username_and_password(
            self.url,
            self.user,
            self.password,
            HTTPS_verify=self.https_verify)
        
        list_of_all_entries_with = [
            {
                "index":100,
                "type":"HS_ADMIN",
                "data":{
                    "format":"admin",
                    "value":{
                        "handle":"21.T14999/B2HANDLE_INTEGRATION_TESTS",
                        "index":300,
                        "permissions":"011111110011"
                    }
                }
            },
            {
                "index":1,
                "type":"URL",
                "data":"www.url.foo"
            },
            {
                "index":2,
                "type":"10320/LOC",
                "data":{
                    "format":"string",
                    "value":"<locations><location href = 'http://first.foo' /><location href = 'http://second.foo' /></locations> "
                }
            }
        ]

        list_of_all_entries_without = [
            {
                "index":100,
                "type":"HS_ADMIN",
                "data":{
                    "format":"admin",
                    "value":{
                        "handle":"21.T14999/B2HANDLE_INTEGRATION_TESTS",
                        "index":300,
                        "permissions":"011111110011"
                    }
                }
            },
            {
                "index":1,
                "type":"URL",
                "data":"www.url.foo"
            }
        ]

        authstring = b2handle.utilhandle.create_authentication_string(self.user, self.password)
        head = {
            'Content-Type': 'application/json',
            'Authorization': 'Basic '+authstring
        }
        veri = self.https_verify

        testhandle = self.handle_withloc
        url = self.connector.make_handle_URL(testhandle)
        data = json.dumps({'values':list_of_all_entries_with})
        resp = requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', testhandle, url, head, veri, resp)

        testhandle = self.handle_withoutloc
        url = self.connector.make_handle_URL(testhandle)
        data = json.dumps({'values':list_of_all_entries_without})
        requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', testhandle, url, head, veri, resp)

    def tearDown(self):

        veri = self.https_verify
        authstring = b2handle.utilhandle.create_authentication_string(self.user, self.password)
        head = {
            'Content-Type': 'application/json',
            'Authorization': 'Basic '+authstring
        }

        testhandle = self.handle_withloc
        url = self.connector.make_handle_URL(testhandle)
        resp = requests.delete(url, headers=head, verify=veri)
        log_request_response_to_file('DELETE', testhandle, url, head, veri, resp)

        testhandle = self.handle_withoutloc
        url = self.connector.make_handle_URL(testhandle)
        requests.delete(url, headers=head, verify=veri)
        log_request_response_to_file('DELETE', testhandle, url, head, veri, resp)

    # Exchanging:

    def test_exchange_additional_URL_normal(self):
        """Test replacing an URL."""
        log_new_case("test_exchange_additional_URL_normal")

        # Test variables
        testhandle = self.handle_withloc
        old = 'http://first.foo'
        new = 'http://newfirst.foo'

        # Precondition: URL must be included.
        handlerecord_json = self.inst.retrieve_handle_record_json(testhandle)
        contained = self.inst.is_URL_contained_in_10320LOC(testhandle, old, handlerecord_json)
        self.assertTrue(contained,
            'Precondition for test failed! The URL should be present at the start'
            ' of the test: '+str(handlerecord_json))

        # Run the code to be tested:
        log_start_test_code()
        self.inst.exchange_additional_URL(testhandle, old, new)
        log_end_test_code()

        # Check desired effects on handle:
        contained = self.inst.is_URL_contained_in_10320LOC(testhandle, new)
        self.assertTrue(contained,
            'After replacing an URL, the replacement was not there.')
        # TODO Check if index was the same!!

    def test_exchange_additional_URL_doesnotexist(self):
        """Test if replacing an inexistent URL has any effect."""
        log_new_case("test_exchange_additional_URL_doesnotexist")

        # Test variables
        testhandle = self.handle_withloc
        inexistent_old = 'http://sodohfasdkfjhanwikfhbawkedfhbawe.foo'
        new = 'http://newfirst.foo'

        # Precondition: URL must not be there yet.
        contained = self.inst.is_URL_contained_in_10320LOC(testhandle, inexistent_old)
        self.assertFalse(contained,
            'Precondition for test failed! The URL should not be present at the start of the test.')

        # Run the code to be tested:
        log_start_test_code()
        self.inst.exchange_additional_URL(testhandle, inexistent_old, new)
        log_end_test_code()

        # Check desired effects on handle:
        contained = self.inst.is_URL_contained_in_10320LOC(testhandle, new)
        self.assertFalse(contained,
            'After replacing a nonexistent URL, the replacement was there.')

    def test_exchange_additional_URL_no10320LOC(self):
        """Test if replacing an URL has any effect if there is no 10320/LOC."""
        log_new_case("test_exchange_additional_URL_no10320LOC")

        # Test variables
        testhandle = self.handle_withoutloc
        old = 'http://first.foo'
        new = 'http://newfirst.foo'

        # Run the code to be tested:
        log_start_test_code()
        self.inst.exchange_additional_URL(testhandle, old, new)
        log_end_test_code()

        # Check desired effects on handle:
        contained = self.inst.is_URL_contained_in_10320LOC(testhandle, new)
        self.assertFalse(contained,
            'After replacing an URL in nonexistent 10320/LOC, the replacement was there.')
    
    # Adding:

    def test_add_additional_URL_first(self):
        """Test adding the first additional URL'(created the 10320/LOC entry)."""
        log_new_case("test_add_additional_URL_first")

        # Test variables
        testhandle = self.handle_withoutloc
        url = 'http://first.foo'

        # Run code to be tested:
        log_start_test_code()
        self.inst.add_additional_URL(testhandle, url)
        log_end_test_code()

        # Check desired effects on handle:
        contained = self.inst.is_URL_contained_in_10320LOC(testhandle, url)
        self.assertTrue(contained,
            'The (first) URL was not added or the 10320/LOC was not created.')

    def test_add_additional_URL_several_toempty(self):
        """Test adding several (3) additional URLs."""
        log_new_case("test_add_additional_URL_several_toempty")

        # Test variables
        testhandle = self.handle_withoutloc
        url1 = 'http://one'
        url2 = 'http://two'
        url3 = 'http://three'

        # Run code to be tested:
        log_start_test_code()
        self.inst.add_additional_URL(testhandle, url1, url2, url3)
        log_end_test_code()

        # Check desired effects on handle:
        contained1 = self.inst.is_URL_contained_in_10320LOC(testhandle, url1)
        contained2 = self.inst.is_URL_contained_in_10320LOC(testhandle, url2)
        contained3 = self.inst.is_URL_contained_in_10320LOC(testhandle, url3)
        self.assertTrue(contained1,
            'The first added URL was not added.')
        self.assertTrue(contained2,
            'The second added URL was not added.')
        self.assertTrue(contained3,
            'The third added URL was not added.')

    def test_add_additional_URL_another(self):
        """Test adding an additional URL."""
        log_new_case("test_add_additional_URL_another")

        # Test variables:
        testhandle = self.handle_withloc
        url = 'http://third.foo'
        
        # Run code to be tested:
        log_start_test_code()
        self.inst.add_additional_URL(testhandle, url)
        log_end_test_code()

        # Check desired effects on handle:
        contained = self.inst.is_URL_contained_in_10320LOC(testhandle, url)
        self.assertTrue(contained,
            'The URL was not added.')

    def test_add_additional_URL_several(self):
        """Test adding several (3) additional URLs."""
        log_new_case("test_add_additional_URL_several")

        # Test variables
        testhandle = self.handle_withloc
        url1 = 'http://one'
        url2 = 'http://two'
        url3 = 'http://three'
        
        # Run code to be tested:
        log_start_test_code()
        self.inst.add_additional_URL(testhandle, url1, url2, url3)
        log_end_test_code()

        # Check desired effects on handle:
        contained1 = self.inst.is_URL_contained_in_10320LOC(testhandle, url1)
        contained2 = self.inst.is_URL_contained_in_10320LOC(testhandle, url2)
        contained3 = self.inst.is_URL_contained_in_10320LOC(testhandle, url3)
        self.assertTrue(contained1,
            'The first added URL was not added.')
        self.assertTrue(contained2,
            'The second added URL was not added.')
        self.assertTrue(contained3,
            'The third added URL was not added.')

    def test_add_additional_URL_to_inexistent_handle(self):
        """Test exception if handle does not exist."""
        log_new_case("test_add_additional_URL_to_inexistent_handle")

        # Test variables
        testhandle = self.inexistent_handle
        url = 'http://foo.foo'

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(HandleNotFoundException):
            self.inst.add_additional_URL(testhandle, url)
        log_end_test_code()

    def test_add_additional_URL_alreadythere(self):
        """Test adding an URL that is already there."""
        log_new_case("test_add_additional_URL_alreadythere")

        # Test variables
        testhandle = self.handle_withloc
        url = 'http://first.foo'

        # Precondition: URL is already contained:
        record_before = self.inst.retrieve_handle_record_json(testhandle)
        contained = self.inst.is_URL_contained_in_10320LOC(testhandle, url, record_before)
        self.assertTrue(contained,
            'Test precondition failed: URL not there: '+str(record_before))

        # Run code to be tested:
        log_start_test_code()
        self.inst.add_additional_URL(testhandle, url)
        log_end_test_code()

        # Check desired effects on handle:
        # i.e. check if something has changed
        record_after  = self.inst.retrieve_handle_record_json(testhandle)
        self.assertEqual(record_before, record_after,
                        'After adding the same URL again, '
                        'the record was not equal.\n'
                        'Before:\n'+str(record_before)+'\n'
                        'After:\n'+str(record_after))

    # Remove:

    def test_remove_additional_URL(self):
        """Test normal removal of additional URL from 10320/LOC."""
        log_new_case("test_remove_additional_URL")

        # Test variables
        testhandle = self.handle_withloc
        url = 'http://first.foo'

        # Run code to be tested:
        log_start_test_code()
        self.inst.remove_additional_URL(testhandle, url)
        log_end_test_code()

        # Check desired effects on handle:
        contained = self.inst.is_URL_contained_in_10320LOC(testhandle, url)
        self.assertFalse(contained,
            'After removal, the URL was still there.')

    def test_remove_additional_URL_toempty(self):
        """Test removing all URL, which should remove the whole 10320/LOC attribute."""
        log_new_case("test_remove_additional_URL_toempty")

        # Test variables
        testhandle = self.handle_withloc
        url1 =  'http://first.foo'
        url2 = 'http://second.foo'

        # Run code to be tested:
        log_start_test_code()
        self.inst.remove_additional_URL(testhandle, url1)
        self.inst.remove_additional_URL(testhandle, url2)
        log_end_test_code()

        # Check desired effects on handle:
        # Check if empty:
        isempty = self.inst.is_10320LOC_empty(testhandle)
        self.assertTrue(isempty,
            'After removing all URLs from 10320/LOC, it is not empty.')
        # Check if still exists:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val = self.inst.get_value_from_handle(testhandle, '10320/LOC', rec)
        indices = self.inst.get_handlerecord_indices_for_key('10320/LOC', rec['values'])
        self.assertIsNone(val,
            'After removing all URLs from 10320/LOC, the value is not None.')
        self.assertEqual(len(indices), 0,
            'After removing all URLs from 10320/LOC, the entry still exists.')


    def test_remove_additional_URL_several(self):
        """Test removing all URL at the same time, which should remove the whole 10320/LOC attribute."""
        log_new_case("test_remove_additional_URL_several")

        # Test variables
        testhandle = self.handle_withloc
        url1 =  'http://first.foo'
        url2 = 'http://second.foo'

        # Run code to be tested:
        log_start_test_code()
        self.inst.remove_additional_URL(testhandle, url1, url2)
        log_end_test_code()

        # Check desired effects on handle:
        # Check if empty:
        isempty = self.inst.is_10320LOC_empty(testhandle)
        self.assertTrue(isempty,
            'After removing all URLs from 10320/LOC, it is not empty.')
        # Check if still exists:
        rec = self.inst.retrieve_handle_record_json(testhandle)
        val = self.inst.get_value_from_handle(testhandle, '10320/LOC', rec)
        indices = self.inst.get_handlerecord_indices_for_key('10320/LOC', rec['values'])
        self.assertIsNone(val,
            'After removing all URLs from 10320/LOC, the value is not None.')
        self.assertEqual(len(indices), 0,
            'After removing all URLs from 10320/LOC, the entry still exists.')

    def test_remove_additional_URL_inexistent_handle(self):
        """Test normal removal of additional URL from an inexistent handle."""
        log_new_case("test_remove_additional_URL_inexistent_handle")

        # Test variables
        testhandle = self.inexistent_handle
        url = 'http://first.foo'

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(HandleNotFoundException):
            self.inst.remove_additional_URL(testhandle, url)
        log_end_test_code()
 def setUp(self):
     self.inst = HandleSystemConnector()
class EUDATHandleConnectorNoaccessTestCase(unittest.TestCase):


    def setUp(self):
        self.inst = HandleSystemConnector()

    def tearDown(self):
        pass


    # make_handle_url

    def test_make_handle_url(self):

        url = self.inst.make_handle_URL('testhandle')
        self.assertIn('/api/handles/', url,
            'No REST API path specified in URL: '+url)
        self.assertIn('handle.net', url,
            'handle.net missing in URL: '+url)
        self.assertNotIn('index=', url,
            'Index specified in URL: '+url)
        #self.assertIn('overwrite=false', url,
        #    'overwrite=false is missing: '+url)

    def test_make_handle_url_with_indices(self):

        url = self.inst.make_handle_URL('testhandle', [2,3,5])
        self.assertIn('/api/handles/', url,
            'No REST API path specified in URL: '+url)
        self.assertIn('index=2', url,
            'Index 2 specified in URL: '+url)
        self.assertIn('index=3', url,
            'Index 3 specified in URL: '+url)
        self.assertIn('index=5', url,
            'Index 5 specified in URL: '+url)
        #self.assertIn('overwrite=false', url,
        #    'overwrite=false is missing: '+url)

    def test_make_handle_url_overwrite_true(self):

        url = self.inst.make_handle_URL('testhandle', overwrite=True)
        self.assertIn('/api/handles/', url,
            'No REST API path specified in URL: '+url)
        self.assertIn('overwrite=true', url,
            'overwrite=true is missing: '+url)

    def test_make_handle_url_overwrite_false(self):

        url = self.inst.make_handle_URL('testhandle', overwrite=False)
        self.assertIn('/api/handles/', url,
            'No REST API path specified in URL: '+url)
        self.assertIn('overwrite=false', url,
            'overwrite=false is missing: '+url)

    def test_make_handle_url_otherurl(self):

        other = 'http://foo.foo'
        url = self.inst.make_handle_URL('testhandle', other_url=other)
        self.assertNotIn('/api/handles/', url,
            'REST API path should not be specified in URL: '+url)
        self.assertIn(other, url,
            'Other URL missing in URL: '+url)
        self.assertNotIn('handle.net', url,
            'handle.net should not be in URL: '+url)
        self.assertNotIn('index=', url,
            'Index specified in URL: '+url)
        #self.assertIn('overwrite=false', url,
        #    'overwrite=false is missing: '+url)

    # Initiating:

    def test_init_cert_onefile(self):

        inst = HandleSystemConnector(
            certificate_and_key=FILE_BOTH,
            handle_server_url='http://foo.com'
        )
        self.assertIsInstance(inst, HandleSystemConnector)

    def test_init_cert_twofiles(self):

        inst = HandleSystemConnector(
            certificate_only=FILE_CERT,
            private_key=FILE_KEY,
            handle_server_url='http://foo.com'
        )
        self.assertIsInstance(inst, HandleSystemConnector)

    def test_init_cert_serverurl_missing(self):

        with self.assertRaises(TypeError):
            inst = HandleSystemConnector(FILE_BOTH)
        
    def test_init_privatekey_missing(self):

        inst = HandleSystemConnector(
            certificate_only=FILE_BOTH,
            handle_server_url='http://foo.com'
        )
        self.assertIsInstance(inst, HandleSystemConnector)

    def test_init_certificate_missing(self):

        inst = HandleSystemConnector(
            handle_server_url='http://foo.com',
            private_key=FILE_KEY
        )
        self.assertIsInstance(inst, HandleSystemConnector)

    def test_init_cert_onefile_wrongpath(self):

        with self.assertRaises(CredentialsFormatError):
            inst = HandleSystemConnector(
                certificate_and_key=PATH_CRED+'/noexist.pem',
                handle_server_url='http://foo.com'
            )