<commit_before># Amara, universalsubtitles.org
#
# Copyright (C) 2012 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.

from django.test import TestCase
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from teams.moderation_const import APPROVED, UNMODERATED, WAITING_MODERATION
from accountlinker.models import (
    ThirdPartyAccount, YoutubeSyncRule, check_authorization, can_be_synced
)
from videos.models import Video, VideoUrl, SubtitleLanguage
from teams.models import Team, TeamVideo
from auth.models import CustomUser as User
from tasks import get_youtube_data
from apps.testhelpers import views as helpers

from mock import Mock, MagicMock

def _set_subtitles(video, language, original, complete, translations=[]):
    translations = [{'code': lang, 'is_original': False, 'is_complete': True,
                     'num_subs': 1} for lang in translations]

    data = {'code': language, 'is_original': original, 'is_complete': complete,
            'num_subs': 1, 'translations': translations}

    helpers._add_lang_to_video(video, data, None)

def assert_update_subtitles(version_or_language, account):
    assert version_or_language != None
    assert account != None

class AccountTest(TestCase):
    fixtures = ["staging_users.json", "staging_videos.json", "staging_teams.json"]

    def test_retrieval(self):

        acc = ThirdPartyAccount.objects.create(type='Y', username='abc',
                oauth_access_token='a', oauth_refresh_token='b')

        with self.assertRaises(ImproperlyConfigured):
            ThirdPartyAccount.objects.always_push_account()

        setattr(settings, 'YOUTUBE_ALWAYS_PUSH_USERNAME', 'abc')
        self.assertEquals(ThirdPartyAccount.objects.always_push_account().pk,
                acc.pk)

    def test_rules(self):
        video = Video.objects.filter(teamvideo__isnull=False)[0]
        video.user = User.objects.get(username='admin')
        team = video.get_team_video().team
        team = team.slug

        r = YoutubeSyncRule.objects.create()
        self.assertFalse(r.should_sync(video))

        YoutubeSyncRule.objects.all().delete()<commit_msg>2093: PEP8<commit_after># Amara, universalsubtitles.org
#
# Copyright (C) 2012 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.

from django.test import TestCase
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from teams.moderation_const import APPROVED, UNMODERATED, WAITING_MODERATION
from accountlinker.models import (
    ThirdPartyAccount, YoutubeSyncRule, check_authorization, can_be_synced
)
from videos.models import Video, VideoUrl, SubtitleLanguage
from teams.models import Team, TeamVideo
from auth.models import CustomUser as User
from tasks import get_youtube_data
from apps.testhelpers import views as helpers

from mock import Mock, MagicMock


def _set_subtitles(video, language, original, complete, translations=[]):
    translations = [{'code': lang, 'is_original': False, 'is_complete': True,
                     'num_subs': 1} for lang in translations]

    data = {'code': language, 'is_original': original, 'is_complete': complete,
            'num_subs': 1, 'translations': translations}

    helpers._add_lang_to_video(video, data, None)


def assert_update_subtitles(version_or_language, account):
    assert version_or_language != None
    assert account != None


class AccountTest(TestCase):
    fixtures = ["staging_users.json", "staging_videos.json", "staging_teams.json"]

    def test_retrieval(self):

        acc = ThirdPartyAccount.objects.create(type='Y', username='abc',
                oauth_access_token='a', oauth_refresh_token='b')

        with self.assertRaises(ImproperlyConfigured):
            ThirdPartyAccount.objects.always_push_account()

        setattr(settings, 'YOUTUBE_ALWAYS_PUSH_USERNAME', 'abc')
        self.assertEquals(ThirdPartyAccount.objects.always_push_account().pk,
                acc.pk)

    def test_rules(self):
        video = Video.objects.filter(teamvideo__isnull=False)[0]
        video.user = User.objects.get(username='admin')
        team = video.get_team_video().team
        team = team.slug

        r = YoutubeSyncRule.objects.create()
        self.assertFalse(r.should_sync(video))

        YoutubeSyncRule.objects.all().delete()
{'fraction_non_alphanumeric': 0.08745992162451015, 'fraction_numerical': 0.004453152832205202, 'mean_word_length': 4.137236962488563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5908636', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1567, 'n_words': 490}