#
# Unit tests for the viewer.web package.
#
# Copyright: (c) 2014 by Petr Zemek <s3rvac@gmail.com> and contributors
# License: BSD, see LICENSE for more details
#

import unittest
from unittest import mock

import viewer
import viewer.web


class WebTests(unittest.TestCase):
    def setUp(self):
        # Create a mocked repository.
        self.repo_mock = mock.MagicMock(spec=viewer.git.Repo)

        # Patch repository creation.
        patcher = mock.patch('viewer.git.Repo', return_value=self.repo_mock)
        self.addCleanup(patcher.stop)
        self.repo_cls_mock = patcher.start()

        self.app = viewer.web.app.test_client()

    def test_index_page_exists(self):
        rv = self.app.get('/')
        self.assertEqual(rv.status_code, 200)

    def test_repo_is_initialized_with_path_from_config(self):
        REPO_PATH = '/path/to/repo'
        viewer.web.app.config['GIT_REPO_PATH'] = REPO_PATH
        rv = self.app.get('/')
        self.repo_cls_mock.assert_called_once_with(REPO_PATH)

    def test_repo_name_is_shown_on_index_page(self):
        REPO_NAME = 'my testing repo'
        type(self.repo_mock).name = mock.PropertyMock(return_value=REPO_NAME)
        rv = self.app.get('/')
        self.assertIn(REPO_NAME, rv.data.decode())

    def test_remote_from_config_is_used_when_getting_branches(self):
        REMOTE = 'test_remote'
        viewer.web.app.config['GIT_REMOTE'] = REMOTE
        rv = self.app.get('/')
        self.repo_mock.get_branches_on_remote.assert_called_with(REMOTE)

    def test_remote_is_shown_on_index_page(self):
        REMOTE = 'test_remote'
        viewer.web.app.config['GIT_REMOTE'] = REMOTE
        rv = self.app.get('/')
        self.assertIn(REMOTE, rv.data.decode())

    def test_branches_are_shown_on_index_page(self):
        REMOTE = 'test_remote'
        BRANCHES = [
            viewer.git.Branch(self.repo_mock, REMOTE, 'test_branch1'),
            viewer.git.Branch(self.repo_mock, REMOTE, 'test_branch2')
        ]
        self.repo_mock.get_branches_on_remote.return_value = BRANCHES
        rv = self.app.get('/')
        for branch in BRANCHES:
            self.assertIn(branch.name, rv.data.decode())

@@ -270,16 +270,59 @@
 tCase):%0A
+    %22%22%22A base class for all web tests.%22%22%22%0A%0A
     def 
@@ -678,24 +678,109 @@
 t_client()%0A%0A
+%0Aclass GeneralIndexPageTests(WebTests):%0A    %22%22%22General tests for the index page.%22%22%22%0A%0A
     def test
@@ -1410,49 +1410,30 @@
 ote_
-from_config_is_used_when_getting_branches
+is_shown_on_index_page
 (sel
@@ -1568,335 +1568,190 @@
 elf.
-repo_mock.get_branches_on_remote.assert_called_with(REMOTE)%0A%0A    def test_remote_is_shown_on_i
+assertIn(REMOTE, rv.data.decode())%0A%0A%0Aclass BranchesOnI
 ndex
-_page(self):%0A        REMOTE = 'test_remote'%0A        viewer.web.app.config%5B'GIT_REMOTE'%5D = REMOTE%0A        rv = self.app.get('/')%0A        self.assertIn(REMOTE, rv.data.decode())%0A%0A    def test_branches_are_shown_on_index_page(self):
+PageTests(WebTests):%0A    %22%22%22Tests for the branches shown on the index page.%22%22%22%0A%0A    def setUp(self):%0A        super().setUp()
 %0A   
@@ -1747,32 +1747,37 @@
 setUp()%0A        
+self.
 REMOTE = 'test_r
@@ -1791,16 +1791,21 @@
         
+self.
 BRANCHES
@@ -1847,32 +1847,37 @@
 self.repo_mock, 
+self.
 REMOTE, 'test_br
@@ -1931,16 +1931,21 @@
 o_mock, 
+self.
 REMOTE, 
@@ -1969,16 +1969,307 @@
        %5D
+%0A%0A    def test_remote_from_config_is_used_when_getting_branches(self):%0A        viewer.web.app.config%5B'GIT_REMOTE'%5D = self.REMOTE%0A        rv = self.app.get('/')%0A        self.repo_mock.get_branches_on_remote.assert_called_with(self.REMOTE)%0A%0A    def test_branches_are_shown_on_index_page(self):
 %0A       
@@ -2322,16 +2322,21 @@
 value = 
+self.
 BRANCHES
@@ -2389,16 +2389,21 @@
 anch in 
+self.
 BRANCHES

Refactor the used test cases.