# Path: osf_tests/factories.py
# def get_default_metaschema():
#     """This needs to be a method so it gets called after the test database is set up"""
#     return models.RegistrationSchema.objects.first()
# 
# class DraftRegistrationFactory(DjangoModelFactory):
#     class Meta:
#         model = models.DraftRegistration
# 
#     @classmethod
#     def _create(cls, *args, **kwargs):
#         title = kwargs.pop('title', None)
#         initiator = kwargs.get('initiator', None)
#         description = kwargs.pop('description', None)
#         branched_from = kwargs.get('branched_from', None)
#         registration_schema = kwargs.get('registration_schema')
#         registration_metadata = kwargs.get('registration_metadata')
#         provider = kwargs.get('provider')
#         branched_from_creator = branched_from.creator if branched_from else None
#         initiator = initiator or branched_from_creator or kwargs.get('user', None) or kwargs.get('creator', None) or UserFactory()
#         registration_schema = registration_schema or get_default_metaschema()
#         registration_metadata = registration_metadata or {}
#         provider = provider or models.RegistrationProvider.get_default()
#         provider.schemas.add(registration_schema)
#         draft = models.DraftRegistration.create_from_node(
#             node=branched_from,
#             user=initiator,
#             schema=registration_schema,
#             data=registration_metadata,
#             provider=provider,
#         )
#         if title:
#             draft.title = title
#         if description:
#             draft.description = description
#         draft.registration_responses = draft.flatten_registration_metadata()
#         draft.save()
#         return draft


import mock
import pytest
from addons.wiki.models import WikiVersion
from django.core.exceptions import ValidationError
from django.utils import timezone
from framework.auth.core import Auth
from framework.exceptions import PermissionsError
from nose.tools import assert_raises
from osf.models import Node, Registration, Sanction, RegistrationSchema, NodeLog
from addons.wiki.models import WikiPage
from osf.utils.permissions import ADMIN
from osf.registrations.utils import get_registration_provider_submissions_url
from website import settings
from . import factories
from .utils import assert_datetime_equal, mock_archive
from osf_tests.factories import get_default_metaschema, DraftRegistrationFactory
from addons.wiki.tests.factories import WikiFactory, WikiVersionFactory
from api.providers.workflows import Workflows
from osf.migrations import update_provider_auth_groups
from osf.models.action import RegistrationAction
from osf_tests.management_commands.test_migration_registration_responses import (
    prereg_registration_responses,
    prereg_registration_metadata_built,
    veer_registration_responses,
    veer_condensed
)
from osf.utils.workflows import (
    RegistrationModerationStates,
    RegistrationModerationTriggers,
    ApprovalStates
)
@pytest.fixture()
def project(user, auth, fake):
    ret = factories.ProjectFactory(creator=user)
    ret.add_tag(fake.word(), auth=auth)
    return ret


@pytest.fixture()
def auth(user):
    return Auth(user)


# copied from tests/test_models.py
def test_factory(user, project):
    # Create a registration with kwargs
    registration1 = factories.RegistrationFactory(
        title='t1', description='d1', creator=user,
    )
    assert registration1.title == 't1'
    assert registration1.description == 'd1'
    assert registration1.contributors.count() == 1
    assert user in registration1.contributors.all()
    assert registration1.registered_user == user
    assert registration1.private_links.count() == 0

    # Create a registration from a project
    user2 = factories.UserFactory()
    project.add_contributor(user2)

    data = {'some': 'data'}

# Path: osf_tests/factories.py
# def get_default_metaschema():
#     """This needs to be a method so it gets called after the test database is set up"""
#     return models.RegistrationSchema.objects.first()
# 
# class DraftRegistrationFactory(DjangoModelFactory):
#     class Meta:
#         model = models.DraftRegistration
# 
#     @classmethod
#     def _create(cls, *args, **kwargs):
#         title = kwargs.pop('title', None)
#         initiator = kwargs.get('initiator', None)
#         description = kwargs.pop('description', None)
#         branched_from = kwargs.get('branched_from', None)
#         registration_schema = kwargs.get('registration_schema')
#         registration_metadata = kwargs.get('registration_metadata')
#         provider = kwargs.get('provider')
#         branched_from_creator = branched_from.creator if branched_from else None
#         initiator = initiator or branched_from_creator or kwargs.get('user', None) or kwargs.get('creator', None) or UserFactory()
#         registration_schema = registration_schema or get_default_metaschema()
#         registration_metadata = registration_metadata or {}
#         provider = provider or models.RegistrationProvider.get_default()
#         provider.schemas.add(registration_schema)
#         draft = models.DraftRegistration.create_from_node(
#             node=branched_from,
#             user=initiator,
#             schema=registration_schema,
#             data=registration_metadata,
#             provider=provider,
#         )
#         if title:
#             draft.title = title
#         if description:
#             draft.description = description
#         draft.registration_responses = draft.flatten_registration_metadata()
#         draft.save()
#         return draft


# Path: osf_tests/test_registrations.py
import mock
import pytest
from addons.wiki.models import WikiVersion
from django.core.exceptions import ValidationError
from django.utils import timezone
from framework.auth.core import Auth
from framework.exceptions import PermissionsError
from nose.tools import assert_raises
from osf.models import Node, Registration, Sanction, RegistrationSchema, NodeLog
from addons.wiki.models import WikiPage
from osf.utils.permissions import ADMIN
from osf.registrations.utils import get_registration_provider_submissions_url
from website import settings
from . import factories
from .utils import assert_datetime_equal, mock_archive
from osf_tests.factories import get_default_metaschema, DraftRegistrationFactory
from addons.wiki.tests.factories import WikiFactory, WikiVersionFactory
from api.providers.workflows import Workflows
from osf.migrations import update_provider_auth_groups
from osf.models.action import RegistrationAction
from osf_tests.management_commands.test_migration_registration_responses import (
    prereg_registration_responses,
    prereg_registration_metadata_built,
    veer_registration_responses,
    veer_condensed
)
from osf.utils.workflows import (
    RegistrationModerationStates,
    RegistrationModerationTriggers,
    ApprovalStates
)

@pytest.fixture()
def project(user, auth, fake):
    ret = factories.ProjectFactory(creator=user)
    ret.add_tag(fake.word(), auth=auth)
    return ret


@pytest.fixture()
def auth(user):
    return Auth(user)


# copied from tests/test_models.py
def test_factory(user, project):
    # Create a registration with kwargs
    registration1 = factories.RegistrationFactory(
        title='t1', description='d1', creator=user,
    )
    assert registration1.title == 't1'
    assert registration1.description == 'd1'
    assert registration1.contributors.count() == 1
    assert user in registration1.contributors.all()
    assert registration1.registered_user == user
    assert registration1.private_links.count() == 0

    # Create a registration from a project
    user2 = factories.UserFactory()
    project.add_contributor(user2)

    data = {'some': 'data'}

    draft_reg = DraftRegistrationFactory(registration_metadata=data, branched_from=project)