def test_notify_reviewers(db, mailoutbox):
    """Test time range 2017-7-1 till 2017-7-31."""
    # a reviewer which will be notified
    reviewer_work = UserFactory.create()
    project_work = ProjectFactory.create()
    project_work.reviewers.add(reviewer_work)
    task_work = TaskFactory.create(project=project_work)
    ReportFactory.create(date=date(2017, 7, 1), task=task_work,
                         verified_by=None)

    # a reviewer which doesn't have any unverfied reports
    reviewer_no_work = UserFactory.create()
    project_no_work = ProjectFactory.create()
    project_no_work.reviewers.add(reviewer_no_work)
    task_no_work = TaskFactory.create(project=project_no_work)
    ReportFactory.create(date=date(2017, 7, 1), task=task_no_work,
                         verified_by=reviewer_no_work)

    call_command('notify_reviewers_unverified')

    # checks
    assert len(mailoutbox) == 1
    mail = mailoutbox[0]
    assert mail.to == [reviewer_work.email]
    url = (
        'http://localhost:4200/reschedule?from_date=2017-07-01&'
        'to_date=2017-07-31&reviewer=%d'
    ) % reviewer_work.id
    assert url in mail.body
Example #2
0
    def setUp(self):
        """Set the environment for the tests up."""
        super().setUp()

        self.projects = ProjectFactory.create_batch(10)

        ProjectFactory.create_batch(10, archived=True)
def test_notify_reviewers_with_cc_and_message(db, mailoutbox, cc, message):
    """Test time range 2017-7-1 till 2017-7-31."""
    # a reviewer which will be notified
    reviewer_work = UserFactory.create()
    project_work = ProjectFactory.create()
    project_work.reviewers.add(reviewer_work)
    task_work = TaskFactory.create(project=project_work)
    ReportFactory.create(date=date(2017, 7, 1),
                         task=task_work,
                         verified_by=None)

    # a reviewer which doesn't have any unverfied reports
    reviewer_no_work = UserFactory.create()
    project_no_work = ProjectFactory.create()
    project_no_work.reviewers.add(reviewer_no_work)
    task_no_work = TaskFactory.create(project=project_no_work)
    ReportFactory.create(date=date(2017, 7, 1),
                         task=task_no_work,
                         verified_by=reviewer_no_work)

    call_command(
        "notify_reviewers_unverified",
        "--cc={0}".format(cc),
        "--message={0}".format(message),
    )

    # checks
    assert len(mailoutbox) == 1
    mail = mailoutbox[0]
    assert mail.to == [reviewer_work.email]
    url = ("http://localhost:4200/analysis?fromDate=2017-07-01&"
           "toDate=2017-07-31&reviewer=%d&editable=1") % reviewer_work.id
    assert url in mail.body
    assert message in mail.body
    assert mail.cc[0] == cc
Example #4
0
def test_project_list_not_archived(auth_client):
    project = ProjectFactory.create(archived=False)
    ProjectFactory.create(archived=True)

    url = reverse('project-list')

    response = auth_client.get(url, data={'archived': 0})
    assert response.status_code == status.HTTP_200_OK

    json = response.json()
    assert len(json['data']) == 1
    assert json['data'][0]['id'] == str(project.id)
Example #5
0
def test_project_list_not_archived(auth_client):
    project = ProjectFactory.create(archived=False)
    ProjectFactory.create(archived=True)

    url = reverse("project-list")

    response = auth_client.get(url, data={"archived": 0})
    assert response.status_code == status.HTTP_200_OK

    json = response.json()
    assert len(json["data"]) == 1
    assert json["data"][0]["id"] == str(project.id)
Example #6
0
def test_subscription_package_filter_customer(auth_client):
    customer = CustomerFactory.create()
    billing_type = BillingTypeFactory.create()
    package = PackageFactory.create(billing_type=billing_type)
    ProjectFactory.create_batch(2, billing_type=billing_type, customer=customer)

    url = reverse("subscription-package-list")

    res = auth_client.get(url, data={"customer": customer.id})
    assert res.status_code == HTTP_200_OK

    json = res.json()
    assert len(json["data"]) == 1
    assert json["data"][0]["id"] == str(package.id)
def test_subscription_project_list(auth_client):
    customer = CustomerFactory.create()
    billing_type = BillingTypeFactory()
    project = ProjectFactory.create(billing_type=billing_type,
                                    customer=customer,
                                    customer_visible=True)
    PackageFactory.create_batch(2, billing_type=billing_type)
    # create spent hours
    task = TaskFactory.create(project=project)
    TaskFactory.create(project=project)
    ReportFactory.create(task=task, duration=timedelta(hours=2))
    ReportFactory.create(task=task, duration=timedelta(hours=3))
    # not billable reports should not be included in spent hours
    ReportFactory.create(not_billable=True,
                         task=task,
                         duration=timedelta(hours=4))
    # project of same customer but without customer_visible set
    # should not appear
    ProjectFactory.create(customer=customer)

    # create purchased time
    OrderFactory.create(project=project,
                        acknowledged=True,
                        duration=timedelta(hours=2))
    OrderFactory.create(project=project,
                        acknowledged=True,
                        duration=timedelta(hours=4))

    # report on different project should not be included in spent time
    ReportFactory.create(duration=timedelta(hours=2))
    # not acknowledged order should not be included in purchased time
    OrderFactory.create(project=project, duration=timedelta(hours=2))

    url = reverse('subscription-project-list')

    res = auth_client.get(url,
                          data={
                              'customer': customer.id,
                              'ordering': 'id'
                          })
    assert res.status_code == HTTP_200_OK

    json = res.json()
    assert len(json['data']) == 1
    assert json['data'][0]['id'] == str(project.id)

    attrs = json['data'][0]['attributes']
    assert attrs['spent-time'] == '05:00:00'
    assert attrs['purchased-time'] == '06:00:00'
Example #8
0
def test_subscription_package_filter_customer(auth_client):
    other_project = ProjectFactory.create()
    PackageFactory.create(billing_type=other_project.billing_type)

    my_project = ProjectFactory.create()
    package = PackageFactory.create(billing_type=my_project.billing_type)

    url = reverse('subscription-package-list')

    res = auth_client.get(url, data={'customer': my_project.customer.id})
    assert res.status_code == HTTP_200_OK

    json = res.json()
    assert len(json['data']) == 1
    assert json['data'][0]['id'] == str(package.id)
def test_work_report_multiple_projects(auth_client, django_assert_num_queries):
    NUM_PROJECTS = 2

    user = auth_client.user
    customer = CustomerFactory.create(name="Customer")
    report_date = date(2017, 8, 17)
    for i in range(NUM_PROJECTS):
        project = ProjectFactory.create(customer=customer, name="Project{0}".format(i))
        task = TaskFactory.create(project=project)
        ReportFactory.create_batch(10, user=user, task=task, date=report_date)

    url = reverse("work-report-list")
    with django_assert_num_queries(3):
        res = auth_client.get(url, data={"user": auth_client.user.id, "verified": 0})
    assert res.status_code == status.HTTP_200_OK
    assert "20170901-WorkReports.zip" in (res["Content-Disposition"])

    content = io.BytesIO(res.content)
    with ZipFile(content, "r") as zipfile:
        for i in range(NUM_PROJECTS):
            ods_content = zipfile.read(
                "1708-20170901-Customer-Project{0}.ods".format(i)
            )
            doc = ezodf.opendoc(io.BytesIO(ods_content))
            table = doc.sheets[0]
            assert table["C5"].value == "2017-08-17"
            assert table["C6"].value == "2017-08-17"
Example #10
0
def test_project_update(auth_client):
    project = ProjectFactory.create()

    url = reverse("project-detail", args=[project.id])

    response = auth_client.patch(url)
    assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED
Example #11
0
    def test_project_default_tasks(self):
        """Should generate tasks based on task templates for a new project."""
        templates = TaskTemplateFactory.create_batch(5)
        project = ProjectFactory.create()
        tasks = Task.objects.filter(project=project)

        assert len(templates) == len(tasks)
Example #12
0
def test_project_detail_no_auth(client):
    project = ProjectFactory.create()

    url = reverse("project-detail", args=[project.id])

    res = client.get(url)
    assert res.status_code == status.HTTP_401_UNAUTHORIZED
Example #13
0
def test_work_report_single_project(auth_client, django_assert_num_queries):
    user = auth_client.user
    # spaces should be replaced with underscore
    customer = CustomerFactory.create(name='Customer Name')
    # slashes should be dropped from file name
    project = ProjectFactory.create(customer=customer, name='Project/')
    task = TaskFactory.create(project=project)
    ReportFactory.create_batch(
        10, user=user, verified_by=user, task=task, date=date(2017, 8, 17)
    )

    url = reverse('work-report-list')
    with django_assert_num_queries(4):
        res = auth_client.get(url, data={
            'user': auth_client.user.id,
            'from_date': '2017-08-01',
            'to_date': '2017-08-31',
            'verified': 1
        })
    assert res.status_code == HTTP_200_OK
    assert '1708-20170901-Customer_Name-Project.ods' in (
        res['Content-Disposition']
    )

    content = io.BytesIO(res.content)
    doc = ezodf.opendoc(content)
    table = doc.sheets[0]
    assert table['C5'].value == '2017-08-01'
    assert table['C6'].value == '2017-08-31'
    assert table['C9'].value == 'Test User'
    assert table['C10'].value == 'Test User'
Example #14
0
def test_work_report_multiple_projects(auth_client, django_assert_num_queries):
    NUM_PROJECTS = 2

    user = auth_client.user
    customer = CustomerFactory.create(name='Customer')
    report_date = date(2017, 8, 17)
    for i in range(NUM_PROJECTS):
        project = ProjectFactory.create(
            customer=customer, name='Project{0}'.format(i)
        )
        task = TaskFactory.create(project=project)
        ReportFactory.create_batch(10, user=user, task=task, date=report_date)

    url = reverse('work-report-list')
    with django_assert_num_queries(4):
        res = auth_client.get(url, data={
            'user': auth_client.user.id,
            'verified': 0
        })
    assert res.status_code == HTTP_200_OK
    assert '20170901-WorkReports.zip' in (
        res['Content-Disposition']
    )

    content = io.BytesIO(res.content)
    with ZipFile(content, 'r') as zipfile:
        for i in range(NUM_PROJECTS):
            ods_content = zipfile.read(
                '1708-20170901-Customer-Project{0}.ods'.format(i)
            )
            doc = ezodf.opendoc(io.BytesIO(ods_content))
            table = doc.sheets[0]
            assert table['C5'].value == '2017-08-17'
            assert table['C6'].value == '2017-08-17'
Example #15
0
def test_work_report_single_project(auth_client, django_assert_num_queries):
    user = auth_client.user
    # spaces should be replaced with underscore
    customer = CustomerFactory.create(name="Customer Name")
    # slashes should be dropped from file name
    project = ProjectFactory.create(customer=customer, name="Project/")
    task = TaskFactory.create(project=project)
    ReportFactory.create_batch(10,
                               user=user,
                               verified_by=user,
                               task=task,
                               date=date(2017, 8, 17))

    url = reverse("work-report-list")
    with django_assert_num_queries(3):
        res = auth_client.get(
            url,
            data={
                "user": auth_client.user.id,
                "from_date": "2017-08-01",
                "to_date": "2017-08-31",
                "verified": 1,
            },
        )
    assert res.status_code == status.HTTP_200_OK
    assert "1708-20170901-Customer_Name-Project.ods" in (
        res["Content-Disposition"])

    content = io.BytesIO(res.content)
    doc = ezodf.opendoc(content)
    table = doc.sheets[0]
    assert table["C5"].value == "2017-08-01"
    assert table["C6"].value == "2017-08-31"
    assert table["C9"].value == "Test User"
    assert table["C10"].value == "Test User"
Example #16
0
def test_user_is_reviewer_filter(auth_client, value, expected):
    """Should filter users if they are a reviewer."""
    user = UserFactory.create()
    project = ProjectFactory.create()
    UserFactory.create_batch(3)

    project.reviewers.add(user)

    res = auth_client.get(reverse('user-list'), {'is_reviewer': value})
    assert len(res.json()['data']) == expected
def test_subscription_project_detail(auth_client):
    billing_type = BillingTypeFactory()
    project = ProjectFactory.create(billing_type=billing_type)
    PackageFactory.create_batch(2, billing_type=billing_type)

    url = reverse('subscription-project-detail', args=[project.id])
    res = auth_client.get(url)
    assert res.status_code == HTTP_200_OK
    json = res.json()
    assert json['data']['id'] == str(project.id)
Example #18
0
def test_project_detail_no_reports(auth_client):
    project = ProjectFactory.create()

    url = reverse("project-detail", args=[project.id])

    res = auth_client.get(url)

    assert res.status_code == status.HTTP_200_OK
    json = res.json()

    assert json["meta"]["spent-time"] == "00:00:00"
Example #19
0
def test_project_detail_no_reports(auth_client):
    project = ProjectFactory.create()

    url = reverse('project-detail', args=[project.id])

    res = auth_client.get(url)

    assert res.status_code == status.HTTP_200_OK
    json = res.json()

    assert json['meta']['spent-time'] == '00:00:00'
def test_subscription_project_detail(auth_client):
    billing_type = BillingTypeFactory()
    project = ProjectFactory.create(billing_type=billing_type,
                                    customer_visible=True)
    PackageFactory.create_batch(2, billing_type=billing_type)

    url = reverse("subscription-project-detail", args=[project.id])
    res = auth_client.get(url)
    assert res.status_code == HTTP_200_OK
    json = res.json()
    assert json["data"]["id"] == str(project.id)
def test_generate_work_report_name(db, customer_name, project_name, expected):
    test_date = date(2017, 8, 18)
    view = WorkReportViewSet()

    # spaces should be replaced with underscore
    customer = CustomerFactory.create(name=customer_name)
    # slashes should be dropped from file name
    project = ProjectFactory.create(customer=customer, name=project_name)

    name = view._generate_workreport_name(test_date, test_date, project)
    assert name == expected
Example #22
0
def test_project_detail_with_reports(auth_client):
    project = ProjectFactory.create()
    task = TaskFactory.create(project=project)
    ReportFactory.create_batch(10, task=task, duration=timedelta(hours=1))

    url = reverse("project-detail", args=[project.id])

    res = auth_client.get(url)

    assert res.status_code == status.HTTP_200_OK
    json = res.json()

    assert json["meta"]["spent-time"] == "10:00:00"
Example #23
0
def test_report_list_filter_cost_center(auth_client):
    cost_center = CostCenterFactory.create()
    # 1st valid case: report with task of given cost center
    # but different project cost center
    task = TaskFactory.create(cost_center=cost_center)
    report_task = ReportFactory.create(task=task)
    # 2nd valid case: report with project of given cost center
    project = ProjectFactory.create(cost_center=cost_center)
    task = TaskFactory.create(cost_center=None, project=project)
    report_project = ReportFactory.create(task=task)
    # Invalid case: report without cost center
    project = ProjectFactory.create(cost_center=None)
    task = TaskFactory.create(cost_center=None, project=project)
    ReportFactory.create(task=task)

    url = reverse("report-list")

    res = auth_client.get(url, data={"cost_center": cost_center.id})
    assert res.status_code == status.HTTP_200_OK
    json = res.json()
    assert len(json["data"]) == 2
    ids = {int(entry["id"]) for entry in json["data"]}
    assert {report_task.id, report_project.id} == ids
def test_work_report_count(
    auth_client, settings, settings_count, given_count, expected_status
):
    user = auth_client.user
    customer = CustomerFactory.create(name="Customer")
    report_date = date(2017, 8, 17)

    settings.WORK_REPORTS_EXPORT_MAX_COUNT = settings_count

    project = ProjectFactory.create(customer=customer)
    task = TaskFactory.create(project=project)
    ReportFactory.create_batch(given_count, user=user, task=task, date=report_date)

    url = reverse("work-report-list")
    res = auth_client.get(url, data={"user": auth_client.user.id, "verified": 0})

    assert res.status_code == expected_status
def test_redmine_report_no_estimated_time(db, freezer, mocker):
    redmine_instance = mocker.MagicMock()
    issue = mocker.MagicMock()
    redmine_instance.issue.get.return_value = issue
    redmine_class = mocker.patch('redminelib.Redmine')
    redmine_class.return_value = redmine_instance

    freezer.move_to('2017-07-28')
    project = ProjectFactory.create(estimated_time=None)
    task = TaskFactory.create(project=project)
    report = ReportFactory.create(comment='ADSY <=> Other', task=task)
    RedmineProject.objects.create(project=report.task.project, issue_id=1000)

    freezer.move_to('2017-07-31')
    call_command('redmine_report', options={'--last-days': '7'})

    redmine_instance.issue.get.assert_called_once_with(1000)
    issue.save.assert_called_once_with()
def test_redmine_report_no_estimated_time(db, freezer, mocker):
    redmine_instance = mocker.MagicMock()
    issue = mocker.MagicMock()
    redmine_instance.issue.get.return_value = issue
    redmine_class = mocker.patch("redminelib.Redmine")
    redmine_class.return_value = redmine_instance

    freezer.move_to("2017-07-28")
    project = ProjectFactory.create(estimated_time=None)
    task = TaskFactory.create(project=project)
    report = ReportFactory.create(comment="ADSY <=> Other", task=task)
    RedmineProject.objects.create(project=report.task.project, issue_id=1000)

    freezer.move_to("2017-07-31")
    call_command("redmine_report", last_days=7)

    redmine_instance.issue.get.assert_called_once_with(1000)
    issue.save.assert_called_once_with()
Example #27
0
def test_project_list_include(auth_client, django_assert_num_queries):
    project = ProjectFactory.create()
    users = UserFactory.create_batch(2)
    project.reviewers.add(*users)

    url = reverse('project-list')

    with django_assert_num_queries(6):
        response = auth_client.get(
            url,
            data={
                'include':
                ','.join(ProjectSerializer.included_serializers.keys())
            })
    assert response.status_code == status.HTTP_200_OK

    json = response.json()
    assert len(json['data']) == 1
    assert json['data'][0]['id'] == str(project.id)
def test_notify_reviewers(db, mailoutbox):
    """Test time range 2017-7-1 till 2017-7-31."""
    # a reviewer which will be notified
    reviewer_work = UserFactory.create()
    project_work = ProjectFactory.create()
    project_work.reviewers.add(reviewer_work)
    task_work = TaskFactory.create(project=project_work)
    ReportFactory.create(date=date(2017, 7, 1),
                         task=task_work,
                         verified_by=None)

    call_command("notify_reviewers_unverified")

    # checks
    assert len(mailoutbox) == 1
    mail = mailoutbox[0]
    assert mail.to == [reviewer_work.email]
    url = ("http://localhost:4200/analysis?fromDate=2017-07-01&"
           "toDate=2017-07-31&reviewer=%d&editable=1") % reviewer_work.id
    assert url in mail.body