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
38 changes: 38 additions & 0 deletions pydis_site/apps/api/tests/test_rules.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import itertools
import re
from pathlib import Path

from django.urls import reverse

from .base import AuthenticatedAPITestCase
Expand Down Expand Up @@ -33,3 +37,37 @@ def test_get_returns_400_for_wrong_link_format(self):
url = reverse('api:rules')
response = self.client.get(url + '?link_format=unknown')
self.assertEqual(response.status_code, 400)


class RuleCorrectnessTests(AuthenticatedAPITestCase):
"""Verifies that the rules from the API and by the static rules in the content app match."""

@classmethod
def setUpTestData(cls):
cls.markdown_rule_re = re.compile(r'^> \d+\. (.*)$')

def test_rules_in_markdown_file_roughly_equal_api_rules(self) -> None:
url = reverse('api:rules')
api_response = self.client.get(url + '?link_format=md')
api_rules = tuple(rule for (rule, _tags) in api_response.json())

markdown_rules_path = (
Path(__file__).parent.parent.parent / 'content' / 'resources' / 'rules.md'
)

markdown_rules = []
for line in markdown_rules_path.read_text().splitlines():
matches = self.markdown_rule_re.match(line)
if matches is not None:
markdown_rules.append(matches.group(1))

zipper = itertools.zip_longest(api_rules, markdown_rules)
for idx, (api_rule, markdown_rule) in enumerate(zipper):
with self.subTest(f"Rule {idx}"):
self.assertIsNotNone(
markdown_rule, f"The API has more rules than {markdown_rules_path}"
)
self.assertIsNotNone(
api_rule, f"{markdown_rules_path} has more rules than the API endpoint"
)
self.assertEqual(markdown_rule, api_rule)
2 changes: 1 addition & 1 deletion pydis_site/apps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def get(self, request, format=None): # noqa: D102,ANN001,ANN201
link_format
)
discord_tos = self._format_link(
'Terms Of Service',
'Terms of Service',
'https://discordapp.com/terms',
link_format
)
Expand Down
2 changes: 1 addition & 1 deletion pydis_site/apps/content/resources/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ icon: fab fa-discord
---
We have a small but strict set of rules on our server. Please read over them and take them on board. If you don't understand a rule or need to report an incident, please send a direct message to <code>@ModMail</code>!

> 1. Follow the [Python Discord Code of Conduct](/pages/code-of-conduct/).
> 1. Follow the [Python Discord Code of Conduct](https://pythondiscord.com/pages/code-of-conduct/).
> 2. Follow the [Discord Community Guidelines](https://discordapp.com/guidelines) and [Terms of Service](https://discordapp.com/terms).
> 3. Respect staff members and listen to their instructions.
> 4. Use English to the best of your ability. Be polite if someone speaks English imperfectly.
Expand Down