<|file_name|>test_withdrawn_registrations.py<|end_file_name|><｜fim▁begin｜>from urlparse import urlparse
from api_tests.nodes.views.test_node_contributors_list import NodeCRUDTestCase

from nose.tools import *  # flake8: noqa

from api.base.settings.defaults import API_BASE
from framework.auth.core import Auth

from tests.base import fake
from osf_tests.factories import (
    ProjectFactory,
    CommentFactory,
    RegistrationFactory,
    WithdrawnRegistrationFactory,
)


class TestWithdrawnRegistrations(NodeCRUDTestCase):

    def setUp(self):
        super(TestWithdrawnRegistrations, self).setUp()
        self.registration = RegistrationFactory(creator=self.user, project=self.public_project)
        self.withdrawn_registration = WithdrawnRegistrationFactory(registration=self.registration, user=self.registration.creator)

        self.public_pointer_project = ProjectFactory(is_public=True)
        self.public_pointer = self.public_project.add_pointer(self.public_pointer_project,
                                                              auth=Auth(self.user),
                                                              save=True)
        self.withdrawn_url = '/{}registrations/{}/?version=2.2'.format(API_BASE, self.registration._id)
        self.withdrawn_registration.justification = 'We made a major error.'
        self.withdrawn_registration.save()

    def test_can_access_withdrawn_contributors(self):
        url = '/{}registrations/{}/contributors/'.format(API_BASE, self.registration._id)
        res = self.app.get(url, auth=self.user.auth, expect_errors=True)
        assert_equal(res.status_code, 200)

    def test_cannot_access_withdrawn_children(self):
        url = '/{}registrations/{}/children/'.format(API_BASE, self.registration._id)
        res = self.app.get(url, auth=self.user.auth, expect_errors=True)
        assert_equal(res.status_code, 403)

    def test_cannot_access_withdrawn_comments(self):
        self.public_project = ProjectFactory(is_public=True, creator=self.user)
        self.public_comment = CommentFactory(node=self.public_project, user=self.user)
        url = '/{}registrations/{}/comments/'.format(API_BASE, self.registration._id)
        res = self.app.get(url, auth=self.user.auth, expect_errors=True)
        assert_equal(res.status_code, 403)

    def test_can_access_withdrawn_contributor_detail(self):
        url = '/{}registrations/{}/contributors/{}/'.format(API_BASE, self.registration._id, self.user._id)
        res = self.app.get(url, auth=self.user.auth, expect_errors=True)
        assert_equal(res.status_code, 200)

    def test_cannot_return_a_withdrawn_registration_at_node_detail_endpoint(self):
        url = '/{}nodes/{}/'.format(API_BASE, self.registration._id)
        res = self.app.get(url, auth=self.user.auth, expect_errors=True)
        assert_equal(res.status_code, 404)

    def test_cannot_delete_a_withdrawn_registration(self):
        url = '/{}registrations/{}/'.format(API_BASE, self.registration._id)
        res = self.app.delete_json_api(url, auth=self.user.auth, expect_errors=True)
        self.registration.reload()
        assert_equal(res.status_code, 405)

    def test_cannot_access_withdrawn_files_list(self):
        url = '/{}registrations/{}/files/'.format(API_BASE, self.registration._id)
        res = self.app.get(url, auth=self.user.auth, expect_errors=True)
        assert_equal(res.status_code, 403)

    def test_cannot_access_withdrawn_node_links_detail(self):
        url = '/{}registrations/{}/node_links/{}/'.format(API_BASE, self.registration._id, self.public_pointer._id)
        res = self.app.get(url, auth=self.user.auth, expect_errors=True)
        assert_equal(res.status_code, 403)

    def <|fim_middle|>(self):
        url = '/{}registrations/{}/node_links/'.format(API_BASE, self.registration._id)
        res = self.app.get(url, auth=self.user.auth, expect_errors=True)
        assert_equal(res.status_code, 403)

    def test_cannot_access_withdrawn_node_logs(self):
        self.public_project = ProjectFactory(is_public=True, creator=self.user)
        url = '/{}registrations/{}/logs/'.format(API_BASE, self.registration._id)
        res = self.app.get(url, auth=self.user.auth, expect_errors=True)
        assert_equal(res.status_code, 403)

    def test_cannot_access_withdrawn_registrations_list(self):
        self.registration.save()
        url = '/{}registrations/{}/registrations/'.format(API_BASE, self.registration._id)
        res = self.app.get(url, auth=self.user.auth, expect_errors=True)
        assert_equal(res.status_code, 403)

    def test_withdrawn_registrations_display_limited_fields(self):
        registration = self.registration
        res = self.app.get(self.withdrawn_url, auth=self.user.auth)
        assert_equal(res.status_code, 200)
        attributes = res.json['data']['attributes']
        registration.reload()
        expected_attributes = {
            'title': registration.title,
            'description': registration.description,
            'date_created': registration.date_created.isoformat().replace('+00:00', 'Z'),
            'date_registered': registration.registered_date.isoformat().replace('+00:00', 'Z'),
            'date_modified': registration.date_modified.isoformat().replace('+00:00', 'Z'),
            'withdrawal_justification': registration.retraction.justification,
            'public': None,
            'category': None,
            'registration': True,
            'fork': None,
            'collection': None,
            'tags': None,
            'withdrawn': True,
            'pending_withdrawal': None,
            'pending_registration_approval': None,
            'pending_embargo_approval': None,
            'embargo_end_date': None,
            'registered_meta': None,
            'current_user_permissions': None,
            'registration_supplement': registration.registered_schema.first().name
        }

        for attribute in expected_attributes:
            assert_equal(expected_attributes[attribute], attributes[attribute])

        contributors = urlparse(res.json['data']['relationships']['contributors']['links']['related']['href']).path
        assert_equal(contributors, '/{}registrations/{}/contributors/'.format(API_BASE, registration._id))

        assert_not_in('children', res.json['data']['relationships'])
        assert_not_in('comments', res.json['data']['relationships'])
        assert_not_in('node_links', res.json['data']['relationships'])
        assert_not_in('registrations', res.json['data']['relationships'])
        assert_not_in('parent', res.json['data']['relationships'])
        assert_not_in('forked_from', res.json['data']['relationships'])
        assert_not_in('files', res.json['data']['relationships'])
        assert_not_in('logs', res.json['data']['relationships'])
        assert_not_in('registered_by', res.json['data']['relationships'])
        assert_not_in('registered_from', res.json['data']['relationships'])
        assert_not_in('root', res.json['data']['relationships'])

    def test_field_specific_related_counts_ignored_if_hidden_field_on_withdrawn_registration(self):
        url = '/{}registrations/{}/?related_counts=children'.format(API_BASE, self.registration._id)
        res = self.app.get(url, auth=self.user.auth)
        assert_equal(res.status_code, 200)
        assert_not_in('children', res.json['data']['relationships'])
        assert_in('contributors', res.json['data']['relationships'])

    def test_field_specific_related_counts_retrieved_if_visible_field_on_withdrawn_registration(self):
        url = '/{}registrations/{}/?related_counts=contributors'.format(API_BASE, self.registration._id)
        res = self.app.get(url, auth=self.user.auth)
        assert_equal(res.status_code, 200)
        assert_equal(res.json['data']['relationships']['contributors']['links']['related']['meta']['count'], 1)

<｜fim▁end｜>
test_cannot_access_withdrawn_node_links_list