From ba18e1e34b1b8e427fb41f30c2ce25196f2c6793 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 27 Jan 2022 13:23:29 -0800 Subject: [PATCH 1/2] Adds tests for initdb command --- bookwyrm/management/commands/initdb.py | 18 ++-- bookwyrm/tests/management/test_initdb.py | 114 +++++++++++++++++++++++ 2 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 bookwyrm/tests/management/test_initdb.py diff --git a/bookwyrm/management/commands/initdb.py b/bookwyrm/management/commands/initdb.py index b63d7c5a6..09d864626 100644 --- a/bookwyrm/management/commands/initdb.py +++ b/bookwyrm/management/commands/initdb.py @@ -19,9 +19,7 @@ def init_permissions(): { "codename": "edit_instance_settings", "name": "change the instance info", - "groups": [ - "admin", - ], + "groups": ["admin"], }, { "codename": "set_user_group", @@ -66,15 +64,12 @@ def init_permissions(): for group_name in permission["groups"]: Group.objects.get(name=group_name).permissions.add(permission_obj) - # while the groups and permissions shouldn't be changed because the code - # depends on them, what permissions go with what groups should be editable - def init_connectors(): """access book data sources""" models.Connector.objects.create( identifier="bookwyrm.social", - name="BookWyrm dot Social", + name="Bookwyrm.social", connector_file="bookwyrm_connector", base_url="https://bookwyrm.social", books_url="https://bookwyrm.social/book", @@ -84,6 +79,7 @@ def init_connectors(): priority=2, ) + # pylint: disable=line-too-long models.Connector.objects.create( identifier="inventaire.io", name="Inventaire", @@ -127,7 +123,7 @@ def init_settings(): ) -def init_link_domains(*_): +def init_link_domains(): """safe book links""" domains = [ ("standardebooks.org", "Standard EBooks"), @@ -144,10 +140,15 @@ def init_link_domains(*_): ) +# pylint: disable=no-self-use +# pylint: disable=unused-argument class Command(BaseCommand): + """command-line options""" + help = "Initializes the database with starter data" def add_arguments(self, parser): + """specify which function to run""" parser.add_argument( "--limit", default=None, @@ -155,6 +156,7 @@ class Command(BaseCommand): ) def handle(self, *args, **options): + """execute init""" limit = options.get("limit") tables = [ "group", diff --git a/bookwyrm/tests/management/test_initdb.py b/bookwyrm/tests/management/test_initdb.py new file mode 100644 index 000000000..c94c45f3f --- /dev/null +++ b/bookwyrm/tests/management/test_initdb.py @@ -0,0 +1,114 @@ +""" test populating user streams """ +from django.contrib.auth.models import Group, Permission +from django.test import TestCase + +from bookwyrm import models +from bookwyrm.management.commands import initdb + + +class InitDB(TestCase): + """gotta init that db""" + + def test_init_groups(self): + """Create groups""" + initdb.init_groups() + self.assertEqual(Group.objects.count(), 3) + self.assertTrue(Group.objects.filter(name="admin").exists()) + self.assertTrue(Group.objects.filter(name="moderator").exists()) + self.assertTrue(Group.objects.filter(name="editor").exists()) + + def test_init_permissions(self): + """User permissions""" + initdb.init_groups() + initdb.init_permissions() + + group = Group.objects.get(name="admin") + self.assertTrue( + group.permissions.filter(codename="edit_instance_settings").exists() + ) + self.assertTrue(group.permissions.filter(codename="set_user_group").exists()) + self.assertTrue( + group.permissions.filter(codename="control_federation").exists() + ) + self.assertTrue(group.permissions.filter(codename="create_invites").exists()) + self.assertTrue(group.permissions.filter(codename="moderate_user").exists()) + self.assertTrue(group.permissions.filter(codename="moderate_post").exists()) + self.assertTrue(group.permissions.filter(codename="edit_book").exists()) + + group = Group.objects.get(name="moderator") + self.assertTrue(group.permissions.filter(codename="set_user_group").exists()) + self.assertTrue( + group.permissions.filter(codename="control_federation").exists() + ) + self.assertTrue(group.permissions.filter(codename="create_invites").exists()) + self.assertTrue(group.permissions.filter(codename="moderate_user").exists()) + self.assertTrue(group.permissions.filter(codename="moderate_post").exists()) + self.assertTrue(group.permissions.filter(codename="edit_book").exists()) + + group = Group.objects.get(name="editor") + self.assertTrue(group.permissions.filter(codename="edit_book").exists()) + + def test_init_connectors(self): + """Outside data sources""" + initdb.init_connectors() + self.assertTrue( + models.Connector.objects.filter(identifier="bookwyrm.social").exists() + ) + self.assertTrue( + models.Connector.objects.filter(identifier="inventaire.io").exists() + ) + self.assertTrue( + models.Connector.objects.filter(identifier="openlibrary.org").exists() + ) + + def test_init_settings(self): + """Create the settings file""" + initdb.init_settings() + settings = models.Settings.objects.get() + self.asesrtEqual(settings.instance_name, "BookWyrm") + + def test_init_link_domains(self): + """Common trusted domains for links""" + initdb.init_link_domains() + self.assertTrue( + models.LinkDomain.objects.filter( + status="approved", domain="standardebooks.org" + ).exists() + ) + self.assertTrue( + models.LinkDomain.objects.filter( + status="approved", domain="theanarchistlibrary.org" + ).exists() + ) + + def test_command_no_args(self): + """command line calls""" + command = initdb.Command() + command.handle() + + # everything should have been called + self.assertEqual(Group.objects.count(), 3) + self.assertTrue(Permission.objects.exists()) + self.assertEqual(models.Connector.objects.count(), 3) + self.assertEqual(models.FederatedServer.objects.count(), 2) + self.assertEqual(models.Settings.objects.count(), 1) + self.assertEqual(models.LinkDomain.objects.count(), 5) + + def test_command_with_args(self): + """command line calls""" + command = initdb.Command() + command.handle(limit="group") + + # everything should have been called + self.assertEqual(Group.objects.count(), 3) + self.assertEqual(Permission.objects.count(), 0) + self.assertEqual(models.Connector.objects.count(), 0) + self.assertEqual(models.FederatedServer.objects.count(), 0) + self.assertEqual(models.Settings.objects.count(), 0) + self.assertEqual(models.LinkDomain.objects.count(), 0) + + def test_command_invalid_args(self): + """command line calls""" + command = initdb.Command() + with self.assertRaises(Exception): + command.handle(limit="sdkfjhsdkjf") From d711c7a5a2c3011b1d61ef40aedcc56caaf7a14b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 27 Jan 2022 13:27:56 -0800 Subject: [PATCH 2/2] Test fixes --- bookwyrm/tests/management/test_initdb.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bookwyrm/tests/management/test_initdb.py b/bookwyrm/tests/management/test_initdb.py index c94c45f3f..e0a037160 100644 --- a/bookwyrm/tests/management/test_initdb.py +++ b/bookwyrm/tests/management/test_initdb.py @@ -64,8 +64,8 @@ class InitDB(TestCase): def test_init_settings(self): """Create the settings file""" initdb.init_settings() - settings = models.Settings.objects.get() - self.asesrtEqual(settings.instance_name, "BookWyrm") + settings = models.SiteSettings.objects.get() + self.assertEqual(settings.name, "BookWyrm") def test_init_link_domains(self): """Common trusted domains for links""" @@ -91,7 +91,7 @@ class InitDB(TestCase): self.assertTrue(Permission.objects.exists()) self.assertEqual(models.Connector.objects.count(), 3) self.assertEqual(models.FederatedServer.objects.count(), 2) - self.assertEqual(models.Settings.objects.count(), 1) + self.assertEqual(models.SiteSettings.objects.count(), 1) self.assertEqual(models.LinkDomain.objects.count(), 5) def test_command_with_args(self): @@ -101,10 +101,9 @@ class InitDB(TestCase): # everything should have been called self.assertEqual(Group.objects.count(), 3) - self.assertEqual(Permission.objects.count(), 0) self.assertEqual(models.Connector.objects.count(), 0) self.assertEqual(models.FederatedServer.objects.count(), 0) - self.assertEqual(models.Settings.objects.count(), 0) + self.assertEqual(models.SiteSettings.objects.count(), 0) self.assertEqual(models.LinkDomain.objects.count(), 0) def test_command_invalid_args(self):