<commit_before>import mock
import unittest
from nose.tools import *

from github3.repos import Repository

from tests.base import OsfTestCase
from tests.factories import UserFactory, ProjectFactory

from framework.auth import Auth
from website.addons.github import settings as github_settings
from website.addons.github.exceptions import NotFoundError, TooBigToRenderError
from website.addons.github.tests.factories import GitHubOauthSettingsFactory
from website.addons.github.model import AddonGitHubUserSettings, AddonGitHubOauthSettings, GithubGuidFile

from .utils import create_mock_github
mock_github = create_mock_github()


class TestFileGuid(OsfTestCase):

    def setUp(self):
        super(OsfTestCase, self).setUp()
        self.user = UserFactory()
        self.project = ProjectFactory(creator=self.user)

        self.project.add_addon('github', auth=Auth(self.user))
        self.node_addon = self.project.get_addon('github')

    def test_provider(self):
        assert_equal('github', GithubGuidFile().provider)

    def test_correct_path(self):
        guid, _ = self.node_addon.find_or_create_file_guid('perth')
        assert_equal(guid.waterbutler_path, 'perth')
        assert_equal(guid.waterbutler_path, guid.path)

    @mock.patch('website.addons.base.requests.get')
    def test_unique_identifier(self, mock_get):
        mock_response = mock.Mock(ok=True, status_code=200)
        mock_get.return_value = mock_response
        mock_response.json.return_value = {
            'data': {
                'name': 'Morty',
                'extra': {
                    'fileSha': 'Im a little tea pot'
                }
            }
        }

        guid, _ = self.node_addon.find_or_create_file_guid('perth')
        guid.enrich()

        assert_equal(guid.unique_identifier, 'Im a little tea pot')

    def test_exception_from_response(self):
        mock_response = mock.Mock()
        mock_response.json.return_value = {'errors': [{'code': 'too_large'}]}

        guid, _ = self.node_addon.find_or_create_file_guid('perth')

        with assert_raises(TooBigToRenderError):
            guid._exception_from_response(mock_response)

    def test_node_addon_get_or_create(self):
        guid, created = self.node_addon.find_or_create_file_guid('/4/2')

        assert_true(created)
        assert_equal(guid.waterbutler_path, '/4/2')

    def test_node_addon_get_or_create_finds(self):
        guid1, created1 = self.node_addon.find_or_create_file_guid('/foo/bar')
        guid2, created2 = self.node_addon.find_or_create_file_guid('/foo/bar')

        assert_true(created1)
        assert_false(created2)
        assert_equals(guid1, guid2)


class TestCallbacks(OsfTestCase):

    def setUp(self):

        super(TestCallbacks, self).setUp()

        self.project = ProjectFactory.build()
        self.consolidated_auth = Auth(self.project.creator)
        self.non_authenticator = UserFactory()
        self.project.save()
        self.project.add_contributor(
            contributor=self.non_authenticator,
            auth=self.consolidated_auth,
        )

        self.project.add_addon('github', auth=self.consolidated_auth)
        self.project.creator.add_addon('github')
        self.node_settings = self.project.get_addon('github')
        self.user_settings = self.project.creator.get_addon('github')
        self.node_settings.user_settings = self.user_settings<commit_msg>Add github model test [skip ci]<commit_after>import mock
import unittest
from nose.tools import *

from github3.repos import Repository
from github3.git import Tree

from tests.base import OsfTestCase
from tests.factories import UserFactory, ProjectFactory

from framework.auth import Auth
from website.addons.github import settings as github_settings
from website.addons.github.exceptions import NotFoundError, TooBigToRenderError
from website.addons.github.tests.factories import GitHubOauthSettingsFactory
from website.addons.github.model import AddonGitHubUserSettings, AddonGitHubOauthSettings, GithubGuidFile

from .utils import create_mock_github
mock_github = create_mock_github()


class TestFileGuid(OsfTestCase):

    def setUp(self):
        super(OsfTestCase, self).setUp()
        self.user = UserFactory()
        self.project = ProjectFactory(creator=self.user)

        self.project.add_addon('github', auth=Auth(self.user))
        self.node_addon = self.project.get_addon('github')

    def test_provider(self):
        assert_equal('github', GithubGuidFile().provider)

    def test_correct_path(self):
        guid, _ = self.node_addon.find_or_create_file_guid('perth')
        assert_equal(guid.waterbutler_path, 'perth')
        assert_equal(guid.waterbutler_path, guid.path)

    @mock.patch('website.addons.base.requests.get')
    def test_unique_identifier(self, mock_get):
        mock_response = mock.Mock(ok=True, status_code=200)
        mock_get.return_value = mock_response
        mock_response.json.return_value = {
            'data': {
                'name': 'Morty',
                'extra': {
                    'fileSha': 'Im a little tea pot'
                }
            }
        }

        guid, _ = self.node_addon.find_or_create_file_guid('perth')
        guid.enrich()

        assert_equal(guid.unique_identifier, 'Im a little tea pot')

    def test_exception_from_response(self):
        mock_response = mock.Mock()
        mock_response.json.return_value = {'errors': [{'code': 'too_large'}]}

        guid, _ = self.node_addon.find_or_create_file_guid('perth')

        with assert_raises(TooBigToRenderError):
            guid._exception_from_response(mock_response)

    def test_node_addon_get_or_create(self):
        guid, created = self.node_addon.find_or_create_file_guid('/4/2')

        assert_true(created)
        assert_equal(guid.waterbutler_path, '/4/2')

    def test_node_addon_get_or_create_finds(self):
        guid1, created1 = self.node_addon.find_or_create_file_guid('/foo/bar')
        guid2, created2 = self.node_addon.find_or_create_file_guid('/foo/bar')

        assert_true(created1)
        assert_false(created2)
        assert_equals(guid1, guid2)

    @mock.patch('website.addons.github.api.GitHub.tree')
    def test_get_existing_files(self, mock_tree):
        path = 'github_testing.txt'
        mock_tree.return_value = Tree.from_json({
            'tree': [
                {
                    'type': 'blob',
                    'path': path,
                }
            ]
        })
        guid, _ = self.node_addon.find_or_create_file_guid('/{}'.format(path))
        self.node_addon.hook_id = 123
        self.node_addon.repo = True
        files = self.node_addon.get_existing_files()
        assert_equal(len(files), 1)
        assert_equal(files[0], guid)


class TestCallbacks(OsfTestCase):

    def setUp(self):

        super(TestCallbacks, self).setUp()

        self.project = ProjectFactory.build()
        self.consolidated_auth = Auth(self.project.creator)
        self.non_authenticator = UserFactory()
        self.project.save()
        self.project.add_contributor(
            contributor=self.non_authenticator,
            auth=self.consolidated_auth,
        )

        self.project.add_addon('github', auth=self.consolidated_auth)
        self.project.creator.add_addon('github')
        self.node_settings = self.project.get_addon('github')
        self.user_settings = self.project.creator.get_addon('github')
        self.node_settings.user_settings = self.user_settings
{'fraction_non_alphanumeric': 0.10081235850312958, 'fraction_numerical': 0.00506059395392196, 'mean_word_length': 3.063852813852814, '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': '6260328', 'n_tokens_mistral': 2368, 'n_tokens_neox': 2280, 'n_words': 361}