<commit_before>from doajtest.helpers import DoajTestCase
from portality.api.v1.data_objects import OutgoingJournal
from portality.api.v1 import JournalsCrudApi, Api401Error, Api404Error
from portality import models
from doajtest.fixtures import JournalFixtureFactory
import time


class TestCrudJournal(DoajTestCase):

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

    def tearDown(self):
        super(TestCrudJournal, self).tearDown()

    def test_01_outgoing_journal_do(self):
        # make a blank one successfully
        oj = OutgoingJournal()

        # make one from an incoming journal model fixture
        data = JournalFixtureFactory.make_journal_source(include_obsolete_fields=True)
        j = models.Journal(**data)
        oj = OutgoingJournal.from_model(j)

        # check that it does not contain information that it shouldn't
        assert oj.data.get("index") is None
        assert oj.data.get("history") is None
        assert oj.data.get("admin", {}).get("active") is None
        assert oj.data.get("admin", {}).get("notes") is None
        assert oj.data.get("admin", {}).get("editor_group") is None
        assert oj.data.get("admin", {}).get("editor") is None

    def test_02_outgoing_journal_urls(self):
        """ We've relaxed the URL constraints for outgoing journals - https://github.com/DOAJ/doajPM/issues/2268 """

        data = JournalFixtureFactory.make_journal_source(include_obsolete_fields=True)

        invalid_url = 'an invalid url $321 >>,'
        data['bibjson']['submission_charges_url'] = invalid_url
        data['bibjson']['editorial_review']['url'] = invalid_url
        data['bibjson']['plagiarism_detection']['url'] = invalid_url
        data['bibjson']['article_statistics']['url'] = invalid_url
        data['bibjson']['author_copyright']['url'] = invalid_url
        data['bibjson']['author_publishing_rights']['url'] = invalid_url

        for l in data['bibjson']['link']:
            l['url'] = invalid_url

        # Even with all of the dodgy URLS above, we should still have a successful OutgoingJournal object.
        j = models.Journal(**data)
        OutgoingJournal.from_model(j)

    def test_03_retrieve_public_journal_success(self):
        # set up all the bits we need
        data = JournalFixtureFactory.make_journal_source(in_doaj=True, include_obsolete_fields=True)
        j = models.Journal(**data)
        j.save()
        time.sleep(2)
        
        a = JournalsCrudApi.retrieve(j.id, account=None)
        # check that we got back the object we expected
        assert isinstance(a, OutgoingJournal)
        assert a.id == j.id
        <commit_msg>test_api_crud_journal UT<commit_after>from doajtest.helpers import DoajTestCase
from portality.api.v1.data_objects import OutgoingJournal
from portality.api.v1 import JournalsCrudApi, Api401Error, Api404Error
from portality import models
from doajtest.fixtures import JournalFixtureFactory
import time


class TestCrudJournal(DoajTestCase):

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

    def tearDown(self):
        super(TestCrudJournal, self).tearDown()

    def test_01_outgoing_journal_do(self):
        # make a blank one successfully
        oj = OutgoingJournal()

        # make one from an incoming journal model fixture
        data = JournalFixtureFactory.make_journal_source(include_obsolete_fields=True)
        j = models.Journal(**data)
        oj = OutgoingJournal.from_model(j)

        # check that it does not contain information that it shouldn't
        assert oj.data.get("index") is None
        assert oj.data.get("history") is None
        assert oj.data.get("admin", {}).get("active") is None
        assert oj.data.get("admin", {}).get("notes") is None
        assert oj.data.get("admin", {}).get("editor_group") is None
        assert oj.data.get("admin", {}).get("editor") is None

    def test_02_outgoing_journal_urls(self):
        """ We've relaxed the URL constraints for outgoing journals - https://github.com/DOAJ/doajPM/issues/2268 """

        data = JournalFixtureFactory.make_journal_source(include_obsolete_fields=True)

        invalid_url = 'an invalid url $321 >>,'
        data['bibjson']['other_charges']['url'] = invalid_url
        data['bibjson']['editorial']['review_url'] = invalid_url
        data['bibjson']['plagiarism']['url'] = invalid_url
        data['bibjson']['copyright']['url'] = invalid_url
        data['bibjson']['ref']['journal'] = invalid_url

        # Even with all of the dodgy URLS above, we should still have a successful OutgoingJournal object.
        j = models.Journal(**data)
        OutgoingJournal.from_model(j)

    def test_03_retrieve_public_journal_success(self):
        # set up all the bits we need
        data = JournalFixtureFactory.make_journal_source(in_doaj=True, include_obsolete_fields=True)
        j = models.Journal(**data)
        j.save()
        time.sleep(2)
        
        a = JournalsCrudApi.retrieve(j.id, account=None)
        # check that we got back the object we expected
        assert isinstance(a, OutgoingJournal)
        assert a.id == j.id
        
{'fraction_non_alphanumeric': 0.11013986013986014, 'fraction_numerical': 0.008547008547008548, 'mean_word_length': 3.2944120100083403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '656902', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1485, 'n_words': 361}