import mock
from nose.tools import *  # PEP8 asserts
from tests.base import DbTestCase
from tests.factories import ProjectFactory, AuthUserFactory
from website.addons.github.tests.utils import create_mock_github

from webtest_plus import TestApp
import website.app
app = website.app.init_app(
    routes=True, set_backends=False, settings_module='website.settings'
)


class TestGitHubPage(DbTestCase):

    def setUp(self):
        self.user = AuthUserFactory()
        self.app = TestApp(app)
        self.project = ProjectFactory(creator=self.user)
        self.project.add_addon('github')
        self.project.creator.add_addon('github')

        self.github = create_mock_github(user='fred', private=False)

        self.node_settings = self.project.get_addon('github')
        self.node_settings.user_settings = self.project.creator.get_addon('github')
        # Set the node addon settings to correspond to the values of the mock repo
        self.node_settings.user = self.github.repo.return_value['owner']['login']
        self.node_settings.repo = self.github.repo.return_value['name']
        self.node_settings.save()

    def test_can_see_files_tab(self):
        url = "/project/{0}/".format(self.project._id)
        res = self.app.get(url, auth=self.user.auth)
        assert_in('a href="/{0}/files/"'.format(self.project._id), res)

    def test_github_widget_present(self):
        url = "/project/{0}/".format(self.project._id)
        res = self.app.get(url, auth=self.user.auth)
        addon_headers = res.html.find_all(attrs={'class': 'addon-widget-header'})
        assert_true(
            any([
                'GitHub' in header.text
                for header in addon_headers
            ])
        )

    def test_files_widget_present(self):
        url = "/project/{0}/".format(self.project._id)
        res = self.app.get(url, auth=self.user.auth)
        addon_headers = res.html.find_all(attrs={'class': 'addon-widget-header'})
        assert_true(
            any([
                'Files' in header.text
                for header in addon_headers
            ])
        )

    def test_github_widget_without_auth(self):
        self.node_settings.user = "nosense"
        self.node_settings.save()
        url = "/project/{0}/".format(self.project._id)
        res = self.app.get(url, auth=self.user.auth)
        assert_in(" GitHub add-on is not configured properly. Configure this add-on", res)
        self.node_settings.user = self.github.repo.return_value['owner']['login']
        self.node_settings.save()

    @mock.patch('website.addons.github.api.GitHub.commits')
    @mock.patch('website.addons.github.api.GitHub.file')
    @mock.patch('website.addons.github.api.GitHub.repo')
    def test_file_view(self, mock_repo, mock_file, mock_commits):
        mock_commits.return_value = [{
            "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
            "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
            "commit": {
                "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
                "author": {
                    "name": "Monalisa Octocat",
                    "email": "support@github.com",
                   "date": "2011-04-14T16:00:49Z"
                }
            }
        }]

        mock_repo.return_value = {
            "default_branch": "dev",
            'url': u'https://api.github.com/repos/{user}/mock-repo/git/trees/dev'.format(user=self.user),
            'sha': 'dev',
            'private': False,
            'tree': [
                {u'mode': u'100644',
                 u'path': u'coveragerc',
                 u'sha': u'92029ff5ce192425d346b598d7e7dd25f5f05185',
                 u'size': 245,
                 u'type': u'file',
                 u'url': u'https://api.github.com/repos/{user}/mock-repo/git/blobs/92029ff5ce192425d346b598d7e7dd25f5f05185'.format(user=self.user)}]
        }

        mock_file.return_value = {
            u'name': u'coveragerc',
            u'content': u'ClRleHRCbG9iOiBTaW1wbGlmaWVkIFRleHQgUHJvY2Vzc2luZwo9PT09PT09',
            u'size': 245
        }

        url = "/project/{0}/github/file/{1}/".format(
            self.project._id,
            "coveragerc"
        )
        self.app.auth = self.user.auth
        res = self.app.get(url).maybe_follow()
        assert_in("6dcb09b5b57875f334f61aebed695e2e4193db5e", res)
        assert_in("2011-04-14T16:00:49Z", res)
        assert_in("file-version-history", res)
        assert_in("icon-download-alt", res)

@@ -382,12 +382,14 @@
 tHub
-Pa
+Wid
 ge
+t
 (DbT
@@ -1264,32 +1264,32 @@
 self.user.auth)%0A
+
         assert_i
@@ -1349,1209 +1349,8 @@
 s)%0A%0A
-    def test_github_widget_present(self):%0A        url = %22/project/%7B0%7D/%22.format(self.project._id)%0A        res = self.app.get(url, auth=self.user.auth)%0A        addon_headers = res.html.find_all(attrs=%7B'class': 'addon-widget-header'%7D)%0A        assert_true(%0A            any(%5B%0A                'GitHub' in header.text%0A                for header in addon_headers%0A            %5D)%0A        )%0A%0A    def test_files_widget_present(self):%0A        url = %22/project/%7B0%7D/%22.format(self.project._id)%0A        res = self.app.get(url, auth=self.user.auth)%0A        addon_headers = res.html.find_all(attrs=%7B'class': 'addon-widget-header'%7D)%0A        assert_true(%0A            any(%5B%0A                'Files' in header.text%0A                for header in addon_headers%0A            %5D)%0A        )%0A%0A    def test_github_widget_without_auth(self):%0A        self.node_settings.user = %22nosense%22%0A        self.node_settings.save()%0A        url = %22/project/%7B0%7D/%22.format(self.project._id)%0A        res = self.app.get(url, auth=self.user.auth)%0A        assert_in(%22 GitHub add-on is not configured properly. Configure this add-on%22, res)%0A        self.node_settings.user = self.github.repo.return_value%5B'owner'%5D%5B'login'%5D%0A        self.node_settings.save()%0A%0A
     

Remove github widget tests since widget was removed from project page