diff options
| author | 2019-08-22 20:54:26 +0200 | |
|---|---|---|
| committer | 2019-08-22 20:54:26 +0200 | |
| commit | 24bc7ced26a3d6aa8bd039f0b2cd95a6b5c85d5c (patch) | |
| tree | ef117853f53cd376f37ca409732fa2b6793e7af7 /pydis_site/apps/api | |
| parent | Making User.top_role test only query once (diff) | |
Fixing top_role bug for users without roles & adding appropriate test
Diffstat (limited to 'pydis_site/apps/api')
| -rw-r--r-- | pydis_site/apps/api/models/bot/user.py | 9 | ||||
| -rw-r--r-- | pydis_site/apps/api/tests/test_users.py | 74 | 
2 files changed, 58 insertions, 25 deletions
| diff --git a/pydis_site/apps/api/models/bot/user.py b/pydis_site/apps/api/models/bot/user.py index 8049d319..00c24d3d 100644 --- a/pydis_site/apps/api/models/bot/user.py +++ b/pydis_site/apps/api/models/bot/user.py @@ -54,5 +54,12 @@ class User(ModelReprMixin, models.Model):      @property      def top_role(self) -> Role: -        """Attribute that returns the user's top role.""" +        """ +        Attribute that returns the user's top role. + +        This will fall back to the Developers role if the user does not have any roles. +        """ +        roles = self.roles.all() +        if not roles: +            return Role.objects.get(name="Developers")          return max(self.roles.all()) diff --git a/pydis_site/apps/api/tests/test_users.py b/pydis_site/apps/api/tests/test_users.py index 1afba40d..bbdd3ff4 100644 --- a/pydis_site/apps/api/tests/test_users.py +++ b/pydis_site/apps/api/tests/test_users.py @@ -44,28 +44,6 @@ class CreationTests(APISubdomainTestCase):              permissions=0b01010010101,              position=1          ) -        cls.role_bottom = Role.objects.create( -            id=6, -            name="Low test role", -            colour=2, -            permissions=0b01010010101, -            position=0, -        ) -        cls.role_top = Role.objects.create( -            id=7, -            name="High test role", -            colour=2, -            permissions=0b01010010101, -            position=10, -        ) -        cls.role_test_user = User.objects.create( -            id=1, -            avatar_hash="coolavatarhash", -            name="Test User", -            discriminator=1111, -            in_guild=True, -        ) -        cls.role_test_user.roles.add(cls.role_bottom, cls.role_top)      def test_accepts_valid_data(self):          url = reverse('bot:user-list', host='api') @@ -143,8 +121,56 @@ class CreationTests(APISubdomainTestCase):          response = self.client.post(url, data=data)          self.assertEqual(response.status_code, 400) -    def test_correct_top_role_property(self): + +class UserModelTests(APISubdomainTestCase): +    @classmethod +    def setUpTestData(cls):  # noqa +        cls.role_top = Role.objects.create( +            id=777, +            name="High test role", +            colour=2, +            permissions=0b01010010101, +            position=10, +        ) +        cls.role_bottom = Role.objects.create( +            id=888, +            name="Low test role", +            colour=2, +            permissions=0b01010010101, +            position=1, +        ) +        cls.developers_role = Role.objects.create( +            id=1234567, +            name="Developers", +            colour=1234, +            permissions=0b01010010101, +            position=2, +        ) +        cls.user_with_roles = User.objects.create( +            id=1, +            avatar_hash="coolavatarhash", +            name="Test User with two roles", +            discriminator=1111, +            in_guild=True, +        ) +        cls.user_with_roles.roles.add(cls.role_bottom, cls.role_top) + +        cls.user_without_roles = User.objects.create( +            id=2, +            avatar_hash="coolavatarhash", +            name="Test User without roles", +            discriminator=2222, +            in_guild=True, +        ) + +    def test_correct_top_role_property_user_with_roles(self):          """Tests if the top_role property returns the correct role.""" -        top_role = self.role_test_user.top_role +        top_role = self.user_with_roles.top_role          self.assertIsInstance(top_role, Role)          self.assertEqual(top_role.id, self.role_top.id) + +    def test_correct_top_role_property_user_without_roles(self): +        """Tests if the top_role property returns the correct role.""" +        top_role = self.user_without_roles.top_role +        self.assertIsInstance(top_role, Role) +        self.assertEqual(top_role.id, self.developers_role.id) | 
