<commit_before>from django.contrib.auth.models import Group
from django.test import TestCase
from rest_framework.test import APIRequestFactory

import factories
from feed.views import WorkflowLevel1ViewSet
from workflow.models import (WorkflowTeam, WorkflowLevel1,
                             ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_TEAM,
                             ROLE_PROGRAM_ADMIN)


class WorkflowLevel1ViewsTest(TestCase):
    def setUp(self):
        self.factory = APIRequestFactory()
        self.tola_user = factories.TolaUser()
        factories.Group()

    def test_list_workflowlevel1_superuser(self):
        wflvl1 = factories.WorkflowLevel1()
        wflvl2 = factories.WorkflowLevel2(workflowlevel1=wflvl1)

        self.tola_user.user.is_staff = True
        self.tola_user.user.is_superuser = True
        self.tola_user.user.save()

        request = self.factory.get('/api/workflowlevel1/')
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['name'], wflvl1.name)
        self.assertEqual(response.data[0]['budget'],
                         wflvl2.total_estimated_budget)
        self.assertEqual(response.data[0]['actuals'], wflvl2.actual_cost)

    def test_list_workflowlevel1_org_admin(self):
        wflvl1 = factories.WorkflowLevel1(
            organization=self.tola_user.organization)
        wflvl2 = factories.WorkflowLevel2(workflowlevel1=wflvl1)
        group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN)
        self.tola_user.user.groups.add(group_org_admin)

        request = self.factory.get('/api/workflowlevel1/')
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['name'], wflvl1.name)
        self.assertEqual(response.data[0]['budget'],
                         wflvl2.total_estimated_budget)
        self.assertEqual(response.data[0]['actuals'], wflvl2.actual_cost)

    def test_list_workflowlevel1_program_admin(self):
        wflvl1 = factories.WorkflowLevel1(
            organization=self.tola_user.organization)
        wflvl2 = factories.WorkflowLevel2(workflowlevel1=wflvl1)
        group_program_admin = factories.Group(name=ROLE_PROGRAM_ADMIN)
        WorkflowTeam.objects.create(
            workflow_user=self.tola_user, workflowlevel1=wflvl1,
            role=group_program_admin)

        request = self.factory.get('/api/workflowlevel1/')
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['name'], wflvl1.name)
        self.assertEqual(response.data[0]['budget'],
                         wflvl2.total_estimated_budget)
        self.assertEqual(response.data[0]['actuals'], wflvl2.actual_cost)

    def test_list_workflowlevel1_program_team(self):
        wflvl1 = factories.WorkflowLevel1(
            organization=self.tola_user.organization)
        wflvl2 = factories.WorkflowLevel2(workflowlevel1=wflvl1)
        group_program_team = factories.Group(name=ROLE_PROGRAM_TEAM)
        WorkflowTeam.objects.create(
            workflow_user=self.tola_user, workflowlevel1=wflvl1,
            role=group_program_team)

        request = self.factory.get('/api/workflowlevel1/')
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['name'], wflvl1.name)
        self.assertEqual(response.data[0]['budget'],
                         wflvl2.total_estimated_budget)
        self.assertEqual(response.data[0]['actuals'], wflvl2.actual_cost)

    def test_list_workflowlevel1_normal_user_same_org(self):
        factories.WorkflowLevel1(organization=self.tola_user.organization)

        request = self.factory.get('/api/workflowlevel1/')
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 0)

    def test_create_workflowlevel1_superuser(self):
        self.tola_user.user.is_staff = True
        self.tola_user.user.is_superuser = True
        self.tola_user.user.save()

        data = {'name': 'Save the Children'}
        request = self.factory.post('/api/workflowlevel1/', data)
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'post': 'create'})
        response = view(request)
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['name'], u'Save the Children')

        WorkflowTeam.objects.get(
            workflowlevel1__id=response.data['id'],
            workflow_user=self.tola_user,
            role__name=ROLE_PROGRAM_ADMIN)

        wflvl1 = WorkflowLevel1.objects.get(pk=response.data['id'])
        self.assertEqual(wflvl1.organization, self.tola_user.organization)
        self.assertEqual(wflvl1.user_access.all().count(), 1)
        self.assertEqual(wflvl1.user_access.first(), self.tola_user)

    def test_create_workflowlevel1_org_admin(self):
        WorkflowTeam.objects.create(
            workflow_user=self.tola_user,
            partner_org=self.tola_user.organization,
            role=factories.Group(name=ROLE_ORGANIZATION_ADMIN))

        data = {'name': 'Save the Children'}
        request = self.factory.post('/api/workflowlevel1/', data)
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'post': 'create'})
        response = view(request)
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['name'], u'Save the Children')

        WorkflowTeam.objects.get(
            workflowlevel1__id=response.data['id'],
            workflow_user=self.tola_user,
            role__name=ROLE_PROGRAM_ADMIN)

        wflvl1 = WorkflowLevel1.objects.get(pk=response.data['id'])<commit_msg>Fix test WorkflowLevel1<commit_after>from django.contrib.auth.models import Group
from django.test import TestCase
from rest_framework.test import APIRequestFactory

import factories
from feed.views import WorkflowLevel1ViewSet
from workflow.models import (WorkflowTeam, WorkflowLevel1,
                             ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_TEAM,
                             ROLE_PROGRAM_ADMIN)


class WorkflowLevel1ViewsTest(TestCase):
    def setUp(self):
        self.factory = APIRequestFactory()
        self.tola_user = factories.TolaUser()
        factories.Group()

    def test_list_workflowlevel1_superuser(self):
        wflvl1 = factories.WorkflowLevel1()
        wflvl2 = factories.WorkflowLevel2(workflowlevel1=wflvl1)

        self.tola_user.user.is_staff = True
        self.tola_user.user.is_superuser = True
        self.tola_user.user.save()

        request = self.factory.get('/api/workflowlevel1/')
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['name'], wflvl1.name)
        self.assertEqual(response.data[0]['budget'],
                         wflvl2.total_estimated_budget)
        self.assertEqual(response.data[0]['actuals'], wflvl2.actual_cost)

    def test_list_workflowlevel1_org_admin(self):
        wflvl1 = factories.WorkflowLevel1(
            organization=self.tola_user.organization)
        wflvl2 = factories.WorkflowLevel2(workflowlevel1=wflvl1)
        group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN)
        self.tola_user.user.groups.add(group_org_admin)

        request = self.factory.get('/api/workflowlevel1/')
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['name'], wflvl1.name)
        self.assertEqual(response.data[0]['budget'],
                         wflvl2.total_estimated_budget)
        self.assertEqual(response.data[0]['actuals'], wflvl2.actual_cost)

    def test_list_workflowlevel1_program_admin(self):
        wflvl1 = factories.WorkflowLevel1(
            organization=self.tola_user.organization)
        wflvl2 = factories.WorkflowLevel2(workflowlevel1=wflvl1)
        group_program_admin = factories.Group(name=ROLE_PROGRAM_ADMIN)
        WorkflowTeam.objects.create(
            workflow_user=self.tola_user, workflowlevel1=wflvl1,
            role=group_program_admin)

        request = self.factory.get('/api/workflowlevel1/')
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['name'], wflvl1.name)
        self.assertEqual(response.data[0]['budget'],
                         wflvl2.total_estimated_budget)
        self.assertEqual(response.data[0]['actuals'], wflvl2.actual_cost)

    def test_list_workflowlevel1_program_team(self):
        wflvl1 = factories.WorkflowLevel1(
            organization=self.tola_user.organization)
        wflvl2 = factories.WorkflowLevel2(workflowlevel1=wflvl1)
        group_program_team = factories.Group(name=ROLE_PROGRAM_TEAM)
        WorkflowTeam.objects.create(
            workflow_user=self.tola_user, workflowlevel1=wflvl1,
            role=group_program_team)

        request = self.factory.get('/api/workflowlevel1/')
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['name'], wflvl1.name)
        self.assertEqual(response.data[0]['budget'],
                         wflvl2.total_estimated_budget)
        self.assertEqual(response.data[0]['actuals'], wflvl2.actual_cost)

    def test_list_workflowlevel1_normal_user_same_org(self):
        factories.WorkflowLevel1(organization=self.tola_user.organization)

        request = self.factory.get('/api/workflowlevel1/')
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 0)

    def test_create_workflowlevel1_superuser(self):
        self.tola_user.user.is_staff = True
        self.tola_user.user.is_superuser = True
        self.tola_user.user.save()

        data = {'name': 'Save the Children'}
        request = self.factory.post('/api/workflowlevel1/', data)
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'post': 'create'})
        response = view(request)
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['name'], u'Save the Children')

        WorkflowTeam.objects.get(
            workflowlevel1__id=response.data['id'],
            workflow_user=self.tola_user,
            role__name=ROLE_PROGRAM_ADMIN)

        wflvl1 = WorkflowLevel1.objects.get(pk=response.data['id'])
        self.assertEqual(wflvl1.organization, self.tola_user.organization)
        self.assertEqual(wflvl1.user_access.all().count(), 1)
        self.assertEqual(wflvl1.user_access.first(), self.tola_user)

    def test_create_workflowlevel1_org_admin(self):
        group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN)
        self.tola_user.user.groups.add(group_org_admin)

        data = {'name': 'Save the Children'}
        request = self.factory.post('/api/workflowlevel1/', data)
        request.user = self.tola_user.user
        view = WorkflowLevel1ViewSet.as_view({'post': 'create'})
        response = view(request)
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['name'], u'Save the Children')

        WorkflowTeam.objects.get(
            workflowlevel1__id=response.data['id'],
            workflow_user=self.tola_user,
            role__name=ROLE_PROGRAM_ADMIN)

        wflvl1 = WorkflowLevel1.objects.get(pk=response.data['id'])
{'fraction_non_alphanumeric': 0.10989095377054216, 'fraction_numerical': 0.017432038089387192, 'mean_word_length': 3.049440298507463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2999824', 'n_tokens_mistral': 4191, 'n_tokens_neox': 4023, 'n_words': 494}