import pytest
from web_test_base import *

class TestIATIValidator(WebTestBase):
    requests_to_load = {
        'IATI Validator': {
            'url': 'http://validator.iatistandard.org/'
        }
        , 'Valid paste data': {
            'url': 'http://validator.iatistandard.org/index.php'
            , 'method': 'POST'
            , 'data': {
                'paste': utility.load_file_contents('valid.xml')
            }
        }
        , 'Invalid paste data': {
            'url': 'http://validator.iatistandard.org/index.php'
            , 'method': 'POST'
            , 'data': {
                'paste': utility.load_file_contents('invalid.xml')
            }
        }
    }

    def test_contains_links(self, loaded_request):
        """
        Test that each page contains links to the defined URLs.
        """
        result = utility.get_links_from_page(loaded_request)

        assert "http://iatistandard.org/" in result

    @pytest.mark.parametrize("target_request", ["IATI Validator"])
    def test_contains_form(self, target_request):
        """
        Test that the validator contains a form on each of three tabs.
        """
        req = self.loaded_request_from_test_name(target_request)

        assert len(utility.locate_xpath_result(req, '//*[@id="status"]/div/form')) == 1
        assert len(utility.locate_xpath_result(req, '//*[@id="fileTab"]/div/form')) == 1
        assert len(utility.locate_xpath_result(req, '//*[@id="extra"]/div/form')) == 1

    @pytest.mark.parametrize("target_request", ["Valid paste data"])
    def test_valid_pasted_xml(self, target_request):
        """
        Tests that the validator correctly detects valid XML as valid.
        """
        text_to_find = "This is a well formed xml file."
        xpath_to_locate = '//*[@id="status"]/div[2]'

        assert self.passes_text_detection_test(target_request, text_to_find, xpath_to_locate)

    @pytest.mark.parametrize("target_request", ["Invalid paste data"])
    def test_invalid_pasted_xml(self, target_request):
        """
        Tests that the validator correctly detects invalid XML as invalid.
        """
        text_to_find = "This is not a well-formed xml file"
        xpath_to_locate = '//*[@id="status"]/div[2]'

        assert self.passes_text_detection_test(target_request, text_to_find, xpath_to_locate)

@@ -679,22 +679,604 @@
       %7D%0A
+        , 'Valid URL data': %7B%0A            'url': 'http://validator.iatistandard.org/index.php'%0A            , 'method': 'POST'%0A            , 'data': %7B%0A                'url': 'https://raw.githubusercontent.com/IATI/IATI-Extra-Documentation/version-2.01/en/activity-standard/activity-standard-example-annotated.xml'%0A            %7D%0A        %7D%0A        , 'Invalid URL data': %7B%0A            'url': 'http://validator.iatistandard.org/index.php'%0A            , 'method': 'POST'%0A            , 'data': %7B%0A                'url': 'http://validator.iatistandard.org/index.php'%0A            %7D%0A        %7D%0A
     %7D%0A
-
 %0A    def
@@ -2118,32 +2118,50 @@
 , %5B%22Valid paste 
+data%22, %22Valid URL 
 data%22%5D)%0A    def 
@@ -2506,16 +2506,16 @@
 ocate)%0A%0A
-
     @pyt
@@ -2565,24 +2565,44 @@
 valid paste 
+data%22, %22Invalid URL 
 data%22%5D)%0A    

Add URL-loading validator tests