<commit_before>import pytest

from test.testexception import AuthenticationError, UnprocessableError


class TestDeviceQuery:
    def test_query(self, helper):
        admin_user = helper.admin_user()
        clare = helper.given_new_user(self, "clare")
        clares_group = helper.make_unique_group_name(self, "clares_group")
        clare.create_group(clares_group)
        terminator = helper.given_new_device(self, "Terminator", clares_group)
        terminator2 = helper.given_new_device(self, "Terminator", clares_group)

        gary = helper.given_new_user(self, "gary")
        gary_group = helper.make_unique_group_name(self, "garys_group")
        gary.create_group(gary_group)
        gary_device = helper.given_new_device(self, "Gary_device", gary_group)

        devices = clare.query_devices(devices=[terminator])
        ids = set([device["id"] for device in devices])
        assert ids == set([terminator.get_id()])

        devices = clare.query_devices(devices=[terminator, terminator2])
        ids = set([device["id"] for device in devices])
        assert ids == set([terminator.get_id(), terminator2.get_id()])

        devices = clare.query_devices(groups=[clares_group])
        ids = set([device["id"] for device in devices])
        assert ids == set([terminator.get_id(), terminator2.get_id()])

        devices = clare.query_devices(groups=[gary_group])
        ids = set([device["id"] for device in devices])
        assert ids == set()

        devices = gary.query_devices(devices=[terminator], groups=[gary_group, clares_group])
        ids = set([device["id"] for device in devices])
        assert ids == set([gary_device.get_id()])

        devices = admin_user.query_devices(devices=[terminator], groups=[gary_group, clares_group])
        ids = set([device["id"] for device in devices])
        assert ids == set([terminator.get_id(), terminator2.get_id(), gary_device.get_id()])

    def test_access(self, helper):
        clare = helper.given_new_user(self, "clare")
        clares_group = helper.make_unique_group_name(self, "clares_group")
        clare.create_group(clares_group)
        terminator = helper.given_new_device(self, "Terminator", clares_group)
        terminator2 = helper.given_new_device(self, "Terminator", clares_group)
        terminator3 = helper.given_new_device(self, "Terminator", clares_group)
        access = {"devices": "r"}
        clare.new_token(access, set_token=True)
        devices = clare.query_devices(devices=[terminator, terminator2])
        ids = [device["id"] for device in devices]
        assert ids == [terminator.get_id(), terminator2.get_id()]
        new_group = helper.make_unique_group_name(self, "clares_group")
        with pytest.raises(AuthenticationError):
            clare.create_group(new_group)

        devices = clare.query_devices(
            devices=[terminator, terminator2], groups=[clares_group], operator="AND"
        )
        ids = [device["id"] for device in devices]
        assert ids == [terminator.get_id(), terminator2.get_id()]
        with pytest.raises(UnprocessableError):
            devices = clare.query_devices(devices=[terminator, terminator2], operator="in")<commit_msg>Fixed sporadically failing device query tests<commit_after>import pytest

from test.testexception import AuthenticationError, UnprocessableError


class TestDeviceQuery:
    def test_query(self, helper):
        admin_user = helper.admin_user()
        clare = helper.given_new_user(self, "clare")
        clares_group = helper.make_unique_group_name(self, "clares_group")
        clare.create_group(clares_group)
        terminator = helper.given_new_device(self, "Terminator", clares_group)
        terminator2 = helper.given_new_device(self, "Terminator", clares_group)

        gary = helper.given_new_user(self, "gary")
        gary_group = helper.make_unique_group_name(self, "garys_group")
        gary.create_group(gary_group)
        gary_device = helper.given_new_device(self, "Gary_device", gary_group)

        devices = clare.query_devices(devices=[terminator])
        ids = set([device["id"] for device in devices])
        assert ids == set([terminator.get_id()])

        devices = clare.query_devices(devices=[terminator, terminator2])
        ids = set([device["id"] for device in devices])
        assert ids == set([terminator.get_id(), terminator2.get_id()])

        devices = clare.query_devices(groups=[clares_group])
        ids = set([device["id"] for device in devices])
        assert ids == set([terminator.get_id(), terminator2.get_id()])

        devices = clare.query_devices(groups=[gary_group])
        ids = set([device["id"] for device in devices])
        assert not ids

        devices = gary.query_devices(devices=[terminator], groups=[gary_group, clares_group])
        ids = set([device["id"] for device in devices])
        assert ids == set([gary_device.get_id()])

        devices = admin_user.query_devices(devices=[terminator], groups=[gary_group, clares_group])
        ids = set([device["id"] for device in devices])
        assert ids == set([terminator.get_id(), terminator2.get_id(), gary_device.get_id()])

    def test_access(self, helper):
        clare = helper.given_new_user(self, "clare")
        clares_group = helper.make_unique_group_name(self, "clares_group")
        clare.create_group(clares_group)
        terminator = helper.given_new_device(self, "Terminator", clares_group)
        terminator2 = helper.given_new_device(self, "Terminator", clares_group)
        terminator3 = helper.given_new_device(self, "Terminator", clares_group)
        access = {"devices": "r"}
        clare.new_token(access, set_token=True)
        devices = clare.query_devices(devices=[terminator, terminator2])
        ids = set([device["id"] for device in devices])
        assert ids == set([terminator.get_id(), terminator2.get_id()])
        new_group = helper.make_unique_group_name(self, "clares_group")
        with pytest.raises(AuthenticationError):
            clare.create_group(new_group)

        devices = clare.query_devices(
            devices=[terminator, terminator2], groups=[clares_group], operator="AND"
        )
        ids = set(device["id"] for device in devices)
        assert ids == set([terminator.get_id(), terminator2.get_id()])
        with pytest.raises(UnprocessableError):
            devices = clare.query_devices(devices=[terminator, terminator2], operator="in")
{'fraction_non_alphanumeric': 0.12184157494962021, 'fraction_numerical': 0.0037203534335761897, 'mean_word_length': 3.6517664023071377, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1749421', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1935, 'n_words': 370}