diff options
| author | 2023-04-22 11:01:51 +0100 | |
|---|---|---|
| committer | 2023-04-22 11:01:51 +0100 | |
| commit | 12b28f963223ab1a0e731bccae2c100e9c184914 (patch) | |
| tree | b9f2c6eb24c4f8fe4c5dd009e36b43853add06e2 /pydis_site/apps | |
| parent | Merge pull request #951 from python-discord/dependabot/pip/sqlparse-0.4.4 (diff) | |
| parent | Merge branch 'main' into rule-crosscheck (diff) | |
Merge pull request #950 from python-discord/rule-crosscheck
Crosscheck rules between API and static rules
Diffstat (limited to 'pydis_site/apps')
| -rw-r--r-- | pydis_site/apps/api/tests/test_rules.py | 38 | ||||
| -rw-r--r-- | pydis_site/apps/api/views.py | 2 | ||||
| -rw-r--r-- | pydis_site/apps/content/resources/rules.md | 2 | 
3 files changed, 40 insertions, 2 deletions
| diff --git a/pydis_site/apps/api/tests/test_rules.py b/pydis_site/apps/api/tests/test_rules.py index d08c5fae..3ee2d4e0 100644 --- a/pydis_site/apps/api/tests/test_rules.py +++ b/pydis_site/apps/api/tests/test_rules.py @@ -1,3 +1,7 @@ +import itertools +import re +from pathlib import Path +  from django.urls import reverse  from .base import AuthenticatedAPITestCase @@ -33,3 +37,37 @@ class RuleAPITests(AuthenticatedAPITestCase):          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) diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py index 54fbf809..b1b7dc0f 100644 --- a/pydis_site/apps/api/views.py +++ b/pydis_site/apps/api/views.py @@ -120,7 +120,7 @@ class RulesView(APIView):              link_format          )          discord_tos = self._format_link( -            'Terms Of Service', +            'Terms of Service',              'https://discordapp.com/terms',              link_format          ) diff --git a/pydis_site/apps/content/resources/rules.md b/pydis_site/apps/content/resources/rules.md index a0bdb38d..e55c6715 100644 --- a/pydis_site/apps/content/resources/rules.md +++ b/pydis_site/apps/content/resources/rules.md @@ -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. | 
