from import call_command
from django.db import connection
from django.db.migrations.executor import MigrationExecutor

import pytest
import requests_mock
from graphene.test import Client as GrapheneClient
from rest_framework.test import APIClient

from normandy.schema import schema as normandy_schema
from normandy.base.tests import UserFactory
from import geolocation as geolocation_module
from import fake_sign

def api_client():
    """Fixture to provide a DRF API client."""
    user = UserFactory(is_superuser=True)
    client = APIClient()
    return client

def gql_client():
    """Fixture to provide a Graphene client."""
    client = GrapheneClient(normandy_schema)
    return client

def geolocation():
    """Fixture to load geolocation data."""
    if geolocation_module.geoip_reader is None:
        return geolocation_module

def mocked_autograph(mocker):
    mocked = mocker.patch("")
    mocked.return_value.sign_data.side_effect = fake_sign
    return mocked

def mocked_remotesettings(mocker):
    return mocker.patch("")

def rs_settings(settings):
    settings.REMOTE_SETTINGS_URL = ""
    settings.REMOTE_SETTINGS_USERNAME = "normandy"
    settings.REMOTE_SETTINGS_PASSWORD = "n0rm4ndy"
    return settings

def migrations(transactional_db):
    This fixture returns a helper object to test Django data migrations.
    Based on:

    class Migrator(object):
        def migrate(self, app, to):
            migration = [(app, to)]
            executor = MigrationExecutor(connection)
            return executor.loader.project_state(migration).apps

        def reset(self):
            call_command("migrate", no_input=True)

    return Migrator()

def requestsmock():
    """Return a context where requests are all mocked.

        def test_something(requestsmock):
                content=b'The content'
            # Do stuff that involves requests.get('')
    with requests_mock.mock() as m:
        yield m

def storage(settings):
    settings.DEFAULT_FILE_STORAGE = ""

    from import default_storage

    yield default_storage

    dirs_to_delete = ["/"]
    while len(dirs_to_delete) > 0:
        dir_path = dirs_to_delete.pop()
        paths, new_dirs = default_storage.listdir(dir_path)
        for path in paths: