<commit_before>from nose.tools import *

from tests.base import ApiTestCase
from tests.factories import AuthUserFactory, PreprintFactory, PreprintProviderFactory

from api.base.settings.defaults import API_BASE


class TestPreprintRelationshipPreprintProvider(ApiTestCase):
    def setUp(self):
        super(TestPreprintRelationshipPreprintProvider, self).setUp()
        self.user = AuthUserFactory()
        self.preprint = PreprintFactory(creator=self.user, provider=None)
        self.preprint_provider = PreprintProviderFactory()
        self.preprint_preprint_providers_url = self.create_url(self.preprint._id)

    def create_url(self, preprint_id):
        return '/{0}preprints/{1}/relationships/preprint_provider/'.format(API_BASE, preprint_id)

    def create_payload(self, preprint_provider_id):
        return {'data': {'type': 'preprint_providers', 'id': preprint_provider_id}}

    def test_update_preprint_provider(self):
        assert_equal(self.preprint.preprint_provider, None)
        res = self.app.patch_json_api(
            self.preprint_preprint_providers_url,
            self.create_payload(self.preprint_provider._id),
            auth=self.user.auth
        )

        assert_equal(res.status_code, 200)

        # check the relationship
        self.preprint.reload()
        assert_equal(self.preprint.preprint_provider, self.preprint_provider)

    def test_preprint_with_no_permissions(self):
        user = AuthUserFactory()
        user.save()
        res = self.app.patch_json_api(
            self.preprint_preprint_providers_url,
            self.create_payload([self.preprint_provider._id]),
            auth=user.auth,
            expect_errors=True,
        )

        assert_equal(res.status_code, 403)

    def test_change_preprint_provider(self):
        preprint_with_provider = PreprintFactory()
        provider = preprint_with_provider.preprint_provider
        assert_not_equal(provider, self.preprint_provider)
        res = self.app.put_json_api(
            self.create_url(preprint_with_provider._id),
            self.create_payload(self.preprint_provider._id),
            auth=self.user.auth,
            expect_errors=True
        )

        assert_equal(res.status_code, 400)

    def test_get_relationship_information(self):
        res = self.app.get(self.preprint_preprint_providers_url,auth=self.user.auth)

        assert_equal(res.status_code, 200)

    def test_invalid_relationship_type(self):
        invalid_type_payload = self.create_payload(self.preprint_provider._id)
        invalid_type_payload['type'] = 'socks'

        res = self.app.put_json_api(
            self.preprint_preprint_providers_url,
            invalid_type_payload,
            auth=self.user.auth,
            expect_errors=True
        )

        assert_equal(res.status_code, 400)

    def test_invalid_relationship_id(self):
        res = self.app.put_json_api(
            self.preprint_preprint_providers_url,
            self.create_payload('nope nope nope'),
            auth=self.user.auth,
            expect_errors=True
        )

        assert_equal(res.status_code, 400)<commit_msg>Rename a variable to be clearer, adjust status code and payload sending<commit_after>from nose.tools import *

from tests.base import ApiTestCase
from tests.factories import AuthUserFactory, PreprintFactory, PreprintProviderFactory

from api.base.settings.defaults import API_BASE


class TestPreprintRelationshipPreprintProvider(ApiTestCase):
    def setUp(self):
        super(TestPreprintRelationshipPreprintProvider, self).setUp()
        self.user = AuthUserFactory()
        self.preprint = PreprintFactory(creator=self.user, provider=None)
        self.preprint_provider = PreprintProviderFactory()
        self.preprint_preprint_providers_url = self.create_url(self.preprint._id)

    def create_url(self, preprint_id):
        return '/{0}preprints/{1}/relationships/preprint_provider/'.format(API_BASE, preprint_id)

    def create_payload(self, preprint_provider_id):
        return {'data': {'type': 'preprint_providers', 'id': preprint_provider_id}}

    def test_update_preprint_provider(self):
        assert_equal(self.preprint.preprint_provider, None)
        res = self.app.patch_json_api(
            self.preprint_preprint_providers_url,
            self.create_payload(self.preprint_provider._id),
            auth=self.user.auth
        )

        assert_equal(res.status_code, 200)

        # check the relationship
        self.preprint.reload()
        assert_equal(self.preprint.preprint_provider, self.preprint_provider)

    def test_preprint_with_no_permissions(self):
        new_user = AuthUserFactory()
        new_user.save()
        res = self.app.patch_json_api(
            self.preprint_preprint_providers_url,
            self.create_payload([self.preprint_provider._id]),
            auth=new_user.auth,
            expect_errors=True,
        )

        assert_equal(res.status_code, 403)

    def test_change_preprint_provider(self):
        preprint_with_provider = PreprintFactory()
        provider = preprint_with_provider.preprint_provider
        assert_not_equal(provider, self.preprint_provider)
        res = self.app.put_json_api(
            self.create_url(preprint_with_provider._id),
            self.create_payload(self.preprint_provider._id),
            auth=self.user.auth,
            expect_errors=True
        )

        assert_equal(res.status_code, 400)

    def test_get_relationship_information(self):
        res = self.app.get(self.preprint_preprint_providers_url,auth=self.user.auth)

        assert_equal(res.status_code, 200)

    def test_invalid_relationship_type(self):
        invalid_type_payload = self.create_payload(self.preprint_provider._id)
        invalid_type_payload['data']['type'] = 'socks'

        res = self.app.put_json_api(
            self.preprint_preprint_providers_url,
            invalid_type_payload,
            auth=self.user.auth,
            expect_errors=True
        )

        assert_equal(res.status_code, 409)

    def test_invalid_relationship_id(self):
        res = self.app.put_json_api(
            self.preprint_preprint_providers_url,
            self.create_payload('nope nope nope'),
            auth=self.user.auth,
            expect_errors=True
        )

        assert_equal(res.status_code, 400)
{'fraction_non_alphanumeric': 0.0857187794904058, 'fraction_numerical': 0.0062912865681031774, 'mean_word_length': 2.994346733668342, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6722315', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1868, 'n_words': 260}