Пример #1
0
    def setUp(self):
        super(TestPreprintUpdateLicense, self).setUp()

        ensure_licenses()

        self.admin_contributor = AuthUserFactory()
        self.rw_contributor = AuthUserFactory()
        self.read_contributor = AuthUserFactory()
        self.non_contributor = AuthUserFactory()

        self.preprint_provider = PreprintProviderFactory()
        self.preprint = PreprintFactory(creator=self.admin_contributor, provider=self.preprint_provider)

        self.preprint.node.add_contributor(self.rw_contributor, auth=Auth(self.admin_contributor))
        self.preprint.node.add_contributor(self.read_contributor, auth=Auth(self.admin_contributor), permissions=['read'])
        self.preprint.node.save()

        self.cc0_license = NodeLicense.find_one(Q('name', 'eq', 'CC0 1.0 Universal'))
        self.mit_license = NodeLicense.find_one(Q('name', 'eq', 'MIT License'))
        self.no_license = NodeLicense.find_one(Q('name', 'eq', 'No license'))

        self.preprint_provider.licenses_acceptable = [self.cc0_license, self.no_license]
        self.preprint_provider.save()

        self.url = '/{}preprints/{}/'.format(API_BASE, self.preprint._id)
Пример #2
0
 def setUp(self):
     super(TestPreprintProviderSubjects, self).setUp()
     self.lawless_preprint_provider = PreprintProviderFactory()
     self.ruled_preprint_provider = PreprintProviderFactory()
     self.ruled_preprint_provider.subjects_acceptable = self.create_subject_rules(
     )
     self.ruled_preprint_provider.save()
     self.lawless_url = '/{}preprint_providers/{}/taxonomies/?page[size]=15&'.format(
         API_BASE, self.lawless_preprint_provider._id)
     self.ruled_url = '/{}preprint_providers/{}/taxonomies/?page[size]=15&'.format(
         API_BASE, self.ruled_preprint_provider._id)
    def setUp(self):
        super(TestPreprintRelationshipPreprintProvider, self).setUp()
        self.user = AuthUserFactory()
        self.read_write_user = AuthUserFactory()

        self.preprint = PreprintFactory(creator=self.user, providers=None)
        self.preprint.add_contributor(self.read_write_user)
        self.preprint.save()

        self.preprint_provider_one = PreprintProviderFactory()
        self.preprint_provider_two = PreprintProviderFactory()

        self.preprint_preprint_providers_url = self.create_url(self.preprint._id)
 def setUp(self):
     super(TestPreprintProviderSubjects, self).setUp()
     self.lawless_preprint_provider = PreprintProviderFactory()
     self.ruled_preprint_provider = PreprintProviderFactory()
     self.ruled_preprint_provider.subjects_acceptable = self.create_subject_rules()
     self.ruled_preprint_provider.save()
     self.lawless_url = '/{}preprint_providers/{}/taxonomies/?page[size]=15&'.format(API_BASE, self.lawless_preprint_provider._id)
     self.ruled_url = '/{}preprint_providers/{}/taxonomies/?page[size]=15&'.format(API_BASE, self.ruled_preprint_provider._id)
Пример #5
0
def set_preprint_providers():
    """Populate `PreprintProvider` to test database for testing."""

    providers = {
        'osf': 'Open Science Framework',
        'socarxiv': 'SocArXiv',
        'engrxiv': 'EngrXiv',
        'psyarxiv': 'PsyArXiv',
    }

    for key, value in providers.items():
        provider = PreprintProviderFactory()
        provider._id = key
        provider.name = value
        try:
            provider.save()
        except KeyExistsException:
            continue
Пример #6
0
    def setUp(self):
        super(TestPreprintFiltering, self).setUp()
        self.user = AuthUserFactory()
        self.provider = PreprintProviderFactory(name='wwe')
        self.preprint = PreprintFactory(creator=self.user, providers=[self.provider])

        self.preprint.add_tag('nature boy', Auth(self.user), save=False)
        self.preprint.add_tag('ric flair', Auth(self.user), save=False)
        self.preprint.save()

        self.provider_two = PreprintProviderFactory(name='wcw')
        self.preprint_two = PreprintFactory(creator=self.user, filename='woo.txt', providers=[self.provider_two])
        self.preprint_two.add_tag('nature boy', Auth(self.user), save=False)
        self.preprint_two.add_tag('woo', Auth(self.user), save=False)
        self.preprint_two.save()

        self.preprint_three = PreprintFactory(creator=self.user, filename='stonecold.txt', providers=[self.provider])
        self.preprint_three.add_tag('stone', Auth(self.user), save=False)
        self.preprint_two.add_tag('cold', Auth(self.user), save=False)
        self.preprint_three.save()
Пример #7
0
 def test_project_made_public(self):
     assert_false(self.project.is_public)
     self.preprint.set_primary_file(self.file, auth=self.auth, save=True)
     assert_false(self.project.is_public)
     with assert_raises(ValueError):
         self.preprint.set_published(True, auth=self.auth, save=True)
     self.preprint.provider = PreprintProviderFactory()
     self.preprint.set_subjects([[SubjectFactory()._id]], auth=self.auth, save=True)
     self.project.reload()
     assert_false(self.project.is_public)
     self.preprint.set_published(True, auth=self.auth, save=True)
     self.project.reload()
     assert_true(self.project.is_public)
Пример #8
0
    def setUp(self):
        super(TestPreprintFiltering, self).setUp()
        self.user = AuthUserFactory()
        self.provider = PreprintProviderFactory(name='wwe')
        self.provider_two = PreprintProviderFactory(name='wcw')

        self.subject = SubjectFactory()
        self.subject_two = SubjectFactory()

        self.preprint = PreprintFactory(creator=self.user,
                                        provider=self.provider,
                                        subjects=[[self.subject._id]])
        self.preprint_two = PreprintFactory(creator=self.user,
                                            filename='woo.txt',
                                            provider=self.provider_two,
                                            subjects=[[self.subject_two._id]])
        self.preprint_three = PreprintFactory(creator=self.user,
                                              filename='stonecold.txt',
                                              provider=self.provider,
                                              subjects=[[self.subject._id],
                                                        [self.subject_two._id]
                                                        ])
Пример #9
0
def create_fake_project(creator, n_users, privacy, n_components, name, n_tags,
                        presentation_name, is_registration, is_preprint,
                        preprint_providers):
    auth = Auth(user=creator)
    project_title = name if name else fake.science_sentence()
    if is_preprint:
        providers_to_add = []
        if preprint_providers:
            providers = preprint_providers.split(',')
            for provider in providers:
                try:
                    preprint_provider = models.PreprintProvider.find_one(
                        Q('_id', 'eq', provider))
                except NoResultsFound:
                    preprint_provider = PreprintProviderFactory(name=provider)
                providers_to_add.append(preprint_provider)
        privacy = 'public'
        project = PreprintFactory(title=project_title,
                                  description=fake.science_paragraph(),
                                  creator=creator,
                                  providers=providers_to_add)
    elif is_registration:
        project = RegistrationFactory(title=project_title,
                                      description=fake.science_paragraph(),
                                      creator=creator)
    else:
        project = ProjectFactory(title=project_title,
                                 description=fake.science_paragraph(),
                                 creator=creator)
    project.set_privacy(privacy)
    for _ in range(n_users):
        contrib = create_fake_user()
        project.add_contributor(contrib, auth=auth)
    if isinstance(n_components, int):
        for _ in range(n_components):
            NodeFactory(project=project,
                        title=fake.science_sentence(),
                        description=fake.science_paragraph(),
                        creator=creator)
    elif isinstance(n_components, list):
        render_generations_from_node_structure_list(project, creator,
                                                    n_components)
    for _ in range(n_tags):
        project.add_tag(fake.science_word(), auth=auth)
    if presentation_name is not None:
        project.add_tag(presentation_name, auth=auth)
        project.add_tag('poster', auth=auth)

    project.save()
    logger.info('Created project: {0}'.format(project.title))
    return project
Пример #10
0
    def setUp(self):
        super(TestPreprintCreate, self).setUp()

        self.user = AuthUserFactory()
        self.other_user = AuthUserFactory()
        self.private_project = ProjectFactory(creator=self.user)
        self.public_project = ProjectFactory(creator=self.user, public=True)
        self.public_project.add_contributor(
            self.other_user,
            permissions=[permissions.DEFAULT_CONTRIBUTOR_PERMISSIONS],
            save=True)
        self.subject = SubjectFactory()
        self.provider = PreprintProviderFactory()

        self.user_two = AuthUserFactory()

        self.file_one_public_project = test_utils.create_test_file(
            self.public_project, self.user, 'millionsofdollars.pdf')
        self.file_one_private_project = test_utils.create_test_file(
            self.private_project, self.user, 'woowoowoo.pdf')

        self.url = '/{}preprints/'.format(API_BASE)
Пример #11
0
def set_preprint_providers():
    """Populate `PreprintProvider` to test database for testing."""

    providers = {
        'osf': 'Open Science Framework',
        'socarxiv': 'SocArXiv',
        'engrxiv': 'EngrXiv',
        'psyarxiv': 'PsyArXiv',
    }

    for key, value in providers.items():
        provider = PreprintProviderFactory()
        provider._id = key
        provider.name = value
        try:
            provider.save()
        except KeyExistsException:
            continue
Пример #12
0
 def setUp(self):
     super(TestPreprintProviders, self).setUp()
     self.preprint = PreprintFactory(providers=[])
     self.provider = PreprintProviderFactory(name='WWEArxiv')
Пример #13
0
class TestPreprintUpdateLicense(ApiTestCase):

    def setUp(self):
        super(TestPreprintUpdateLicense, self).setUp()

        ensure_licenses()

        self.admin_contributor = AuthUserFactory()
        self.rw_contributor = AuthUserFactory()
        self.read_contributor = AuthUserFactory()
        self.non_contributor = AuthUserFactory()

        self.preprint_provider = PreprintProviderFactory()
        self.preprint = PreprintFactory(creator=self.admin_contributor, provider=self.preprint_provider)

        self.preprint.node.add_contributor(self.rw_contributor, auth=Auth(self.admin_contributor))
        self.preprint.node.add_contributor(self.read_contributor, auth=Auth(self.admin_contributor), permissions=['read'])
        self.preprint.node.save()

        self.cc0_license = NodeLicense.find_one(Q('name', 'eq', 'CC0 1.0 Universal'))
        self.mit_license = NodeLicense.find_one(Q('name', 'eq', 'MIT License'))
        self.no_license = NodeLicense.find_one(Q('name', 'eq', 'No license'))

        self.preprint_provider.licenses_acceptable = [self.cc0_license, self.no_license]
        self.preprint_provider.save()

        self.url = '/{}preprints/{}/'.format(API_BASE, self.preprint._id)

    def make_payload(self, node_id, license_id=None, license_year=None, copyright_holders=None):
        attributes = {}

        if license_year and copyright_holders:
            attributes = {
                'license_record': {
                    'year': license_year,
                    'copyright_holders': copyright_holders
                }
            }
        elif license_year:
            attributes = {
                'license_record': {
                    'year': license_year
                }
            }
        elif copyright_holders:
            attributes = {
                'license_record': {
                    'copyright_holders': copyright_holders
                }
            }

        return {
            'data': {
                'id': node_id,
                'attributes': attributes,
                'relationships': {
                    'license': {
                        'data': {
                            'type': 'licenses',
                            'id': license_id
                        }
                    }
                }
            }
        } if license_id else {
            'data': {
                'id': node_id,
                'attributes': attributes
            }
        }

    def make_request(self, url, data, auth=None, expect_errors=False):
        return self.app.patch_json_api(url, data, auth=auth, expect_errors=expect_errors)

    def test_admin_can_update_license(self):
        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.cc0_license._id
        )

        assert_equal(self.preprint.license, None)

        res = self.make_request(self.url, data, auth=self.admin_contributor.auth)
        assert_equal(res.status_code, 200)
        self.preprint.reload()

        assert_equal(self.preprint.license.node_license, self.cc0_license)
        assert_equal(self.preprint.license.year, None)
        assert_equal(self.preprint.license.copyright_holders, [])

        # check logs
        log = self.preprint.node.logs[-1]
        assert_equal(log.action, 'preprint_license_updated')
        assert_equal(log.params.get('preprint'), self.preprint._id)

    def test_admin_can_update_license_record(self):
        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.no_license._id,
            license_year='2015',
            copyright_holders=['Bojack Horseman, Princess Carolyn']
        )

        assert_equal(self.preprint.license, None)

        res = self.make_request(self.url, data, auth=self.admin_contributor.auth)
        assert_equal(res.status_code, 200)
        self.preprint.reload()

        assert_equal(self.preprint.license.node_license, self.no_license)
        assert_equal(self.preprint.license.year, '2015')
        assert_equal(self.preprint.license.copyright_holders, ['Bojack Horseman, Princess Carolyn'])

    def test_rw_contributor_cannot_update_license(self):
        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.cc0_license._id
        )

        res = self.make_request(self.url, data, auth=self.rw_contributor.auth, expect_errors=True)
        assert_equal(res.status_code, 403)
        assert_equal(res.json['errors'][0]['detail'], 'User must be an admin to update a preprint.')

    def test_read_contributor_cannot_update_license(self):
        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.cc0_license._id
        )

        res = self.make_request(self.url, data, auth=self.read_contributor.auth, expect_errors=True)
        assert_equal(res.status_code, 403)
        assert_equal(res.json['errors'][0]['detail'], 'You do not have permission to perform this action.')

    def test_non_contributor_cannot_update_license(self):
        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.cc0_license._id
        )

        res = self.make_request(self.url, data, auth=self.non_contributor.auth, expect_errors=True)
        assert_equal(res.status_code, 403)
        assert_equal(res.json['errors'][0]['detail'], 'You do not have permission to perform this action.')

    def test_unauthenticated_user_cannot_update_license(self):
        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.cc0_license._id
        )

        res = self.make_request(self.url, data, expect_errors=True)
        assert_equal(res.status_code, 401)
        assert_equal(res.json['errors'][0]['detail'], 'Authentication credentials were not provided.')

    def test_update_preprint_with_invalid_license_for_provider(self):
        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.mit_license._id
        )

        assert_equal(self.preprint.license, None)

        res = self.make_request(self.url, data, auth=self.admin_contributor.auth, expect_errors=True)
        assert_equal(res.status_code, 403)
        assert_equal(res.json['errors'][0]['detail'], 'Invalid license chosen for {}'.format(self.preprint_provider.name))

    def test_update_preprint_with_existing_license_year_attribute_only(self):
        self.preprint.set_preprint_license(
            {
                'id': self.no_license.id,
                'year': '2014',
                'copyrightHolders': ['Diane', 'Mr. Peanut Butter']
            },
            Auth(self.admin_contributor),
        )
        self.preprint.save()

        assert_equal(self.preprint.license.node_license, self.no_license)
        assert_equal(self.preprint.license.year, '2014')
        assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter'])

        data = self.make_payload(
            node_id=self.preprint._id,
            license_year='2015'
        )

        res = self.make_request(self.url, data, auth=self.admin_contributor.auth)
        assert_equal(res.status_code, 200)
        self.preprint.license.reload()

        assert_equal(self.preprint.license.node_license, self.no_license)
        assert_equal(self.preprint.license.year, '2015')
        assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter'])

    def test_update_preprint_with_existing_license_copyright_holders_attribute_only(self):
        self.preprint.set_preprint_license(
            {
                'id': self.no_license.id,
                'year': '2014',
                'copyrightHolders': ['Diane', 'Mr. Peanut Butter']
            },
            Auth(self.admin_contributor),
        )
        self.preprint.save()

        assert_equal(self.preprint.license.node_license, self.no_license)
        assert_equal(self.preprint.license.year, '2014')
        assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter'])

        data = self.make_payload(
            node_id=self.preprint._id,
            copyright_holders=['Bojack Horseman', 'Princess Carolyn']
        )

        res = self.make_request(self.url, data, auth=self.admin_contributor.auth)
        assert_equal(res.status_code, 200)
        self.preprint.license.reload()

        assert_equal(self.preprint.license.node_license, self.no_license)
        assert_equal(self.preprint.license.year, '2014')
        assert_equal(self.preprint.license.copyright_holders, ['Bojack Horseman', 'Princess Carolyn'])

    def test_update_preprint_with_existing_license_relationship_only(self):
        self.preprint.set_preprint_license(
            {
                'id': self.no_license.id,
                'year': '2014',
                'copyrightHolders': ['Diane', 'Mr. Peanut Butter']
            },
            Auth(self.admin_contributor),
        )
        self.preprint.save()

        assert_equal(self.preprint.license.node_license, self.no_license)
        assert_equal(self.preprint.license.year, '2014')
        assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter'])

        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.cc0_license._id
        )

        res = self.make_request(self.url, data, auth=self.admin_contributor.auth)
        assert_equal(res.status_code, 200)
        self.preprint.license.reload()

        assert_equal(self.preprint.license.node_license, self.cc0_license)
        assert_equal(self.preprint.license.year, '2014')
        assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter'])

    def test_update_preprint_with_existing_license_relationship_and_attributes(self):
        self.preprint.set_preprint_license(
            {
                'id': self.no_license.id,
                'year': '2014',
                'copyrightHolders': ['Diane', 'Mr. Peanut Butter']
            },
            Auth(self.admin_contributor),
            save=True
        )

        assert_equal(self.preprint.license.node_license, self.no_license)
        assert_equal(self.preprint.license.year, '2014')
        assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter'])

        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.cc0_license._id,
            license_year='2015',
            copyright_holders=['Bojack Horseman', 'Princess Carolyn']
        )

        res = self.make_request(self.url, data, auth=self.admin_contributor.auth)
        assert_equal(res.status_code, 200)
        self.preprint.license.reload()

        assert_equal(self.preprint.license.node_license, self.cc0_license)
        assert_equal(self.preprint.license.year, '2015')
        assert_equal(self.preprint.license.copyright_holders, ['Bojack Horseman', 'Princess Carolyn'])

    def test_update_preprint_license_without_required_year_in_payload(self):
        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.no_license._id,
            copyright_holders=['Rick', 'Morty']
        )

        res = self.make_request(self.url, data, auth=self.admin_contributor.auth, expect_errors=True)
        assert_equal(res.status_code, 400)
        assert_equal(res.json['errors'][0]['detail'], 'year must be specified for this license')

    def test_update_preprint_license_without_required_copyright_holders_in_payload_(self):
        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.no_license._id,
            license_year='1994'
        )

        res = self.make_request(self.url, data, auth=self.admin_contributor.auth, expect_errors=True)
        assert_equal(res.status_code, 400)
        assert_equal(res.json['errors'][0]['detail'], 'copyrightHolders must be specified for this license')

    def test_update_preprint_license_does_not_change_project_license(self):
        self.preprint.node.set_node_license(
            {
                'id': self.no_license.id,
                'year': '2015',
                'copyrightHolders': ['Simba', 'Mufasa']
            },
            auth=Auth(self.admin_contributor)
        )
        self.preprint.node.save()
        assert_equal(self.preprint.node.node_license.node_license, self.no_license)

        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.cc0_license._id
        )

        res = self.make_request(self.url, data, auth=self.admin_contributor.auth)
        assert_equal(res.status_code, 200)
        self.preprint.reload()

        assert_equal(self.preprint.license.node_license, self.cc0_license)
        assert_equal(self.preprint.node.node_license.node_license, self.no_license)

    def test_update_preprint_license_without_change_does_not_add_log(self):
        self.preprint.set_preprint_license(
            {
                'id': self.no_license.id,
                'year': '2015',
                'copyrightHolders': ['Kim', 'Kanye']
            },
            auth=Auth(self.admin_contributor),
            save=True
        )

        before_num_logs = len(self.preprint.node.logs)
        before_update_log = self.preprint.node.logs[-1]

        data = self.make_payload(
            node_id=self.preprint._id,
            license_id=self.no_license._id,
            license_year='2015',
            copyright_holders=['Kanye', 'Kim']
        )
        res = self.make_request(self.url, data, auth=self.admin_contributor.auth)
        self.preprint.node.reload()

        after_num_logs = len(self.preprint.node.logs)
        after_update_log = self.preprint.node.logs[-1]

        assert_equal(res.status_code, 200)
        assert_equal(before_num_logs, after_num_logs)
        assert_equal(before_update_log._id, after_update_log._id)
class TestPreprintProviderSubjects(ApiTestCase):
    def create_subject_rules(self):
        '''
        Subject Hierarchy
        +-----------------------------+
        |                    +-->E    |
        |      +-------->B+--+        |
        |      |             +-->F    |
        |  A+----------->C            |
        |      |                      |
        |      +-------->D+----->G    |
        |                             |
        |  H+------>I+----->J         |
        |            |                |
        |            +----->K         |
        |                             |
        |  L+------>M+----->N         |
        |            |                |
        |            +------->E       |
        |                             |
        |  O                          |
        +-----------------------------+
        '''
        self.subA = SubjectFactory(text='A')
        self.subB = SubjectFactory(text='B', parents=[self.subA])
        self.subC = SubjectFactory(text='C', parents=[self.subA])
        self.subD = SubjectFactory(text='D', parents=[self.subA])
        self.subF = SubjectFactory(text='F', parents=[self.subB])
        self.subG = SubjectFactory(text='G', parents=[self.subD])
        self.subH = SubjectFactory(text='H')
        self.subI = SubjectFactory(text='I', parents=[self.subH])
        self.subJ = SubjectFactory(text='J', parents=[self.subI])
        self.subK = SubjectFactory(text='K', parents=[self.subI])
        self.subL = SubjectFactory(text='L')
        self.subM = SubjectFactory(text='M', parents=[self.subL])
        self.subE = SubjectFactory(text='E', parents=[self.subB, self.subM])
        self.subN = SubjectFactory(text='N', parents=[self.subM])
        self.subO = SubjectFactory(text='O')
        rules = [
            ([self.subA._id, self.subB._id], False),
            ([self.subA._id, self.subD._id], True),
            ([self.subH._id, self.subI._id, self.subJ._id], True),
            ([self.subL._id], True)
        ]
        #  This should allow: A, B, D, G, H, I, J, L, M, N and E
        #  This should not allow: C, F, K, O
        return rules

    def setUp(self):
        super(TestPreprintProviderSubjects, self).setUp()
        self.lawless_preprint_provider = PreprintProviderFactory()
        self.ruled_preprint_provider = PreprintProviderFactory()
        self.ruled_preprint_provider.subjects_acceptable = self.create_subject_rules()
        self.ruled_preprint_provider.save()
        self.lawless_url = '/{}preprint_providers/{}/taxonomies/?page[size]=15&'.format(API_BASE, self.lawless_preprint_provider._id)
        self.ruled_url = '/{}preprint_providers/{}/taxonomies/?page[size]=15&'.format(API_BASE, self.ruled_preprint_provider._id)

    def test_no_rules_grabs_all(self):
        res = self.app.get(self.lawless_url)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 15)

    def test_rules_only_grab_acceptable_subjects(self):
        res = self.app.get(self.ruled_url)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 11)

    def test_no_rules_with_null_parent_filter(self):
        res = self.app.get(self.lawless_url + 'filter[parents]=null')

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 4)

    def test_rules_enforced_with_null_parent_filter(self):
        res = self.app.get(self.ruled_url + 'filter[parents]=null')

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 3)
        texts = [item['attributes']['text'] for item in res.json['data']]
        assert_in('A', texts)
        assert_in('H', texts)
        assert_in('L', texts)
        assert_not_in('O', texts)

    def test_no_rules_with_parent_filter(self):
        res = self.app.get(self.lawless_url + 'filter[parents]=' + self.subB._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 2)

        res = self.app.get(self.lawless_url + 'filter[parents]=' + self.subI._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 2)

        res = self.app.get(self.lawless_url + 'filter[parents]=' + self.subM._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 2)

    def test_rules_enforced_with_parent_filter(self):
        res = self.app.get(self.ruled_url + 'filter[parents]=' + self.subB._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 1)
        texts = [item['attributes']['text'] for item in res.json['data']]
        assert_in('E', texts)
        assert_not_in('F', texts)

        res = self.app.get(self.ruled_url + 'filter[parents]=' + self.subI._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 1)
        texts = [item['attributes']['text'] for item in res.json['data']]
        assert_in('J', texts)
        assert_not_in('K', texts)

        res = self.app.get(self.ruled_url + 'filter[parents]=' + self.subM._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 2)
        texts = [item['attributes']['text'] for item in res.json['data']]
        assert_in('N', texts)
        assert_in('E', texts)

    def test_no_rules_with_grandparent_filter(self):
        res = self.app.get(self.lawless_url + 'filter[parents]=' + self.subA._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 3)

    def test_rules_enforced_with_grandparent_filter(self):
        res = self.app.get(self.ruled_url + 'filter[parents]=' + self.subA._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 2)
        texts = [item['attributes']['text'] for item in res.json['data']]
        assert_in('B', texts)
        assert_in('D', texts)
        assert_not_in('C', texts)
Пример #15
0
class TestPreprintProviderSubjects(ApiTestCase):
    def create_subject_rules(self):
        '''
        Subject Hierarchy
        +-----------------------------+
        |                    +-->E    |
        |      +-------->B+--+        |
        |      |             +-->F    |
        |  A+----------->C            |
        |      |                      |
        |      +-------->D+----->G    |
        |                             |
        |  H+------>I+----->J         |
        |            |                |
        |            +----->K         |
        |                             |
        |  L+------>M+----->N         |
        |            |                |
        |            +------->E       |
        |                             |
        |  O                          |
        +-----------------------------+
        '''
        self.subA = SubjectFactory(text='A')
        self.subB = SubjectFactory(text='B', parents=[self.subA])
        self.subC = SubjectFactory(text='C', parents=[self.subA])
        self.subD = SubjectFactory(text='D', parents=[self.subA])
        self.subF = SubjectFactory(text='F', parents=[self.subB])
        self.subG = SubjectFactory(text='G', parents=[self.subD])
        self.subH = SubjectFactory(text='H')
        self.subI = SubjectFactory(text='I', parents=[self.subH])
        self.subJ = SubjectFactory(text='J', parents=[self.subI])
        self.subK = SubjectFactory(text='K', parents=[self.subI])
        self.subL = SubjectFactory(text='L')
        self.subM = SubjectFactory(text='M', parents=[self.subL])
        self.subE = SubjectFactory(text='E', parents=[self.subB, self.subM])
        self.subN = SubjectFactory(text='N', parents=[self.subM])
        self.subO = SubjectFactory(text='O')
        rules = [([self.subA._id, self.subB._id], False),
                 ([self.subA._id, self.subD._id], True),
                 ([self.subH._id, self.subI._id, self.subJ._id], True),
                 ([self.subL._id], True)]
        #  This should allow: A, B, D, G, H, I, J, L, M, N and E
        #  This should not allow: C, F, K, O
        return rules

    def setUp(self):
        super(TestPreprintProviderSubjects, self).setUp()
        self.lawless_preprint_provider = PreprintProviderFactory()
        self.ruled_preprint_provider = PreprintProviderFactory()
        self.ruled_preprint_provider.subjects_acceptable = self.create_subject_rules(
        )
        self.ruled_preprint_provider.save()
        self.lawless_url = '/{}preprint_providers/{}/taxonomies/?page[size]=15&'.format(
            API_BASE, self.lawless_preprint_provider._id)
        self.ruled_url = '/{}preprint_providers/{}/taxonomies/?page[size]=15&'.format(
            API_BASE, self.ruled_preprint_provider._id)

    def test_no_rules_grabs_all(self):
        res = self.app.get(self.lawless_url)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 15)

    def test_rules_only_grab_acceptable_subjects(self):
        res = self.app.get(self.ruled_url)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 11)

    def test_no_rules_with_null_parent_filter(self):
        res = self.app.get(self.lawless_url + 'filter[parents]=null')

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 4)

    def test_rules_enforced_with_null_parent_filter(self):
        res = self.app.get(self.ruled_url + 'filter[parents]=null')

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 3)
        texts = [item['attributes']['text'] for item in res.json['data']]
        assert_in('A', texts)
        assert_in('H', texts)
        assert_in('L', texts)
        assert_not_in('O', texts)

    def test_no_rules_with_parent_filter(self):
        res = self.app.get(self.lawless_url + 'filter[parents]=' +
                           self.subB._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 2)

        res = self.app.get(self.lawless_url + 'filter[parents]=' +
                           self.subI._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 2)

        res = self.app.get(self.lawless_url + 'filter[parents]=' +
                           self.subM._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 2)

    def test_rules_enforced_with_parent_filter(self):
        res = self.app.get(self.ruled_url + 'filter[parents]=' + self.subB._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 1)
        texts = [item['attributes']['text'] for item in res.json['data']]
        assert_in('E', texts)
        assert_not_in('F', texts)

        res = self.app.get(self.ruled_url + 'filter[parents]=' + self.subI._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 1)
        texts = [item['attributes']['text'] for item in res.json['data']]
        assert_in('J', texts)
        assert_not_in('K', texts)

        res = self.app.get(self.ruled_url + 'filter[parents]=' + self.subM._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 2)
        texts = [item['attributes']['text'] for item in res.json['data']]
        assert_in('N', texts)
        assert_in('E', texts)

    def test_no_rules_with_grandparent_filter(self):
        res = self.app.get(self.lawless_url + 'filter[parents]=' +
                           self.subA._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 3)

    def test_rules_enforced_with_grandparent_filter(self):
        res = self.app.get(self.ruled_url + 'filter[parents]=' + self.subA._id)

        assert_equal(res.status_code, 200)
        assert_equal(res.json['links']['meta']['total'], 2)
        texts = [item['attributes']['text'] for item in res.json['data']]
        assert_in('B', texts)
        assert_in('D', texts)
        assert_not_in('C', texts)