# -*- coding: utf-8 -*-
#   Copyright 2008-2009 agile42 GmbH, Berlin (Germany)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#   
#    Authors: 
#       - Andrea Tomasini <andrea.tomasini__at__agile42.com>
#       - Felix Schwarz <felix.schwarz__at__agile42.com>

from trac.tests.functional import tc

from agilo.scrum.contingent import ContingentModelManager
from agilo.scrum.sprint import SprintModelManager

from agilo.test import Usernames
from agilo.test.functional import AgiloTestCase


class TestAddTimeToContingent(AgiloTestCase):
    
    def _create_contingents(self, team_name, sprint_name):
        self._tester.create_sprint_with_team(sprint_name, team_name)
        self._tester.create_new_contingent('foo', '6', team_name, sprint_name)
        self._tester.create_new_contingent('bar', '4', team_name, sprint_name)
    
    def _check_actual_contingent_value(self, sprint_name, name, value):
        env = self._testenv.get_trac_environment()
        contingent = ContingentModelManager(env).get(sprint=sprint_name, 
                                                     name=name)
        self.assertTrue(contingent.exists)
        self.assertEqual(name, contingent.name)
        self.assertEqual(value, contingent.actual)
    
    def runTest(self):
        sprint_name = 'AddTimeToContingentSprint'
        team_name = 'AddTimeToContingentTeam'
        
        self._tester.login_as(Usernames.admin)
        self._create_contingents(team_name, sprint_name)
        
        self._tester.add_time_to_contingent('foo', sprint_name, 2)
        
        # product owner must not update the actual time 
        self._tester.login_as(Usernames.product_owner)
        self._tester.navigate_to_sprint_backlog(sprint_name)
        tc.notfind('col_add_time_foo')
        
        self._check_actual_contingent_value(sprint_name, 'foo', 2)


class TestDeleteContingent(AgiloTestCase):
    
    def _product_owner_can_not_touch_contingents(self, sprint_name):
        self._tester.login_as(Usernames.product_owner)
        self._tester.navigate_to_sprint_backlog(sprint_name)
        tc.notfind('type="checkbox" value="bar" name="sel"')
    
    def runTest(self):
        sprint_name = 'DeleteContingentSprint'
        team_name = 'DeleteContingentTeam'
        contingent_name = 'bar'
        
        self._tester.login_as(Usernames.admin)
        self._tester.create_sprint_with_team(sprint_name, team_name)
        self._tester.create_new_contingent(contingent_name, '6', team_name, 
                                           sprint_name)
        self._tester.create_new_contingent('quux', '4', team_name, sprint_name)
        
        self._product_owner_can_not_touch_contingents(sprint_name)
        
        self._tester.login_as(Usernames.scrum_master)
        self._tester.navigate_to_sprint_backlog(sprint_name)
        tc.formvalue('contingent_form', 'sel', '+%s' % \
                     contingent_name)
        tc.submit('remove')
        tc.code(200)
        
        env = self._testenv.get_trac_environment()
        sprint = SprintModelManager(env).get(name=sprint_name)
        self.assertNotEqual(None, sprint)
        contingent = ContingentModelManager(env).get(sprint=sprint, 
                         name=contingent_name)
        self.assertEqual(None, contingent)


class TestCanNotCreateContingentWithoutName(AgiloTestCase):
    """Regression test: A new contingent needs to have a name."""
    
    def runTest(self):
        sprint_name = 'AddTimeToContingentSprint'
        team_name = 'AddTimeToContingentTeam'
        
        self._tester.login_as(Usernames.admin)
        self._tester.create_sprint_with_team(sprint_name, team_name)
        
        self.tester.create_new_contingent('', '4', team_name, 
                sprint_name, should_fail=True)
        
        env = self._testenv.get_trac_environment()
        contingent = ContingentModelManager(env).get(sprint=sprint_name, 
                                                     name='')
        self.assertEqual(None, contingent)


class TestErrorMessageIsDisplayedWhenNonNumericAmountIsEntered(AgiloTestCase):
    
    def runTest(self):
        sprint_name = 'AddTimeToContingentSprint'
        team_name = 'AddTimeToContingentTeam'
        
        self._tester.login_as(Usernames.admin)
        self._tester.create_sprint_with_team(sprint_name, team_name)
        self.tester.create_new_contingent('Bug-Fixing', '4', team_name, sprint_name)
        
        self._tester.add_time_to_contingent('foo', sprint_name, 'invalid_amount', 
                                            should_fail=True)
        tc.find('Invalid number for additional time')


if __name__ == '__main__':
    from agilo.test.testfinder import run_all_tests
    run_all_tests(__file__)

