Beispiel #1
0
class TestPreferencesData(TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
        tracemalloc.start()

    def setUp(self) -> None:
        self.secret = os.urandom(32)
        self.dir = TemporaryDirectory()
        self.home = Path(self.dir.name)
        self.facade = Facade(self.home, self.secret,
            SetupPersonPortfolio().perform(PersonData(**Generate.church_data()[0])),
            role=Const.A_ROLE_PRIMARY, server=False
        )

    def tearDown(self) -> None:
        self.facade.close()
        self.dir.cleanup()

    def test___setitem__(self):
        self.facade.data.prefs["AnimalCount"] = True

    def test___getitem__(self):
        self.facade.data.prefs["AnimalCount"] = True
        self.facade.close()
        self.facade = None
        self.facade = Facade(self.home, self.secret)
        self.assertTrue(self.facade.data.prefs["AnimalCount"])
Beispiel #2
0
class TestPortfolioData(TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
        tracemalloc.start()

    def setUp(self) -> None:
        self.secret = os.urandom(32)
        self.dir = TemporaryDirectory()
        self.home = Path(self.dir.name)
        self.facade = Facade(self.home,
                             self.secret,
                             SetupPersonPortfolio().perform(
                                 PersonData(**Generate.person_data()[0])),
                             role=Const.A_ROLE_PRIMARY,
                             server=False)

    def tearDown(self) -> None:
        self.facade.close()
        self.dir.cleanup()

    def test_node(self):
        self.assertIsInstance(self.facade.data.portfolio, PrivatePortfolio)
        self.assertIsNotNone(self.facade.data.portfolio.entity)
        self.assertIsInstance(self.facade.data.portfolio.entity.id, uuid.UUID)
        self.assertIn(self.facade.data.portfolio.node,
                      self.facade.data.portfolio.nodes)
Beispiel #3
0
 def setUp(self) -> None:
     self.secret = os.urandom(32)
     self.dir = TemporaryDirectory()
     self.home = Path(self.dir.name)
     self.facade = Facade(self.home, self.secret,
         SetupPersonPortfolio().perform(PersonData(**Generate.church_data()[0])),
         role=Const.A_ROLE_PRIMARY, server=False
     )
Beispiel #4
0
 def __init__(self,
              portfolio: PrivatePortfolio,
              server: bool,
              admin: Signer = False):
     self.dir = TemporaryDirectory()
     self.secret = Generate.new_secret()
     if admin:
         self.facade = AdminFacade.setup(admin)
     else:
         self.facade = Facade(Path(self.dir.name), self.secret, portfolio,
                              Const.A_ROLE_PRIMARY, server)
Beispiel #5
0
class TestSettingsAPI(TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
        tracemalloc.start()

    def setUp(self) -> None:
        self.secret = os.urandom(32)
        self.dir = TemporaryDirectory()
        self.home = Path(self.dir.name)
        self.facade = Facade(self.home,
                             self.secret,
                             SetupPersonPortfolio().perform(
                                 PersonData(**Generate.person_data()[0])),
                             role=Const.A_ROLE_PRIMARY,
                             server=False)

    def tearDown(self) -> None:
        self.facade.close()
        self.dir.cleanup()

    @run_async
    async def test_load_preferences(self):
        await self.facade.api.settings.load_preferences()

    @run_async
    async def test_save_preferences(self):
        self.assertTrue(await self.facade.api.settings.save_preferences())

    @run_async
    async def test_load_set(self):
        data = {("roland", str(uuid.uuid4()), 1),
                ("bertil", str(uuid.uuid4()), 2),
                ("august", str(uuid.uuid4()), 3)}
        await self.facade.api.settings.save_set("alerts.csv", data)
        reloaded = await self.facade.api.settings.load_set("alerts.csv")
        self.assertEqual(data, reloaded)

    @run_async
    async def test_save_set(self):
        data = {("roland", str(uuid.uuid4()), 1),
                ("bertil", str(uuid.uuid4()), 2),
                ("august", str(uuid.uuid4()), 3)}
        self.assertTrue(await
                        self.facade.api.settings.save_set("alerts.csv", data))

    @run_async
    async def test_networks(self):
        await self.facade.api.settings.networks()
    async def _setup(cls, app_cls, portfolio: PrivatePortfolio):
        """Set up stub application environment."""
        secret = Generate.new_secret()
        tmp_dir = TemporaryDirectory()

        app = app_cls(Facade(Path(tmp_dir.name), secret, portfolio, Const.A_ROLE_PRIMARY, app_cls.STUB_SERVER))
        return cls(tmp_dir, secret, app)
class TestPoolStorage(TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
        tracemalloc.start()

    def setUp(self) -> None:
        self.secret = os.urandom(32)
        self.dir = TemporaryDirectory()
        self.home = Path(self.dir.name)
        self.facade = Facade(self.home, self.secret,
            SetupChurchPortfolio().perform(ChurchData(**Generate.church_data()[0])),
            role=Const.A_ROLE_PRIMARY, server=True
        )

    def tearDown(self) -> None:
        self.facade.close()
        self.dir.cleanup()

    def test_run(self):
        self.assertIsInstance(self.facade.storage.pool, PoolStorage)
Beispiel #8
0
    async def create(cls, home_dir: Path, secret: bytes, data: BaseData) -> TypeFacadeMixin:
        """Implement facade generation logic here."""
        if isinstance(data, PersonData):
            portfolio = SetupPersonPortfolio.perform(data, server=cls.STUB_SERVER)
        elif isinstance(data, MinistryData):
            portfolio = SetupMinistryPortfolio.perform(data, server=cls.STUB_SERVER)
        elif isinstance(data, ChurchData):
            portfolio = SetupChurchPortfolio.perform(data, server=cls.STUB_SERVER)
        else:
            raise TypeError()

        return Facade(home_dir, secret, Const.A_ROLE_PRIMARY, cls.STUB_SERVER, portfolio=portfolio)
Beispiel #9
0
class FacadeContext:
    """Environmental context for a facade."""
    def __init__(self,
                 portfolio: PrivatePortfolio,
                 server: bool,
                 admin: Signer = False):
        self.dir = TemporaryDirectory()
        self.secret = Generate.new_secret()
        if admin:
            self.facade = AdminFacade.setup(admin)
        else:
            self.facade = Facade(Path(self.dir.name), self.secret, portfolio,
                                 Const.A_ROLE_PRIMARY, server)

    def __del__(self):
        self.facade.close()
        self.dir.cleanup()

    @classmethod
    def create_server(cls) -> "FacadeContext":
        """Create a stub server."""
        return cls(
            SetupChurchPortfolio().perform(
                ChurchData(**Generate.church_data()[0]), server=True), True)

    @classmethod
    def create_client(cls) -> "FacadeContext":
        """Create a stub client."""
        return cls(
            SetupPersonPortfolio().perform(
                PersonData(**Generate.person_data()[0]), server=False), False)

    @classmethod
    def create_admin(
        cls,
        signer: Signer = Signer(NaCl.random_bytes(32))) -> "FacadeContext":
        """Create a stub admin."""
        return cls(None, server=False, admin=signer)
    async def test_facade(self):
        facade = Facade(self.home,
                        self.secret,
                        SetupMinistryPortfolio().perform(
                            MinistryData(**Generate.ministry_data()[0])),
                        role=Const.A_ROLE_PRIMARY,
                        server=False)
        self.services_client(facade)
        self.assertIsInstance(facade, MinistryClientFacade)
        self.assertIsInstance(facade.data.portfolio.entity, Ministry)
        self.assertIsInstance(facade.data.portfolio.node, Node)
        self.extension_type(facade)
        facade.close()

        facade = Facade(self.home, self.secret)
        self.assertIsInstance(facade.data.portfolio.entity, Ministry)
        self.assertIsInstance(facade.data.portfolio.node, Node)
        self.extension_type(facade)
        facade.close()
    async def test_facade(self):
        facade = Facade(self.home,
                        self.secret,
                        SetupChurchPortfolio().perform(
                            ChurchData(**Generate.church_data()[0])),
                        role=Const.A_ROLE_PRIMARY,
                        server=True)
        self.services_server(facade)
        self.assertIsInstance(facade, ChurchServerFacade)
        self.assertIsInstance(facade.data.portfolio.entity, Church)
        self.assertIsInstance(facade.data.portfolio.node, Node)
        self.extension_type(facade)
        facade.close()

        facade = Facade(self.home, self.secret)
        self.assertIsInstance(facade.data.portfolio.entity, Church)
        self.assertIsInstance(facade.data.portfolio.node, Node)
        self.extension_type(facade)
        facade.close()
Beispiel #12
0
 async def __setup(cls, operation, generator, home: Path, secret: bytes, server: bool):
     return Facade(home, secret, Const.A_ROLE_PRIMARY, server,
         portfolio=operation.perform(generator()[0], server=server))
Beispiel #13
0
 async def open(cls, home_dir: Path, secret: bytes) -> TypeFacadeMixin:
     return Facade(home_dir, secret)
Beispiel #14
0
 def test___getitem__(self):
     self.facade.data.prefs["AnimalCount"] = True
     self.facade.close()
     self.facade = None
     self.facade = Facade(self.home, self.secret)
     self.assertTrue(self.facade.data.prefs["AnimalCount"])
Beispiel #15
0
 def test_setup(self):
     self.facade.close()  # Setup is done at facade creation, closing and opening asserts.
     self.facade = Facade(self.home, self.secret)
Beispiel #16
0
class TestVaultStorage(TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
        tracemalloc.start()

    def setUp(self) -> None:
        self.secret = os.urandom(32)
        self.dir = TemporaryDirectory()
        self.home = Path(self.dir.name)
        self.facade = Facade(self.home, self.secret,
            SetupPersonPortfolio().perform(PersonData(**Generate.person_data()[0])),
            role=Const.A_ROLE_PRIMARY, server=False
        )

    def tearDown(self) -> None:
        self.facade.close()
        self.dir.cleanup()

    def test_setup(self):
        self.facade.close()  # Setup is done at facade creation, closing and opening asserts.
        self.facade = Facade(self.home, self.secret)

    @run_async
    async def test_save(self):
        names = [PurePosixPath("/", Generate.filename(".doc")) for _ in range(10)]
        for filename in names:
            self.assertIsInstance(await self.facade.storage.vault.save(filename, StubDocument()), uuid.UUID)

    @run_async
    async def test_delete(self):
        names = [PurePosixPath("/", Generate.filename(".doc")) for _ in range(10)]
        for filename in names:
            await self.facade.storage.vault.save(filename, StubDocument())

        for filename in names:
            await self.facade.storage.vault.delete(filename)
            self.assertTrue(await self.facade.storage.vault.archive.isfile(filename))

    @run_async
    async def test_link(self):
        names = [PurePosixPath("/", Generate.filename(".doc")) for _ in range(10)]
        for filename in names:
            await self.facade.storage.vault.save(filename, StubDocument())

        for filename in names:
            self.assertIsInstance(await self.facade.storage.vault.link(
                PurePosixPath("/", Generate.filename(".doc")), filename), uuid.UUID)

    @run_async
    async def test_update(self):
        names = [PurePosixPath("/", Generate.filename(".doc")) for _ in range(10)]
        for filename in names:
            await self.facade.storage.vault.save(filename, StubDocument())

        for filename in names:
            self.assertIsInstance(await self.facade.storage.vault.update(filename, StubDocument()), uuid.UUID)

    @run_async
    async def test_issuer(self):
        with self.assertRaises(DeprecationWarning):
            await self.facade.storage.vault.issuer(uuid.uuid4())

    @run_async
    async def test_search(self):
        names = [PurePosixPath("/", Generate.filename(".doc")) for _ in range(10)]
        for filename in names:
            await self.facade.storage.vault.save(filename, StubDocument())

        paths = (await self.facade.storage.vault.search()).values()
        for filename in names:
            self.assertIn(filename, paths)

    @run_async
    async def test_search_docs(self):
        with self.assertRaises(DeprecationWarning):
            await self.facade.storage.vault.search_docs(self.facade.data.portfolio.entity.id)

    @run_async
    async def test_save_settings(self):
        settings = io.StringIO(Generate.lipsum().decode())
        await self.facade.storage.vault.save_settings("monkeys.csv", settings)

    @run_async
    async def test_load_settings(self):
        settings = io.StringIO(Generate.lipsum().decode())
        await self.facade.storage.vault.save_settings("monkeys.csv", settings)
        data = await self.facade.storage.vault.load_settings("monkeys.csv")
        self.assertEqual(settings.getvalue(), data.getvalue())
Beispiel #17
0
class TestMailStorage(TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
        tracemalloc.start()

    def setUp(self) -> None:
        self.secret = os.urandom(32)
        self.dir = TemporaryDirectory()
        self.home = Path(self.dir.name)
        self.facade = Facade(self.home,
                             self.secret,
                             SetupChurchPortfolio().perform(
                                 ChurchData(**Generate.church_data()[0])),
                             role=Const.A_ROLE_PRIMARY,
                             server=True)

    def tearDown(self) -> None:
        self.facade.close()
        self.dir.cleanup()

    @run_async
    async def test_save(self):
        names = [
            PurePosixPath("/", Generate.filename(".doc")) for _ in range(10)
        ]
        for filename in names:
            self.assertIsInstance(
                await self.facade.storage.mail.save(filename, StubDocument()),
                uuid.UUID)

    @run_async
    async def test_delete(self):
        names = [
            PurePosixPath("/", Generate.filename(".doc")) for _ in range(10)
        ]
        for filename in names:
            await self.facade.storage.mail.save(filename, StubDocument())

        for filename in names:
            await self.facade.storage.mail.delete(filename)
            self.assertTrue(await
                            self.facade.storage.mail.archive.isfile(filename))

    @run_async
    async def test_update(self):
        names = [
            PurePosixPath("/", Generate.filename(".doc")) for _ in range(10)
        ]
        for filename in names:
            await self.facade.storage.mail.save(filename, StubDocument())

        for filename in names:
            self.assertIsInstance(
                await self.facade.storage.mail.update(filename,
                                                      StubDocument()),
                uuid.UUID)

    @run_async
    async def test_issuer(self):
        with self.assertRaises(DeprecationWarning):
            await self.facade.storage.mail.issuer(uuid.uuid4())

    @run_async
    async def test_search(self):
        names = [
            PurePosixPath("/", Generate.filename(".doc")) for _ in range(10)
        ]
        for filename in names:
            await self.facade.storage.mail.save(filename, StubDocument())

        paths = (await self.facade.storage.mail.search()).values()
        for filename in names:
            self.assertIn(filename, paths)