<commit_before># -*- coding: utf-8 -*-
import os
import io
import pytest
import re
import shutil
import store
import unittest
import zipfile

from flask import current_app

os.environ['SECUREDROP_ENV'] = 'test'  # noqa
from sdconfig import config
import journalist_app
import utils

from store import Storage


def test_path_returns_filename_of_folder(journalist_app, config):
    """`Storage.path` is called in this way in
        journalist.delete_collection
    """
    filesystem_id = 'example'
    generated_absolute_path = journalist_app.storage.path(filesystem_id)

    expected_absolute_path = os.path.join(config.STORE_DIR, filesystem_id)
    assert generated_absolute_path == expected_absolute_path


def test_path_returns_filename_of_items_within_folder(journalist_app, config):
    """`Storage.path` is called in this way in journalist.bulk_delete"""
    filesystem_id = 'example'
    item_filename = '1-quintuple_cant-msg.gpg'
    generated_absolute_path = journalist_app.storage.path(filesystem_id,
                                                          item_filename)

    expected_absolute_path = os.path.join(config.STORE_DIR,
                                          filesystem_id, item_filename)
    assert generated_absolute_path == expected_absolute_path


def test_verify_path_not_absolute(journalist_app, config):
    with pytest.raises(store.PathException):
        journalist_app.storage.verify(
            os.path.join(config.STORE_DIR, '..', 'etc', 'passwd'))


def test_verify_in_store_dir(journalist_app, config):
    with pytest.raises(store.PathException) as e:
        journalist_app.storage.verify(config.STORE_DIR + "_backup")

    assert 'Invalid directory' in str(e)


def test_verify_store_path_not_absolute(journalist_app):
    with pytest.raises(store.PathException) as e:
        journalist_app.storage.verify('..')

    assert 'The path is not absolute and/or normalized' in str(e)


class TestStore(unittest.TestCase):

    """The set of tests for store.py."""

    def setUp(self):
        self.__context = journalist_app.create_app(config).app_context()
        self.__context.push()
        utils.env.setup()

    def tearDown(self):
        utils.env.teardown()
        self.__context.pop()

    def create_file_in_source_dir(self, filesystem_id, filename):
        """Helper function for simulating files"""
        source_directory = os.path.join(config.STORE_DIR,
                                        filesystem_id)
        os.makedirs(source_directory)

        file_path = os.path.join(source_directory, filename)
        with io.open(file_path, 'a'):
            os.utime(file_path, None)

        return source_directory, file_path

    def test_verify_store_dir_not_absolute(self):
        with pytest.raises(store.PathException) as exc_info:
            Storage('..', '/', '<not a gpg key>')

        msg = str(exc_info.value)
        assert re.compile('storage_path.*is not absolute').match(msg)

    def test_verify_store_temp_dir_not_absolute(self):
        with pytest.raises(store.PathException) as exc_info:
            Storage('/', '..', '<not a gpg key>')

        msg = str(exc_info.value)
        assert re.compile('temp_dir.*is not absolute').match(msg)

    def test_verify_flagged_file_in_sourcedir_returns_true(self):
        source_directory, file_path = self.create_file_in_source_dir(
            'example-filesystem-id', '_FLAG'
        )
<commit_msg>pytest fixtures: test_verify_store_dir_not_absolute<commit_after># -*- coding: utf-8 -*-
import os
import io
import pytest
import re
import shutil
import store
import unittest
import zipfile

from flask import current_app

os.environ['SECUREDROP_ENV'] = 'test'  # noqa
from sdconfig import config
import journalist_app
import utils

from store import Storage


def test_path_returns_filename_of_folder(journalist_app, config):
    """`Storage.path` is called in this way in
        journalist.delete_collection
    """
    filesystem_id = 'example'
    generated_absolute_path = journalist_app.storage.path(filesystem_id)

    expected_absolute_path = os.path.join(config.STORE_DIR, filesystem_id)
    assert generated_absolute_path == expected_absolute_path


def test_path_returns_filename_of_items_within_folder(journalist_app, config):
    """`Storage.path` is called in this way in journalist.bulk_delete"""
    filesystem_id = 'example'
    item_filename = '1-quintuple_cant-msg.gpg'
    generated_absolute_path = journalist_app.storage.path(filesystem_id,
                                                          item_filename)

    expected_absolute_path = os.path.join(config.STORE_DIR,
                                          filesystem_id, item_filename)
    assert generated_absolute_path == expected_absolute_path


def test_verify_path_not_absolute(journalist_app, config):
    with pytest.raises(store.PathException):
        journalist_app.storage.verify(
            os.path.join(config.STORE_DIR, '..', 'etc', 'passwd'))


def test_verify_in_store_dir(journalist_app, config):
    with pytest.raises(store.PathException) as e:
        journalist_app.storage.verify(config.STORE_DIR + "_backup")

    assert 'Invalid directory' in str(e)


def test_verify_store_path_not_absolute(journalist_app):
    with pytest.raises(store.PathException) as e:
        journalist_app.storage.verify('..')

    assert 'The path is not absolute and/or normalized' in str(e)


def test_verify_store_dir_not_absolute():
    with pytest.raises(store.PathException) as exc_info:
        Storage('..', '/', '<not a gpg key>')

    msg = str(exc_info.value)
    assert re.compile('storage_path.*is not absolute').match(msg)


class TestStore(unittest.TestCase):

    """The set of tests for store.py."""

    def setUp(self):
        self.__context = journalist_app.create_app(config).app_context()
        self.__context.push()
        utils.env.setup()

    def tearDown(self):
        utils.env.teardown()
        self.__context.pop()

    def create_file_in_source_dir(self, filesystem_id, filename):
        """Helper function for simulating files"""
        source_directory = os.path.join(config.STORE_DIR,
                                        filesystem_id)
        os.makedirs(source_directory)

        file_path = os.path.join(source_directory, filename)
        with io.open(file_path, 'a'):
            os.utime(file_path, None)

        return source_directory, file_path

    def test_verify_store_temp_dir_not_absolute(self):
        with pytest.raises(store.PathException) as exc_info:
            Storage('/', '..', '<not a gpg key>')

        msg = str(exc_info.value)
        assert re.compile('temp_dir.*is not absolute').match(msg)

    def test_verify_flagged_file_in_sourcedir_returns_true(self):
        source_directory, file_path = self.create_file_in_source_dir(
            'example-filesystem-id', '_FLAG'
        )

{'fraction_non_alphanumeric': 0.09667102776566361, 'fraction_numerical': 0.0005814798662596307, 'mean_word_length': 3.1849148418491486, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4986968', 'n_tokens_mistral': 2088, 'n_tokens_neox': 2057, 'n_words': 416}