Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/apps/owasp/management/commands/owasp_sync_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import re
from typing import Any

import yaml
import yaml.scanner
Expand Down Expand Up @@ -77,6 +78,7 @@ def handle(self, *args, **options) -> None:
if not post_content.startswith("---"):
continue

metadata: dict[str, Any] = {}
try:
if match := yaml_pattern.search(post_content):
metadata_yaml = match.group(1)
Expand Down
7 changes: 2 additions & 5 deletions backend/apps/owasp/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,8 @@ def get_leaders_emails(self):
)

for match in matches:
if match[0] and match[1]: # Name with email
leaders[match[0].strip()] = match[1].strip()
elif match[2]: # Name without email
leaders[match[2].strip()] = None

name, value = match[0].strip(), match[1].strip()
leaders[name] = value if "@" in value else None
return leaders

def get_metadata(self):
Expand Down
35 changes: 18 additions & 17 deletions backend/tests/apps/api/decorators/cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@
from apps.api.decorators.cache import cache_response, generate_key


@pytest.mark.parametrize(
("full_path", "prefix", "expected_key"),
[
("/api/test", "p1", "p1:/api/test"),
("/api/test?a=1", "p2", "p2:/api/test?a=1"),
("/api/test?a=1&b=2", "p3", "p3:/api/test?a=1&b=2"),
("/api/test?b=2&a=1", "p4", "p4:/api/test?b=2&a=1"),
],
)
def test_generate_cache_key(full_path, prefix, expected_key):
"""Test cases for the generate cache key function."""
request = HttpRequest()
parsed_url = urlparse(full_path)
request.path = parsed_url.path
request.META["QUERY_STRING"] = parsed_url.query

assert generate_key(request, prefix) == expected_key
class TestGenerateCacheKey:
@pytest.mark.parametrize(
("full_path", "prefix", "expected_key"),
[
("/api/test", "p1", "p1:/api/test"),
("/api/test?a=1", "p2", "p2:/api/test?a=1"),
("/api/test?a=1&b=2", "p3", "p3:/api/test?a=1&b=2"),
("/api/test?b=2&a=1", "p4", "p4:/api/test?b=2&a=1"),
],
)
def test_generate_cache_key(self, full_path, prefix, expected_key):
"""Test cases for the generate cache key function."""
request = HttpRequest()
parsed_url = urlparse(full_path)
request.path = parsed_url.path
request.META["QUERY_STRING"] = parsed_url.query

assert generate_key(request, prefix) == expected_key


class TestCacheResponse:
Expand Down
121 changes: 61 additions & 60 deletions backend/tests/apps/api/rest/v0/chapter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,67 @@
from apps.api.rest.v0.chapter import ChapterDetail, get_chapter, list_chapters


@pytest.mark.parametrize(
"chapter_data",
[
{
"country": "America",
"created_at": "2024-11-01T00:00:00Z",
"key": "nagoya",
"latitude": 35.1815,
"longitude": 136.9066,
"name": "OWASP Nagoya",
"region": "Europe",
"updated_at": "2024-07-02T00:00:00Z",
},
{
"country": "India",
"created_at": "2023-12-01T00:00:00Z",
"key": "something",
"latitude": 12.9716,
"longitude": 77.5946,
"name": "OWASP something",
"region": "Asia",
"updated_at": "2023-09-02T00:00:00Z",
},
],
)
def test_chapter_serializer_validation(chapter_data):
class MockMember:
def __init__(self, login):
self.login = login

class MockEntityMember:
def __init__(self, name, login=None):
self.member = MockMember(login) if login else None
self.member_name = name

class MockChapter:
def __init__(self, data):
for key, value in data.items():
setattr(self, key, value)
self.nest_key = data["key"]
self.entity_leaders = [
MockEntityMember("Alice", "alice"),
MockEntityMember("Bob"),
]

chapter = ChapterDetail.from_orm(MockChapter(chapter_data))

assert chapter.country == chapter_data["country"]
assert chapter.created_at == datetime.fromisoformat(chapter_data["created_at"])
assert chapter.key == chapter_data["key"]
assert chapter.latitude == chapter_data["latitude"]
assert chapter.longitude == chapter_data["longitude"]
assert len(chapter.leaders) == 2
assert chapter.leaders[0].key == "alice"
assert chapter.leaders[0].name == "Alice"
assert chapter.leaders[1].key is None
assert chapter.leaders[1].name == "Bob"
assert chapter.name == chapter_data["name"]
assert chapter.region == chapter_data["region"]
assert chapter.updated_at == datetime.fromisoformat(chapter_data["updated_at"])
class TestChapterSerializerValidation:
@pytest.mark.parametrize(
"chapter_data",
[
{
"country": "America",
"created_at": "2024-11-01T00:00:00Z",
"key": "nagoya",
"latitude": 35.1815,
"longitude": 136.9066,
"name": "OWASP Nagoya",
"region": "Europe",
"updated_at": "2024-07-02T00:00:00Z",
},
{
"country": "India",
"created_at": "2023-12-01T00:00:00Z",
"key": "something",
"latitude": 12.9716,
"longitude": 77.5946,
"name": "OWASP something",
"region": "Asia",
"updated_at": "2023-09-02T00:00:00Z",
},
],
)
def test_chapter_serializer_validation(self, chapter_data):
class MockMember:
def __init__(self, login):
self.login = login

class MockEntityMember:
def __init__(self, name, login=None):
self.member = MockMember(login) if login else None
self.member_name = name

class MockChapter:
def __init__(self, data):
for key, value in data.items():
setattr(self, key, value)
self.nest_key = data["key"]
self.entity_leaders = [
MockEntityMember("Alice", "alice"),
MockEntityMember("Bob"),
]

chapter = ChapterDetail.from_orm(MockChapter(chapter_data))

assert chapter.country == chapter_data["country"]
assert chapter.created_at == datetime.fromisoformat(chapter_data["created_at"])
assert chapter.key == chapter_data["key"]
assert chapter.latitude == chapter_data["latitude"]
assert chapter.longitude == chapter_data["longitude"]
assert len(chapter.leaders) == 2
assert chapter.leaders[0].key == "alice"
assert chapter.leaders[0].name == "Alice"
assert chapter.leaders[1].key is None
assert chapter.leaders[1].name == "Bob"
assert chapter.name == chapter_data["name"]
assert chapter.region == chapter_data["region"]
assert chapter.updated_at == datetime.fromisoformat(chapter_data["updated_at"])


class TestListChapters:
Expand Down
67 changes: 34 additions & 33 deletions backend/tests/apps/api/rest/v0/committee_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,40 @@
from apps.api.rest.v0.committee import CommitteeDetail, get_committee, list_committees


@pytest.mark.parametrize(
"committee_data",
[
{
"created_at": "2024-11-01T00:00:00Z",
"description": "A test committee",
"key": "test-committee",
"name": "Test Committee",
"updated_at": "2024-07-02T00:00:00Z",
},
{
"created_at": "2023-12-01T00:00:00Z",
"description": "A committee without a name",
"key": "this-is-a-committee",
"name": "this is a committee",
"updated_at": "2023-09-02T00:00:00Z",
},
],
)
def test_committee_serializer_validation(committee_data):
class MockCommittee:
def __init__(self, data):
for key, value in data.items():
setattr(self, key, value)
self.nest_key = data["key"]

committee = CommitteeDetail.from_orm(MockCommittee(committee_data))

assert committee.created_at == datetime.fromisoformat(committee_data["created_at"])
assert committee.description == committee_data["description"]
assert committee.key == committee_data["key"]
assert committee.name == committee_data["name"]
assert committee.updated_at == datetime.fromisoformat(committee_data["updated_at"])
class TestCommitteeSerializerValidation:
@pytest.mark.parametrize(
"committee_data",
[
{
"created_at": "2024-11-01T00:00:00Z",
"description": "A test committee",
"key": "test-committee",
"name": "Test Committee",
"updated_at": "2024-07-02T00:00:00Z",
},
{
"created_at": "2023-12-01T00:00:00Z",
"description": "A committee without a name",
"key": "this-is-a-committee",
"name": "this is a committee",
"updated_at": "2023-09-02T00:00:00Z",
},
],
)
def test_committee_serializer_validation(self, committee_data):
class MockCommittee:
def __init__(self, data):
for key, value in data.items():
setattr(self, key, value)
self.nest_key = data["key"]

committee = CommitteeDetail.from_orm(MockCommittee(committee_data))

assert committee.created_at == datetime.fromisoformat(committee_data["created_at"])
assert committee.description == committee_data["description"]
assert committee.key == committee_data["key"]
assert committee.name == committee_data["name"]
assert committee.updated_at == datetime.fromisoformat(committee_data["updated_at"])


class TestListCommittees:
Expand Down
47 changes: 24 additions & 23 deletions backend/tests/apps/api/rest/v0/common_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
from apps.api.rest.v0.common import LocationFilter


@pytest.mark.parametrize(
"location_data",
[
{
"latitude_gte": -5.0,
"latitude_lte": 5.0,
"longitude_gte": -10.0,
"longitude_lte": 10.0,
},
{
"latitude_gte": 15.0,
"latitude_lte": 25.0,
"longitude_gte": 20.0,
"longitude_lte": 30.0,
},
],
)
def test_location_filter_validation(location_data):
location_filter = LocationFilter(**location_data)
class TestLocationFilter:
@pytest.mark.parametrize(
"location_data",
[
{
"latitude_gte": -5.0,
"latitude_lte": 5.0,
"longitude_gte": -10.0,
"longitude_lte": 10.0,
},
{
"latitude_gte": 15.0,
"latitude_lte": 25.0,
"longitude_gte": 20.0,
"longitude_lte": 30.0,
},
],
)
def test_location_filter_validation(self, location_data):
location_filter = LocationFilter(**location_data)

assert location_filter.latitude_gte == location_data["latitude_gte"]
assert location_filter.latitude_lte == location_data["latitude_lte"]
assert location_filter.longitude_gte == location_data["longitude_gte"]
assert location_filter.longitude_lte == location_data["longitude_lte"]
assert location_filter.latitude_gte == location_data["latitude_gte"]
assert location_filter.latitude_lte == location_data["latitude_lte"]
assert location_filter.longitude_gte == location_data["longitude_gte"]
assert location_filter.longitude_lte == location_data["longitude_lte"]
Loading