-
-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathtest_fetch_patch_url.py
More file actions
55 lines (42 loc) · 1.86 KB
/
test_fetch_patch_url.py
File metadata and controls
55 lines (42 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# VulnerableCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
from unittest import mock
from unittest.mock import MagicMock
import pytest
from vulnerabilities.models import PackageCommitPatch
from vulnerabilities.models import Patch
from vulnerabilities.pipelines.v2_improvers.fetch_patch_url import FetchPatchURLImproverPipeline
@pytest.mark.django_db
@mock.patch("vulnerabilities.utils.requests.get")
def test_collect_patch_text_success(mock_get):
res1 = MagicMock(status_code=200, text="diff --git a/file1")
res2 = MagicMock(status_code=200, text="diff --git a/file2")
mock_get.side_effect = [res1, res2]
pcp = PackageCommitPatch.objects.create(
vcs_url="https://github.com/nexB/vulnerablecode", commit_hash="abc1234", patch_text=None
)
patch = Patch.objects.create(
patch_url="https://gitlab.com/nexB/vulnerablecode/-/commit/def5678.patch", patch_text=None
)
pipeline = FetchPatchURLImproverPipeline()
pipeline.collect_patch_text()
pcp.refresh_from_db()
patch.refresh_from_db()
assert pcp.patch_text == "diff --git a/file1"
assert patch.patch_text == "diff --git a/file2"
@pytest.mark.django_db
@mock.patch("vulnerabilities.utils.requests.get")
def test_collect_patch_text_failure(mock_get):
mock_get.side_effect = Exception("Connection Error")
pcp = PackageCommitPatch.objects.create(
vcs_url="https://github.com/nexB/vulnerablecode", commit_hash="abc1234", patch_text=None
)
pipeline = FetchPatchURLImproverPipeline()
pipeline.collect_patch_text()
assert pcp.patch_text is None