|
| 1 | +import minio |
| 2 | +from ddf import G |
| 3 | +from unittest.mock import patch |
| 4 | +from core.models import Repository |
| 5 | +from codecov_auth.models import Owner |
| 6 | +from rest_framework.reverse import reverse |
| 7 | +from rest_framework.test import APITestCase |
| 8 | + |
| 9 | + |
| 10 | +class UploadDownloadHelperTest(APITestCase): |
| 11 | + def _get(self, kwargs={}, data={}): |
| 12 | + path = f"/upload/{kwargs.get('service')}/{kwargs.get('owner_username')}/{kwargs.get('repo_name')}/download" |
| 13 | + return self.client.get(path, data=data) |
| 14 | + |
| 15 | + def setUp(self): |
| 16 | + self.org = G(Owner, username="codecovtest", service="github") |
| 17 | + self.repo = G( |
| 18 | + Repository, |
| 19 | + author=self.org, |
| 20 | + name="upload-test-repo", |
| 21 | + upload_token="test27s4f3uz3ha9pi0foipg5bqojtrmbt67", |
| 22 | + ) |
| 23 | + self.repo = G( |
| 24 | + Repository, author=self.org, name="private-upload-test-repo", private=True |
| 25 | + ) |
| 26 | + |
| 27 | + def test_no_path_param(self): |
| 28 | + response = self._get() |
| 29 | + assert response.status_code == 404 |
| 30 | + |
| 31 | + def test_invalid_path_param(self): |
| 32 | + response = self._get(data={"path": "v2"}) |
| 33 | + assert response.status_code == 404 |
| 34 | + |
| 35 | + def test_invalid_owner(self): |
| 36 | + response = self._get( |
| 37 | + kwargs={ |
| 38 | + "service": "gh", |
| 39 | + "owner_username": "invalid", |
| 40 | + "repo_name": "invalid", |
| 41 | + }, |
| 42 | + data={"path": "v4/raw"}, |
| 43 | + ) |
| 44 | + assert response.status_code == 404 |
| 45 | + |
| 46 | + def test_invalid_repo(self): |
| 47 | + response = self._get( |
| 48 | + kwargs={ |
| 49 | + "service": "gh", |
| 50 | + "owner_username": "codecovtest", |
| 51 | + "repo_name": "invalid", |
| 52 | + }, |
| 53 | + data={"path": "v4/raw"}, |
| 54 | + ) |
| 55 | + assert response.status_code == 404 |
| 56 | + |
| 57 | + @patch("services.archive.ArchiveService.get_archive_hash") |
| 58 | + @patch("services.archive.ArchiveService.read_file") |
| 59 | + def test_invalid_archive_path(self, read_file, get_archive_hash): |
| 60 | + read_file.side_effect = [minio.error.NoSuchKey] |
| 61 | + get_archive_hash.return_value = "path" |
| 62 | + response = self._get( |
| 63 | + kwargs={ |
| 64 | + "service": "gh", |
| 65 | + "owner_username": "codecovtest", |
| 66 | + "repo_name": "upload-test-repo", |
| 67 | + }, |
| 68 | + data={"path": "v4/raw/path"}, |
| 69 | + ) |
| 70 | + assert response.status_code == 404 |
| 71 | + |
| 72 | + @patch("services.archive.ArchiveService.get_archive_hash") |
| 73 | + @patch("services.archive.ArchiveService.read_file") |
| 74 | + def test_valid_repo_archive_path(self, mock_read_file, get_archive_hash): |
| 75 | + mock_read_file.return_value = "Report!" |
| 76 | + get_archive_hash.return_value = "hasssshhh" |
| 77 | + response = self._get( |
| 78 | + kwargs={ |
| 79 | + "service": "gh", |
| 80 | + "owner_username": "codecovtest", |
| 81 | + "repo_name": "upload-test-repo", |
| 82 | + }, |
| 83 | + data={"path": "v4/raw/hasssshhh"}, |
| 84 | + ) |
| 85 | + assert response.status_code == 200 |
| 86 | + headers = response._headers |
| 87 | + assert headers["content-type"] == ("Content-Type", "text/plain") |
| 88 | + |
| 89 | + @patch("services.archive.ArchiveService.read_file") |
| 90 | + def test_invalid_repo_archive_path(self, mock_read_file): |
| 91 | + mock_read_file.return_value = "Report!" |
| 92 | + response = self._get( |
| 93 | + kwargs={ |
| 94 | + "service": "gh", |
| 95 | + "owner_username": "codecovtest", |
| 96 | + "repo_name": "upload-test-repo", |
| 97 | + }, |
| 98 | + data={"path": "v4/raw"}, |
| 99 | + ) |
| 100 | + assert response.status_code == 404 |
| 101 | + |
| 102 | + def test_private_valid_archive_path(self): |
| 103 | + response = self._get( |
| 104 | + kwargs={ |
| 105 | + "service": "gh", |
| 106 | + "owner_username": "codecovtest", |
| 107 | + "repo_name": "private-upload-test-repo", |
| 108 | + }, |
| 109 | + data={"path": "v4/raw"}, |
| 110 | + ) |
| 111 | + assert response.status_code == 404 |
0 commit comments