Skip to content

Commit

Permalink
Require issue url be github, jira, or launchpad
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-selo committed Sep 16, 2024
1 parent 7b886d4 commit dfa2c23
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
2 changes: 2 additions & 0 deletions backend/test_observer/common/constants.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
PREVIOUS_TEST_RESULT_COUNT = 10

VALID_ISSUE_HOSTS = {"github.com", "warthogs.atlassian.net", "bugs.launchpad.net"}
13 changes: 12 additions & 1 deletion backend/test_observer/controllers/environments/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
from datetime import datetime

from pydantic import BaseModel, HttpUrl
from pydantic import BaseModel, HttpUrl, field_validator

from test_observer.common.constants import VALID_ISSUE_HOSTS


class ReportedIssueRequest(BaseModel):
environment_name: str
description: str
url: HttpUrl

@field_validator("url")
@classmethod
def name_must_contain_space(
cls: type["ReportedIssueRequest"], url: HttpUrl
) -> HttpUrl:
if url.host not in VALID_ISSUE_HOSTS:
raise ValueError(f"Issue url must belong to one of {VALID_ISSUE_HOSTS}")
return url


class ReportedIssueResponse(BaseModel):
id: int
Expand Down
14 changes: 13 additions & 1 deletion backend/test_observer/controllers/test_cases/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from datetime import datetime

from pydantic import BaseModel, HttpUrl, model_validator
from pydantic import BaseModel, HttpUrl, field_validator, model_validator

from test_observer.common.constants import VALID_ISSUE_HOSTS


class ReportedIssueRequest(BaseModel):
Expand All @@ -13,8 +15,18 @@ class ReportedIssueRequest(BaseModel):
def check_a_or_b(self):
if not self.case_name and not self.template_id:
raise ValueError("Either case_name or template_id is required")

return self

@field_validator("url")
@classmethod
def name_must_contain_space(
cls: type["ReportedIssueRequest"], url: HttpUrl
) -> HttpUrl:
if url.host not in VALID_ISSUE_HOSTS:
raise ValueError(f"Issue url must belong to one of {VALID_ISSUE_HOSTS}")
return url


class ReportedIssueResponse(BaseModel):
id: int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
endpoint = "/v1/environments/reported-issues"
valid_post_data = {
"environment_name": "template 1",
"url": "http://issue.link/",
"url": "https://github.com/",
"description": "some description",
}

Expand All @@ -30,6 +30,17 @@ def test_post_validates_url(test_client: TestClient):
assert_fails_validation(response, "url", "url_parsing")


def test_url_cannot_be_canonical_chat(test_client: TestClient):
response = test_client.post(
endpoint,
json={
**valid_post_data,
"url": "https://chat.canonical.com/canonical/pl/n7oahef13jdpde7p6nf7s5yisw",
},
)
assert response.status_code == 422


def test_valid_post(test_client: TestClient):
response = test_client.post(endpoint, json=valid_post_data)
assert response.status_code == 200
Expand Down
12 changes: 11 additions & 1 deletion backend/tests/controllers/test_cases/test_reported_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
valid_post_data = {
"template_id": "template 1",
"case_name": "case",
"url": "http://issue.link/",
"url": "https://github.com/",
"description": "some description",
}

Expand Down Expand Up @@ -81,6 +81,16 @@ def test_post_validates_url(post: Post):
assert_fails_validation(response, "url", "url_parsing")


def test_url_cannot_be_canonical_chat(post: Post):
response = post(
{
**valid_post_data,
"url": "https://chat.canonical.com/canonical/pl/n7oahef13jdpde7p6nf7s5yisw",
}
)
assert response.status_code == 422


def test_valid_template_id_post(post: Post):
data = {k: v for k, v in valid_post_data.items() if k != "case_name"}
response = post(data)
Expand Down

0 comments on commit dfa2c23

Please sign in to comment.