Example #1
0
    def test_Scan_index_files_key_missing(self):
        test_files = [
            models.File({'path': 'path1/a'}),
            models.File({'path': 'path1/b'})
        ]

        scan = models.Scan()
        scan.files = test_files

        with pytest.raises(AttributeError):
            index = scan.index_files('missing')
Example #2
0
 def test_File_size_difference(self):
     a = {'path': '', 'name': '', 'sha1': '', 'size': 4096}
     b = {'path': '', 'name': '', 'sha1': '', 'size': 4096}
     a_file = models.File(a)
     b_file = models.File(b)
     assert 0 == a_file.size_difference(b_file)
     b['size'] = 2048
     b_file = models.File(b)
     assert 2048 == a_file.size_difference(b_file)
     b['size'] = 8192
     b_file = models.File(b)
     assert -4096 == a_file.size_difference(b_file)
Example #3
0
    def test_Scan_index_files_key_path(self):
        test_files = [
            models.File({'path': 'path1/a'}),
            models.File({'path': 'path1/b'})
        ]

        scan = models.Scan()
        scan.files = test_files

        index = scan.index_files('path')

        assert index.get('path1/a') == [test_files[0]]
        assert index.get('path1/b') == [test_files[1]]
Example #4
0
    def test_File_empty(self):
        empty_file = models.File()

        assert empty_file.path == ''
        assert empty_file.type == ''
        assert empty_file.name == ''
        assert empty_file.size == ''
        assert empty_file.sha1 == ''
        assert empty_file.licenses == []
        assert empty_file.copyrights == []
Example #5
0
    def test_File_create_object_copyright_missing(self):
        data = {
            'path': 'a/b/file1.txt',
            'type': 'file',
            'name': 'file1.txt',
            'size': 20,
            'sha1': '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b',
        }

        result = models.File(data)

        assert [] == result.copyrights
Example #6
0
    def test_Scan_index_files_key_sha1_foo(self):
        test_files = [
            models.File({
                'path': 'path1/a/foo',
                'sha1': '3340d86b1da9323067db8022f86dc97cfccee1d0'
            }),
            models.File({
                'path': 'path1/b/foo',
                'sha1': '3340d86b1da9323067db8022f86dc97cfccee1d0'
            })
        ]

        scan = models.Scan()
        scan.files = test_files

        index = scan.index_files('sha1')

        result = test_files

        assert len(index.get('3340d86b1da9323067db8022f86dc97cfccee1d0')) == 2
        assert index.get('3340d86b1da9323067db8022f86dc97cfccee1d0') == result
Example #7
0
    def test_File_to_dict_simple_w_copyright(self):
        data = {
            'path':
            'a/b/file1.txt',
            'type':
            'file',
            'name':
            'file1.txt',
            'size':
            20,
            'sha1':
            '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b',
            'licenses': [],
            'copyrights': [{
                "statements": [
                    "Copyright (c) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler"
                ],
                "holders": ["Jean-loup Gailly, Mark Adler"],
                "authors": [],
                "start_line":
                1,
                "end_line":
                3
            }]
        }

        expected = {
            'path':
            'a/b/file1.txt',
            'type':
            'file',
            'name':
            'file1.txt',
            'size':
            20,
            'sha1':
            '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b',
            'original_path':
            '',
            'licenses': [],
            'copyrights': [{
                "statements": [
                    "Copyright (c) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler"
                ],
                "holders": ["Jean-loup Gailly, Mark Adler"]
            }]
        }

        result = models.File(data).to_dict()

        assert result == expected
        with pytest.raises(AttributeError):
            assert result.made_up_key == "a_string"
Example #8
0
    def test_File_create_object(self):
        data = {
            'path': 'a/b/file1.txt',
            'type': 'file',
            'name': 'file1.txt',
            'size': 20,
            'sha1': '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b',
        }

        result = models.File(data)

        assert 'a/b/file1.txt' == result.path
        assert 'file' == result.type
        assert 'file1.txt' == result.name
        assert 20 == result.size
        assert '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b' == result.sha1
Example #9
0
    def test_File_to_dict_empty(self):
        empty_file = models.File()

        expected = {
            'path': '',
            'type': '',
            'name': '',
            'size': '',
            'sha1': '',
            'original_path': '',
            'licenses': [],
            'copyrights': []
        }

        result = empty_file.to_dict()
        assert result == expected
Example #10
0
    def test_Copyright_unusual_characters(self):
        new = {
            'path':
            'path/modified.txt',
            'type':
            'file',
            'name':
            'modified.txt',
            'size':
            20,
            'sha1':
            'a',
            'original_path':
            '',
            'licenses': [],
            'copyrights': [{
                "statements": [
                    "~@ \n \r", " ", "\x80abc", "\xc3", "\xa0", "\xaa",
                    "\xb9", "\xa9", "\xa8", "\xb4", "\xae", "-", "\xe2",
                    "\x80", "\x99", "\xa2", "\xa7", "\xbb", "\xaf", "U+00E9",
                    "\xc3\xa9"
                ],
                "holders": [
                    "~@ \n \r", " ", "\x80abc", "\xc3", "\xa0", "\xaa",
                    "\xb9", "\xa9", "\xa8", "\xb4", "\xae", "-", "\xe2",
                    "\x80", "\x99", "\xa2", "\xa7", "\xbb", "\xaf", "U+00E9",
                    "\xc3\xa9"
                ],
                "authors": []
            }]
        }

        result_new = models.File(new)
        new_copyrights = result_new.copyrights.pop()

        assert new_copyrights.statements == [
            "~@ \n \r", " ", "\x80abc", "\xc3", "\xa0", "\xaa", "\xb9",
            "\xa9", "\xa8", "\xb4", "\xae", "-", "\xe2", "\x80", "\x99",
            "\xa2", "\xa7", "\xbb", "\xaf", "U+00E9", "\xc3\xa9"
        ]

        assert new_copyrights.holders == [
            "~@ \n \r", " ", "\x80abc", "\xc3", "\xa0", "\xaa", "\xb9",
            "\xa9", "\xa8", "\xb4", "\xae", "-", "\xe2", "\x80", "\x99",
            "\xa2", "\xa7", "\xbb", "\xaf", "U+00E9", "\xc3\xa9"
        ]
Example #11
0
    def test_File_create_object_license_one(self):
        data = {
            'path':
            'a/b/file1.txt',
            'type':
            'file',
            'name':
            'file1.txt',
            'size':
            20,
            'sha1':
            '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b',
            'licenses': [{
                "key": "apache-2.0",
                "score": 80.0,
                "short_name": "Apache 2.0",
                "category": "Permissive",
                "owner": "Apache Software Foundation",
                "homepage_url": "http://www.apache.org/licenses/",
                "text_url": "http://www.apache.org/licenses/LICENSE-2.0",
                "reference_url":
                "https://enterprise.dejacode.com/urn/urn:dje:license:apache-2.0",
                "spdx_license_key": "Apache-2.0",
                "spdx_url": "https://spdx.org/licenses/Apache-2.0",
                "start_line": 3,
                "end_line": 3,
                "matched_rule": {
                    "identifier": "apache-2.0_57.RULE",
                    "license_choice": False,
                    "licenses": ["apache-2.0"]
                }
            }],
        }

        result = models.File(data)

        assert 'a/b/file1.txt' == result.path
        assert 'file' == result.type
        assert 'file1.txt' == result.name
        assert 20 == result.size
        assert '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b' == result.sha1
        assert [] == result.copyrights
        assert len(result.licenses) == 1
        assert result.licenses[0].key == 'apache-2.0'
        with pytest.raises(AttributeError):
            assert result.spdx_license_key == "Apache-2.0"
Example #12
0
    def test_File_create_object_copyright_none(self):
        data = {
            'path': 'a/b/file1.txt',
            'type': 'file',
            'name': 'file1.txt',
            'size': 20,
            'sha1': '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b',
            'copyrights': []
        }

        result = models.File(data)

        assert 'a/b/file1.txt' == result.path
        assert 'file' == result.type
        assert 'file1.txt' == result.name
        assert 20 == result.size
        assert '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b' == result.sha1
        assert [] == result.licenses
        assert [] == result.copyrights
Example #13
0
    def test_File_to_dict_simple(self):
        data = {
            'path': 'a/b/file1.txt',
            'type': 'file',
            'name': 'file1.txt',
            'size': 20,
            'sha1': '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b',
        }

        expected = {
            'path': 'a/b/file1.txt',
            'type': 'file',
            'name': 'file1.txt',
            'size': 20,
            'sha1': '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b',
            'original_path': '',
            'licenses': [],
            'copyrights': []
        }

        result = models.File(data).to_dict()

        assert result == expected
Example #14
0
    def test_File_create_object_copyright_one(self):
        data = {
            'path':
            'a/b/file1.txt',
            'type':
            'file',
            'name':
            'file1.txt',
            'size':
            20,
            'sha1':
            '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b',
            'licenses': [],
            'copyrights': [{
                "statements": [
                    "Copyright (c) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler"
                ],
                "holders": ["Jean-loup Gailly, Mark Adler"],
                "authors": []
            }]
        }

        result = models.File(data)

        assert 'a/b/file1.txt' == result.path
        assert 'file' == result.type
        assert 'file1.txt' == result.name
        assert 20 == result.size
        assert '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b' == result.sha1
        assert [] == result.licenses
        assert len(result.copyrights) == 1
        assert result.copyrights[0].statements == [
            "Copyright (c) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler"
        ]
        with pytest.raises(AttributeError):
            assert result.made_up_key == "a_string"
Example #15
0
    def test_Copyright_multiple_statements_and_holders(self):
        new = {
            'path':
            'path/modified.txt',
            'type':
            'file',
            'name':
            'modified.txt',
            'size':
            20,
            'sha1':
            'a',
            'original_path':
            '',
            'licenses': [],
            'copyrights': [{
                "statements": [
                    "Copyright (c) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler",
                    "Copyright (c) 1998 by Andreas R. Kleinert",
                    "Copyright (c) 2002-2004 Dmitriy Anisimkov",
                    "Copyright (c) 1998, 2007 Brian Raiter",
                    "Copyright (c) 1997,99 Borland Corp.",
                    "(c) Copyright Henrik Ravn 2004",
                    "Copyright (c) 1995-2010 Jean-loup Gailly, Brian Raiter and Gilles Vollant.",
                    "Copyright (c) 2003 Chris Anderson",
                    "Copyright (c) 1997 Christian Michelsen Research as Advanced Computing",
                    "Copyright (c) 2009-2010 Mathias Svensson http://result42.com",
                    "Copyright (c) 1990-2000 Info-ZIP.",
                    "Copyright (c) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler",
                    "Copyright (c) 1998 by Andreas R. Kleinert",
                    "Copyright (c) 2002-2004 Dmitriy Anisimkov",
                    "Copyright (c) 1998, 2007 Brian Raiter",
                    "Copyright (c) 1997,99 Borland Corp.",
                    "(c) Copyright Henrik Ravn 2004",
                    "Copyright (c) 1995-2010 Jean-loup Gailly, Brian Raiter and Gilles Vollant.",
                    "Copyright (c) 2003 Chris Anderson",
                    "Copyright (c) 1997 Christian Michelsen Research as Advanced Computing",
                    "Copyright (c) 2009-2010 Mathias Svensson http://result42.com",
                    "Copyright (c) 1990-2000 Info-ZIP."
                ],
                "holders": [
                    "Jean-loup Gailly, Mark Adler", "Andreas R. Kleinert",
                    "Dmitriy Anisimkov", "Brian Raiter", "Borland Corp.",
                    "Henrik Ravn",
                    "Jean-loup Gailly, Brian Raiter, Gilles Vollant",
                    "Chris Anderson",
                    "Christian Michelsen Research as Advanced Computing",
                    "Mathias Svensson", "Info-ZIP",
                    "Jean-loup Gailly, Mark Adler", "Andreas R. Kleinert",
                    "Dmitriy Anisimkov", "Brian Raiter", "Borland Corp.",
                    "Henrik Ravn",
                    "Jean-loup Gailly, Brian Raiter, Gilles Vollant",
                    "Chris Anderson",
                    "Christian Michelsen Research as Advanced Computing",
                    "Mathias Svensson", "Info-ZIP"
                ],
                "authors": []
            }]
        }

        result_new = models.File(new)
        new_copyrights = result_new.copyrights.pop()

        assert new_copyrights.statements == [
            "Copyright (c) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler",
            "Copyright (c) 1998 by Andreas R. Kleinert",
            "Copyright (c) 2002-2004 Dmitriy Anisimkov",
            "Copyright (c) 1998, 2007 Brian Raiter",
            "Copyright (c) 1997,99 Borland Corp.",
            "(c) Copyright Henrik Ravn 2004",
            "Copyright (c) 1995-2010 Jean-loup Gailly, Brian Raiter and Gilles Vollant.",
            "Copyright (c) 2003 Chris Anderson",
            "Copyright (c) 1997 Christian Michelsen Research as Advanced Computing",
            "Copyright (c) 2009-2010 Mathias Svensson http://result42.com",
            "Copyright (c) 1990-2000 Info-ZIP.",
            "Copyright (c) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler",
            "Copyright (c) 1998 by Andreas R. Kleinert",
            "Copyright (c) 2002-2004 Dmitriy Anisimkov",
            "Copyright (c) 1998, 2007 Brian Raiter",
            "Copyright (c) 1997,99 Borland Corp.",
            "(c) Copyright Henrik Ravn 2004",
            "Copyright (c) 1995-2010 Jean-loup Gailly, Brian Raiter and Gilles Vollant.",
            "Copyright (c) 2003 Chris Anderson",
            "Copyright (c) 1997 Christian Michelsen Research as Advanced Computing",
            "Copyright (c) 2009-2010 Mathias Svensson http://result42.com",
            "Copyright (c) 1990-2000 Info-ZIP."
        ]

        assert new_copyrights.holders == [
            "Jean-loup Gailly, Mark Adler", "Andreas R. Kleinert",
            "Dmitriy Anisimkov", "Brian Raiter", "Borland Corp.",
            "Henrik Ravn", "Jean-loup Gailly, Brian Raiter, Gilles Vollant",
            "Chris Anderson",
            "Christian Michelsen Research as Advanced Computing",
            "Mathias Svensson", "Info-ZIP", "Jean-loup Gailly, Mark Adler",
            "Andreas R. Kleinert", "Dmitriy Anisimkov", "Brian Raiter",
            "Borland Corp.", "Henrik Ravn",
            "Jean-loup Gailly, Brian Raiter, Gilles Vollant", "Chris Anderson",
            "Christian Michelsen Research as Advanced Computing",
            "Mathias Svensson", "Info-ZIP"
        ]
Example #16
0
    def test_File_to_dict_simple_w_license(self):
        data = {
            'path':
            'a/b/file1.txt',
            'type':
            'file',
            'name':
            'file1.txt',
            'size':
            20,
            'sha1':
            '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b',
            'licenses': [{
                "key": "apache-2.0",
                "score": 80.0,
                "short_name": "Apache 2.0",
                "category": "Permissive",
                "owner": "Apache Software Foundation",
                "homepage_url": "http://www.apache.org/licenses/",
                "text_url": "http://www.apache.org/licenses/LICENSE-2.0",
                "reference_url":
                "https://enterprise.dejacode.com/urn/urn:dje:license:apache-2.0",
                "spdx_license_key": "Apache-2.0",
                "spdx_url": "https://spdx.org/licenses/Apache-2.0",
                "start_line": 3,
                "end_line": 3,
                "matched_rule": {
                    "identifier": "apache-2.0_57.RULE",
                    "license_choice": False,
                    "licenses": ["apache-2.0"]
                }
            }],
        }

        expected = {
            'path':
            'a/b/file1.txt',
            'type':
            'file',
            'name':
            'file1.txt',
            'size':
            20,
            'sha1':
            '26d82f1931cbdbd83c2a6871b2cecd5cbcc8c26b',
            'original_path':
            '',
            'licenses': [{
                "key": "apache-2.0",
                "score": 80.0,
                "short_name": "Apache 2.0",
                "category": "Permissive",
                "owner": "Apache Software Foundation"
            }],
            'copyrights': []
        }

        result = models.File(data).to_dict()

        assert result == expected
        with pytest.raises(AttributeError):
            assert result.spdx_license_key == "Apache-2.0"