Skip to content

Commit 1d684ea

Browse files
Add GitHub product API settings
1 parent f15a141 commit 1d684ea

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

dojo/product/api/serializer.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from rest_framework import serializers
22
from rest_framework.exceptions import PermissionDenied
33

4+
from dojo.github.models import GITHUB_Conf, GITHUB_PKey
45
from dojo.models import DojoMeta, Product, Product_API_Scan_Configuration
56

67

@@ -19,6 +20,13 @@ class Meta:
1920
class ProductSerializer(serializers.ModelSerializer):
2021
findings_count = serializers.SerializerMethodField()
2122
findings_list = serializers.SerializerMethodField()
23+
github_project = serializers.CharField(allow_blank=True, max_length=200, required=False, write_only=True)
24+
github_configuration = serializers.PrimaryKeyRelatedField(
25+
allow_null=True,
26+
queryset=GITHUB_Conf.objects.all(),
27+
required=False,
28+
write_only=True,
29+
)
2230

2331
business_criticality = serializers.ChoiceField(choices=Product.BUSINESS_CRITICALITY_CHOICES, allow_blank=True, allow_null=True, required=False)
2432
platform = serializers.ChoiceField(choices=Product.PLATFORM_CHOICES, allow_blank=True, allow_null=True, required=False)
@@ -43,6 +51,18 @@ def get_fields(self):
4351
fields["tags"] = TagListSerializerField(required=False)
4452
return fields
4553

54+
def create(self, validated_data):
55+
github_project = validated_data.pop("github_project", "")
56+
github_configuration = validated_data.pop("github_configuration", None)
57+
product = super().create(validated_data)
58+
if github_project or github_configuration:
59+
GITHUB_PKey.objects.create(
60+
product=product,
61+
git_project=github_project,
62+
git_conf=github_configuration,
63+
)
64+
return product
65+
4666
def validate(self, data):
4767
async_updating = getattr(self.instance, "async_updating", None)
4868
if async_updating:
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)