|
1 | 1 | import json |
2 | 2 | import os |
3 | 3 | from datetime import datetime |
| 4 | +from unittest import mock |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 | import pytz |
7 | 8 |
|
8 | 9 | from vulnerabilities.package_managers_2 import GoproxyVersionAPI |
9 | 10 | from vulnerabilities.package_managers_2 import LegacyVersion |
10 | 11 | from vulnerabilities.package_managers_2 import NugetVersionAPI |
| 12 | +from vulnerabilities.package_managers_2 import PypiVersionAPI |
| 13 | +from vulnerabilities.package_managers_2 import RubyVersionAPI |
11 | 14 |
|
12 | 15 | BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
13 | 16 | TEST_DATA = os.path.join(BASE_DIR, "test_data", "package_manager_data") |
@@ -69,3 +72,103 @@ def test_nuget_extract_version(): |
69 | 72 | value="3.5.1", release_date=datetime(2015, 1, 23, 1, 5, 44, 447000, tzinfo=pytz.UTC) |
70 | 73 | ), |
71 | 74 | } |
| 75 | + |
| 76 | + |
| 77 | +def test_nuget_extract_version_with_illformed_data(): |
| 78 | + assert NugetVersionAPI.extract_versions({"items": [{"items": [{"catalogEntry": {}}]}]}) == set() |
| 79 | + |
| 80 | + |
| 81 | +@mock.patch("vulnerabilities.package_managers_2.get_response") |
| 82 | +def test_pypi_fetch_data(mock_response): |
| 83 | + pypi_api = PypiVersionAPI() |
| 84 | + with open(os.path.join(TEST_DATA, "pypi.json"), "r") as f: |
| 85 | + mock_response.return_value = json.load(f) |
| 86 | + pypi_api.fetch("django") |
| 87 | + assert pypi_api.cache == { |
| 88 | + "django": { |
| 89 | + LegacyVersion( |
| 90 | + value="1.10.5", |
| 91 | + release_date=datetime(2017, 1, 4, 19, 23, 0, 596664, tzinfo=pytz.UTC), |
| 92 | + ), |
| 93 | + LegacyVersion( |
| 94 | + value="1.10.8", |
| 95 | + release_date=datetime(2017, 9, 5, 15, 31, 58, 221021, tzinfo=pytz.UTC), |
| 96 | + ), |
| 97 | + LegacyVersion( |
| 98 | + value="1.10rc1", |
| 99 | + release_date=datetime(2016, 7, 18, 18, 5, 5, 503584, tzinfo=pytz.UTC), |
| 100 | + ), |
| 101 | + LegacyVersion( |
| 102 | + value="1.10.4", |
| 103 | + release_date=datetime(2016, 12, 1, 23, 46, 50, 215935, tzinfo=pytz.UTC), |
| 104 | + ), |
| 105 | + LegacyVersion( |
| 106 | + value="1.10a1", |
| 107 | + release_date=datetime(2016, 5, 20, 12, 24, 59, 952686, tzinfo=pytz.UTC), |
| 108 | + ), |
| 109 | + LegacyVersion( |
| 110 | + value="1.10.3", |
| 111 | + release_date=datetime(2016, 11, 1, 13, 57, 16, 55061, tzinfo=pytz.UTC), |
| 112 | + ), |
| 113 | + LegacyVersion( |
| 114 | + value="1.10.1", |
| 115 | + release_date=datetime(2016, 9, 1, 23, 18, 18, 672706, tzinfo=pytz.UTC), |
| 116 | + ), |
| 117 | + LegacyVersion( |
| 118 | + value="1.10.2", |
| 119 | + release_date=datetime(2016, 10, 1, 20, 5, 31, 330942, tzinfo=pytz.UTC), |
| 120 | + ), |
| 121 | + LegacyVersion( |
| 122 | + value="1.10.7", |
| 123 | + release_date=datetime(2017, 4, 4, 14, 27, 54, 235551, tzinfo=pytz.UTC), |
| 124 | + ), |
| 125 | + LegacyVersion( |
| 126 | + value="1.10.6", |
| 127 | + release_date=datetime(2017, 3, 1, 13, 37, 40, 243134, tzinfo=pytz.UTC), |
| 128 | + ), |
| 129 | + LegacyVersion( |
| 130 | + value="1.1.4", |
| 131 | + release_date=datetime(2011, 2, 9, 4, 13, 7, 75, tzinfo=pytz.UTC), |
| 132 | + ), |
| 133 | + LegacyVersion( |
| 134 | + value="1.10b1", |
| 135 | + release_date=datetime(2016, 6, 22, 1, 15, 17, 267637, tzinfo=pytz.UTC), |
| 136 | + ), |
| 137 | + LegacyVersion( |
| 138 | + value="1.1.3", |
| 139 | + release_date=datetime(2010, 12, 23, 5, 14, 23, 509436, tzinfo=pytz.UTC), |
| 140 | + ), |
| 141 | + LegacyVersion( |
| 142 | + value="1.10", |
| 143 | + release_date=datetime(2016, 8, 1, 18, 32, 16, 280614, tzinfo=pytz.UTC), |
| 144 | + ), |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | +@mock.patch("vulnerabilities.package_managers_2.get_response") |
| 150 | +def test_pypi_fetch_with_no_release(mock_response): |
| 151 | + pypi_api = PypiVersionAPI() |
| 152 | + mock_response.return_value = {"info": {}} |
| 153 | + pypi_api.fetch("django") |
| 154 | + assert pypi_api.cache == {"django": set()} |
| 155 | + |
| 156 | + |
| 157 | +@mock.patch("vulnerabilities.package_managers_2.get_response") |
| 158 | +def test_pypi_fetch_with_no_release(mock_response): |
| 159 | + ruby_api = RubyVersionAPI() |
| 160 | + with open(os.path.join(TEST_DATA, "gem.json"), "r") as f: |
| 161 | + mock_response.return_value = json.load(f) |
| 162 | + ruby_api.fetch("rails") |
| 163 | + assert ruby_api.cache == { |
| 164 | + "rails": { |
| 165 | + LegacyVersion( |
| 166 | + value="7.0.2.3", |
| 167 | + release_date=datetime(2022, 3, 8, 17, 50, 52, 496000, tzinfo=pytz.UTC), |
| 168 | + ), |
| 169 | + LegacyVersion( |
| 170 | + value="7.0.2.2", |
| 171 | + release_date=datetime(2022, 2, 11, 19, 44, 19, 17000, tzinfo=pytz.UTC), |
| 172 | + ), |
| 173 | + } |
| 174 | + } |
0 commit comments