def test_03_name_and_version(self):
        """
        Take an example supported article and check just the handler fields
        """
        record = {}
        record['bibjson'] = {}
        record['provider'] = {}
        record['provider']['url'] = ['http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0035089']
        record = models.MessageObject(record=record)

        plos = PLOSPlugin()
        plos.license_detect(record)

        record = record.record
        
        # just barebones checks to make sure the license and provenance objects
        # exist in the first place so the handler fields can be checked
        assert record['bibjson'].has_key('license')
        assert record['bibjson']['license']

        assert 'provenance' in record['bibjson']['license'][-1]

        assert 'handler' in record['bibjson']['license'][-1]['provenance']
        assert record['bibjson']['license'][-1]['provenance']['handler'] == 'plos', 'was actually: ' + record['bibjson']['license'][-1]['provenance']['handler']
        assert record['bibjson']['license'][-1]['provenance']['handler_version'] == '0.1'
 def test_03_resource_and_result(self):
     global CURRENT_REQUEST
     
     # go through each file and result object
     for path, comparison in RESOURCE_AND_RESULT.iteritems():
         # construct a request object, using the provenance/source url as the provider url
         record = {}
         record['bibjson'] = {}
         record['provider'] = {}
         record['provider']['url'] = [comparison['provenance']['source']]
         record = models.MessageObject(record=record)
         
         # set the current request so that the monkey patch knows how to respond
         CURRENT_REQUEST = comparison['provenance']['source']
         
         # run the plugin
         p = MyPlugin()
         p.license_detect(record)
         
         record = record.record
         # check if all the top-level keys were created
         assert "bibjson" in record
         assert "license" in record['bibjson']
         assert record['bibjson']['license'] is not None
         
         # The rules for the comparison licence object are:
         # - if a key has a value, there resulting object's value must match exactly
         # - if a key has been omitted, it will not be tested
         # - if a key's value is the empty string, the resulting object's key's value must be the empty string
         # - if a key's value is None, the resulting object MUST NOT have the key or MUST be the empty string
         # - if a key's value is -1, the resulting object MUST have the key
         licence = record['bibjson']['license'][0]
         for key, value in comparison.iteritems():
             if key == "provenance":
                 # for better layout of code, let's do provenance separately
                 continue
             if value is None:
                 # the resulting object MUST NOT have the key or MUST be the empty string
                 assert key not in licence or licence.get(key) == "", ((key, value), licence.get(key))
             elif value == -1:
                 # the resulting object MUST have the key
                 assert key in licence, ((key, value), licence.get(key))
             else:
                 # the resulting object must match the comparison object
                 assert value == licence.get(key), ((key, value), licence.get(key))
         
         prov = licence.get("provenance", {})
         for key, value in comparison.get("provenance", {}).iteritems():
             if value is None:
                 # the resulting object MUST NOT have the key
                 assert key not in prov or prov.get(key) == "", ((key, value), prov.get(key))
             elif value == -1:
                 # the resulting object MUST have the key
                 assert key in prov, ((key, value), prov.get(key))
             else:
                 # the resulting object must match the comparison object
                 assert value == prov.get(key), ((key, value), prov.get(key))
    def test_05_plos_CC_BY(self):
        record = {}
        record['bibjson'] = {}
        record['provider'] = {}
        record['provider']['url'] = ['http://www.plosbiology.org/article/info%3Adoi%2F10.1371%2Fjournal.pbio.1001406']
        record = models.MessageObject(record=record)
        
        plos = PLOSPlugin()
        plos.license_detect(record)

        record = record.record

        # check if all the important keys were created
        assert record['bibjson'].has_key('license')
        assert record['bibjson']['license']

        # NB: some examples may fail the 'url' test since the Open Definition
        # data we're using as the basis for our licenses dictionary does not
        # have 'url' for all licenses. Fix by modifying licenses.py - add the data.
        # TODO remove this "all" assertion - if it fails, doesn't say which key missing
        assert all (key in record['bibjson']['license'][-1] for key in keys_in_license)

        assert all (key in record['bibjson']['license'][-1]['provenance'] for key in keys_in_provenance)

        # some content checks now
        assert record['bibjson']['license'][-1]['type'] == 'cc-by'
        assert record['bibjson']['license'][-1]['version'] == ''
        assert 'id' not in record['bibjson']['license'][-1] # should not have "id" - due to bibserver
        assert not record['bibjson']['license'][-1]['jurisdiction']
        assert record['bibjson']['license'][-1]['open_access']
        assert record['bibjson']['license'][-1]['BY']
        assert not record['bibjson']['license'][-1]['NC']
        assert not record['bibjson']['license'][-1]['SA']
        assert not record['bibjson']['license'][-1]['ND']

        assert record['bibjson']['license'][-1]['provenance']['agent'] == config.agent
        assert record['bibjson']['license'][-1]['provenance']['source'] == record['provider']['url'][0]
        assert record['bibjson']['license'][-1]['provenance']['date']
        assert record['bibjson']['license'][-1]['provenance']['category'] == 'page_scrape'
        assert record['bibjson']['license'][-1]['provenance']['description'] == 'License decided by scraping the resource at http://www.plosbiology.org/article/info%3Adoi%2F10.1371%2Fjournal.pbio.1001406 and looking for the following license statement: "This is an open-access article distributed under the terms of the Creative Commons Attribution License, which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited.".'
    def test_08_plos_public_domain_dedication(self):
        record = {}
        record['bibjson'] = {}
        record['provider'] = {}
        record['provider']['url'] = ['http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0037743']
        record = models.MessageObject(record=record)
        
        plos = PLOSPlugin()
        plos.license_detect(record)
        
        record = record.record
        
        # check if all the important keys were created
        assert record['bibjson'].has_key('license')
        assert record['bibjson']['license']

        # NB: some examples may fail the 'url' test since the Open Definition
        # data we're using as the basis for our licenses dictionary does not
        # have 'url' for all licenses. Fix by modifying licenses.py - add the data.
        # TODO remove this "all" assertion - if it fails, doesn't say which key missing
        assert all (key in record['bibjson']['license'][-1] for key in keys_in_license)

        assert all (key in record['bibjson']['license'][-1]['provenance'] for key in keys_in_provenance)

        # some content checks now
        assert record['bibjson']['license'][-1]['type'] == 'cc-zero'
        assert record['bibjson']['license'][-1]['version'] == ''
        assert 'id' not in record['bibjson']['license'][-1] # should not have "id" - due to bibserver
        assert not record['bibjson']['license'][-1]['jurisdiction']
        assert record['bibjson']['license'][-1]['open_access']
        assert not record['bibjson']['license'][-1]['BY']
        assert not record['bibjson']['license'][-1]['NC']
        assert not record['bibjson']['license'][-1]['SA']
        assert not record['bibjson']['license'][-1]['ND']

        assert record['bibjson']['license'][-1]['provenance']['agent'] == config.agent
        assert record['bibjson']['license'][-1]['provenance']['source'] == record['provider']['url'][0]
        assert record['bibjson']['license'][-1]['provenance']['date']
        assert record['bibjson']['license'][-1]['provenance']['category'] == 'page_scrape'
        assert record['bibjson']['license'][-1]['provenance']['description'] == 'License decided by scraping the resource at http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0037743 and looking for the following license statement: "This is an open-access article, free of all copyright, and may be freely reproduced, distributed, transmitted, modified, built upon, or otherwise used by anyone for any lawful purpose. The work is made available under the Creative Commons CC0 public domain dedication.".'
 def test_02_plos_supports_fail(self):
     plos = PLOSPlugin()
     test_urls = ["http://www.biomedcentral.com/", "askjdfsakjdhfsa"]
     for url in test_urls:
         assert not plos.supports({"url" : [url]})
 def test_01_plos_supports_success(self):
     plos = PLOSPlugin()
     test_urls = ["http://www.plosone.org/1234", "www.plosbiology.org/fakjsskjdaf", "https://www.plosmedicine.org/asdfdsafa",
                     "www.ploscompbiol.org", "www.plosgenetics.org", "www.plospathogens.org", "www.plosntds.org"]
     for url in test_urls:
         assert plos.supports({"url" : [url]})
 def test_02_supports_fail(self):
     p = MyPlugin()
     for url in UNSUPPORTED_URLS:
         assert not p.supports({"url" : [url]})
 def test_01_supports_success(self):
     p = MyPlugin()
     for url in SUPPORTED_URLS:
         assert p.supports({"url" : [url]})