<commit_before>import datetime
import json
import os

import pymongo
import requests


# load required envvars w/ the same name
SCITRAN_CORE_DRONE_SECRET = os.environ['SCITRAN_CORE_DRONE_SECRET']
SCITRAN_PERSISTENT_DB_URI = os.environ['SCITRAN_PERSISTENT_DB_URI']
SCITRAN_SITE_API_URL = os.environ['SCITRAN_SITE_API_URL']


class BaseUrlSession(requests.Session):
    """Requests session subclass using core api's base url"""
    def request(self, method, url, **kwargs):
        return super(BaseUrlSession, self).request(method, SCITRAN_SITE_API_URL + url, **kwargs)


def main():
    abao_user = '<EMAIL>'
    abao_api_key = '<KEY>'

    as_drone = BaseUrlSession()
    as_drone.headers.update({
        'X-SciTran-Method': 'bootstrapper',
        'X-SciTran-Name': 'Bootstrapper',
        'X-SciTran-Auth': SCITRAN_CORE_DRONE_SECRET,
    })

    as_drone.post('/users', json={
        '_id': abao_user,
        'firstname': 'Abao',
        'lastname': 'User',
        'root': True,
    })
    api_db = pymongo.MongoClient(SCITRAN_PERSISTENT_DB_URI).get_default_database()
    api_db.apikeys.insert_one({
        '_id': abao_api_key,
        'created': datetime.datetime.utcnow(),
        'last_seen': None,
        'type': 'user',
        'uid': abao_user
    })

    as_root = BaseUrlSession()
    as_root.headers.update({'Authorization': 'scitran-user {}'.format(abao_api_key)})
    as_root.params.update({'root': 'true'})

    # create scitran group
    r = as_root.post('/groups', json={'_id': 'scitran'})
    assert r.ok

    # create test-group
    r = as_root.post('/groups', json={'_id': 'test-group'})
    assert r.ok

    # upload file to test-project-1/test-session-1/test-acquisition-1
    # depends on 'create test-group'
    r = as_root.post('/upload/label', files={
        'file': ('test-1.dcm', open('test/integration_tests/abao/test_files/test-1.dcm', 'rb')),
        'metadata': ('', json.dumps({
            'group': { '_id': 'test-group' },
            'project': {
                'label': 'test-project-1'
            },
            'session': {
                'label': 'test-session-1',
                'subject': {
                    'age': 25,
                    'sex': 'male',
                    'firstname': 'xyz'
                }
            },
            'acquisition': {
                'label': 'test-acquisition-1',
                'files': [{ 'name': 'test-1.dcm' }]
            }
        }))
    })
    assert r.ok

    # list projects
    # depends on 'upload file to test-project-1/test-session-1/test-acquisition-1'
    r = as_root.get('/projects')
    assert r.ok
    assert r.json()[0]['label'] == 'test-project-1'
    test_project = r.json()[0]

    # list sessions
    # depends on 'upload file to test-project-1/test-session-1/test-acquisition-1'
    r = as_root.get('/sessions')
    assert r.ok
    assert r.json()[0]['label'] == 'test-session-1'
    test_session = r.json()[0]

    # list acquisitions for test-session-1
    # depends on 'upload file to test-project-1/test-session-1/test-acquisition-1'
    r = as_root.get('/sessions/' + test_session['_id'] + '/acquisitions')
    assert r.ok
    assert r.json()[0]['label'] == 'test-acquisition-1'
    test_acquisition = r.json()[0]

    # add test-case-gear
    r = as_root.post('/gears/test-case-gear', json={
        'category': 'converter',
        'gear': {
            'inputs': {
                'wat': {
                    'base': 'file',
                    'type': { 'enum': [ 'wat' ] }
                }
            },
            'maintainer': 'Example',
            'description': 'Example',
            'license': 'BSD-2-Clause',
            'author': 'Example',
            'url': 'https://example.example',
            'label': 'wat',
            'flywheel': '0',
            'source': 'https://example.example',
            'version': '0.0.1',
            'config': {},
            'name': 'test-case-gear'
        },
        'exchange': {
            'git-commit': 'aex',
            'rootfs-hash': 'sha384:oy',
            'rootfs-url': 'https://example.example'
        }
    })
    assert r.ok
    test_gear = r.json()
<commit_msg>Fix aboa fixture loading<commit_after>import datetime
import json
import os

import pymongo
import requests


# load required envvars w/ the same name
SCITRAN_CORE_DRONE_SECRET = os.environ['SCITRAN_CORE_DRONE_SECRET']
SCITRAN_PERSISTENT_DB_URI = os.environ['SCITRAN_PERSISTENT_DB_URI']
SCITRAN_SITE_API_URL = os.environ['SCITRAN_SITE_API_URL']


class BaseUrlSession(requests.Session):
    """Requests session subclass using core api's base url"""
    def request(self, method, url, **kwargs):
        return super(BaseUrlSession, self).request(method, SCITRAN_SITE_API_URL + url, **kwargs)


def main():
    abao_user = '<EMAIL>'
    abao_api_key = '<KEY>'

    as_drone = BaseUrlSession()
    as_drone.headers.update({
        'X-SciTran-Method': 'bootstrapper',
        'X-SciTran-Name': 'Bootstrapper',
        'X-SciTran-Auth': SCITRAN_CORE_DRONE_SECRET,
    })

    as_drone.post('/users', json={
        '_id': abao_user,
        'firstname': 'Abao',
        'lastname': 'User',
        'root': True,
    })
    api_db = pymongo.MongoClient(SCITRAN_PERSISTENT_DB_URI).get_default_database()
    api_db.apikeys.insert_one({
        '_id': abao_api_key,
        'created': datetime.datetime.utcnow(),
        'last_seen': None,
        'type': 'user',
        'uid': abao_user
    })

    as_root = BaseUrlSession()
    as_root.headers.update({'Authorization': 'scitran-user {}'.format(abao_api_key)})
    as_root.params.update({'root': 'true'})

    # create scitran group
    r = as_root.post('/groups', json={'_id': 'scitran'})
    assert r.ok

    # create test-group
    r = as_root.post('/groups', json={'_id': 'test-group'})
    assert r.ok

    # upload file to test-project-1/test-session-1/test-acquisition-1
    # depends on 'create test-group'
    r = as_root.post('/upload/label', files={
        'file': ('test-1.dcm', open('test/integration_tests/abao/test_files/test-1.dcm', 'rb')),
        'metadata': ('', json.dumps({
            'group': { '_id': 'test-group' },
            'project': {
                'label': 'test-project-1'
            },
            'session': {
                'label': 'test-session-1',
                'subject': {
                    'age': 25,
                    'sex': 'male',
                    'firstname': 'xyz'
                }
            },
            'acquisition': {
                'label': 'test-acquisition-1',
                'files': [{ 'name': 'test-1.dcm' }]
            }
        }))
    })
    assert r.ok

    # list projects
    # depends on 'upload file to test-project-1/test-session-1/test-acquisition-1'
    r = as_root.get('/projects')
    assert r.ok
    assert r.json()[0]['label'] == 'test-project-1'
    test_project = r.json()[0]

    # list sessions
    # depends on 'upload file to test-project-1/test-session-1/test-acquisition-1'
    r = as_root.get('/sessions')
    assert r.ok
    assert r.json()[0]['label'] == 'test-session-1'
    test_session = r.json()[0]

    # list acquisitions for test-session-1
    # depends on 'upload file to test-project-1/test-session-1/test-acquisition-1'
    r = as_root.get('/sessions/' + test_session['_id'] + '/acquisitions')
    assert r.ok
    assert r.json()[0]['label'] == 'test-acquisition-1'
    test_acquisition = r.json()[0]

    # add test-case-gear
    r = as_root.post('/gears/test-case-gear', json={
        'category': 'converter',
        'gear': {
            'inputs': {
                'dicom': {
                    'base': 'file',
                    'type': { 'enum': [ 'wat' ] }
                }
            },
            'maintainer': 'Example',
            'description': 'Example',
            'license': 'BSD-2-Clause',
            'author': 'Example',
            'url': 'https://example.example',
            'label': 'wat',
            'flywheel': '0',
            'source': 'https://example.example',
            'version': '0.0.1',
            'config': {},
            'name': 'test-case-gear'
        },
        'exchange': {
            'git-commit': 'aex',
            'rootfs-hash': 'sha384:oy',
            'rootfs-url': 'https://example.example'
        }
    })
    assert r.ok
    test_gear = r.json()

{'fraction_non_alphanumeric': 0.1656066242649706, 'fraction_numerical': 0.009120364814592583, 'mean_word_length': 2.091246290801187, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 6, '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': '5757559', 'n_tokens_mistral': 2864, 'n_tokens_neox': 2797, 'n_words': 501}