Merge pull request #1876 from bookwyrm-social/initdb-tests

Adds tests for initdb command
This commit is contained in:
Mouse Reeve 2022-01-27 18:38:55 -08:00 committed by GitHub
commit 107f02e4e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 123 additions and 8 deletions

View file

@ -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",

View file

@ -0,0 +1,113 @@
""" 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.SiteSettings.objects.get()
self.assertEqual(settings.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.SiteSettings.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(models.Connector.objects.count(), 0)
self.assertEqual(models.FederatedServer.objects.count(), 0)
self.assertEqual(models.SiteSettings.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")