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".')
예제 #2
0
    def test_search_handle_prefixfilter(self):
        """Test filtering for prefixes."""
        log_new_case("test_search_handle_prefixfilter")

        prefix1 = self.prefix_inexistent
        prefix2 = self.handle.split('/')[0]

        log_start_test_code()
        val1 = self.inst.search_handle(URL='*dkrz*', prefix=prefix1)
        log_end_test_code()
        log_start_test_code()
        val2 = self.inst.search_handle(URL='*dkrz*', prefix=prefix2)
        log_end_test_code()

        # Check desired outcome:
        self.assertEqual(type(val1),type([]),
            'Searching did not return a list, but: '+str(val1)+', type: '+str(type(val1)))
        for item in val1:
            self.assertEqual(item.split('/')[0], prefix1,
                'This search result has the wrong prefix: '+item+', should be '+prefix1)

        self.assertEqual(type(val2),type([]),
            'Searching did not return a list, but: '+str(val1)+', type: '+str(type(val1)))
        for item in val2:
            self.assertEqual(item.split('/')[0], prefix2,
                'This search result has the wrong prefix: '+item+', should be '+prefix2)
    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_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_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_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.')
    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_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()
예제 #9
0
    def test_search_handle_emptylist(self):
        """Test empty search result."""
        log_new_case("test_search_handle_emptylist")

        log_start_test_code()
        val = self.inst.search_handle(URL=self.url_inexistent)
        log_end_test_code()

        # Check desired outcome:
        self.assertEqual(type(val),type([]),
            'Searching did not return a list, but: '+str(val)+', type: '+str(type(val)))
        self.assertEqual(len(val),0,
            'Searching did not return an empty list, but: '+str(val))
    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 = RESTHandleClient(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()
예제 #11
0
        def test_search_handle(self):
            """Test searching for handles with any url (server should return list of handles)."""
            log_new_case("test_search_handle")

            log_start_test_code()
            val = self.inst.search_handle(URL='*')
            log_end_test_code()

            # Check desired outcome:
            self.assertEqual(type(val),type([]),
                'Searching did not return a list, but: '+str(val))
            self.assertTrue(len(val) > 0,
                'Searching did not return any handles!')
            self.assertTrue(self.inst.check_handle_syntax(val[0]),
                'Searching returned handles with a wrong syntax, e.g.: '+str(val[0]))
예제 #12
0
    def test_search_handle_for_url(self):
        """Test searching for url with wildcards."""
        log_new_case("test_search_handle_for_url")

        log_start_test_code()
        val1 = self.inst.search_handle(URL='*dkrz*')
        log_end_test_code()
        log_start_test_code()
        val2 = self.inst.search_handle('*dkrz*')
        log_end_test_code()

        # Check desired outcome:
        self.assertEqual(type(val1),type([]),
            'Searching did not return a list, but: '+str(val1)+', type: '+str(type(val1)))
        self.assertEqual(val1, val2,
            'Searching with or without keyword did not return the same result:'+\
            '\nwith keyword: '+str(val1)+'\nwithout: '+str(val2))
    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))
    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))
예제 #15
0
    def test_search_handle_wrong_url_test(self):
        """Test exception when wrong search servlet URL is given."""
        log_new_case("test_search_handle_wrong_url_test")

        # Make new client instance with existing but wrong url for searching:
        inst = RESTHandleClient.instantiate_for_read_and_search(
            self.url,
            self.searchuser,
            self.searchpassword,
            reverselookup_baseuri=self.searchurl_wrong,
            reverselookup_url_extension=self.searchpath,
            HTTPS_verify=self.https_verify)

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(ReverseLookupException):
            inst.search_handle(URL='*')
        log_end_test_code()
예제 #16
0
    def test_search_handle_hs_url_test(self):
        """Test exception when wrong search servlet URL (Handle Server REST API URL) is given."""
        log_new_case("test_search_handle_hs_url_test")

        # Make new instance with handle server url as search url:
        self.inst = RESTHandleClient.instantiate_for_read_and_search(
            self.url,
            self.searchuser,
            self.searchpassword,
            reverselookup_baseuri=self.url,
            reverselookup_url_extension=self.path_to_api,
            HTTPS_verify=self.https_verify)

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(ReverseLookupException):
            self.inst.search_handle(URL='*')
            # TODO specify exception
        log_end_test_code()
예제 #17
0
    def test_search_handle_for_checksum(self):
        """Test searching for checksum with wildcards."""
        log_new_case("test_search_handle_for_checksum")

        log_start_test_code()
        val1 = self.inst.search_handle(None, CHECKSUM='*1111111111111*')

        log_end_test_code()
        log_start_test_code()
        val2 = self.inst.search_handle(URL=None, CHECKSUM='*1111111111111*')
        log_end_test_code()

        # Check desired outcome:
        self.assertEqual(type(val1),type([]),
            'Searching did not return a list, but: '+str(val1)+', type: '+str(type(val1)))
        self.assertEqual(val1, val2,
            'Searching with or without keyword did not return the same result:'+\
            '\nwith keyword: '+str(val1)+'\nwithout: '+str(val2))
        self.assertEqual(val1, [], 'val1 is: '+str(val1)+', instead of []')
        self.assertEqual(val2, [], 'val2 is: '+str(val2)+', instead of []')
    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_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_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_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()