Skip to content

Commit

Permalink
[Issue #174] fixed wrong variable name in the work entry form
Browse files Browse the repository at this point in the history
[Issue #174] because of failed test in case of non existing Human Resource for active user there was an unhandled exception. Created proper exception handling
  • Loading branch information
scaphilo committed Oct 10, 2018
1 parent 55103c9 commit d6a137a
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 3 deletions.
9 changes: 9 additions & 0 deletions koalixcrm/crm/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ def __str__(self):
return repr(self.value)


class UserIsNoHumanResource(Exception):
def __init__(self, value):
self.value = value
self.view = "/koalixcrm/crm/reporting/user_is_not_human_resource"

def __str__(self):
return repr(self.value)


class ReportingPeriodDoneDeleteNotPossible(Exception):
def __init__(self, value):
self.value = value
Expand Down
2 changes: 2 additions & 0 deletions koalixcrm/crm/reporting/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from koalixcrm.crm.views.time_tracking import work_report
from koalixcrm.crm.views.user_extension_missing import user_extension_missing
from koalixcrm.crm.views.reporting_period_missing import reporting_period_missing
from koalixcrm.crm.views.user_is_not_human_resource import user_is_not_human_resource

urlpatterns = [
url(r'^time_tracking/$', work_report, name="monthly_report"),
url(r'^user_extension_missing/$', user_extension_missing, name="user_extension_missing"),
url(r'^reporting_period_missing/$', reporting_period_missing, name="reporting_period_missing"),
url(r'^user_is_not_human_resource/$', user_is_not_human_resource, name="user_is_not_human_resource"),
]
70 changes: 70 additions & 0 deletions koalixcrm/crm/tests/test_user_is_not_human_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import pytest
from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from koalixcrm.test_support_functions import *
from koalixcrm.crm.factories.factory_user import AdminUserFactory
from koalixcrm.crm.factories.factory_customer import StandardCustomerFactory
from koalixcrm.crm.factories.factory_customer_group import StandardCustomerGroupFactory
from koalixcrm.crm.factories.factory_currency import StandardCurrencyFactory
from koalixcrm.crm.factories.factory_human_resource import StandardHumanResourceFactory
from koalixcrm.djangoUserExtension.factories.factory_user_extension import StandardUserExtensionFactory


class TimeTrackingAddRow(LiveServerTestCase):

def setUp(self):
firefox_options = webdriver.firefox.options.Options()
firefox_options.set_headless(headless=True)
self.selenium = webdriver.Firefox(firefox_options=firefox_options)
self.test_user = AdminUserFactory.create()
self.test_customer_group = StandardCustomerGroupFactory.create()
self.test_customer = StandardCustomerFactory.create(is_member_of=(self.test_customer_group,))
self.test_currency = StandardCurrencyFactory.create()
self.test_user_extension = StandardUserExtensionFactory.create(user=self.test_user)

def tearDown(self):
self.selenium.quit()

@pytest.mark.front_end_tests
def test_add_new_row(self):
selenium = self.selenium
# login
selenium.get('%s%s' % (self.live_server_url, '/koalixcrm/crm/reporting/time_tracking/'))
# the browser will be redirected to the login page
timeout = 5
try:
element_present = expected_conditions.presence_of_element_located((By.ID, 'id_username'))
WebDriverWait(selenium, timeout).until(element_present)
except TimeoutException:
print("Timed out waiting for page to load")
username = selenium.find_element_by_xpath('//*[@id="id_username"]')
password = selenium.find_element_by_xpath('//*[@id="id_password"]')
submit_button = selenium.find_element_by_xpath('/html/body/div/article/div/div/form/div/ul/li/input')
username.send_keys("admin")
password.send_keys("admin")
submit_button.send_keys(Keys.RETURN)
# after the login, the browser is redirected to the target url /koalixcrm/crm/reporting/time_tracking
try:
element_present = expected_conditions.presence_of_element_located((By.ID, 'id_form-0-project'))
WebDriverWait(selenium, timeout).until(element_present)
except TimeoutException:
print("Timed out waiting for page to load")
# find the form element
assert_when_element_does_not_exist(self, '//*[@id="id_form-0-project"]')
assert_when_element_does_not_exist(self, '//*[@id="id_form-0-task"]')
assert_when_element_does_not_exist(self, '//*[@id="id_form-0-date"]')
assert_when_element_does_not_exist(self, '//*[@id="id_form-0-start_time"]')
assert_when_element_does_not_exist(self, '//*[@id="id_form-0-stop_time"]')
assert_when_element_does_not_exist(self, '//*[@id="id_form-0-description"]')
assert_when_element_does_not_exist(self, 'save')
# check existence of a second form before pressing add more
assert_when_element_exists(self, '//*[@id="id_form-1-project"]')
add_more_button = selenium.find_element_by_xpath('//*[@id="add_more"]')
add_more_button.send_keys(Keys.RETURN)
# check existence of a second form after pressing add more
assert_when_element_does_not_exist(self, '//*[@id="id_form-1-project"]')
10 changes: 8 additions & 2 deletions koalixcrm/crm/views/time_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.contrib.auth.decorators import login_required
from koalixcrm.djangoUserExtension.models import UserExtension
from koalixcrm.crm.exceptions import ReportingPeriodNotFound
from koalixcrm.crm.exceptions import UserIsNoHumanResource
from koalixcrm.djangoUserExtension.exceptions import UserExtensionMissing, TooManyUserExtensionsAvailable
from koalixcrm.crm.views.range_selection_form import RangeSelectionForm
from koalixcrm.crm.views.work_entry_formset import BaseWorkEntryFormset
Expand All @@ -18,7 +19,9 @@
@login_required
def work_report(request):
try:
human_resource = HumanResource.objects.get(user=UserExtension.get_user_extension(request.user))
human_resource = HumanResource.objects.filter(user=UserExtension.get_user_extension(request.user))
if human_resource is None:
raise UserIsNoHumanResource()
if request.POST.get('post'):
if 'cancel' in request.POST:
return HttpResponseRedirect('/admin/')
Expand Down Expand Up @@ -58,12 +61,15 @@ def work_report(request):
return render(request, 'crm/admin/time_reporting.html', c)
except (UserExtensionMissing,
ReportingPeriodNotFound,
TooManyUserExtensionsAvailable) as e:
TooManyUserExtensionsAvailable,
UserIsNoHumanResource) as e:
if isinstance(e, UserExtensionMissing):
return HttpResponseRedirect(e.view)
elif isinstance(e, ReportingPeriodNotFound):
return HttpResponseRedirect(e.view)
elif isinstance(e, TooManyUserExtensionsAvailable):
return HttpResponseRedirect(e.view)
elif isinstance(e, UserIsNoHumanResource):
return HttpResponseRedirect(e.view)
else:
raise Http404
45 changes: 45 additions & 0 deletions koalixcrm/crm/views/user_is_not_human_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-

from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render
from django.template.context_processors import csrf
from django.contrib.admin import helpers
from django.contrib.admin.widgets import *
from koalixcrm.djangoUserExtension.exceptions import TooManyUserExtensionsAvailable


class ReportingPeriodMissingForm(forms.Form):
NEXT_STEPS = (
('create_human_resource', 'Create Human Resource'),
('return_to_start', 'Return To Start'),
)
next_steps = forms.ChoiceField(required=True,
widget=forms.Select,
choices=NEXT_STEPS)


def user_is_not_human_resource(request):
try:
if request.POST.get('post'):
if 'confirm_selection' in request.POST:
reporting_period_missing_form = ReportingPeriodMissingForm(request.POST)
if reporting_period_missing_form.is_valid():
if reporting_period_missing_form.cleaned_data['next_steps'] == 'return_to_start':
return HttpResponseRedirect('/admin/')
else:
return HttpResponseRedirect('/admin/crm/humanresource/add/')
else:
reporting_period_missing_form = ReportingPeriodMissingForm(initial={'next_steps': 'create_user_extension'})
title = "User is not registered as Human Resource"
description = "The operation you have selected requires that the currently active user is a registered " \
"as a Human Resource. "
c = {'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
'form': reporting_period_missing_form,
'description': description,
'title': title}
c.update(csrf(request))
return render(request, 'crm/admin/exception.html', c)
except TooManyUserExtensionsAvailable:
raise Http404


3 changes: 2 additions & 1 deletion koalixcrm/crm/views/work_entry_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from koalixcrm.global_support_functions import limit_string_length
from koalixcrm.djangoUserExtension.models import UserExtension
from koalixcrm.crm.reporting.reporting_period import ReportingPeriod
from koalixcrm.crm.reporting.human_resource import HumanResource


class WorkEntry(forms.Form):
Expand Down Expand Up @@ -100,7 +101,7 @@ def update_work(self, request):
work.task = self.cleaned_data['task']
work.reporting_period = ReportingPeriod.get_reporting_period(project=self.cleaned_data['task'].project,
search_date=self.cleaned_data['date'])
work.resource = UserExtension.get_user_extension(request.user)
work.human_resource = HumanResource.objects.get(user=UserExtension.get_user_extension(request.user))
work.date = self.cleaned_data['date']
if bool(self.cleaned_data['start_time']) & bool(self.cleaned_data['stop_time']):
work.start_time = datetime.datetime.combine(self.cleaned_data['date'],
Expand Down

0 comments on commit d6a137a

Please sign in to comment.