|
| 1 | +from django.urls import reverse |
| 2 | +from rest_framework.authtoken.models import Token |
| 3 | +from rest_framework.test import APIClient |
| 4 | + |
| 5 | +from dojo.github.models import GITHUB_Conf, GITHUB_PKey |
| 6 | +from dojo.models import Product, User |
| 7 | +from unittests.dojo_test_case import DojoAPITestCase, versioned_fixtures |
| 8 | + |
| 9 | + |
| 10 | +@versioned_fixtures |
| 11 | +class ProductGithubApiTest(DojoAPITestCase): |
| 12 | + fixtures = ["dojo_testdata.json"] |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + user = User.objects.get(username="admin") |
| 16 | + token = Token.objects.get(user=user) |
| 17 | + self.client = APIClient() |
| 18 | + self.client.credentials(HTTP_AUTHORIZATION="Token " + token.key) |
| 19 | + self.url = reverse("product-list") |
| 20 | + self.github_configuration = GITHUB_Conf.objects.create( |
| 21 | + configuration_name="Product API GitHub configuration", |
| 22 | + api_key="not-a-real-secret", |
| 23 | + ) |
| 24 | + |
| 25 | + def test_create_product_with_github_settings(self): |
| 26 | + response = self.client.post( |
| 27 | + self.url, |
| 28 | + { |
| 29 | + "name": "Product with GitHub settings", |
| 30 | + "description": "Created through the product API", |
| 31 | + "prod_type": 1, |
| 32 | + "github_project": "DefectDojo/django-DefectDojo", |
| 33 | + "github_configuration": self.github_configuration.id, |
| 34 | + }, |
| 35 | + format="json", |
| 36 | + ) |
| 37 | + |
| 38 | + self.assertEqual(201, response.status_code, response.content[:1000]) |
| 39 | + github_settings = GITHUB_PKey.objects.get(product_id=response.data["id"]) |
| 40 | + self.assertEqual("DefectDojo/django-DefectDojo", github_settings.git_project) |
| 41 | + self.assertEqual(self.github_configuration, github_settings.git_conf) |
| 42 | + |
| 43 | + def test_create_product_without_github_settings(self): |
| 44 | + response = self.client.post( |
| 45 | + self.url, |
| 46 | + { |
| 47 | + "name": "Product without GitHub settings", |
| 48 | + "description": "Created through the product API", |
| 49 | + "prod_type": 1, |
| 50 | + }, |
| 51 | + format="json", |
| 52 | + ) |
| 53 | + |
| 54 | + self.assertEqual(201, response.status_code, response.content[:1000]) |
| 55 | + self.assertFalse(GITHUB_PKey.objects.filter(product_id=response.data["id"]).exists()) |
| 56 | + |
| 57 | + def test_create_product_rejects_unknown_github_configuration(self): |
| 58 | + product_name = "Product with unknown GitHub configuration" |
| 59 | + response = self.client.post( |
| 60 | + self.url, |
| 61 | + { |
| 62 | + "name": product_name, |
| 63 | + "description": "Created through the product API", |
| 64 | + "prod_type": 1, |
| 65 | + "github_project": "DefectDojo/django-DefectDojo", |
| 66 | + "github_configuration": 999999, |
| 67 | + }, |
| 68 | + format="json", |
| 69 | + ) |
| 70 | + |
| 71 | + self.assertEqual(400, response.status_code, response.content[:1000]) |
| 72 | + self.assertFalse(Product.objects.filter(name=product_name).exists()) |
0 commit comments