From 2bc6d14b215d9e0e7121f0558f1154900bc9b273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mu=C3=B1oz=20C=C3=A1rdenas?= Date: Tue, 27 Mar 2018 13:14:58 +0200 Subject: [PATCH] Delete feed (API) --- feed/__init__.py | 0 feed/permissions.py | 241 --- feed/serializers.py | 624 -------- feed/tests/__init__.py | 0 feed/tests/test_approvaltypeview.py | 115 -- feed/tests/test_approvalworkflowview.py | 48 - feed/tests/test_awardview.py | 112 -- feed/tests/test_beneficiaryview.py | 127 -- feed/tests/test_browseableapi.py | 34 - feed/tests/test_budgetview.py | 217 --- feed/tests/test_checklistview.py | 170 -- feed/tests/test_codedfieldview.py | 88 -- feed/tests/test_collecteddataview.py | 691 -------- feed/tests/test_contactview.py | 301 ---- feed/tests/test_customformview.py | 701 --------- feed/tests/test_dashboardview.py | 236 --- feed/tests/test_disaggregationtypeview.py | 85 - feed/tests/test_documentationview.py | 290 ---- feed/tests/test_externalserviceview.py | 111 -- feed/tests/test_frequencyview.py | 25 - feed/tests/test_fundcodeview.py | 132 -- feed/tests/test_groupview.py | 42 - feed/tests/test_indicatortypeview.py | 132 -- feed/tests/test_indicatorview.py | 609 ------- feed/tests/test_issueregisterview.py | 98 -- feed/tests/test_levelview.py | 539 ------- feed/tests/test_milestoneview.py | 126 -- feed/tests/test_objectiveview.py | 241 --- feed/tests/test_organizationview.py | 28 - feed/tests/test_portfolioview.py | 367 ----- feed/tests/test_profiletypeview.py | 113 -- .../test_programindicatorreadonlyview.py | 27 - feed/tests/test_projecttypeview.py | 132 -- feed/tests/test_riskregisterview.py | 46 - feed/tests/test_sectorview.py | 84 - feed/tests/test_siteprofileview.py | 174 -- feed/tests/test_stakeholdertypeview.py | 112 -- feed/tests/test_stakeholderview.py | 150 -- feed/tests/test_strategicobjectiveview.py | 117 -- feed/tests/test_tolauserview.py | 75 - feed/tests/test_widget.py | 43 - feed/tests/test_workflowlevel1view.py | 611 ------- feed/tests/test_workflowlevel2sortview.py | 227 --- feed/tests/test_workflowlevel2view.py | 679 -------- feed/tests/test_workflowteamview.py | 573 ------- feed/views.py | 1397 ----------------- tola/settings/base.py | 4 - tola/urls.py | 65 - tola/views.py | 20 +- 49 files changed, 1 insertion(+), 11178 deletions(-) delete mode 100755 feed/__init__.py delete mode 100644 feed/permissions.py delete mode 100755 feed/serializers.py delete mode 100644 feed/tests/__init__.py delete mode 100644 feed/tests/test_approvaltypeview.py delete mode 100644 feed/tests/test_approvalworkflowview.py delete mode 100644 feed/tests/test_awardview.py delete mode 100644 feed/tests/test_beneficiaryview.py delete mode 100644 feed/tests/test_browseableapi.py delete mode 100644 feed/tests/test_budgetview.py delete mode 100755 feed/tests/test_checklistview.py delete mode 100644 feed/tests/test_codedfieldview.py delete mode 100644 feed/tests/test_collecteddataview.py delete mode 100755 feed/tests/test_contactview.py delete mode 100644 feed/tests/test_customformview.py delete mode 100644 feed/tests/test_dashboardview.py delete mode 100755 feed/tests/test_disaggregationtypeview.py delete mode 100644 feed/tests/test_documentationview.py delete mode 100644 feed/tests/test_externalserviceview.py delete mode 100644 feed/tests/test_frequencyview.py delete mode 100644 feed/tests/test_fundcodeview.py delete mode 100755 feed/tests/test_groupview.py delete mode 100644 feed/tests/test_indicatortypeview.py delete mode 100644 feed/tests/test_indicatorview.py delete mode 100755 feed/tests/test_issueregisterview.py delete mode 100644 feed/tests/test_levelview.py delete mode 100644 feed/tests/test_milestoneview.py delete mode 100644 feed/tests/test_objectiveview.py delete mode 100644 feed/tests/test_organizationview.py delete mode 100644 feed/tests/test_portfolioview.py delete mode 100644 feed/tests/test_profiletypeview.py delete mode 100644 feed/tests/test_programindicatorreadonlyview.py delete mode 100644 feed/tests/test_projecttypeview.py delete mode 100644 feed/tests/test_riskregisterview.py delete mode 100644 feed/tests/test_sectorview.py delete mode 100755 feed/tests/test_siteprofileview.py delete mode 100644 feed/tests/test_stakeholdertypeview.py delete mode 100755 feed/tests/test_stakeholderview.py delete mode 100755 feed/tests/test_strategicobjectiveview.py delete mode 100644 feed/tests/test_tolauserview.py delete mode 100644 feed/tests/test_widget.py delete mode 100644 feed/tests/test_workflowlevel1view.py delete mode 100644 feed/tests/test_workflowlevel2sortview.py delete mode 100644 feed/tests/test_workflowlevel2view.py delete mode 100755 feed/tests/test_workflowteamview.py delete mode 100755 feed/views.py diff --git a/feed/__init__.py b/feed/__init__.py deleted file mode 100755 index e69de29bb..000000000 diff --git a/feed/permissions.py b/feed/permissions.py deleted file mode 100644 index 44d6ff0ea..000000000 --- a/feed/permissions.py +++ /dev/null @@ -1,241 +0,0 @@ -from rest_framework import permissions -from rest_framework.relations import ManyRelatedField - -from django.http import QueryDict - -from formlibrary.models import Distribution, CustomForm, Beneficiary -from indicators.models import ( - Frequency, DisaggregationType, Level, ExternalService, StrategicObjective, - Objective, Indicator, CollectedData) -from workflow.models import ( - ROLE_ORGANIZATION_ADMIN, ROLE_VIEW_ONLY, ROLE_PROGRAM_ADMIN, - ROLE_PROGRAM_TEAM, WorkflowTeam, ProjectType, Sector, SiteProfile, FundCode, - StakeholderType, ProfileType, Checklist, Budget, Contact, ApprovalType, - CodedField, IssueRegister, RiskRegister, Organization, Award, Milestone, - Portfolio, WorkflowLevel1, WorkflowLevel2, WorkflowLevel2Sort, - Documentation) - - -class IsSuperUserBrowseableAPI(permissions.BasePermission): - - def has_permission(self, request, view): - if request.user.is_authenticated(): - if view.__class__.__name__ == 'APIRootView': - return request.user.is_superuser - else: - return True - return False - - -class IsOrgMember(permissions.BasePermission): - def has_permission(self, request, view): - if request.user.is_superuser: - return True - - if view.action == 'create': - user_org = request.user.tola_user.organization - - if 'organization' in request.data: - org_serializer = view.serializer_class().get_fields()[ - 'organization'] - primitive_value = request.data.get('organization') - org = org_serializer.run_validation(primitive_value) - return org == user_org - - return True - - def has_object_permission(self, request, view, obj): - """ - Object level permissions are used to determine if a user - should be allowed to act on a particular object - """ - - if request.user.is_superuser: - return True - user_groups = request.user.groups.values_list('name', flat=True) - org_admin = True if ROLE_ORGANIZATION_ADMIN in user_groups else False - - user_org = request.user.tola_user.organization - try: - if obj.__class__ in [Sector, ProjectType, SiteProfile, Frequency, - FundCode, DisaggregationType, Level, - ExternalService, StrategicObjective, - StakeholderType, ProfileType, Contact, - ApprovalType, Distribution, CustomForm, - CodedField, IssueRegister, Award, Milestone, - Portfolio, WorkflowLevel1]: - return obj.organization == user_org - elif obj.__class__ in [Objective, Beneficiary, Documentation, - CollectedData, WorkflowLevel2, - WorkflowLevel2Sort]: - return obj.workflowlevel1.organization == user_org - elif obj.__class__ in [Checklist, Budget, RiskRegister]: - return obj.workflowlevel2.workflowlevel1.organization == \ - user_org - elif obj.__class__ in [Organization]: - return obj == user_org - elif obj.__class__ in [WorkflowTeam]: - if org_admin: - return obj.workflow_user.organization == user_org - else: - return obj.workflowlevel1.organization == user_org - elif obj.__class__ in [Indicator]: - return obj.workflowlevel1.filter( - organization=user_org).exists() - except AttributeError: - pass - return False - - -class AllowTolaRoles(permissions.BasePermission): - def has_permission(self, request, view): - if request.user.is_superuser: - return True - user_groups = request.user.groups.values_list('name', flat=True) - - queryset = self._queryset(view) - model_cls = queryset.model - if view.action == 'create': - user_org = request.user.tola_user.organization - - if 'workflowlevel1' in request.data: - wflvl1_serializer = view.serializer_class().get_fields()[ - 'workflowlevel1'] - - # Check if the field is Many-To-Many or not - if wflvl1_serializer.__class__ == ManyRelatedField and \ - isinstance(request.data, QueryDict): - primitive_value = request.data.getlist('workflowlevel1') - else: - primitive_value = request.data.get('workflowlevel1') - - # Get objects using their URLs - wflvl1 = wflvl1_serializer.run_validation(primitive_value) - - # We use a list to fetch the program teams - if not isinstance(wflvl1, list): - wflvl1 = [wflvl1] - team_groups = WorkflowTeam.objects.filter( - workflow_user=request.user.tola_user, - workflowlevel1__in=wflvl1).values_list( - 'role__name', flat=True) - - if model_cls in [Contact, CustomForm, Documentation, Indicator, - Level, CollectedData, Objective, - WorkflowLevel2]: - return ((ROLE_VIEW_ONLY not in team_groups or - ROLE_ORGANIZATION_ADMIN in user_groups) and - all(x.organization == user_org for x in wflvl1)) - elif model_cls is WorkflowTeam: - return (((ROLE_VIEW_ONLY not in team_groups and - ROLE_PROGRAM_TEAM not in team_groups) or - ROLE_ORGANIZATION_ADMIN in user_groups) and - all(x.organization == user_org for x in wflvl1)) - - elif model_cls is Portfolio: - return ROLE_ORGANIZATION_ADMIN in user_groups - - return True - - def _queryset(self, view): - """ - Return the queryset of the view - :param view: - :return: QuerySet - """ - assert hasattr(view, 'get_queryset') \ - or getattr(view, 'queryset', None) is not None, ( - 'Cannot apply {} on a view that does not set ' - '`.queryset` or have a `.get_queryset()` method.' - ).format(self.__class__.__name__) - - if hasattr(view, 'get_queryset'): - queryset = view.get_queryset() - assert queryset is not None, ( - '{}.get_queryset() returned None'.format( - view.__class__.__name__) - ) - return queryset - return view.queryset - - def has_object_permission(self, request, view, obj): - """ - Object level permissions are used to determine if a user - should be allowed to act on a particular object - """ - if request.user and request.user.is_authenticated(): - if request.user.is_superuser: - return True - user_groups = request.user.groups.values_list('name', flat=True) - if ROLE_ORGANIZATION_ADMIN in user_groups: - return True - - queryset = self._queryset(view) - model_cls = queryset.model - if model_cls is Portfolio: - team_groups = WorkflowTeam.objects.filter( - workflow_user=request.user.tola_user, - workflowlevel1__portfolio=obj).values_list( - 'role__name', flat=True) - if ROLE_PROGRAM_ADMIN in team_groups or ROLE_PROGRAM_TEAM in \ - team_groups: - return view.action == 'retrieve' - elif model_cls is WorkflowTeam: - team_groups = WorkflowTeam.objects.filter( - workflow_user=request.user.tola_user, - workflowlevel1=obj.workflowlevel1).values_list( - 'role__name', flat=True) - if ROLE_PROGRAM_ADMIN in team_groups: - return True - else: - return view.action == 'retrieve' - elif model_cls is WorkflowLevel1: - team_groups = WorkflowTeam.objects.filter( - workflow_user=request.user.tola_user, - workflowlevel1=obj).values_list( - 'role__name', flat=True) - if ROLE_PROGRAM_ADMIN in team_groups: - return True - elif ROLE_PROGRAM_TEAM in team_groups: - return view.action != 'destroy' - elif model_cls is Indicator: - team_groups = WorkflowTeam.objects.filter( - workflow_user=request.user.tola_user, - workflowlevel1__indicator=obj).values_list( - 'role__name', flat=True) - if ROLE_PROGRAM_ADMIN in team_groups: - return True - elif ROLE_PROGRAM_TEAM in team_groups: - return view.action != 'destroy' - elif ROLE_VIEW_ONLY in team_groups: - return view.action == 'retrieve' - elif model_cls in [CollectedData, Level, WorkflowLevel2]: - team_groups = WorkflowTeam.objects.filter( - workflow_user=request.user.tola_user, - workflowlevel1=obj.workflowlevel1).values_list( - 'role__name', flat=True) - if ROLE_PROGRAM_ADMIN in team_groups: - return True - elif ROLE_PROGRAM_TEAM in team_groups: - return view.action != 'destroy' - elif ROLE_VIEW_ONLY in team_groups: - return view.action == 'retrieve' - elif model_cls is CustomForm: - if obj.created_by == request.user: - if 'workflowlevel1' in request.data: - serializer = view.serializer_class().get_fields()[ - 'workflowlevel1'] - wflvl1 = serializer.run_validation(request.data.get( - 'workflowlevel1')) - team_groups = WorkflowTeam.objects.filter( - workflow_user=request.user.tola_user, - workflowlevel1=wflvl1).values_list( - 'role__name', flat=True) - return ROLE_VIEW_ONLY not in team_groups - return True - else: - return False - else: - return True - - return False diff --git a/feed/serializers.py b/feed/serializers.py deleted file mode 100755 index 17f8052c6..000000000 --- a/feed/serializers.py +++ /dev/null @@ -1,624 +0,0 @@ -from rest_framework import serializers -from rest_framework.reverse import reverse - -from formlibrary.models import (Distribution, CustomForm, Beneficiary, - CustomFormField, FieldType) -from indicators.models import ( - Indicator, IndicatorType, Level, Frequency, Objective, DisaggregationType, - ExternalService, ExternalServiceRecord, StrategicObjective, PeriodicTarget, - CollectedData, TolaTable, DisaggregationValue, DisaggregationLabel) -from workflow import models as wfm - - -class UserSerializer(serializers.ModelSerializer): - - class Meta: - model = wfm.User - fields = '__all__' - - -class GroupSerializer(serializers.ModelSerializer): - url = serializers.SerializerMethodField('get_self') - - def get_self(self, obj): - request = self.context['request'] - return reverse('group-detail', kwargs={'pk': obj.id}, request=request) - - class Meta: - model = wfm.Group - fields = '__all__' - - -class WorkflowLevel1Serializer(serializers.HyperlinkedModelSerializer): - workflow_key = serializers.UUIDField(read_only=True) - id = serializers.ReadOnlyField() - status = serializers.SerializerMethodField() - budget = serializers.ReadOnlyField() - actuals = serializers.ReadOnlyField() - difference = serializers.SerializerMethodField() - - def get_status(self, obj): - get_projects = wfm.WorkflowLevel2.objects.all().filter( - workflowlevel1=obj) - score = [] - red = "" - yellow = "" - green = "" - - for project_status in get_projects: - if project_status.status == "red": - score.append(red) - if project_status.status == "yellow": - score.append(yellow) - if project_status.status == "green": - score.append(green) - if score: - calculated_status = max(score) - else: - calculated_status = "green" - - return calculated_status - - def get_difference(self, obj): - try: - if obj.budget: - return obj.budget - obj.actuals - else: - return 0 - except AttributeError: - return None - - class Meta: - model = wfm.WorkflowLevel1 - fields = '__all__' - - -class SectorSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Sector - fields = '__all__' - - -class ProjectTypeSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.ProjectType - fields = '__all__' - - -class OfficeSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Office - fields = '__all__' - - -class BudgetSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Budget - fields = '__all__' - - -class SiteProfileSerializer(serializers.HyperlinkedModelSerializer): - site_key = serializers.UUIDField(read_only=True) - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.SiteProfile - fields = '__all__' - - -class WorkflowLevel2Serializer(serializers.HyperlinkedModelSerializer): - agreement_key = serializers.UUIDField(read_only=True) - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.WorkflowLevel2 - fields = '__all__' - - -class CountrySerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Country - fields = '__all__' - - -class IndicatorSerializer(serializers.HyperlinkedModelSerializer): - indicator_key = serializers.UUIDField(read_only=True) - id = serializers.ReadOnlyField() - actuals = serializers.ReadOnlyField() - - class Meta: - model = Indicator - fields = '__all__' - - -class IndicatorTypeLightSerializer(serializers.ModelSerializer): - class Meta: - model = IndicatorType - fields = ('id', 'indicator_type') - - -class IndicatorLevelLightSerializer(serializers.ModelSerializer): - class Meta: - model = Level - fields = ('id', 'name') - - -class IndicatorLightSerializer(serializers.ModelSerializer): - sector = serializers.SerializerMethodField() - indicator_type = IndicatorTypeLightSerializer(many=True, read_only=True) - level = IndicatorLevelLightSerializer(read_only=True) - datacount = serializers.SerializerMethodField() - - def get_datacount(self, obj): - # Returns the number of collecteddata points by an indicator - return obj.collecteddata_set.count() - - def get_sector(self, obj): - if obj.sector is None: - return '' - return {"id": obj.sector.id, "name": obj.sector.sector} - - class Meta: - model = Indicator - fields = ('name', 'number', 'lop_target', 'indicator_type', 'level', - 'sector', 'datacount') - - -class ProgramIndicatorSerializer(serializers.ModelSerializer): - indicator_set = IndicatorLightSerializer(many=True, read_only=True) - indicators_count = serializers.SerializerMethodField() - - def get_indicators_count(self, obj): - return obj.indicator_set.count() - - class Meta: - model = wfm.WorkflowLevel1 - fields = ('id', 'name', 'indicators_count', 'indicator_set') - - -class FrequencySerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = Frequency - fields = '__all__' - - -class TolaUserSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.TolaUser - fields = '__all__' - depth = 1 - - -class IndicatorTypeSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = IndicatorType - fields = '__all__' - - -class ObjectiveSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = Objective - fields = '__all__' - - -class FundCodeSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.FundCode - fields = '__all__' - - -class DisaggregationTypeSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - disaggregation_type = serializers.CharField(max_length=135) - - class Meta: - model = DisaggregationType - fields = '__all__' - - -class LevelSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = Level - fields = '__all__' - - -class StakeholderSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Stakeholder - fields = '__all__' - - -class ExternalServiceSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = ExternalService - fields = '__all__' - - -class ExternalServiceRecordSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = ExternalServiceRecord - fields = '__all__' - - -class StrategicObjectiveSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = StrategicObjective - fields = '__all__' - - -class StakeholderTypeSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.StakeholderType - fields = '__all__' - - -class ProfileTypeSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.ProfileType - fields = '__all__' - - -class ProvinceSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.AdminLevelOne - fields = '__all__' - - -class DistrictSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.AdminLevelTwo - fields = '__all__' - - -class AdminLevelThreeSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.AdminLevelThree - fields = '__all__' - - -class VillageSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.AdminLevelFour - fields = '__all__' - - -class ContactSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Contact - fields = '__all__' - - -class DocumentationSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Documentation - fields = '__all__' - - -class PeriodicTargetSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = PeriodicTarget - fields = '__all__' - - -class CollectedDataSerializer(serializers.HyperlinkedModelSerializer): - data_key = serializers.UUIDField(read_only=True) - id = serializers.ReadOnlyField() - - class Meta: - model = CollectedData - fields = '__all__' - - -class TolaTableSerializer(serializers.ModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = TolaTable - fields = '__all__' - - -class DisaggregationValueSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = DisaggregationValue - fields = '__all__' - - -class DisaggregationLabelSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = DisaggregationLabel - fields = '__all__' - - -class ChecklistSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Checklist - fields = '__all__' - - -class OrganizationSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Organization - fields = '__all__' - - -class WorkflowModulesSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.WorkflowModules - fields = '__all__' - - -class CurrencySerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Currency - fields = '__all__' - - -class ApprovalTypeSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.ApprovalType - fields = '__all__' - - -class ApprovalWorkflowSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.ApprovalWorkflow - fields = '__all__' - - -class BeneficiarySerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = Beneficiary - fields = '__all__' - - -class DistributionSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = Distribution - fields = '__all__' - - -class RiskRegisterSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.RiskRegister - fields = '__all__' - - -class IssueRegisterSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.IssueRegister - fields = '__all__' - - -class CustomFormSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = CustomForm - fields = '__all__' - - -class CustomFormFieldSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = CustomFormField - fields = '__all__' - - -class CodedFieldSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.CodedField - fields = '__all__' - - -class CodedFieldValuesSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.CodedFieldValues - fields = '__all__' - - -class FieldTypeSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = FieldType - fields = '__all__' - - -class LandTypeSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.LandType - fields = '__all__' - - -class InternationalizationSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Internationalization - fields = '__all__' - - -class TolaUserFilterSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.TolaUserFilter - fields = '__all__' - - -class StakeholderFullSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - country = CountrySerializer(read_only=True) - contact = ContactSerializer(read_only=True, many=True) - sectors = SectorSerializer(read_only=True, many=True) - approval = ApprovalTypeSerializer(read_only=True, many=True) - - class Meta: - model = wfm.Stakeholder - fields = '__all__' - - -class WorkflowLevel2FullSerializer(serializers.HyperlinkedModelSerializer): - agreement_key = serializers.UUIDField(read_only=True) - id = serializers.ReadOnlyField() - workflowlevel1 = WorkflowLevel1Serializer(read_only=True) - project_type = ProjectTypeSerializer(read_only=True) - office = OfficeSerializer(read_only=True) - sector = SectorSerializer(read_only=True) - budget = BudgetSerializer(read_only=True) - - class Meta: - model = wfm.WorkflowLevel2 - fields = '__all__' - - -class WorkflowLevel2SortSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.WorkflowLevel2Sort - fields = '__all__' - - -class AwardSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Award - fields = '__all__' - - -class WorkflowTeamSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.WorkflowTeam - fields = '__all__' - - -class MilestoneSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Milestone - fields = '__all__' - - -class PortfolioSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Portfolio - fields = '__all__' - - -class DashboardSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - user = serializers.ReadOnlyField() - - class Meta: - model = wfm.Dashboard - fields = '__all__' - - -class WidgetSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.Widget - fields = '__all__' - - -class SectorRelatedSerializer(serializers.ModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.SectorRelated - fields = '__all__' - - -class WorkflowLevel1SectorSerializer(serializers.ModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = wfm.WorkflowLevel1Sector - fields = '__all__' diff --git a/feed/tests/__init__.py b/feed/tests/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/feed/tests/test_approvaltypeview.py b/feed/tests/test_approvaltypeview.py deleted file mode 100644 index 8c625ed52..000000000 --- a/feed/tests/test_approvaltypeview.py +++ /dev/null @@ -1,115 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import ApprovalTypeViewSet -from workflow.models import WorkflowTeam, ROLE_ORGANIZATION_ADMIN, \ - ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY - - -class ApprovalTypeListViewsTest(TestCase): - def setUp(self): - factories.Organization(id=1) - factories.ApprovalType.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_approvaltype_superuser(self): - request = self.factory.get('/api/approvaltype/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = ApprovalTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_approvaltype_org_admin(self): - request = self.factory.get('/api/approvaltype/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = self.tola_user.user - view = ApprovalTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ApprovalType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_approvaltype_program_admin(self): - request = self.factory.get('/api/approvaltype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = ApprovalTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ApprovalType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_approvaltype_program_team(self): - request = self.factory.get('/api/approvaltype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = ApprovalTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ApprovalType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_approvaltype_view_only(self): - request = self.factory.get('/api/approvaltype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = ApprovalTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ApprovalType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class ApprovalTypeFilterViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_filter_approvaltype_superuser(self): - another_org = factories.Organization(name='Another Org') - approvaltype1_1 = factories.ApprovalType( - organization=self.tola_user.organization) - factories.ApprovalType( - name='Another Approval Type', - organization=another_org) - - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('/api/approvaltype/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = ApprovalTypeViewSet.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'], approvaltype1_1.name) diff --git a/feed/tests/test_approvalworkflowview.py b/feed/tests/test_approvalworkflowview.py deleted file mode 100644 index e1be0477d..000000000 --- a/feed/tests/test_approvalworkflowview.py +++ /dev/null @@ -1,48 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import factories -from feed.views import ApprovalWorkflowViewSet - - -class ApprovalWorkflowViewTest(TestCase): - def setUp(self): - self.user = factories.User(is_superuser=True, is_staff=True) - self.organization = factories.Organization(id=1) - factories.TolaUser(user=self.user, organization=self.organization) - factory = APIRequestFactory() - self.request = factory.post('/api/approvalworkflow/') - - def test_create_approvalworkflow(self): - user_john = factories.User(first_name='John', last_name='Lennon') - user_paul = factories.User(first_name='Paul', last_name='McCartney') - assigned_user = factories.TolaUser(user=user_john, - organization=self.organization) - requested_user = factories.TolaUser(user=user_paul, - organization=self.organization) - approval_type = factories.ApprovalType() - assigned_user_url = reverse('tolauser-detail', - kwargs={'pk': assigned_user.id}, - request=self.request) - requested_user_url = reverse('tolauser-detail', - kwargs={'pk': requested_user.id}, - request=self.request) - approval_type_url = reverse('approvaltype-detail', - kwargs={'pk': approval_type.id}, - request=self.request) - user_url = reverse('user-detail', kwargs={'pk': self.user.id}, - request=self.request) - - data = {'assigned_to': assigned_user_url, - 'requested_from': requested_user_url, - 'approval_type': approval_type_url} - self.request = APIRequestFactory().post('/api/approvalworkflow/', data) - self.request.user = self.user - view = ApprovalWorkflowViewSet.as_view({'post': 'create'}) - response = view(self.request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['assigned_to'], assigned_user_url) - self.assertEqual(response.data['requested_from'], requested_user_url) - self.assertEqual(response.data['created_by'], user_url) diff --git a/feed/tests/test_awardview.py b/feed/tests/test_awardview.py deleted file mode 100644 index 3f8279960..000000000 --- a/feed/tests/test_awardview.py +++ /dev/null @@ -1,112 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import AwardViewSet -from workflow.models import WorkflowTeam, ROLE_ORGANIZATION_ADMIN, \ - ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY - - -class AwardListViewsTest(TestCase): - def setUp(self): - factories.Organization(id=1) - factories.Award.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_award_superuser(self): - request = self.factory.get('/api/award/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = AwardViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_award_org_admin(self): - request = self.factory.get('/api/award/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = self.tola_user.user - view = AwardViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Award(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_award_program_admin(self): - request = self.factory.get('/api/award/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = AwardViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Award(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_award_program_team(self): - request = self.factory.get('/api/award/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = AwardViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Award(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_award_view_only(self): - request = self.factory.get('/api/award/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = AwardViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Award(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class AwardFilterViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_award_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - another_org = factories.Organization(name='Another Org') - award1 = factories.Award( - organization=self.tola_user.organization) - factories.Award(name='Award B', organization=another_org) - request = self.factory.get('/api/award/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = AwardViewSet.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'], award1.name) diff --git a/feed/tests/test_beneficiaryview.py b/feed/tests/test_beneficiaryview.py deleted file mode 100644 index 058d3a2ff..000000000 --- a/feed/tests/test_beneficiaryview.py +++ /dev/null @@ -1,127 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import BeneficiaryViewSet -from workflow.models import WorkflowTeam, ROLE_ORGANIZATION_ADMIN, \ - ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY - - -class BeneficiaryListViewsTest(TestCase): - def setUp(self): - factories.Beneficiary.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_beneficiary_superuser(self): - request = self.factory.get('/api/beneficiary/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = BeneficiaryViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_beneficiary_org_admin(self): - request = self.factory.get('/api/beneficiary/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = self.tola_user.user - view = BeneficiaryViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - wkflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - factories.Beneficiary(workflowlevel1=[wkflvl1]) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_beneficiary_program_admin(self): - request = self.factory.get('/api/beneficiary/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = BeneficiaryViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - wkflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - factories.Beneficiary(workflowlevel1=[wkflvl1]) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_beneficiary_program_team(self): - request = self.factory.get('/api/beneficiary/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = BeneficiaryViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - wkflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - factories.Beneficiary(workflowlevel1=[wkflvl1]) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_beneficiary_view_only(self): - request = self.factory.get('/api/beneficiary/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = BeneficiaryViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - wkflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - factories.Beneficiary(workflowlevel1=[wkflvl1]) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class BeneficiaryFilterViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_filter_beneficiary_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - another_org = factories.Organization(name='Another Org') - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl1_2 = factories.WorkflowLevel1( - organization=another_org) - beneficiary1 = factories.Beneficiary(workflowlevel1=[wkflvl1_1]) - factories.Beneficiary( - beneficiary_name='James McCartney', father_name='Paul McCartney', - workflowlevel1=[wkflvl1_2]) - - request = self.factory.get( - '/api/beneficiary/?workflowlevel1__organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = BeneficiaryViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['beneficiary_name'], - beneficiary1.beneficiary_name) diff --git a/feed/tests/test_browseableapi.py b/feed/tests/test_browseableapi.py deleted file mode 100644 index c5f855d0e..000000000 --- a/feed/tests/test_browseableapi.py +++ /dev/null @@ -1,34 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIClient - -import factories - - -class BrowsableAPITest(TestCase): - def setUp(self): - self.client = APIClient() - self.tola_user = factories.TolaUser() - - def test_login_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.set_password('1234') - self.tola_user.user.save() - - loggedin = self.client.login(username=self.tola_user.user.username, - password='1234') - response = self.client.get('/api/') - - self.assertTrue(loggedin) - self.assertEqual(response.status_code, 200) - - def test_login_normaluser(self): - self.tola_user.user.set_password('1234') - self.tola_user.user.save() - - loggedin = self.client.login(username=self.tola_user.user.username, - password='1234') - response = self.client.get('/api/') - - self.assertTrue(loggedin) - self.assertEqual(response.status_code, 403) diff --git a/feed/tests/test_budgetview.py b/feed/tests/test_budgetview.py deleted file mode 100644 index 1e26c69d0..000000000 --- a/feed/tests/test_budgetview.py +++ /dev/null @@ -1,217 +0,0 @@ -from decimal import Decimal - -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import factories -from feed.views import BudgetViewSet -from workflow.models import Budget - - -class BudgetListViewTest(TestCase): - def setUp(self): - wflvl1 = factories.WorkflowLevel1() - wflvl2 = factories.WorkflowLevel2(name='WorkflowLevel2', - workflowlevel1=wflvl1) - factories.Budget.create_batch(2, workflowlevel2=wflvl2) - - factory = APIRequestFactory() - self.request = factory.get('/api/budget/') - - def test_list_budget_superuser(self): - self.request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = BudgetViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_budget_normaluser(self): - tola_user = factories.TolaUser() - self.request.user = tola_user.user - view = BudgetViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_budget_normaluser_one_result(self): - tola_user = factories.TolaUser() - wflvl1 = factories.WorkflowLevel1( - name='WorkflowLevel1', organization=tola_user.organization) - wflvl2 = factories.WorkflowLevel2( - name='WorkflowLevel2', workflowlevel1=wflvl1) - factories.Budget(workflowlevel2=wflvl2) - - self.request.user = tola_user.user - view = BudgetViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class BudgetCreateViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_create_budget(self): - request = self.factory.post('/api/budget/') - - wflvl1 = factories.WorkflowLevel1( - name='WorkflowLevel1', organization=self.tola_user.organization) - wflvl2 = factories.WorkflowLevel2( - name='WorkflowLevel2', actual_cost=0, total_estimated_budget=0, - workflowlevel1=wflvl1) - wflvl2_url = reverse('workflowlevel2-detail', - kwargs={'pk': wflvl2.id}, - request=request) - user_url = reverse('user-detail', kwargs={'pk': self.tola_user.user.id}, - request=request) - - data = {'proposed_value': 5678, - 'actual_value': 1234, - 'workflowlevel2': wflvl2_url} - request = self.factory.post('/api/budget/', data) - request.user = self.tola_user.user - view = BudgetViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(Decimal(response.data['proposed_value']), - data['proposed_value']) - self.assertEqual(Decimal(response.data['actual_value']), - data['actual_value']) - self.assertEqual(response.data['created_by'], user_url) - - # Check WorkflowLevel2 - budget = Budget.objects.get(pk=response.data['id']) - wkfl2 = budget.workflowlevel2 - self.assertEquals(wkfl2.total_estimated_budget, data['proposed_value']) - self.assertEquals(wkfl2.actual_cost, data['actual_value']) - - def test_create_budget_without_wkfl2(self): - request = self.factory.post('/api/budget/') - - user_url = reverse('user-detail', kwargs={'pk': self.tola_user.user.id}, - request=request) - - data = {'proposed_value': 5678, - 'actual_value': 1234} - request = self.factory.post('/api/budget/', data) - request.user = self.tola_user.user - view = BudgetViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(Decimal(response.data['proposed_value']), - data['proposed_value']) - self.assertEqual(Decimal(response.data['actual_value']), - data['actual_value']) - self.assertEqual(response.data['created_by'], user_url) - - -class BudgetUpdateViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_update_budget(self): - wflvl1 = factories.WorkflowLevel1( - name='WorkflowLevel1', organization=self.tola_user.organization) - wflvl2 = factories.WorkflowLevel2( - name='WorkflowLevel2', actual_cost=0, total_estimated_budget=0, - workflowlevel1=wflvl1) - budget = factories.Budget(workflowlevel2=wflvl2) - - data = {'proposed_value': 5678, - 'actual_value': 1234} - request = self.factory.post('/api/budget/', data) - request.user = self.tola_user.user - view = BudgetViewSet.as_view({'post': 'update'}) - response = view(request, pk=budget.pk) - - budget = Budget.objects.get(pk=response.data['id']) - self.assertEqual(response.status_code, 200) - self.assertEqual(Decimal(response.data['proposed_value']), - budget.proposed_value) - self.assertEqual(Decimal(response.data['actual_value']), - budget.actual_value) - - # Check WorkflowLevel2 - wkfl2 = budget.workflowlevel2 - self.assertEquals(wkfl2.total_estimated_budget, data['proposed_value']) - self.assertEquals(wkfl2.actual_cost, data['actual_value']) - - def test_update_budget_without_actual_value(self): - wflvl1 = factories.WorkflowLevel1( - name='WorkflowLevel1', organization=self.tola_user.organization) - wflvl2 = factories.WorkflowLevel2( - name='WorkflowLevel2', actual_cost=100, total_estimated_budget=0, - workflowlevel1=wflvl1) - budget = factories.Budget(workflowlevel2=wflvl2, - proposed_value=None, actual_value=None) - - data = {'proposed_value': 5678} - request = self.factory.post('/api/budget/', data) - request.user = self.tola_user.user - view = BudgetViewSet.as_view({'post': 'update'}) - response = view(request, pk=budget.pk) - - budget = Budget.objects.get(pk=response.data['id']) - self.assertEqual(response.status_code, 200) - self.assertEqual(Decimal(response.data['proposed_value']), - budget.proposed_value) - self.assertEqual(Decimal(response.data['actual_value']), 0) - - # Check WorkflowLevel2 - wkfl2 = budget.workflowlevel2 - self.assertEquals(wkfl2.total_estimated_budget, data['proposed_value']) - self.assertEquals(wkfl2.actual_cost, 100) - - def test_update_budget_without_wkfl2(self): - budget = factories.Budget() - - data = {'proposed_value': 5678, - 'actual_value': 1234} - request = self.factory.post('/api/budget/', data) - request.user = self.tola_user.user - view = BudgetViewSet.as_view({'post': 'create'}) - response = view(request, pk=budget.pk) - - budget = Budget.objects.get(pk=response.data['id']) - self.assertEqual(response.status_code, 201) - self.assertEqual(Decimal(response.data['proposed_value']), - budget.proposed_value) - self.assertEqual(Decimal(response.data['actual_value']), - budget.actual_value) - - -class BudgetFilterViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_budget_org_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - another_org = factories.Organization(name='Another Org') - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl1_2 = factories.WorkflowLevel1(organization=another_org) - wkflvl2_1 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_1) - wkflvl2_2 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_2) - budget1 = factories.Budget(workflowlevel2=wkflvl2_1) - factories.Budget(contributor='Paul McCartney', workflowlevel2=wkflvl2_2) - - request = self.factory.get( - '/api/budget/?workflowlevel2__workflowlevel1__organization__id' - '=%s' % self.tola_user.organization.pk) - request.user = self.tola_user.user - view = BudgetViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['contributor'], budget1.contributor) diff --git a/feed/tests/test_checklistview.py b/feed/tests/test_checklistview.py deleted file mode 100755 index f6600576b..000000000 --- a/feed/tests/test_checklistview.py +++ /dev/null @@ -1,170 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import ChecklistViewSet -from workflow.models import Checklist, WorkflowLevel1, WorkflowLevel2, \ - ROLE_ORGANIZATION_ADMIN - - -class ChecklistListViewTest(TestCase): - def setUp(self): - wflvl1 = WorkflowLevel1.objects.create(name='WorkflowLevel1') - wflvl2 = WorkflowLevel2.objects.create(name='WorkflowLevel2', - workflowlevel1=wflvl1) - Checklist.objects.bulk_create([ - Checklist(name='CheckList_0', workflowlevel2=wflvl2), - Checklist(name='CheckList_1', workflowlevel2=wflvl2), - ]) - - factory = APIRequestFactory() - self.request = factory.get('/api/checklist/') - - def test_list_checklist_superuser(self): - self.request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = ChecklistViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_checklist_normaluser(self): - tola_user = factories.TolaUser() - self.request.user = tola_user.user - view = ChecklistViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_checklist_normaluser_one_result(self): - tola_user = factories.TolaUser() - wflvl1 = WorkflowLevel1.objects.create( - name='WorkflowLevel1', organization=tola_user.organization) - wflvl2 = WorkflowLevel2.objects.create(name='WorkflowLevel2', - workflowlevel1=wflvl1) - Checklist.objects.create(name='CheckList_0', workflowlevel2=wflvl2) - - self.request.user = tola_user.user - view = ChecklistViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class ChecklistFilterViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_checklist_org_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - another_org = factories.Organization(name='Another Org') - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl1_2 = factories.WorkflowLevel1(organization=another_org) - wkflvl2_1 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_1) - wkflvl2_2 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_2) - checklist1 = factories.Checklist(name='Checklist A', - workflowlevel2=wkflvl2_1) - factories.Checklist(name='Checklist B', - workflowlevel2=wkflvl2_2) - - request = self.factory.get( - '/api/checklist/?workflowlevel2__workflowlevel1__organization__id' - '=%s' % self.tola_user.organization.pk) - request.user = self.tola_user.user - view = ChecklistViewSet.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'], checklist1.name) - - def test_filter_checklist_country_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - country1 = factories.Country(country='Brazil', code='BR') - country2 = factories.Country(country='Germany', code='DE') - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization, country=[country1]) - wkflvl1_2 = factories.WorkflowLevel1( - organization=self.tola_user.organization, country=[country2]) - wkflvl2_1 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_1) - wkflvl2_2 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_2) - checklist1 = factories.Checklist(name='Checklist A', - workflowlevel2=wkflvl2_1) - factories.Checklist(name='Checklist B', - workflowlevel2=wkflvl2_2) - - request = self.factory.get( - '/api/checklist' - '/?workflowlevel2__workflowlevel1__country__country=%s' - % country1.country) - request.user = self.tola_user.user - view = ChecklistViewSet.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'], checklist1.name) - - def test_filter_checklist_wkflvl2_name_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl1_2 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl2_1 = factories.WorkflowLevel2( - name='WorkflowLevel2 A', workflowlevel1=wkflvl1_1) - wkflvl2_2 = factories.WorkflowLevel2( - name='WorkflowLevel2 B', workflowlevel1=wkflvl1_2) - checklist1 = factories.Checklist( - name='Checklist A', workflowlevel2=wkflvl2_1) - factories.Checklist( - name='Checklist B', workflowlevel2=wkflvl2_2) - - request = self.factory.get( - '/api/checklist' - '/?workflowlevel2__name=%s' - % wkflvl2_1.name) - request.user = self.tola_user.user - view = ChecklistViewSet.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'], checklist1.name) - - def test_filter_checklist_owner_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - another_user = factories.User(first_name='Johnn', last_name='Lennon') - another_tola = factories.TolaUser(user=another_user) - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl1_2 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl2_1 = factories.WorkflowLevel2( - name='WorkflowLevel2 A', workflowlevel1=wkflvl1_1) - wkflvl2_2 = factories.WorkflowLevel2( - name='WorkflowLevel2 B', workflowlevel1=wkflvl1_2) - checklist1 = factories.Checklist( - name='Checklist A', owner=self.tola_user, workflowlevel2=wkflvl2_1) - factories.Checklist( - name='Checklist B', owner=another_tola, workflowlevel2=wkflvl2_2) - - request = self.factory.get( - '/api/checklist' - '/?owner=%s' - % self.tola_user.pk) - request.user = self.tola_user.user - view = ChecklistViewSet.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'], checklist1.name) - diff --git a/feed/tests/test_codedfieldview.py b/feed/tests/test_codedfieldview.py deleted file mode 100644 index 475a7a3eb..000000000 --- a/feed/tests/test_codedfieldview.py +++ /dev/null @@ -1,88 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import CodedFieldViewSet - - -class CodedFieldListViewTest(TestCase): - def setUp(self): - factories.Organization(id=1) - wflvl1 = factories.WorkflowLevel1() - wflvl2 = factories.WorkflowLevel2(name='WorkflowLevel2', - workflowlevel1=wflvl1) - factories.CodedField.create_batch(2, workflowlevel2=[wflvl2]) - - factory = APIRequestFactory() - self.request = factory.get('/api/codedfield/') - - def test_list_codedfield_superuser(self): - self.request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = CodedFieldViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_codedfield_normaluser(self): - tola_user = factories.TolaUser() - self.request.user = tola_user.user - view = CodedFieldViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_codedfield_normaluser_one_result(self): - tola_user = factories.TolaUser() - factories.CodedField(organization=tola_user.organization) - - self.request.user = tola_user.user - view = CodedFieldViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class CodedFieldCreateViewTest(TestCase): - def setUp(self): - self.user = factories.User(is_superuser=True, is_staff=True) - factory = APIRequestFactory() - self.request = factory.post('/api/codedfield/') - - def test_create_codedfield(self): - factories.Organization(id=1) - self.request.user = self.user - view = CodedFieldViewSet.as_view({'post': 'create'}) - response = view(self.request) - - self.assertEqual(response.status_code, 201) - - -class CodedFieldFilterViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_codedfield_org_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - another_org = factories.Organization(name='Another Org') - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl1_2 = factories.WorkflowLevel1(organization=another_org) - wkflvl2_1 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_1) - wkflvl2_2 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_2) - codedfield1 = factories.CodedField(workflowlevel2=[wkflvl2_1]) - factories.CodedField(name='coded_field_b', workflowlevel2=[wkflvl2_2]) - - request = self.factory.get( - '/api/codedfield/?workflowlevel2__workflowlevel1__organization__id' - '=%s' % self.tola_user.organization.pk) - request.user = self.tola_user.user - view = CodedFieldViewSet.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'], codedfield1.name) diff --git a/feed/tests/test_collecteddataview.py b/feed/tests/test_collecteddataview.py deleted file mode 100644 index 3c1e246a1..000000000 --- a/feed/tests/test_collecteddataview.py +++ /dev/null @@ -1,691 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import json -import factories -from feed.views import CollectedDataViewSet -from indicators.models import CollectedData -from workflow.models import WorkflowLevel1, WorkflowTeam, \ - ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, \ - ROLE_VIEW_ONLY - - -class CollectedDataListViewsTest(TestCase): - def setUp(self): - factories.CollectedData.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_collecteddata_superuser(self): - request = self.factory.get('/api/collecteddata/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = CollectedDataViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_collecteddata_org_admin(self): - request = self.factory.get('/api/collecteddata/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - factories.CollectedData(workflowlevel1=wflvl1, indicator=indicator) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_collecteddata_program_admin(self): - request = self.factory.get('/api/collecteddata/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - factories.CollectedData(workflowlevel1=wflvl1, indicator=indicator) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_collecteddata_program_team(self): - request = self.factory.get('/api/collecteddata/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - factories.CollectedData(workflowlevel1=wflvl1, indicator=indicator) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_collecteddata_normaluser(self): - request = self.factory.get('/api/collecteddata/') - wflvl1 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - factories.CollectedData(workflowlevel1=wflvl1, indicator=indicator) - view = CollectedDataViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class CollectedDataCreateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_create_collecteddata_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.post('/api/collecteddata/') - wflvl1 = factories.WorkflowLevel1() - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - indicator_url = reverse('indicator-detail', kwargs={'pk': indicator.id}, - request=request) - - data = {'indicator': indicator_url} - - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['indicator'], indicator_url) - - def test_create_collecteddata_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/collecteddata/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - indicator_url = reverse('indicator-detail', kwargs={'pk': indicator.id}, - request=request) - - data = {'indicator': indicator_url, - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['indicator'], indicator_url) - - def test_create_collecteddata_program_admin(self): - request = self.factory.post('/api/collecteddata/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - indicator_url = reverse('indicator-detail', kwargs={'pk': indicator.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'indicator': indicator_url, - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['indicator'], indicator_url) - - def test_create_collecteddata_program_admin_json(self): - request = self.factory.post('/api/collecteddata/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - indicator_url = reverse('indicator-detail', kwargs={'pk': indicator.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'indicator': indicator_url, - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/collecteddata/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['indicator'], indicator_url) - - def test_create_collecteddata_program_team(self): - request = self.factory.post('/api/collecteddata/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - indicator_url = reverse('indicator-detail', kwargs={'pk': indicator.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - - data = {'indicator': indicator_url, - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['indicator'], indicator_url) - - def test_create_collecteddata_view_only(self): - request = self.factory.post('/api/collecteddata/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - indicator_url = reverse('indicator-detail', kwargs={'pk': indicator.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - - data = {'indicator': indicator_url, - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 403) - - -class CollectedDataUpdateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_update_unexisting_collecteddata(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - data = {'description': 'Intermediate Results'} - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'update'}) - response = view(request, pk=288) - self.assertEqual(response.status_code, 404) - - def test_update_collecteddata_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.post('/api/collecteddata/') - collecteddata = factories.CollectedData() - indicator_url = reverse('indicator-detail', - kwargs={'pk': collecteddata.indicator.id}, - request=request) - - data = {'indicator': indicator_url, - 'description': 'Intermediate Results'} - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'update'}) - response = view(request, pk=collecteddata.pk) - self.assertEqual(response.status_code, 200) - - collecteddata = CollectedData.objects.get(pk=response.data['id']) - self.assertEquals(collecteddata.description, data['description']) - - def test_update_collecteddata_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - request = self.factory.post('/api/collecteddata/') - indicator_url = reverse('indicator-detail', - kwargs={'pk': collecteddata.indicator.id}, - request=request) - - data = {'indicator': indicator_url, - 'description': 'Intermediate Results'} - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'update'}) - response = view(request, pk=collecteddata.pk) - self.assertEqual(response.status_code, 200) - - collecteddata = CollectedData.objects.get(pk=response.data['id']) - self.assertEquals(collecteddata.description, data['description']) - - def test_update_collecteddata_diff_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - request = self.factory.post('/api/collecteddata/') - indicator_url = reverse('indicator-detail', - kwargs={'pk': collecteddata.indicator.id}, - request=request) - - data = {'indicator': indicator_url, - 'description': 'Intermediate Results'} - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'update'}) - response = view(request, pk=collecteddata.pk) - self.assertEqual(response.status_code, 403) - - def test_update_collecteddata_program_admin(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - request = self.factory.post('/api/collecteddata/') - indicator_url = reverse('indicator-detail', - kwargs={'pk': collecteddata.indicator.id}, - request=request) - - data = {'indicator': indicator_url, - 'description': 'Intermediate Results'} - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'update'}) - response = view(request, pk=collecteddata.pk) - self.assertEqual(response.status_code, 200) - - collecteddata = CollectedData.objects.get(pk=response.data['id']) - self.assertEquals(collecteddata.description, data['description']) - - def test_update_collecteddata_program_admin_json(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - request = self.factory.post('/api/collecteddata/') - indicator_url = reverse('indicator-detail', - kwargs={'pk': collecteddata.indicator.id}, - request=request) - - data = {'indicator': indicator_url, - 'description': 'Intermediate Results'} - request = self.factory.post('/api/collecteddata/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'update'}) - response = view(request, pk=collecteddata.pk) - self.assertEqual(response.status_code, 200) - - collecteddata = CollectedData.objects.get(pk=response.data['id']) - self.assertEquals(collecteddata.description, data['description']) - - def test_update_collecteddata_program_team(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - request = self.factory.post('/api/collecteddata/') - indicator_url = reverse('indicator-detail', - kwargs={'pk': collecteddata.indicator.id}, - request=request) - - data = {'indicator': indicator_url, - 'description': 'Intermediate Results'} - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'update'}) - response = view(request, pk=collecteddata.pk) - self.assertEqual(response.status_code, 200) - - collecteddata = CollectedData.objects.get(pk=response.data['id']) - self.assertEquals(collecteddata.description, data['description']) - - def test_update_collecteddata_view_only(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - request = self.factory.post('/api/collecteddata/') - indicator_url = reverse('indicator-detail', - kwargs={'pk': collecteddata.indicator.id}, - request=request) - - data = {'indicator': indicator_url, - 'description': 'Intermediate Results'} - request = self.factory.post('/api/collecteddata/', data) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'post': 'update'}) - response = view(request, pk=collecteddata.pk) - self.assertEqual(response.status_code, 403) - - -class CollectedDataDeleteViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_delete_collecteddata_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - collecteddata = factories.CollectedData() - request = self.factory.delete('/api/collecteddata/') - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=collecteddata.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - CollectedData.DoesNotExist, - CollectedData.objects.get, pk=collecteddata.pk) - - def test_delete_collecteddata_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - request = self.factory.delete('/api/collecteddata/') - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=collecteddata.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - CollectedData.DoesNotExist, - CollectedData.objects.get, pk=collecteddata.pk) - - def test_delete_collecteddata_diff_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - request = self.factory.delete('/api/collecteddata/') - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=collecteddata.pk) - self.assertEquals(response.status_code, 403) - CollectedData.objects.get(pk=collecteddata.pk) - - def test_delete_collecteddata_program_admin(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/collecteddata/') - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=collecteddata.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - CollectedData.DoesNotExist, - CollectedData.objects.get, pk=collecteddata.pk) - - def test_delete_collecteddata_diff_org(self): - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/collecteddata/') - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=collecteddata.pk) - self.assertEquals(response.status_code, 403) - CollectedData.objects.get(pk=collecteddata.pk) - - def test_delete_collecteddata_program_team(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/collecteddata/') - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=collecteddata.pk) - self.assertEquals(response.status_code, 403) - CollectedData.objects.get(pk=collecteddata.pk) - - def test_delete_collecteddata_view_only(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - collecteddata = factories.CollectedData(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/collecteddata/') - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=collecteddata.pk) - self.assertEquals(response.status_code, 403) - CollectedData.objects.get(pk=collecteddata.pk) - - def test_delete_collecteddata_normal_user(self): - collecteddata = factories.CollectedData() - request = self.factory.delete('/api/collecteddata/') - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=collecteddata.pk) - self.assertEquals(response.status_code, 403) - CollectedData.objects.get(pk=collecteddata.pk) - - -class CollectedDataFilterViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_collecteddata_org_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - another_org = factories.Organization(name='Another Org') - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl1_2 = factories.WorkflowLevel1(organization=another_org) - indicator1 = factories.Indicator(workflowlevel1=[wkflvl1_1]) - indicator2 = factories.Indicator(workflowlevel1=[wkflvl1_2]) - collecteddata1 = factories.CollectedData( - data_uuid=111, indicator=indicator1) - factories.CollectedData( - data_uuid=222, indicator=indicator2) - - request = self.factory.get( - '/api/collecteddata/?indicator__workflowlevel1__organization__id=%s' - % self.tola_user.organization.pk) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['data_uuid'], - str(collecteddata1.data_uuid)) - - def test_filter_collecteddata_country_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - country1 = factories.Country(country='Brazil', code='BR') - country2 = factories.Country(country='Germany', code='DE') - wkflvl1_1 = factories.WorkflowLevel1( - country=[country1]) - wkflvl1_2 = factories.WorkflowLevel1(country=[country2]) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1_1) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1_2) - indicator1 = factories.Indicator(workflowlevel1=[wkflvl1_1]) - indicator2 = factories.Indicator(workflowlevel1=[wkflvl1_2]) - collecteddata1 = factories.CollectedData( - data_uuid=111, indicator=indicator1) - factories.CollectedData(data_uuid=222, indicator=indicator2) - - request = self.factory.get( - '/api/collecteddata/?indicator__workflowlevel1__country__country=%s' - % country1.country) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['data_uuid'], - str(collecteddata1.data_uuid)) - - def test_filter_collecteddata_wkflvl1_name_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wkflvl1_1 = factories.WorkflowLevel1() - wkflvl1_2 = factories.WorkflowLevel1(name='Construction Project') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1_1) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1_2) - indicator1 = factories.Indicator(workflowlevel1=[wkflvl1_1]) - indicator2 = factories.Indicator(workflowlevel1=[wkflvl1_2]) - collecteddata1 = factories.CollectedData( - data_uuid=111, indicator=indicator1) - factories.CollectedData(data_uuid=222, indicator=indicator2) - - request = self.factory.get( - '/api/collecteddata/?indicator__workflowlevel1__name=%s' - % wkflvl1_1.name) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['data_uuid'], - str(collecteddata1.data_uuid)) - - def test_filter_collecteddata_indicator_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wkflvl1_1 = factories.WorkflowLevel1() - wkflvl1_2 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1_1) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1_2) - indicator1 = factories.Indicator(workflowlevel1=[wkflvl1_1]) - indicator2 = factories.Indicator(workflowlevel1=[wkflvl1_2]) - collecteddata1 = factories.CollectedData( - data_uuid=111, indicator=indicator1) - factories.CollectedData(data_uuid=222, indicator=indicator2) - - request = self.factory.get( - '/api/collecteddata/?indicator=%s' % indicator1.pk) - request.user = self.tola_user.user - view = CollectedDataViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['data_uuid'], - str(collecteddata1.data_uuid)) diff --git a/feed/tests/test_contactview.py b/feed/tests/test_contactview.py deleted file mode 100755 index 464ef42d9..000000000 --- a/feed/tests/test_contactview.py +++ /dev/null @@ -1,301 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import json -from feed.views import ContactViewSet -import factories -from workflow.models import Contact, WorkflowLevel1, WorkflowTeam, \ - ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_ADMIN - - -class ContactListViewTest(TestCase): - def setUp(self): - factories.Contact.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_contact_superuser(self): - request = self.factory.get('/api/contact/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = ContactViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_contact_normaluser(self): - request = self.factory.get('/api/contact/') - request.user = self.tola_user.user - view = ContactViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_contact_normaluser_one_result(self): - request = self.factory.get('/api/contact/') - wflvl1 = WorkflowLevel1.objects.create( - name='WorkflowLevel1', organization=self.tola_user.organization) - WorkflowTeam.objects.create(workflow_user=self.tola_user, - workflowlevel1=wflvl1) - factories.Contact(organization=self.tola_user.organization, - workflowlevel1=wflvl1) - - request.user = self.tola_user.user - view = ContactViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class ContactCreateViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_create_contact_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/contact/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - organization_url = reverse('organization-detail', - kwargs={'pk': self.tola_user.organization.id} - , request=request) - country_url = reverse('country-detail', - kwargs={'pk': self.tola_user.country.id}, - request=request) - - data = {'name': 'John Lennon', - 'city': 'Liverpool', - 'country': country_url, - 'workflowlevel1': wflvl1_url, - 'organization': organization_url} - - request = self.factory.post('/api/contact/', data) - request.user = self.tola_user.user - view = ContactViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'John Lennon') - - def test_create_contact_program_admin(self): - request = self.factory.post('/api/contact/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - organization_url = reverse('organization-detail', - kwargs={'pk': self.tola_user.organization.id} - , request=request) - country_url = reverse('country-detail', - kwargs={'pk': self.tola_user.country.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'John Lennon', - 'city': 'Liverpool', - 'country': country_url, - 'workflowlevel1': wflvl1_url, - 'organization': organization_url} - - request = self.factory.post('/api/contact/', data) - request.user = self.tola_user.user - view = ContactViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'John Lennon') - - def test_create_contact_program_admin_another_org(self): - request = self.factory.post('/api/contact/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - another_org = factories.Organization(name='Another Org') - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - organization_url = reverse('organization-detail', - kwargs={'pk': another_org.id} - , request=request) - country_url = reverse('country-detail', - kwargs={'pk': self.tola_user.country.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'John Lennon', - 'city': 'Liverpool', - 'country': country_url, - 'workflowlevel1': wflvl1_url, - 'organization': organization_url} - - request = self.factory.post('/api/contact/', data) - request.user = self.tola_user.user - view = ContactViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 403) - - def test_create_contact_program_admin_json(self): - request = self.factory.post('/api/contact/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - organization_url = reverse('organization-detail', - kwargs={'pk': self.tola_user.organization.id} - , request=request) - country_url = reverse('country-detail', - kwargs={'pk': self.tola_user.country.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'John Lennon', - 'city': 'Liverpool', - 'country': country_url, - 'workflowlevel1': wflvl1_url, - 'organization': organization_url} - - request = self.factory.post('/api/contact/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = ContactViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'John Lennon') - - -class ContactDeleteViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_delete_contact_non_existing(self): - request = self.factory.delete('/api/contact/') - request.user = self.tola_user.user - view = ContactViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=288) - self.assertEquals(response.status_code, 404) - - def test_delete_contact_superuser(self): - self.tola_user.user.is_superuser = True - self.tola_user.user.is_staff = True - self.tola_user.user.save() - - contact = factories.Contact() - request = self.factory.delete('/api/contact/') - request.user = self.tola_user.user - view = ContactViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=contact.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Contact.DoesNotExist, - Contact.objects.get, pk=contact.pk) - - def test_delete_contact_org_admin(self): - contact = factories.Contact(organization=self.tola_user.organization) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.delete('/api/contact/') - request.user = self.tola_user.user - view = ContactViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=contact.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Contact.DoesNotExist, - Contact.objects.get, pk=contact.pk) - - def test_delete_contact_diff_org_org_admin(self): - another_org = factories.Organization(name='Another Org') - contact = factories.Contact(organization=another_org) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.delete('/api/contact/') - request.user = self.tola_user.user - view = ContactViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=contact.pk) - self.assertEquals(response.status_code, 403) - Contact.objects.get(pk=contact.pk) - - def test_delete_contact_program_admin(self): - contact = factories.Contact(organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=factories.WorkflowLevel1( - organization=self.tola_user.organization), - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - request = self.factory.delete('/api/contact/') - request.user = self.tola_user.user - view = ContactViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=contact.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Contact.DoesNotExist, - Contact.objects.get, pk=contact.pk) - - def test_delete_contact_diff_org_program_admin(self): - another_org = factories.Organization(name='Another Org') - contact = factories.Contact(organization=another_org) - group_org_admin = factories.Group(name=ROLE_PROGRAM_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.delete('/api/contact/') - request.user = self.tola_user.user - view = ContactViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=contact.pk) - self.assertEquals(response.status_code, 403) - Contact.objects.get(pk=contact.pk) - - def test_delete_contact_normal_user(self): - contact = factories.Contact() - - request = self.factory.delete('/api/contact/') - request.user = self.tola_user.user - view = ContactViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=contact.pk) - self.assertEquals(response.status_code, 403) - Contact.objects.get(pk=contact.pk) - - -class ContactFilterViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_contact_stakeholder_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wkflvl1 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1) - contact1 = factories.Contact(name='John', - organization=self.tola_user.organization, - workflowlevel1=wkflvl1) - factories.Contact(name='Paul', - organization=self.tola_user.organization, - workflowlevel1=wkflvl1) - request = self.factory.get('/api/contact/?name=%s' % contact1.name) - request.user = self.tola_user.user - view = ContactViewSet.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'], contact1.name) diff --git a/feed/tests/test_customformview.py b/feed/tests/test_customformview.py deleted file mode 100644 index 4e10c0b9f..000000000 --- a/feed/tests/test_customformview.py +++ /dev/null @@ -1,701 +0,0 @@ -import json - -from django.test import TestCase, override_settings -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -from mock import Mock, patch - -import factories -from feed.views import CustomFormViewSet -from formlibrary.models import CustomForm -from workflow.models import ( - WorkflowTeam, ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_TEAM, - ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY) - - -class CustomFormListViewsTest(TestCase): - def setUp(self): - factories.Organization(id=1) - factories.CustomForm.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_customform_superuser(self): - request = self.factory.get('/api/customform/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = CustomFormViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_customform_org_admin(self): - request = self.factory.get('/api/customform/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.CustomForm(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_customform_program_admin(self): - request = self.factory.get('/api/customform/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.CustomForm(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_customform_program_team(self): - request = self.factory.get('/api/customform/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.CustomForm(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_customform_view_only(self): - request = self.factory.get('/api/customform/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.CustomForm(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class CustomFormCreateViewsTest(TestCase): - def setUp(self): - org = factories.Organization(id=1) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser(organization=org) - factories.Group() - - def test_create_customform_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - data = {'name': '4W Daily Activity Report'} - - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], '4W Daily Activity Report') - - def test_create_customform_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/customform/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': '4W Daily Activity Report', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], '4W Daily Activity Report') - - def test_create_customform_program_admin(self): - request = self.factory.post('/api/customform/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': '4W Daily Activity Report', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], '4W Daily Activity Report') - - def test_create_customform_program_admin_json(self): - request = self.factory.post('/api/customform/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': '4W Daily Activity Report', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/customform/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], '4W Daily Activity Report') - - def test_create_customform_program_team(self): - request = self.factory.post('/api/customform/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - - data = {'name': '4W Daily Activity Report', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], '4W Daily Activity Report') - - def test_create_customform_view_only(self): - request = self.factory.post('/api/customform/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - - data = {'name': '4W Daily Activity Report', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 403) - - -class CustomFormUpdateViewsTest(TestCase): - def setUp(self): - org = factories.Organization(id=1) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser(organization=org) - factories.Group() - - def test_update_unexisting_customform(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - data = {'name': '4W Daily Activity Report'} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=288) - self.assertEqual(response.status_code, 404) - - def test_update_customform_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - customform = factories.CustomForm() - - data = {'name': '4W Daily Activity Report'} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 200) - - customform = CustomForm.objects.get(pk=response.data['id']) - self.assertEquals(customform.name, data['name']) - - def test_update_customform_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - customform = factories.CustomForm( - organization=self.tola_user.organization) - - data = {'name': '4W Daily Activity Report'} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 200) - - customform = CustomForm.objects.get(pk=response.data['id']) - self.assertEquals(customform.name, data['name']) - - def test_update_customform_org_admin_json(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - customform = factories.CustomForm( - organization=self.tola_user.organization, - created_by=self.tola_user.user) - - data = {'name': '4W Daily Activity Report'} - request = self.factory.post('/api/customform/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 200) - - customform = CustomForm.objects.get(pk=response.data['id']) - self.assertEquals(customform.name, data['name']) - - def test_update_customform_diff_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - another_org = factories.Organization(name='Another Org') - customform = factories.CustomForm( - organization=another_org) - - data = {'name': '4W Daily Activity Report'} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 403) - - def test_update_customform_no_created_by(self): - customform = factories.CustomForm( - organization=self.tola_user.organization) - - data = {'name': '4W Daily Activity Report'} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 403) - - @override_settings(TOLA_TRACK_URL='https://tolatrack.com') - @override_settings(TOLA_TRACK_TOKEN='TheToken') - @patch('feed.views.requests') - def test_update_customform_org_admin_first_wfl1(self, mock_requests): - external_response = {'id': 1234} - mock_requests.post.return_value = Mock( - status_code=201, content=json.dumps(external_response)) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/customform/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - customform = factories.CustomForm( - organization=self.tola_user.organization) - - data = {'name': '4W Daily Activity Report', - 'description': 'It is a test', - 'fields': '[{"name": "name", "type": "text"},' - '{"name": "age", "type": "number"},' - '{"name": "city", "type": "text"}]', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 200) - - customform = CustomForm.objects.get(pk=response.data['id']) - self.assertEquals(customform.name, data['name']) - - @override_settings(TOLA_TRACK_URL='https://tolatrack.com') - @override_settings(TOLA_TRACK_TOKEN='TheToken') - @patch('feed.views.requests') - def test_update_customform_org_admin_wfl1_json(self, mock_requests): - external_response = {'id': 1234} - mock_requests.post.return_value = Mock( - status_code=201, content=json.dumps(external_response)) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/customform/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - customform = factories.CustomForm( - organization=self.tola_user.organization) - - data = {'name': '4W Daily Activity Report', - 'description': 'It is a test', - 'fields': '[{"name": "name", "type": "text"},' - '{"name": "age", "type": "number"},' - '{"name": "city", "type": "text"}]', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/customform/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 200) - - customform = CustomForm.objects.get(pk=response.data['id']) - self.assertEquals(customform.name, data['name']) - - @override_settings(TOLA_TRACK_URL='https://tolatrack.com') - @override_settings(TOLA_TRACK_TOKEN='TheToken') - @patch('feed.views.requests') - def test_update_customform_org_admin_second_wfl1(self, mock_requests): - external_response = {'id': 1234} - mock_requests.get.return_value = Mock( - status_code=200, content=json.dumps('false')) - mock_requests.put.return_value = Mock( - status_code=200, content=json.dumps(external_response)) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/customform/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - customform = factories.CustomForm( - workflowlevel1=wflvl1, - organization=self.tola_user.organization, - silo_id=1234) - - data = {'name': '4W Daily Activity Report', - 'description': 'It is a test', - 'fields': '[{"name": "name", "type": "text"},' - '{"name": "age", "type": "number"},' - '{"name": "city", "type": "text"}]', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 200) - - customform = CustomForm.objects.get(pk=response.data['id']) - self.assertEquals(customform.name, data['name']) - - @override_settings(TOLA_TRACK_URL='https://tolatrack.com') - @override_settings(TOLA_TRACK_TOKEN='TheToken') - @patch('feed.views.requests') - def test_update_customform_org_admin_third_wfl1(self, mock_requests): - external_response = {'id': 1234} - mock_requests.get.return_value = Mock( - status_code=200, content=json.dumps('false')) - mock_requests.put.return_value = Mock( - status_code=200, content=json.dumps(external_response)) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - customform = factories.CustomForm( - workflowlevel1=wflvl1, - organization=self.tola_user.organization, - silo_id=1234) - - data = {'name': '4W Daily Activity Report', - 'description': 'It is a test', - 'fields': '[{"name": "name", "type": "text"},' - '{"name": "age", "type": "number"},' - '{"name": "city", "type": "text"}]'} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 200) - - customform = CustomForm.objects.get(pk=response.data['id']) - self.assertEquals(customform.name, data['name']) - - @override_settings(TOLA_TRACK_URL='https://tolatrack.com') - @override_settings(TOLA_TRACK_TOKEN='TheToken') - @patch('feed.views.requests') - def test_update_customform_org_admin_fourth_wfl1(self, mock_requests): - external_response = {'id': 1234} - mock_requests.get.return_value = Mock( - status_code=200, content=json.dumps('false')) - mock_requests.put.return_value = Mock( - status_code=200, content=json.dumps(external_response)) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/customform/') - wflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_2 = factories.WorkflowLevel1() - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1_2.id}, - request=request) - customform = factories.CustomForm( - workflowlevel1=wflvl1_1, - organization=self.tola_user.organization, - silo_id=1234) - - data = {'name': '4W Daily Activity Report', - 'description': 'It is a test', - 'fields': '[{"name": "name", "type": "text"},' - '{"name": "age", "type": "number"},' - '{"name": "city", "type": "text"}]', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 400) - self.assertEqual(response.data['detail'], - 'You cannot change the Program.') - - @override_settings(TOLA_TRACK_URL='https://tolatrack.com') - @override_settings(TOLA_TRACK_TOKEN='TheToken') - @patch('feed.views.requests') - def test_update_customform_org_admin_fifth_wfl1(self, mock_requests): - mock_requests.get.return_value = Mock( - status_code=200, content=json.dumps('true')) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/customform/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - customform = factories.CustomForm( - workflowlevel1=wflvl1, - organization=self.tola_user.organization, - silo_id=1234) - - data = {'name': '4W Daily Activity Report', - 'description': 'It is a test', - 'fields': '[{"name": "name", "type": "text"},' - '{"name": "age", "type": "number"},' - '{"name": "city", "type": "text"}]', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 409) - self.assertEqual(response.data['detail'], - 'You already have data in the instance.') - - @override_settings(TOLA_TRACK_URL='https://tolatrack.com') - @override_settings(TOLA_TRACK_TOKEN='TheToken') - @patch('feed.views.requests') - def test_update_customform_wfl1_program_admin(self, mock_requests): - # Mock request - external_response = {'id': 1234} - mock_requests.post.return_value = Mock( - status_code=201, content=json.dumps(external_response)) - - # Create a program and program team for the user - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - customform = factories.CustomForm( - organization=self.tola_user.organization, - created_by=self.tola_user.user) - - # Make the request - request = self.factory.post('/api/customform/') - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': '4W Daily Activity Report', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 200) - - customform = CustomForm.objects.get(pk=response.data['id']) - self.assertEquals(customform.name, data['name']) - - def test_update_customform_wfl1_view_only(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - customform = factories.CustomForm( - organization=self.tola_user.organization, - created_by=self.tola_user.user) - - request = self.factory.post('/api/customform/') - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': '4W Daily Activity Report', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/customform/', data) - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'post': 'update'}) - response = view(request, pk=customform.pk) - self.assertEqual(response.status_code, 403) - - -class CustomFormDeleteViewsTest(TestCase): - def setUp(self): - org = factories.Organization(id=1) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser(organization=org) - factories.Group() - - def test_delete_customform_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - customform = factories.CustomForm() - request = self.factory.delete('/api/customform/') - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=customform.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - CustomForm.DoesNotExist, - CustomForm.objects.get, pk=customform.pk) - - def test_delete_customform_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - customform = factories.CustomForm( - organization=self.tola_user.organization) - request = self.factory.delete('/api/customform/') - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=customform.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - CustomForm.DoesNotExist, - CustomForm.objects.get, pk=customform.pk) - - def test_delete_customform_diff_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - another_org = factories.Organization(name='Another Org') - customform = factories.CustomForm(organization=another_org) - request = self.factory.delete('/api/customform/') - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=customform.pk) - self.assertEquals(response.status_code, 403) - CustomForm.objects.get(pk=customform.pk) - - def test_delete_customform_created_by(self): - customform = factories.CustomForm( - organization=self.tola_user.organization, - created_by=self.tola_user.user) - - request = self.factory.delete('/api/customform/') - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=customform.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - CustomForm.DoesNotExist, - CustomForm.objects.get, pk=customform.pk) - - def test_delete_customform_no_created_by(self): - customform = factories.CustomForm( - organization=self.tola_user.organization) - request = self.factory.delete('/api/customform/') - request.user = self.tola_user.user - view = CustomFormViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=customform.pk) - self.assertEquals(response.status_code, 403) - CustomForm.objects.get(pk=customform.pk) - - -class CustomFormFilterViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_customform_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - ext_serv1 = factories.CustomForm( - organization=self.tola_user.organization) - factories.CustomForm(name='Custom Form B') - request = self.factory.get('/api/customform/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = CustomFormViewSet.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'], ext_serv1.name) diff --git a/feed/tests/test_dashboardview.py b/feed/tests/test_dashboardview.py deleted file mode 100644 index c06dc4b4d..000000000 --- a/feed/tests/test_dashboardview.py +++ /dev/null @@ -1,236 +0,0 @@ -import json - -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import DashboardViewSet -from workflow.models import Dashboard - - -class DashboardListViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - factories.Dashboard.create_batch(2) - - def test_list_dashboard_super_user(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('') - request.user = self.tola_user.user - view = DashboardViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_dashboard_created_by(self): - user = factories.User(first_name='John', last_name='Lennon') - tola_user = factories.TolaUser(user=user) - factories.Dashboard(name='My New Dashboard', user=tola_user) - - request = self.factory.get('') - request.user = tola_user.user - view = DashboardViewSet.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'], u'My New Dashboard') - - def test_list_dashboard_shared_with(self): - user = factories.User(first_name='John', last_name='Lennon') - tola_user = factories.TolaUser(user=user) - factories.Dashboard(name='My New Dashboard', user=self.tola_user, - share=[tola_user]) - - request = self.factory.get('') - request.user = tola_user.user - view = DashboardViewSet.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'], u'My New Dashboard') - - def test_list_dashboard_public_org(self): - user = factories.User(first_name='John', last_name='Lennon') - tola_user = factories.TolaUser( - user=user, organization=self.tola_user.organization) - factories.Dashboard(name='My Org Dashboard', user=self.tola_user, - public=Dashboard.PUBLIC_ORG) - - request = self.factory.get('') - request.user = tola_user.user - view = DashboardViewSet.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'], u'My Org Dashboard') - - def test_list_dashboard_public_all(self): - another_org = factories.Organization() - org = self.tola_user.organization - self.tola_user.organization = another_org - self.tola_user.save() - - user = factories.User(first_name='John', last_name='Lennon') - tola_user = factories.TolaUser(user=user, organization=org) - factories.Dashboard(name='Another Org Dashboard', user=self.tola_user, - public=Dashboard.PUBLIC_ALL) - - request = self.factory.get('') - request.user = tola_user.user - view = DashboardViewSet.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'], u'Another Org Dashboard') - - -class DashboardCreateViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_create_dashboard_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - data = {'name': 'YoDash'} - request = self.factory.post('/api/dashboard/', data) - request.user = self.tola_user.user - view = DashboardViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'YoDash') - self.assertEqual(response.data['user'].name, self.tola_user.name) - - def test_create_dashboard_json(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - data = {'name': 'YoDash'} - request = self.factory.post('/api/dashboard/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = DashboardViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'YoDash') - self.assertEqual(response.data['user'].name, self.tola_user.name) - - -class DashboardFilterViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Dashboard.create_batch(2) - - def test_filter_dashboard_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('?public={}'.format(Dashboard.PUBLIC_ORG)) - request.user = self.tola_user.user - view = DashboardViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_filter_dashboard_public_all(self): - another_org = factories.Organization() - org = self.tola_user.organization - self.tola_user.organization = another_org - self.tola_user.save() - user_1 = factories.User(first_name='John', last_name='Lennon') - user_2 = factories.User(first_name='Paul', last_name='McCartney') - tolauser_1 = factories.TolaUser(user=user_1, organization=org) - tolauser_2 = factories.TolaUser(user=user_2, organization=org) - - factories.Dashboard(name='My Org Dashboard', user=tolauser_1, - public=Dashboard.PUBLIC_ORG) - factories.Dashboard(name='Another Org Dashboard', user=self.tola_user, - public=Dashboard.PUBLIC_ALL) - - request = self.factory.get('?public={}'.format(Dashboard.PUBLIC_ALL)) - request.user = tolauser_2.user - view = DashboardViewSet.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'], u'Another Org Dashboard') - self.assertNotEqual(response.data[0]['name'], u'My Org Dashboard') - - def test_filter_dashboard_public_org(self): - another_org = factories.Organization() - org = self.tola_user.organization - self.tola_user.organization = another_org - self.tola_user.save() - user_1 = factories.User(first_name='John', last_name='Lennon') - user_2 = factories.User(first_name='Paul', last_name='McCartney') - tolauser_1 = factories.TolaUser(user=user_1, organization=org) - tolauser_2 = factories.TolaUser(user=user_2, organization=org) - - factories.Dashboard(name='My Org Dashboard', user=tolauser_1, - public=Dashboard.PUBLIC_ORG) - factories.Dashboard(name='Another Org Dashboard', user=self.tola_user, - public=Dashboard.PUBLIC_ALL) - - request = self.factory.get('?public={}'.format(Dashboard.PUBLIC_ORG)) - request.user = tolauser_2.user - view = DashboardViewSet.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'], u'My Org Dashboard') - self.assertNotEqual(response.data[0]['name'], u'Another Org Dashboard') - - def test_filter_dashboard_share_with(self): - user = factories.User(first_name='John', last_name='Lennon') - tolauser = factories.TolaUser( - user=user, organization=self.tola_user.organization) - - factories.Dashboard(name='My Dashboard', user=tolauser) - factories.Dashboard(name='Not shared Dashboard', user=self.tola_user) - factories.Dashboard(name='Shared Dashboard', user=self.tola_user, - share=[tolauser]) - - request = self.factory.get('?share={}'.format(tolauser.id)) - request.user = tolauser.user - view = DashboardViewSet.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'], u'Shared Dashboard') - self.assertNotEqual(response.data[0]['name'], u'Not shared Dashboard') - self.assertNotEqual(response.data[0]['name'], u'My Dashboard') - - def test_filter_dashboard_created_by(self): - user = factories.User(first_name='John', last_name='Lennon') - tolauser = factories.TolaUser( - user=user, organization=self.tola_user.organization) - - factories.Dashboard(name='My Dashboard', user=tolauser) - factories.Dashboard(name='Shared Dashboard', user=self.tola_user, - share=[tolauser]) - - request = self.factory.get('?user={}'.format(tolauser.id)) - request.user = tolauser.user - view = DashboardViewSet.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'], u'My Dashboard') - self.assertNotEqual(response.data[0]['name'], u'Shared Dashboard') diff --git a/feed/tests/test_disaggregationtypeview.py b/feed/tests/test_disaggregationtypeview.py deleted file mode 100755 index 4198d32e7..000000000 --- a/feed/tests/test_disaggregationtypeview.py +++ /dev/null @@ -1,85 +0,0 @@ -from django.test import TestCase -from rest_framework.reverse import reverse -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import DisaggregationTypeViewSet -from indicators.models import DisaggregationType - - -class DisaggregationTypeListViewsTest(TestCase): - def setUp(self): - factories.Organization(id=0) - DisaggregationType.objects.bulk_create([ - DisaggregationType(disaggregation_type='DisaggregationType1'), - DisaggregationType(disaggregation_type='DisaggregationType2'), - ]) - - factory = APIRequestFactory() - self.request_get = factory.get('/api/disaggregationtype/') - - def test_list_disaggregationtype_superuser(self): - self.request_get.user = factories.User.build(is_superuser=True, - is_staff=True) - view = DisaggregationTypeViewSet.as_view({'get': 'list'}) - response = view(self.request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_disaggregationtype_normaluser(self): - tola_user = factories.TolaUser() - self.request_get.user = tola_user.user - view = DisaggregationTypeViewSet.as_view({'get': 'list'}) - response = view(self.request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_disaggregationtype_normaluser_one_result(self): - tola_user = factories.TolaUser() - DisaggregationType.objects.create( - disaggregation_type='DisaggregationType0', - organization=tola_user.organization) - - self.request_get.user = tola_user.user - view = DisaggregationTypeViewSet.as_view({'get': 'list'}) - response = view(self.request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class DisaggregationTypeCreateViewsTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_create_disaggregationtype_normaluser_missing_values(self): - data = {} - request = self.factory.post('', data) - request.user = self.tola_user.user - view = DisaggregationTypeViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 400) - - def test_create_disaggregationtype_normaluser(self): - organization_url = reverse( - 'organization-detail', - kwargs={'pk': self.tola_user.organization.pk}) - data = { - 'disaggregation_type': 'Some Type', - 'description': 'Some Desc', - } - request = self.factory.post('', data) - request.user = self.tola_user.user - view = DisaggregationTypeViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['disaggregation_type'], u'Some Type') - self.assertEqual(response.data['description'], u'Some Desc') - self.assertIn(organization_url, response.data['organization']) - - disaggregation_type = DisaggregationType.objects.get( - pk=response.data['id']) - self.assertEqual(disaggregation_type.disaggregation_type, u'Some Type') - self.assertEqual(disaggregation_type.description, u'Some Desc') - self.assertEqual(disaggregation_type.organization, - self.tola_user.organization) diff --git a/feed/tests/test_documentationview.py b/feed/tests/test_documentationview.py deleted file mode 100644 index 4aa9f213f..000000000 --- a/feed/tests/test_documentationview.py +++ /dev/null @@ -1,290 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -from feed.views import DocumentationViewSet -import factories -import json -from workflow.models import Documentation, WorkflowTeam, \ - ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_ADMIN - - -class DocumentationListViewTest(TestCase): - def setUp(self): - factories.Documentation.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_documentation_superuser(self): - request = self.factory.get('/api/documentation/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = DocumentationViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_documentation_normaluser(self): - request = self.factory.get('/api/documentation/') - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_documentation_normaluser_one_result(self): - request = self.factory.get('/api/documentation/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create(workflow_user=self.tola_user, - workflowlevel1=wflvl1) - factories.Documentation(workflowlevel1=wflvl1) - - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class DocumentationCreateViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_create_documentation_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/documentation/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - - data = {'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/documentation/', data) - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['workflowlevel1'], wflvl1_url) - - def test_create_documentation_program_admin(self): - request = self.factory.post('/api/documentation/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/documentation/', data) - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['workflowlevel1'], wflvl1_url) - - def test_create_documentation_program_admin_json(self): - request = self.factory.post('/api/documentation/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/documentation/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['workflowlevel1'], wflvl1_url) - - -class DocumentationDeleteViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_delete_documentation_non_existing(self): - request = self.factory.delete('/api/documentation/') - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=288) - self.assertEquals(response.status_code, 404) - - def test_delete_documentation_superuser(self): - self.tola_user.user.is_superuser = True - self.tola_user.user.is_staff = True - self.tola_user.user.save() - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - documentation = factories.Documentation(workflowlevel1=wflvl1) - request = self.factory.delete('/api/documentation/') - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=documentation.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Documentation.DoesNotExist, - Documentation.objects.get, pk=documentation.pk) - - def test_delete_documentation_org_admin(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - documentation = factories.Documentation(workflowlevel1=wflvl1) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.delete('/api/documentation/') - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=documentation.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Documentation.DoesNotExist, - Documentation.objects.get, pk=documentation.pk) - - def test_delete_documentation_diff_org_org_admin(self): - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - documentation = factories.Documentation(workflowlevel1=wflvl1) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.delete('/api/documentation/') - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=documentation.pk) - self.assertEquals(response.status_code, 403) - Documentation.objects.get(pk=documentation.pk) - - def test_delete_documentation_program_admin(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - documentation = factories.Documentation(workflowlevel1=wflvl1) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - request = self.factory.delete('/api/documentation/') - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=documentation.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Documentation.DoesNotExist, - Documentation.objects.get, pk=documentation.pk) - - def test_delete_documentation_diff_org_program_admin(self): - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - documentation = factories.Documentation(workflowlevel1=wflvl1) - group_org_admin = factories.Group(name=ROLE_PROGRAM_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.delete('/api/documentation/') - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=documentation.pk) - self.assertEquals(response.status_code, 403) - Documentation.objects.get(pk=documentation.pk) - - def test_delete_documentation_normal_user(self): - wflvl1 = factories.WorkflowLevel1() - documentation = factories.Documentation(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/documentation/') - request.user = self.tola_user.user - view = DocumentationViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=documentation.pk) - self.assertEquals(response.status_code, 403) - Documentation.objects.get(pk=documentation.pk) - - -class DocumentationFilterViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_documentation_country_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - wflvl1_1 = factories.WorkflowLevel1( - name='WorkflowLevel1_1', - organization=self.tola_user.organization) - WorkflowTeam.objects.create(workflow_user=self.tola_user, - workflowlevel1=wflvl1_1) - documentation1 = factories.Documentation(name='Document 1', - workflowlevel1=wflvl1_1) - - another_org = factories.Organization(name='Another Org') - wflvl1_2 = factories.WorkflowLevel1( - name='WorkflowLevel1_2', - organization=another_org) - WorkflowTeam.objects.create(workflow_user=self.tola_user, - workflowlevel1=wflvl1_2) - factories.Documentation(name='Document 2', workflowlevel1=wflvl1_2) - - request = self.factory.get( - '/api/documentation' - '/?workflowlevel1__organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = DocumentationViewSet.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'], documentation1.name) - - def test_filter_documentation_org_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - country1 = factories.Country() - wflvl1_1 = factories.WorkflowLevel1( - name='WorkflowLevel1_1', - country=[country1], - organization=self.tola_user.organization) - WorkflowTeam.objects.create(workflow_user=self.tola_user, - workflowlevel1=wflvl1_1) - documentation1 = factories.Documentation(name='Document 1', - workflowlevel1=wflvl1_1) - - country2 = factories.Country(country='Brazil', code='BR') - wflvl1_2 = factories.WorkflowLevel1( - name='WorkflowLevel1_2', - country=[country2], - organization=self.tola_user.organization) - WorkflowTeam.objects.create(workflow_user=self.tola_user, - workflowlevel1=wflvl1_2) - factories.Documentation(name='Document 2', workflowlevel1=wflvl1_2) - - request = self.factory.get( - '/api/documentation' - '/?workflowlevel1__country__country=%s' % - country1.country) - request.user = self.tola_user.user - view = DocumentationViewSet.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'], documentation1.name) diff --git a/feed/tests/test_externalserviceview.py b/feed/tests/test_externalserviceview.py deleted file mode 100644 index d2b712a5f..000000000 --- a/feed/tests/test_externalserviceview.py +++ /dev/null @@ -1,111 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import ExternalServiceViewSet -from workflow.models import WorkflowTeam, ROLE_ORGANIZATION_ADMIN, \ - ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY - - -class ExternalServiceListViewsTest(TestCase): - def setUp(self): - factories.Organization(id=1) - factories.ExternalService.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_externalservice_superuser(self): - request = self.factory.get('/api/externalservice/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = ExternalServiceViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_externalservice_org_admin(self): - request = self.factory.get('/api/externalservice/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = self.tola_user.user - view = ExternalServiceViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ExternalService(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_externalservice_program_admin(self): - request = self.factory.get('/api/externalservice/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = ExternalServiceViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ExternalService(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_externalservice_program_team(self): - request = self.factory.get('/api/externalservice/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = ExternalServiceViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ExternalService(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_externalservice_view_only(self): - request = self.factory.get('/api/externalservice/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = ExternalServiceViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ExternalService(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class ExternalServiceFilterViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_externalservice_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - ext_serv1 = factories.ExternalService( - organization=self.tola_user.organization) - factories.ExternalService(name='External Service B') - request = self.factory.get('/api/externalservice/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = ExternalServiceViewSet.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'], ext_serv1.name) diff --git a/feed/tests/test_frequencyview.py b/feed/tests/test_frequencyview.py deleted file mode 100644 index 497b1ce3e..000000000 --- a/feed/tests/test_frequencyview.py +++ /dev/null @@ -1,25 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -from feed.views import FrequencyViewSet -import factories - - -class FrequencyListViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - self.frequency = factories.Frequency( - organization=self.tola_user.organization) - - def test_list_frequency(self): - request = self.factory.get('') - request.user = self.tola_user.user - view = FrequencyViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['frequency'], - self.frequency.frequency) - self.assertEqual(response.data[0]['description'], - self.frequency.description) diff --git a/feed/tests/test_fundcodeview.py b/feed/tests/test_fundcodeview.py deleted file mode 100644 index 1409f40b9..000000000 --- a/feed/tests/test_fundcodeview.py +++ /dev/null @@ -1,132 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import FundCodeViewSet -from workflow.models import WorkflowTeam, ROLE_ORGANIZATION_ADMIN, \ - ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY - - -class FundCodeListViewsTest(TestCase): - def setUp(self): - factories.Organization(id=1) - factories.FundCode.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_fundcode_superuser(self): - request = self.factory.get('/api/fundcode/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = FundCodeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_fundcode_org_admin(self): - request = self.factory.get('/api/fundcode/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = self.tola_user.user - view = FundCodeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.FundCode(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_fundcode_program_admin(self): - request = self.factory.get('/api/fundcode/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = FundCodeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.FundCode(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_fundcode_program_team(self): - request = self.factory.get('/api/fundcode/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = FundCodeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.FundCode(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_fundcode_view_only(self): - request = self.factory.get('/api/fundcode/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = FundCodeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.FundCode(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class FundCodeFilterViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_filter_fundcode_superuser(self): - another_org = factories.Organization(name='Another Org') - fundcode1_1 = factories.FundCode( - organization=self.tola_user.organization) - factories.FundCode( - name='Another FundCode', - organization=another_org) - - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('/api/fundcode/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = FundCodeViewSet.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'], fundcode1_1.name) - - def test_filter_fundcode_normaluser(self): - another_org = factories.Organization(name='Another Org') - fundcode1_1 = factories.FundCode( - organization=self.tola_user.organization) - factories.FundCode( - name='Another FundCode', - organization=another_org) - - request = self.factory.get('/api/fundcode/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = FundCodeViewSet.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'], fundcode1_1.name) diff --git a/feed/tests/test_groupview.py b/feed/tests/test_groupview.py deleted file mode 100755 index 09a556f91..000000000 --- a/feed/tests/test_groupview.py +++ /dev/null @@ -1,42 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -from feed.views import GroupViewSet -import factories - - -class GroupViewsTest(TestCase): - def setUp(self): - factories.Group() - factory = APIRequestFactory() - self.request_get = factory.get('/api/groups/') - self.request_post = factory.post('/api/groups/') - - def test_list_groups_superuser(self): - self.request_get.user = factories.User.build(is_superuser=True, - is_staff=True) - view = GroupViewSet.as_view({'get': 'list'}) - response = view(self.request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_groups_normaluser(self): - tola_user = factories.TolaUser() - self.request_get.user = tola_user.user - view = GroupViewSet.as_view({'get': 'list'}) - response = view(self.request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_create_group_error(self): - # create group via POST request - data = {'name': 'TestGroup'} - self.request_post = APIRequestFactory().post('/api/stakeholder/', data) - self.request_post.user = factories.User.build(is_superuser=False, - is_staff=False) - view = GroupViewSet.as_view({'post': 'create'}) - - with self.assertRaises(AttributeError) as context: - view(self.request_post) - self.assertTrue('\'GroupViewSet\' object has no attribute ' - '\'create\'' in context.exception) diff --git a/feed/tests/test_indicatortypeview.py b/feed/tests/test_indicatortypeview.py deleted file mode 100644 index 9c65bee63..000000000 --- a/feed/tests/test_indicatortypeview.py +++ /dev/null @@ -1,132 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import IndicatorTypeViewSet -from workflow.models import WorkflowTeam, ROLE_ORGANIZATION_ADMIN, \ - ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY - - -class IndicatorTypeListViewsTest(TestCase): - def setUp(self): - factories.Organization(id=1) - factories.IndicatorType.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_indicatortype_superuser(self): - request = self.factory.get('/api/indicatortype/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = IndicatorTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_indicatortype_org_admin(self): - request = self.factory.get('/api/indicatortype/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = self.tola_user.user - view = IndicatorTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.IndicatorType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_indicatortype_program_admin(self): - request = self.factory.get('/api/indicatortype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = IndicatorTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.IndicatorType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_indicatortype_program_team(self): - request = self.factory.get('/api/indicatortype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = IndicatorTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.IndicatorType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_indicatortype_view_only(self): - request = self.factory.get('/api/indicatortype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = IndicatorTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.IndicatorType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class IndicatorTypeFilterViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_filter_indicatortype_superuser(self): - another_org = factories.Organization(name='Another Org') - indicatortype1_1 = factories.IndicatorType( - organization=self.tola_user.organization) - factories.IndicatorType(indicator_type='Another IndicatorType', - organization=another_org) - - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('/api/indicatortype/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = IndicatorTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['indicator_type'], - indicatortype1_1.indicator_type) - - def test_filter_indicatortype_normaluser(self): - another_org = factories.Organization(name='Another Org') - indicatortype1_1 = factories.IndicatorType( - organization=self.tola_user.organization) - factories.IndicatorType(indicator_type='Another IndicatorType', - organization=another_org) - - request = self.factory.get('/api/indicatortype/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = IndicatorTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['indicator_type'], - indicatortype1_1.indicator_type) diff --git a/feed/tests/test_indicatorview.py b/feed/tests/test_indicatorview.py deleted file mode 100644 index 4efef854d..000000000 --- a/feed/tests/test_indicatorview.py +++ /dev/null @@ -1,609 +0,0 @@ -import json - -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import factories -from feed.views import IndicatorViewSet -from indicators.models import Indicator -from workflow.models import ( - WorkflowTeam, ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_TEAM, - ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY) - - -class IndicatorListViewsTest(TestCase): - def setUp(self): - factories.Indicator.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_indicator_superuser(self): - request = self.factory.get('/api/indicator/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = IndicatorViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_indicator_superuser_and_org_admin(self): - request = self.factory.get('/api/indicator/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = IndicatorViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_indicator_org_admin(self): - request = self.factory.get('/api/indicator/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - another_org = factories.Organization(name='Another Org') - wflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_2 = factories.WorkflowLevel1( - organization=another_org) - wflvl1_3 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Indicator(workflowlevel1=[wflvl1_1]) - factories.Indicator(workflowlevel1=[wflvl1_2, wflvl1_3]) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_indicator_program_admin(self): - request = self.factory.get('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Indicator(workflowlevel1=[wflvl1]) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_indicator_program_team(self): - request = self.factory.get('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Indicator(workflowlevel1=[wflvl1]) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_indicator_normaluser(self): - request = self.factory.get('/api/indicator/') - wflvl1 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Indicator(workflowlevel1=[wflvl1]) - view = IndicatorViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class IndicatorCreateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_create_indicator_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1() - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Building resilience in Mali', - 'workflowlevel1': [wflvl1_url]} - - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Building resilience in Mali') - - def test_create_indicator_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Building resilience in Mali', - 'workflowlevel1': [wflvl1_url]} - - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Building resilience in Mali') - - def test_create_indicator_program_admin(self): - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - wflvl2 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl2_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl2.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'Building resilience in Mali', - 'workflowlevel1': [wflvl1_url, wflvl2_url]} - - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Building resilience in Mali') - - def test_create_indicator_program_admin_wflvl1_another_org(self): - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - another_org = factories.Organization(name='Another Org') - wflvl2 = factories.WorkflowLevel1(organization=another_org) - wflvl2_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl2.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'Building resilience in Mali', - 'workflowlevel1': [wflvl1_url, wflvl2_url]} - - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 403) - - def test_create_indicator_program_admin_json(self): - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - wflvl2 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl2_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl2.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'Building resilience in Mali', - 'workflowlevel1': [wflvl1_url, wflvl2_url]} - request = self.factory.post('/api/indicator/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Building resilience in Mali') - - def test_create_level_program_team(self): - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - - data = {'name': 'Building resilience in Mali', - 'workflowlevel1': [wflvl1_url]} - - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Building resilience in Mali') - - def test_create_level_view_only(self): - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - - data = {'name': 'Building resilience in Mali', - 'workflowlevel1': [wflvl1_url]} - - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 403) - - -class IndicatorUpdateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_update_unexisting_indicator(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - data = {'name': 'Number of beneficiaries registered'} - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'update'}) - response = view(request, pk=288) - self.assertEqual(response.status_code, 404) - - def test_update_indicator_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Number of beneficiaries registered', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'update'}) - response = view(request, pk=indicator.pk) - self.assertEqual(response.status_code, 200) - - indicator = Indicator.objects.get(pk=response.data['id']) - self.assertEquals(indicator.name, data['name']) - - def test_update_indicator_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Number of beneficiaries registered', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'update'}) - response = view(request, pk=indicator.pk) - self.assertEqual(response.status_code, 200) - - indicator = Indicator.objects.get(pk=response.data['id']) - self.assertEquals(indicator.name, data['name']) - - def test_update_indicator_program_admin(self): - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Number of beneficiaries registered', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'update'}) - response = view(request, pk=indicator.pk) - self.assertEqual(response.status_code, 200) - - indicator = Indicator.objects.get(pk=response.data['id']) - self.assertEquals(indicator.name, data['name']) - - def test_update_indicator_program_team(self): - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Number of beneficiaries registered', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'update'}) - response = view(request, pk=indicator.pk) - self.assertEqual(response.status_code, 200) - - indicator = Indicator.objects.get(pk=response.data['id']) - self.assertEquals(indicator.name, data['name']) - - def test_update_indicator_view_only(self): - request = self.factory.post('/api/indicator/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Number of beneficiaries registered', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/indicator/', data) - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'post': 'update'}) - response = view(request, pk=indicator.pk) - self.assertEqual(response.status_code, 403) - - -class IndicatorDeleteViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_delete_indicator_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - wflvl1 = factories.WorkflowLevel1() - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - request = self.factory.delete('/api/indicator/') - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=indicator.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Indicator.DoesNotExist, - Indicator.objects.get, pk=indicator.pk) - - def test_delete_indicator_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - request = self.factory.delete('/api/indicator/') - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=indicator.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Indicator.DoesNotExist, - Indicator.objects.get, pk=indicator.pk) - - def test_delete_indicator_diff_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - request = self.factory.delete('/api/indicator/') - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=indicator.pk) - self.assertEquals(response.status_code, 403) - Indicator.objects.get(pk=indicator.pk) - - def test_delete_indicator_program_admin(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - - request = self.factory.delete('/api/indicator/') - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=indicator.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Indicator.DoesNotExist, - Indicator.objects.get, pk=indicator.pk) - - def test_delete_indicator_diff_org(self): - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - - request = self.factory.delete('/api/indicator/') - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=indicator.pk) - self.assertEquals(response.status_code, 403) - Indicator.objects.get(pk=indicator.pk) - - def test_delete_indicator_program_team(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - - request = self.factory.delete('/api/indicator/') - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=indicator.pk) - self.assertEquals(response.status_code, 403) - Indicator.objects.get(pk=indicator.pk) - - def test_delete_indicator_view_only(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - indicator = factories.Indicator(workflowlevel1=[wflvl1]) - - request = self.factory.delete('/api/indicator/') - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=indicator.pk) - self.assertEquals(response.status_code, 403) - Indicator.objects.get(pk=indicator.pk) - - def test_delete_indicator_normal_user(self): - indicator = factories.Indicator() - request = self.factory.delete('/api/indicator/') - request.user = self.tola_user.user - view = IndicatorViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=indicator.pk) - self.assertEquals(response.status_code, 403) - Indicator.objects.get(pk=indicator.pk) - - -class IndicatorFilterViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_indicator_superuser(self): - wflvl1_1 = factories.WorkflowLevel1() - wflvl1_2 = factories.WorkflowLevel1(name='Population Health Initiative') - indicator1_1 = factories.Indicator(workflowlevel1=[wflvl1_1]) - factories.Indicator(name='Another Indicator', workflowlevel1=[wflvl1_2]) - - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('/api/indicator/?workflowlevel1__name=%s' % - wflvl1_1.name) - request.user = self.tola_user.user - view = IndicatorViewSet.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'], indicator1_1.name) - - def test_filter_indicator_program_admin(self): - wflvl1_1 = factories.WorkflowLevel1() - wflvl1_2 = factories.WorkflowLevel1(name='Population Health Initiative') - indicator1_1 = factories.Indicator(workflowlevel1=[wflvl1_1]) - factories.Indicator(name='Another Indicator', workflowlevel1=[wflvl1_2]) - - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1_1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1_2, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - request = self.factory.get('/api/indicator/?workflowlevel1__name=%s' % - wflvl1_1.name) - request.user = self.tola_user.user - view = IndicatorViewSet.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'], indicator1_1.name) diff --git a/feed/tests/test_issueregisterview.py b/feed/tests/test_issueregisterview.py deleted file mode 100755 index 9e2df4deb..000000000 --- a/feed/tests/test_issueregisterview.py +++ /dev/null @@ -1,98 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import IssueRegisterViewSet - - -class IssueRegisterListViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - factories.IssueRegister.create_batch(2) - self.factory = APIRequestFactory() - - def test_list_issueregister_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('/api/issueregister/') - request.user = self.tola_user.user - view = IssueRegisterViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_issueregister_normaluser(self): - view = IssueRegisterViewSet.as_view({'get': 'list'}) - request = self.factory.get('/api/issueregister/') - request.user = self.tola_user.user - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_issueregister_normaluser_one_result(self): - factories.IssueRegister( - name='IssueRegister0', organization=self.tola_user.organization) - view = IssueRegisterViewSet.as_view({'get': 'list'}) - request = self.factory.get('/api/issueregister/') - request.user = self.tola_user.user - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class IssueRegisterCreateViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_create_issueregister_normaluser_one_result(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.post('/api/issueregister/') - request.user = self.tola_user.user - view = IssueRegisterViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - - # check if the obj created has the user organization - request = self.factory.get('/api/issueregister/') - request.user = self.tola_user.user - view = IssueRegisterViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class IssueRegisterFilterViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_issueregister_org_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - another_org = factories.Organization(name='Another Org') - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl1_2 = factories.WorkflowLevel1(organization=another_org) - wkflvl2_1 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_1) - wkflvl2_2 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_2) - issueregister1 = factories.IssueRegister(workflowlevel2=wkflvl2_1) - factories.IssueRegister(name='coded_field_b', workflowlevel2=wkflvl2_2) - - request = self.factory.get( - '/api/issueregister' - '/?workflowlevel2__workflowlevel1__organization__id=%s' - % self.tola_user.organization.pk) - request.user = self.tola_user.user - view = IssueRegisterViewSet.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'], issueregister1.name) diff --git a/feed/tests/test_levelview.py b/feed/tests/test_levelview.py deleted file mode 100644 index 56942783a..000000000 --- a/feed/tests/test_levelview.py +++ /dev/null @@ -1,539 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import json -import factories -from feed.views import LevelViewSet -from indicators.models import Level -from workflow.models import WorkflowTeam, ROLE_ORGANIZATION_ADMIN, \ - ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY - - -class LevelListViewsTest(TestCase): - def setUp(self): - factories.Level.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_level_superuser(self): - request = self.factory.get('/api/level/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = LevelViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_level_org_admin(self): - request = self.factory.get('/api/level/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Level(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_level_program_admin(self): - request = self.factory.get('/api/level/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Level(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_level_program_team(self): - request = self.factory.get('/api/level/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Level(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_level_normaluser(self): - request = self.factory.get('/api/level/') - request.user = self.tola_user.user - view = LevelViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Level(organization=self.tola_user.organization) - view = LevelViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class LevelCreateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_create_level_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.post('/api/level/') - wflvl1 = factories.WorkflowLevel1() - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Goal', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Goal') - - def test_create_level_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/level/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Goal', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Goal') - - def test_create_level_program_admin(self): - request = self.factory.post('/api/level/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'Goal', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Goal') - - def test_create_level_program_admin_json(self): - request = self.factory.post('/api/level/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'Goal', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/level/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Goal') - - def test_create_level_program_team(self): - request = self.factory.post('/api/level/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - - data = {'name': 'Goal', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Goal') - - def test_create_level_view_only(self): - request = self.factory.post('/api/level/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - - data = {'name': 'Goal', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 403) - - -class LevelUpdateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_update_unexisting_level(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - data = {'name': 'Intermediate Results'} - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'update'}) - response = view(request, pk=288) - self.assertEqual(response.status_code, 404) - - def test_update_level_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - level = factories.Level() - data = {'name': 'Goal'} - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'update'}) - response = view(request, pk=level.pk) - self.assertEqual(response.status_code, 200) - - level = Level.objects.get(pk=response.data['id']) - self.assertEquals(level.name, data['name']) - - def test_update_level_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - level = factories.Level(organization=self.tola_user.organization) - data = {'name': 'Goal'} - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'update'}) - response = view(request, pk=level.pk) - self.assertEqual(response.status_code, 200) - - level = Level.objects.get(pk=response.data['id']) - self.assertEquals(level.name, data['name']) - - def test_update_level_diff_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - another_org = factories.Organization(name='Another Org') - level = factories.Level(organization=another_org) - data = {'name': 'Goal'} - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'update'}) - response = view(request, pk=level.pk) - self.assertEqual(response.status_code, 403) - - def test_update_level_program_admin(self): - wflvl1 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - level = factories.Level(organization=self.tola_user.organization, - workflowlevel1=wflvl1) - - data = {'name': 'Goal'} - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'update'}) - response = view(request, pk=level.pk) - self.assertEqual(response.status_code, 200) - - level = Level.objects.get(pk=response.data['id']) - self.assertEquals(level.name, data['name']) - - def test_update_level_program_admin_json(self): - wflvl1 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - level = factories.Level(organization=self.tola_user.organization, - workflowlevel1=wflvl1) - - data = {'name': 'Goal'} - request = self.factory.post('/api/level/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'update'}) - response = view(request, pk=level.pk) - self.assertEqual(response.status_code, 200) - - level = Level.objects.get(pk=response.data['id']) - self.assertEquals(level.name, data['name']) - - def test_update_level_program_team(self): - wflvl1 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - level = factories.Level(organization=self.tola_user.organization, - workflowlevel1=wflvl1) - - data = {'name': 'Goal'} - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'update'}) - response = view(request, pk=level.pk) - self.assertEqual(response.status_code, 200) - - level = Level.objects.get(pk=response.data['id']) - self.assertEquals(level.name, data['name']) - - def test_update_level_view_only(self): - wflvl1 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - level = factories.Level(organization=self.tola_user.organization, - workflowlevel1=wflvl1) - - data = {'name': 'Goal'} - request = self.factory.post('/api/level/', data) - request.user = self.tola_user.user - view = LevelViewSet.as_view({'post': 'update'}) - response = view(request, pk=level.pk) - self.assertEqual(response.status_code, 403) - - -class LevelDeleteViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_delete_level_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - level = factories.Level() - request = self.factory.delete('/api/level/') - request.user = self.tola_user.user - view = LevelViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=level.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Level.DoesNotExist, - Level.objects.get, pk=level.pk) - - def test_delete_level_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - level = factories.Level(organization=self.tola_user.organization) - request = self.factory.delete('/api/level/') - request.user = self.tola_user.user - view = LevelViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=level.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Level.DoesNotExist, - Level.objects.get, pk=level.pk) - - def test_delete_level_diff_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - another_org = factories.Organization(name='Another Org') - level = factories.Level(organization=another_org) - request = self.factory.delete('/api/level/') - request.user = self.tola_user.user - view = LevelViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=level.pk) - self.assertEquals(response.status_code, 403) - Level.objects.get(pk=level.pk) - - def test_delete_level_program_admin(self): - wflvl1 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - level = factories.Level(organization=self.tola_user.organization, - workflowlevel1=wflvl1) - - request = self.factory.delete('/api/level/') - request.user = self.tola_user.user - view = LevelViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=level.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Level.DoesNotExist, - Level.objects.get, pk=level.pk) - - def test_delete_level_diff_org(self): - wflvl1 = factories.WorkflowLevel1() - another_org = factories.Organization(name='Another Org') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - level = factories.Level(organization=another_org, - workflowlevel1=wflvl1) - - request = self.factory.delete('/api/level/') - request.user = self.tola_user.user - view = LevelViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=level.pk) - self.assertEquals(response.status_code, 403) - Level.objects.get(pk=level.pk) - - def test_delete_level_program_team(self): - wflvl1 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - level = factories.Level(organization=self.tola_user.organization, - workflowlevel1=wflvl1) - - request = self.factory.delete('/api/level/') - request.user = self.tola_user.user - view = LevelViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=level.pk) - self.assertEquals(response.status_code, 403) - Level.objects.get(pk=level.pk) - - def test_delete_level_view_only(self): - wflvl1 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - level = factories.Level(organization=self.tola_user.organization, - workflowlevel1=wflvl1) - - request = self.factory.delete('/api/level/') - request.user = self.tola_user.user - view = LevelViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=level.pk) - self.assertEquals(response.status_code, 403) - Level.objects.get(pk=level.pk) - - def test_delete_level_normal_user(self): - level = factories.Level() - request = self.factory.delete('/api/level/') - request.user = self.tola_user.user - view = LevelViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=level.pk) - self.assertEquals(response.status_code, 403) - Level.objects.get(pk=level.pk) - - -class LevelFilterViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_level_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - another_org = factories.Organization(name='Another Org') - level1 = factories.Level( - name='Level 1', - organization=self.tola_user.organization) - factories.Level(name='Level 2', organization=another_org) - - request = self.factory.get( - '/api/level/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = LevelViewSet.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'], level1.name) - - def test_filter_level_normaluser(self): - country1 = factories.Country(country='Brazil', code='BR') - country2 = factories.Country() - level1 = factories.Level( - name='Level 1', - country=country1, - organization=self.tola_user.organization - ) - factories.Level(name='Level 2', country=country2, - organization=self.tola_user.organization) - - request = self.factory.get( - '/api/level/?country__country=%s' % - country1.country) - request.user = self.tola_user.user - view = LevelViewSet.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'], level1.name) diff --git a/feed/tests/test_milestoneview.py b/feed/tests/test_milestoneview.py deleted file mode 100644 index fc40a878e..000000000 --- a/feed/tests/test_milestoneview.py +++ /dev/null @@ -1,126 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import json -import factories -from feed.views import MilestoneViewSet -from workflow.models import WorkflowTeam, ROLE_ORGANIZATION_ADMIN, \ - ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY - - -class MilestoneListViewsTest(TestCase): - def setUp(self): - factories.Milestone.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_milestone_superuser(self): - request = self.factory.get('/api/milestone/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = MilestoneViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_milestone_org_admin(self): - request = self.factory.get('/api/milestone/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = self.tola_user.user - view = MilestoneViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Milestone(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_milestone_program_admin(self): - request = self.factory.get('/api/milestone/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = MilestoneViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Milestone(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_milestone_program_team(self): - request = self.factory.get('/api/milestone/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = MilestoneViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Milestone(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_milestone_view_only(self): - request = self.factory.get('/api/milestone/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = MilestoneViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.Milestone(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class MilestoneViewTest(TestCase): - def setUp(self): - self.user = factories.User(is_superuser=True, is_staff=True) - factory = APIRequestFactory() - self.request = factory.post('/api/milestone/') - - def test_create_milestone(self): - user_url = reverse('user-detail', kwargs={'pk': self.user.id}, - request=self.request) - - data = {'name': 'Project Implementation'} - self.request = APIRequestFactory().post('/api/milestone/', data) - self.request.user = self.user - view = MilestoneViewSet.as_view({'post': 'create'}) - response = view(self.request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Project Implementation') - self.assertEqual(response.data['created_by'], user_url) - - def test_create_milestone_json(self): - user_url = reverse('user-detail', kwargs={'pk': self.user.id}, - request=self.request) - - data = {'name': 'Project Implementation'} - self.request = APIRequestFactory().post( - '/api/milestone/', json.dumps(data), - content_type='application/json') - self.request.user = self.user - view = MilestoneViewSet.as_view({'post': 'create'}) - response = view(self.request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Project Implementation') - self.assertEqual(response.data['created_by'], user_url) diff --git a/feed/tests/test_objectiveview.py b/feed/tests/test_objectiveview.py deleted file mode 100644 index 112b9da55..000000000 --- a/feed/tests/test_objectiveview.py +++ /dev/null @@ -1,241 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import json -import factories -from feed.views import ObjectiveViewSet -from indicators.models import Objective -from workflow.models import WorkflowLevel1, WorkflowTeam, \ - ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_ADMIN - - -class ObjectiveViewTest(TestCase): - def setUp(self): - factories.Objective.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_objective_superuser(self): - request = self.factory.get('/api/objective/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = ObjectiveViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_objective_normaluser(self): - request = self.factory.get('/api/objective/') - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_objective_normaluser_one_result(self): - request = self.factory.get('/api/objective/') - wflvl1 = WorkflowLevel1.objects.create( - name='WorkflowLevel1', organization=self.tola_user.organization) - WorkflowTeam.objects.create(workflow_user=self.tola_user, - workflowlevel1=wflvl1) - factories.Objective(workflowlevel1=wflvl1) - - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_create_objective_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/objective/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Macht Deutschland wieder gesund', - 'workflowlevel1': wflvl1_url} - request = self.factory.post('/api/objective/', data) - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], - u'Macht Deutschland wieder gesund') - - def test_create_objective_program_admin(self): - request = self.factory.post('/api/objective/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'Macht Deutschland wieder gesund', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/objective/', data) - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], - u'Macht Deutschland wieder gesund') - - def test_create_objective_program_admin_json(self): - request = self.factory.post('/api/objective/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'Macht Deutschland wieder gesund', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/objective/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], - u'Macht Deutschland wieder gesund') - - def test_delete_objective_non_existing(self): - request = self.factory.delete('/api/objective/') - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=288) - self.assertEquals(response.status_code, 404) - - def test_delete_objective_superuser(self): - self.tola_user.user.is_superuser = True - self.tola_user.user.is_staff = True - self.tola_user.user.save() - - objective = factories.Objective() - request = self.factory.delete('/api/objective/') - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=objective.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Objective.DoesNotExist, - Objective.objects.get, pk=objective.pk) - - def test_delete_objective_org_admin(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - objective = factories.Objective(workflowlevel1=wflvl1) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.delete('/api/objective/') - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=objective.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Objective.DoesNotExist, - Objective.objects.get, pk=objective.pk) - - def test_delete_objective_diff_org_org_admin(self): - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - objective = factories.Objective(workflowlevel1=wflvl1) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.delete('/api/objective/') - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=objective.pk) - self.assertEquals(response.status_code, 403) - Objective.objects.get(pk=objective.pk) - - def test_delete_objective_program_admin(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - objective = factories.Objective(workflowlevel1=wflvl1) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN) - ) - - request = self.factory.delete('/api/objective/') - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=objective.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Objective.DoesNotExist, - Objective.objects.get, pk=objective.pk) - - def test_delete_objective_diff_org_program_admin(self): - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - objective = factories.Objective(workflowlevel1=wflvl1) - group_org_admin = factories.Group(name=ROLE_PROGRAM_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.delete('/api/objective/') - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=objective.pk) - self.assertEquals(response.status_code, 403) - Objective.objects.get(pk=objective.pk) - - def test_delete_objective_normal_user(self): - objective = factories.Objective() - - request = self.factory.delete('/api/objective/') - request.user = self.tola_user.user - view = ObjectiveViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=objective.pk) - self.assertEquals(response.status_code, 403) - Objective.objects.get(pk=objective.pk) - - -class ObjectiveFilterViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_indicator_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - another_org = factories.Organization(name='Another Org') - wflvl1_1 = WorkflowLevel1.objects.create( - name='WorkflowLevel1', organization=self.tola_user.organization) - wflvl1_2 = WorkflowLevel1.objects.create( - name='WorkflowLevel1', organization=another_org) - objective1 = factories.Objective(workflowlevel1=wflvl1_1) - factories.Objective(name='20% increase in incomes', - workflowlevel1=wflvl1_2) - - request = self.factory.get( - '/api/objective/?workflowlevel1__organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = ObjectiveViewSet.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'], objective1.name) diff --git a/feed/tests/test_organizationview.py b/feed/tests/test_organizationview.py deleted file mode 100644 index 9fe137e3a..000000000 --- a/feed/tests/test_organizationview.py +++ /dev/null @@ -1,28 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import OrganizationViewSet - - -class OrganizationViewTest(TestCase): - def setUp(self): - factories.Organization.create_batch(2) - - factory = APIRequestFactory() - self.request = factory.get('/api/organization/') - - def test_list_organization_superuser(self): - self.request.user = factories.User.build(is_superuser=True, is_staff=True) - view = OrganizationViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_organization_normaluser_one_result(self): - tola_user = factories.TolaUser() - self.request.user = tola_user.user - view = OrganizationViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) diff --git a/feed/tests/test_portfolioview.py b/feed/tests/test_portfolioview.py deleted file mode 100644 index f1ce72f2a..000000000 --- a/feed/tests/test_portfolioview.py +++ /dev/null @@ -1,367 +0,0 @@ -from django.test import TestCase -from rest_framework.reverse import reverse -from rest_framework.test import APIRequestFactory - -import json -import factories -from feed.views import PortfolioViewSet -from workflow.models import (ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_ADMIN, - ROLE_PROGRAM_TEAM, ROLE_VIEW_ONLY, Portfolio) - - -class PortfolioListViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - self.portfolio = factories.Portfolio() - - def test_list_portfolio_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('') - request.user = self.tola_user.user - view = PortfolioViewSet.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'], self.portfolio.name) - self.assertEqual(response.data[0]['description'], - self.portfolio.description) - organization_url = reverse( - 'organization-detail', - kwargs={'pk': self.portfolio.organization.pk}) - self.assertIn(organization_url, response.data[0]['organization']) - - def test_list_portfolio_org_admin_same_org(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - self.portfolio.organization = self.tola_user.organization - self.portfolio.save() - - request = self.factory.get('') - request.user = self.tola_user.user - view = PortfolioViewSet.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'], self.portfolio.name) - self.assertEqual(response.data[0]['description'], - self.portfolio.description) - organization_url = reverse( - 'organization-detail', - kwargs={'pk': self.tola_user.organization.pk}) - self.assertIn(organization_url, response.data[0]['organization']) - - def test_list_portfolio_org_admin_different_org(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.get('') - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_portfolio_program_admin(self): - factories.WorkflowTeam( - workflow_user=self.tola_user, - workflowlevel1=factories.WorkflowLevel1( - organization=self.tola_user.organization), - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - self.portfolio.organization = self.tola_user.organization - self.portfolio.save() - - request = self.factory.get('') - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_portfolio_program_team(self): - factories.WorkflowTeam( - workflow_user=self.tola_user, - workflowlevel1=factories.WorkflowLevel1( - organization=self.tola_user.organization), - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - - self.portfolio.organization = self.tola_user.organization - self.portfolio.save() - - request = self.factory.get('') - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_portfolio_view_only(self): - factories.WorkflowTeam( - workflow_user=self.tola_user, - workflowlevel1=factories.WorkflowLevel1( - organization=self.tola_user.organization), - role=factories.Group(name=ROLE_VIEW_ONLY)) - - self.portfolio.organization = self.tola_user.organization - self.portfolio.save() - - request = self.factory.get('') - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_portfolio_other_user(self): - self.portfolio.organization = self.tola_user.organization - self.portfolio.save() - - request = self.factory.get('') - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class PortfolioCreateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_create_portfolio_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - organization_url = reverse( - 'organization-detail', - kwargs={'pk': factories.Organization().pk}) - data = {'name': 'New portfolio', 'organization': organization_url} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - - portfolio = Portfolio.objects.values('name', 'organization').get( - pk=response.data['id']) - self.assertEqual(portfolio['name'], data['name']) - self.assertEqual(portfolio['organization'], - self.tola_user.organization.pk) - - def test_create_portfolio_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - data = {'name': 'New portfolio'} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - - portfolio = Portfolio.objects.values('name', 'organization').get( - pk=response.data['id']) - self.assertEqual(portfolio['name'], data['name']) - self.assertEqual(portfolio['organization'], - self.tola_user.organization.pk) - - def test_create_portfolio_org_admin_json(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - data = {'name': 'New portfolio'} - request = self.factory.post(None, json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - - portfolio = Portfolio.objects.values('name', 'organization').get( - pk=response.data['id']) - self.assertEqual(portfolio['name'], data['name']) - self.assertEqual(portfolio['organization'], - self.tola_user.organization.pk) - - def test_create_portfolio_other_user(self): - role_without_benefits = ROLE_PROGRAM_ADMIN - factories.WorkflowTeam( - workflow_user=self.tola_user, - role=factories.Group(name=role_without_benefits)) - - data = {'name': 'New portfolio'} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 403) - - -class PortfolioUpdateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - self.portfolio = factories.Portfolio() - - def test_update_unexisting_portfolio(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post(None, {}) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'post': 'update'}) - response = view(request, pk=288) - self.assertEqual(response.status_code, 404) - - def test_update_portfolio_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - data = {'name': 'Some name'} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'post': 'update'}) - response = view(request, pk=self.portfolio.pk) - self.assertEqual(response.status_code, 200) - - name = Portfolio.objects.values_list('name', flat=True).get( - pk=self.portfolio.pk) - self.assertEqual(name, 'Some name') - - def test_update_portfolio_org_admin_same_org(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - self.portfolio.organization = self.tola_user.organization - self.portfolio.save() - - data = {'name': 'Some name'} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'post': 'update'}) - response = view(request, pk=self.portfolio.pk) - self.assertEqual(response.status_code, 200) - - name = Portfolio.objects.values_list('name', flat=True).get( - pk=self.portfolio.pk) - self.assertEqual(name, 'Some name') - - def test_update_portfolio_org_admin_same_org_json(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - self.portfolio.organization = self.tola_user.organization - self.portfolio.save() - - data = {'name': 'Some name'} - request = self.factory.post(None, json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'post': 'update'}) - response = view(request, pk=self.portfolio.pk) - self.assertEqual(response.status_code, 200) - - name = Portfolio.objects.values_list('name', flat=True).get( - pk=self.portfolio.pk) - self.assertEqual(name, 'Some name') - - def test_update_portfolio_org_admin_different_org(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - data = {'name': 'Some name'} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'post': 'update'}) - response = view(request, pk=self.portfolio.pk) - self.assertEqual(response.status_code, 403) - - def test_update_portfolio_org_admin_other_user(self): - role_without_benefits = ROLE_PROGRAM_ADMIN - factories.WorkflowTeam( - workflow_user=self.tola_user, - role=factories.Group(name=role_without_benefits)) - - self.portfolio.organization = self.tola_user.organization - self.portfolio.save() - - data = {'name': 'Some name'} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'post': 'update'}) - response = view(request, pk=self.portfolio.pk) - self.assertEqual(response.status_code, 403) - - -class PortfolioDeleteViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - self.portfolio = factories.Portfolio() - - def test_delete_portfolio_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.delete(None) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=self.portfolio.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Portfolio.DoesNotExist, - Portfolio.objects.get, pk=self.portfolio.pk) - - def test_delete_portfolio_org_admin_same_org(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - self.portfolio.organization = self.tola_user.organization - self.portfolio.save() - - request = self.factory.delete(None) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=self.portfolio.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - Portfolio.DoesNotExist, - Portfolio.objects.get, pk=self.portfolio.pk) - - def test_delete_portfolio_org_admin_different_org(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.delete(None) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=self.portfolio.pk) - self.assertEquals(response.status_code, 403) - Portfolio.objects.get(pk=self.portfolio.pk) - - def test_delete_portfolio_org_admin_other_user(self): - role_without_benefits = ROLE_PROGRAM_ADMIN - factories.WorkflowTeam( - workflow_user=self.tola_user, - role=factories.Group(name=role_without_benefits)) - - self.portfolio.organization = self.tola_user.organization - self.portfolio.save() - - request = self.factory.delete(None) - request.user = self.tola_user.user - view = PortfolioViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=self.portfolio.pk) - self.assertEquals(response.status_code, 403) - Portfolio.objects.get(pk=self.portfolio.pk) diff --git a/feed/tests/test_profiletypeview.py b/feed/tests/test_profiletypeview.py deleted file mode 100644 index 8684b983a..000000000 --- a/feed/tests/test_profiletypeview.py +++ /dev/null @@ -1,113 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import ProfileTypeViewSet -from workflow.models import WorkflowTeam, ROLE_ORGANIZATION_ADMIN, \ - ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY - - -class ProfileTypeListViewsTest(TestCase): - def setUp(self): - factories.Organization(id=1) - factories.ProfileType.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_profiletype_superuser(self): - request = self.factory.get('/api/profiletype/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = ProfileTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_profiletype_org_admin(self): - request = self.factory.get('/api/profiletype/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = self.tola_user.user - view = ProfileTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ProfileType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_profiletype_program_admin(self): - request = self.factory.get('/api/profiletype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = ProfileTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ProfileType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_profiletype_program_team(self): - request = self.factory.get('/api/profiletype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = ProfileTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ProfileType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_profiletype_view_only(self): - request = self.factory.get('/api/profiletype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = ProfileTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ProfileType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class ProfileTypeFilterViewsTest(TestCase): - def setUp(self): - factories.Organization(id=1) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_profiletype_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - p_type1 = factories.ProfileType( - organization=self.tola_user.organization) - p_type2 = factories.ProfileType(profile='Camp Site') - request = self.factory.get('/api/profiletype/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = ProfileTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['profile'], p_type1.profile) - self.assertNotEqual(response.data[0]['profile'], p_type2.profile) diff --git a/feed/tests/test_programindicatorreadonlyview.py b/feed/tests/test_programindicatorreadonlyview.py deleted file mode 100644 index a0a1cc880..000000000 --- a/feed/tests/test_programindicatorreadonlyview.py +++ /dev/null @@ -1,27 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import ProgramIndicatorReadOnlyViewSet - - -class ProgramIndicatorReadOnlyListViewsTest(TestCase): - def setUp(self): - factories.Organization(id=1) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_program_indicator(self): - wflvl1 = factories.WorkflowLevel1() - level = factories.Level(organization=self.tola_user.organization) - indicator = factories.Indicator(workflowlevel1=[wflvl1], level=level) - indicator_types = factories.IndicatorType.create_batch(2) - indicator.indicator_type.add(*indicator_types) - factories.Indicator(workflowlevel1=[wflvl1]) - - request = self.factory.get('') - request.user = self.tola_user.user - view = ProgramIndicatorReadOnlyViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data['results']), 2) diff --git a/feed/tests/test_projecttypeview.py b/feed/tests/test_projecttypeview.py deleted file mode 100644 index 1f094d9e3..000000000 --- a/feed/tests/test_projecttypeview.py +++ /dev/null @@ -1,132 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import ProjectTypeViewSet -from workflow.models import WorkflowTeam, ROLE_ORGANIZATION_ADMIN, \ - ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY - - -class ProjectTypeListViewsTest(TestCase): - def setUp(self): - factories.Organization(id=1) - factories.ProjectType.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_projecttype_superuser(self): - request = self.factory.get('/api/projecttype/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = ProjectTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_projecttype_org_admin(self): - request = self.factory.get('/api/projecttype/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = self.tola_user.user - view = ProjectTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ProjectType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_projecttype_program_admin(self): - request = self.factory.get('/api/projecttype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = ProjectTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ProjectType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_projecttype_program_team(self): - request = self.factory.get('/api/projecttype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = ProjectTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ProjectType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_projecttype_view_only(self): - request = self.factory.get('/api/projecttype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = ProjectTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.ProjectType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class ProjectTypeFilterViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_filter_projecttype_superuser(self): - another_org = factories.Organization(name='Another Org') - projecttype1_1 = factories.ProjectType( - organization=self.tola_user.organization) - factories.ProjectType( - name='Another ProjectType', - organization=another_org) - - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('/api/projecttype/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = ProjectTypeViewSet.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'], projecttype1_1.name) - - def test_filter_projecttype_normaluser(self): - another_org = factories.Organization(name='Another Org') - projecttype1_1 = factories.ProjectType( - organization=self.tola_user.organization) - factories.ProjectType( - name='Another ProjectType', - organization=another_org) - - request = self.factory.get('/api/projecttype/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = ProjectTypeViewSet.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'], projecttype1_1.name) diff --git a/feed/tests/test_riskregisterview.py b/feed/tests/test_riskregisterview.py deleted file mode 100644 index 0ae42bc73..000000000 --- a/feed/tests/test_riskregisterview.py +++ /dev/null @@ -1,46 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import RiskRegisterViewSet -from workflow.models import RiskRegister, WorkflowLevel1, WorkflowLevel2 - - -class RiskRegisterViewTest(TestCase): - def setUp(self): - wflvl1 = WorkflowLevel1.objects.create(name='WorkflowLevel1') - wflvl2 = WorkflowLevel2.objects.create(name='WorkflowLevel2', workflowlevel1=wflvl1) - RiskRegister.objects.bulk_create([ - RiskRegister(name='RiskRegister_0', workflowlevel2=wflvl2), - RiskRegister(name='RiskRegister_1', workflowlevel2=wflvl2), - ]) - - factory = APIRequestFactory() - self.request = factory.get('/api/riskregister/') - - def test_list_riskregister_superuser(self): - self.request.user = factories.User.build(is_superuser=True, is_staff=True) - view = RiskRegisterViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_riskregister_normaluser(self): - tola_user = factories.TolaUser() - self.request.user = tola_user.user - view = RiskRegisterViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_riskregister_one_result(self): - tola_user = factories.TolaUser() - wflvl1 = WorkflowLevel1.objects.create(name='WorkflowLevel1', organization=tola_user.organization) - wflvl2 = WorkflowLevel2.objects.create(name='WorkflowLevel2', workflowlevel1=wflvl1) - RiskRegister.objects.create(name='RiskRegister_0', workflowlevel2=wflvl2) - - self.request.user = tola_user.user - view = RiskRegisterViewSet.as_view({'get': 'list'}) - response = view(self.request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) diff --git a/feed/tests/test_sectorview.py b/feed/tests/test_sectorview.py deleted file mode 100644 index 1ba2650e4..000000000 --- a/feed/tests/test_sectorview.py +++ /dev/null @@ -1,84 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import json -import factories -from feed.views import SectorViewSet - - -class SectorCreateViewTest(TestCase): - def setUp(self): - self.organization = factories.Organization(id=1) - self.tola_user = factories.TolaUser(organization=self.organization) - self.factory = APIRequestFactory() - - def test_create_sector(self): - request = self.factory.post('/api/sector/') - user_url = reverse('user-detail', kwargs={'pk': self.tola_user.user.id}, - request=request) - - data = {'sector': 'Agriculture'} - request = self.factory.post('/api/sector/', data) - request.user = self.tola_user.user - view = SectorViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['sector'], u'Agriculture') - self.assertEqual(response.data['created_by'], user_url) - - def test_create_sector_json(self): - request = self.factory.post('/api/sector/') - user_url = reverse('user-detail', kwargs={'pk': self.tola_user.user.pk}, - request=request) - - data = {'sector': 'Agriculture'} - request = self.factory.post( - '/api/sector/', json.dumps(data), content_type='application/json') - request.user = self.tola_user.user - view = SectorViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['sector'], u'Agriculture') - self.assertEqual(response.data['created_by'], user_url) - - -class SectorFilterViewTest(TestCase): - def setUp(self): - self.organization = factories.Organization(id=1) - self.tola_user = factories.TolaUser(organization=self.organization) - self.factory = APIRequestFactory() - - def test_filter_sector_superuser(self): - another_org = factories.Organization(name='Another Org') - sector1 = factories.Sector(organization=self.tola_user.organization) - factories.Sector(sector='Another Sector', organization=another_org) - - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('/api/sector/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = SectorViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['sector'], sector1.sector) - - def test_filter_sector_normaluser(self): - another_org = factories.Organization(name='Another Org') - sector1 = factories.Sector(organization=self.tola_user.organization) - factories.Sector(sector='Another Sector', organization=another_org) - - request = self.factory.get('/api/sector/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = SectorViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['sector'], sector1.sector) diff --git a/feed/tests/test_siteprofileview.py b/feed/tests/test_siteprofileview.py deleted file mode 100755 index 97334dd72..000000000 --- a/feed/tests/test_siteprofileview.py +++ /dev/null @@ -1,174 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import json -import factories -from feed.views import SiteProfileViewSet -from workflow.models import SiteProfile - - -class SiteProfileListViewTest(TestCase): - def setUp(self): - self.country = factories.Country() - SiteProfile.objects.bulk_create([ - SiteProfile(name='SiteProfile1', country=self.country), - SiteProfile(name='SiteProfile2', country=self.country), - ]) - factories.Indicator.create_batch(2) - - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_siteprofile_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('/api/siteprofile/') - request.user = self.tola_user.user - view = SiteProfileViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_siteprofile_normaluser(self): - request = self.factory.get('/api/siteprofile/') - request.user = self.tola_user.user - view = SiteProfileViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_siteprofile_normaluser_one_result(self): - SiteProfile.objects.create(name='SiteProfile0', - organization=self.tola_user.organization, - country=self.country) - - request = self.factory.get('/api/siteprofile/') - request.user = self.tola_user.user - view = SiteProfileViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class SiteProfileCreateViewTest(TestCase): - def setUp(self): - self.country = factories.Country() - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_create_siteprofile_normaluser_one_result(self): - request = self.factory.post('/api/siteprofile/') - user_url = reverse('user-detail', kwargs={'pk': self.tola_user.user.id}, - request=request) - wflvl1_1 = factories.WorkflowLevel1() - wflvl1_2 = factories.WorkflowLevel1() - wflvl1_1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1_1.id}, - request=request) - wflvl1_2_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1_2.id}, - request=request) - country_url = reverse('country-detail', - kwargs={'pk': self.country.id}, - request=request) - - data = { - 'name': 'Site Profile 1', - 'country': country_url, - 'workflowlevel1': [wflvl1_1_url, wflvl1_2_url] - } - request = self.factory.post('/api/siteprofile/', data) - request.user = self.tola_user.user - view = SiteProfileViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['created_by'], user_url) - - # check if the obj created has the user organization - request = self.factory.get('/api/siteprofile/') - request.user = self.tola_user.user - view = SiteProfileViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_create_siteprofile_normaluser_one_result_json(self): - request = self.factory.post('/api/siteprofile/') - user_url = reverse('user-detail', kwargs={'pk': self.tola_user.user.id}, - request=request) - wflvl1_1 = factories.WorkflowLevel1() - wflvl1_2 = factories.WorkflowLevel1() - wflvl1_1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1_1.id}, - request=request) - wflvl1_2_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1_2.id}, - request=request) - country_url = reverse('country-detail', - kwargs={'pk': self.country.id}, - request=request) - - data = { - 'name': 'Site Profile 1', - 'country': country_url, - 'workflowlevel1': [wflvl1_1_url, wflvl1_2_url] - } - request = self.factory.post('/api/siteprofile/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = SiteProfileViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['created_by'], user_url) - - # check if the obj created has the user organization - request = self.factory.get('/api/siteprofile/') - request.user = self.tola_user.user - view = SiteProfileViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class SiteProfileFilterViewTest(TestCase): - def setUp(self): - self.country = factories.Country(country='Brazil', code='BR') - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_siteprofile_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - site1 = factories.SiteProfile(name='SiteProfile1', - country=self.country) - factories.SiteProfile(name='SiteProfile2', country=factories.Country()) - - request = self.factory.get('/api/siteprofile/?country__country=%s' % - self.country.country) - request.user = self.tola_user.user - view = SiteProfileViewSet.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'], site1.name) - - def test_filter_siteprofile_normaluser(self): - site1 = factories.SiteProfile( - name='SiteProfile1', country=self.country, - organization=self.tola_user.organization) - factories.SiteProfile(name='SiteProfile2', country=factories.Country(), - organization=self.tola_user.organization) - - request = self.factory.get('/api/siteprofile/?country__country=%s' % - self.country.country) - request.user = self.tola_user.user - view = SiteProfileViewSet.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'], site1.name) diff --git a/feed/tests/test_stakeholdertypeview.py b/feed/tests/test_stakeholdertypeview.py deleted file mode 100644 index 0337c7bb7..000000000 --- a/feed/tests/test_stakeholdertypeview.py +++ /dev/null @@ -1,112 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import StakeholderTypeViewSet -from workflow.models import WorkflowTeam, ROLE_ORGANIZATION_ADMIN, \ - ROLE_PROGRAM_TEAM, ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY - - -class StakeholderTypeListViewsTest(TestCase): - def setUp(self): - factories.Organization(id=1) - factories.StakeholderType.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_stakeholdertype_superuser(self): - request = self.factory.get('/api/stakeholdertype/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = StakeholderTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_stakeholdertype_org_admin(self): - request = self.factory.get('/api/stakeholdertype/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request.user = self.tola_user.user - view = StakeholderTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.StakeholderType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_stakeholdertype_program_admin(self): - request = self.factory.get('/api/stakeholdertype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = StakeholderTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.StakeholderType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_stakeholdertype_program_team(self): - request = self.factory.get('/api/stakeholdertype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = StakeholderTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.StakeholderType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_stakeholdertype_view_only(self): - request = self.factory.get('/api/stakeholdertype/') - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = StakeholderTypeViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.StakeholderType(organization=self.tola_user.organization) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class StakeholderTypeFilterViewTest(TestCase): - def setUp(self): - factories.Organization(id=1) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_stakeholdertype_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - stkh_type1 = factories.StakeholderType( - organization=self.tola_user.organization) - factories.StakeholderType(name='Business') - request = self.factory.get('/api/stakeholdertype/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = StakeholderTypeViewSet.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'], stkh_type1.name) diff --git a/feed/tests/test_stakeholderview.py b/feed/tests/test_stakeholderview.py deleted file mode 100755 index 5fcab69b8..000000000 --- a/feed/tests/test_stakeholderview.py +++ /dev/null @@ -1,150 +0,0 @@ -import json - -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import factories -from feed.views import StakeholderViewSet -from workflow.models import Stakeholder, WorkflowLevel1, WorkflowTeam - - -class StakeholderListViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - factories.Stakeholder.create_batch(2) - self.factory = APIRequestFactory() - - def test_list_stakeholder_superuser(self): - self.tola_user.user.is_superuser = True - self.tola_user.user.is_staff = True - self.tola_user.user.save() - - request = self.factory.get('/api/stakeholder/') - request.user = self.tola_user.user - view = StakeholderViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_stakeholder_normaluser(self): - request = self.factory.get('/api/stakeholder/') - request.user = self.tola_user.user - view = StakeholderViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_stakeholder_normaluser_one_result(self): - wflvl1_1 = WorkflowLevel1.objects.create(name='WorkflowLevel1_1') - wflvl1_2 = WorkflowLevel1.objects.create(name='WorkflowLevel1_2') - WorkflowTeam.objects.create(workflow_user=self.tola_user, - workflowlevel1=wflvl1_1) - WorkflowTeam.objects.create(workflow_user=self.tola_user, - workflowlevel1=wflvl1_2) - stk = Stakeholder.objects.create( - name='Stakeholder_0', organization=self.tola_user.organization) - stk.workflowlevel1.add(wflvl1_1, wflvl1_2) - - request = self.factory.get('/api/stakeholder/') - request.user = self.tola_user.user - view = StakeholderViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class StakeholderCreateViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - Stakeholder.objects.create(name='Stakeholder_0', - organization=self.tola_user.organization) - - def test_create_stakeholder_normaluser_one_result(self): - request = self.factory.post('/api/stakeholder/') - wflvl1 = WorkflowLevel1.objects.create(name='WorkflowLevel1') - WorkflowTeam.objects.create(workflow_user=self.tola_user, - workflowlevel1=wflvl1) - user_url = reverse('user-detail', kwargs={'pk': self.tola_user.user.id}, - request=request) - - # create stakeholder via POST request - data = {'workflowlevel1': [ - 'http://testserver/api/workflowlevel1/%s/' % wflvl1.id]} - request = self.factory.post('/api/stakeholder/', data) - request.user = self.tola_user.user - view = StakeholderViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['created_by'], user_url) - - # check if the obj created has the user organization - request = self.factory.get('/api/stakeholder/') - request.user = self.tola_user.user - view = StakeholderViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_create_stakeholder_normaluser_one_result_json(self): - request = self.factory.post('/api/stakeholder/') - wflvl1 = WorkflowLevel1.objects.create(name='WorkflowLevel1') - WorkflowTeam.objects.create(workflow_user=self.tola_user, - workflowlevel1=wflvl1) - user_url = reverse('user-detail', kwargs={'pk': self.tola_user.user.id}, - request=request) - - # create stakeholder via POST request - data = {'workflowlevel1': [ - 'http://testserver/api/workflowlevel1/%s/' % wflvl1.id]} - request = self.factory.post( - '/api/stakeholder/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = StakeholderViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['created_by'], user_url) - - # check if the obj created has the user organization - request = self.factory.get('/api/stakeholder/') - request.user = self.tola_user.user - view = StakeholderViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class StakeholderFilterViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - factories.Stakeholder.create_batch(2) - self.factory = APIRequestFactory() - - def test_filter_stakeholder(self): - wflvl1_1 = factories.WorkflowLevel1(name='WorkflowLevel1') - wflvl1_2 = factories.WorkflowLevel1() - factories.WorkflowTeam(workflow_user=self.tola_user, - workflowlevel1=wflvl1_1) - factories.WorkflowTeam(workflow_user=self.tola_user, - workflowlevel1=wflvl1_2) - stk1 = factories.Stakeholder( - name='Stakeholder_1', - organization=self.tola_user.organization, - workflowlevel1=[wflvl1_1] - ) - factories.Stakeholder( - name='Stakeholder_2', - organization=self.tola_user.organization, - workflowlevel1=[wflvl1_2] - ) - - request = self.factory.get( - '/api/stakeholder/?workflowlevel1__name=%s' % wflvl1_1.name) - request.user = self.tola_user.user - view = StakeholderViewSet.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'], stk1.name) diff --git a/feed/tests/test_strategicobjectiveview.py b/feed/tests/test_strategicobjectiveview.py deleted file mode 100755 index 2cab6e702..000000000 --- a/feed/tests/test_strategicobjectiveview.py +++ /dev/null @@ -1,117 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import StrategicObjectiveViewSet - - -class StrategicObjectiveListViewTest(TestCase): - def setUp(self): - factories.StrategicObjective.create_batch(2) - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_list_strategicobjective_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('/api/strategicobjective/') - request.user = self.tola_user.user - view = StrategicObjectiveViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_strategicobjective_normaluser(self): - request = self.factory.get('/api/strategicobjective/') - request.user = self.tola_user.user - view = StrategicObjectiveViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_strategicobjective_normaluser_one_result(self): - - factories.StrategicObjective( - name='StrategicObjective0', - organization=self.tola_user.organization) - request = self.factory.get('/api/strategicobjective/') - request.user = self.tola_user.user - view = StrategicObjectiveViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class StrategicObjectiveCreateViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser() - self.factory = APIRequestFactory() - - def test_create_strategicobjective_normaluser_one_result(self): - request = self.factory.post('/api/strategicobjective/') - request.user = self.tola_user.user - view = StrategicObjectiveViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - - # check if the obj created has the user organization - request = self.factory.get('/api/strategicobjective/') - request.user = self.tola_user.user - view = StrategicObjectiveViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class StrategicObjectiveFilterViewTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_strategicobjective_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - another_org = factories.Organization(name='Another Org') - s_obj1 = factories.StrategicObjective( - name='StrategicObjective1', - organization=self.tola_user.organization) - factories.StrategicObjective(name='StrategicObjective2', - organization=another_org) - - request = self.factory.get( - '/api/strategicobjective/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = StrategicObjectiveViewSet.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'], s_obj1.name) - - def test_filter_strategicobjective_normaluser(self): - country1 = factories.Country(country='Brazil', code='BR') - country2 = factories.Country() - s_obj1 = factories.StrategicObjective( - name='StrategicObjective1', - country=country1, - organization=self.tola_user.organization - ) - factories.StrategicObjective( - name='StrategicObjective2', - country=country2, - organization=self.tola_user.organization - ) - - request = self.factory.get( - '/api/strategicobjective/?country__country=%s' % - country1.country) - request.user = self.tola_user.user - view = StrategicObjectiveViewSet.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'], s_obj1.name) diff --git a/feed/tests/test_tolauserview.py b/feed/tests/test_tolauserview.py deleted file mode 100644 index f4a633a7d..000000000 --- a/feed/tests/test_tolauserview.py +++ /dev/null @@ -1,75 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory - -import factories -from feed.views import TolaUserViewSet - - -class TolaUserListViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - self.tola_user_ringo = factories.TolaUser( - user=factories.User(first_name='Ringo', last_name='Starr'), - organization=self.tola_user.organization) - self.tola_user_george = factories.TolaUser( - user=factories.User(first_name='George', last_name='Harrison'), - organization=factories.Organization(name='Other Org')) - - def test_list_tolauser_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('') - request.user = self.tola_user.user - view = TolaUserViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 3) - - for i, name in enumerate((u'George Harrison', u'Ringo Starr', - u'Thom Yorke')): - self.assertEqual(response.data[i]['name'], name) - - def test_list_tolauser_other_user(self): - request = self.factory.get('') - request.user = self.tola_user.user - view = TolaUserViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - for i, name in enumerate((u'Ringo Starr', u'Thom Yorke')): - self.assertEqual(response.data[i]['name'], name) - - -class TolaUserFilterViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - self.tolauser1 = self.tola_user_ringo = factories.TolaUser( - user=factories.User(first_name='Ringo', last_name='Starr'), - organization=self.tola_user.organization) - self.tolauser2 = self.tola_user_george = factories.TolaUser( - user=factories.User(first_name='George', last_name='Harrison'), - organization=factories.Organization(name='Other Org')) - - def test_filter_tolauser_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.get('/api/tolauser/?organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = TolaUserViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - for i, name in enumerate((u'Ringo Starr', - u'Thom Yorke')): - self.assertEqual(response.data[i]['name'], name) diff --git a/feed/tests/test_widget.py b/feed/tests/test_widget.py deleted file mode 100644 index 646c6b748..000000000 --- a/feed/tests/test_widget.py +++ /dev/null @@ -1,43 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import json -import factories -from feed.views import WidgetViewSet - - -class WidgetCreateViewTest(TestCase): - def setUp(self): - self.organization = factories.Organization(id=1) - self.tola_user = factories.TolaUser(organization=self.organization) - self.factory = APIRequestFactory() - - def test_create_widget(self): - request = self.factory.post('/api/widget/') - user_url = reverse('user-detail', kwargs={'pk': self.tola_user.user.id}, - request=request) - - data = {'title': 'YoWidget'} - request = self.factory.post('/api/widget/', data) - request.user = self.tola_user.user - view = WidgetViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 400) - - def test_create_widget_json(self): - request = self.factory.post('/api/widget/') - user_url = reverse('user-detail', kwargs={'pk': self.tola_user.user.pk}, - request=request) - - data = {'title': 'YoWidget'} - request = self.factory.post( - '/api/widget/', json.dumps(data), content_type='application/json') - request.user = self.tola_user.user - view = WidgetViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 400) - - diff --git a/feed/tests/test_workflowlevel1view.py b/feed/tests/test_workflowlevel1view.py deleted file mode 100644 index ac9b89053..000000000 --- a/feed/tests/test_workflowlevel1view.py +++ /dev/null @@ -1,611 +0,0 @@ -from django.test import TestCase -from rest_framework.reverse import reverse -from rest_framework.test import APIRequestFactory - -from mock import Mock, patch - -import json -import factories -from feed.views import WorkflowLevel1ViewSet -from workflow.models import (WorkflowTeam, WorkflowLevel1, - ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_TEAM, - ROLE_PROGRAM_ADMIN, ROLE_VIEW_ONLY) - - -class WorkflowLevel1ListViewsTest(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_superuser_and_org_admin(self): - wflvl1 = factories.WorkflowLevel1() - wflvl2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - 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_filter_workflowlevel1_program_admin(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_2 = factories.WorkflowLevel1( - name='Population Health Initiative', - organization=self.tola_user.organization) - group_program_admin = factories.Group(name=ROLE_PROGRAM_ADMIN) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, workflowlevel1=wflvl1, - role=group_program_admin) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, workflowlevel1=wflvl1_2, - role=group_program_admin) - - request = self.factory.get('/api/workflowlevel1/?name=%s' % - wflvl1.name) - 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.assertNotEqual(response.data[0]['name'], wflvl1_2.name) - - 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): - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - partner_org=self.tola_user.organization, - role=factories.Group(name=ROLE_VIEW_ONLY)) - 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) - - -class WorkflowLevel1CreateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - 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() - - organization_url = reverse( - 'organization-detail', - kwargs={'pk': self.tola_user.organization.pk}) - data = {'name': 'Save the Children', 'organization': organization_url} - 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']) - 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_program_admin(self): - """ - A ProgramAdmin member of any other program can create a new program - in the same organization. - """ - organization_url = reverse( - 'organization-detail', - kwargs={'pk': self.tola_user.organization.pk}) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=factories.WorkflowLevel1( - organization=self.tola_user.organization), - role=factories.Group(name=ROLE_PROGRAM_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') - self.assertIn(organization_url, response.data['organization']) - - 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_program_admin_json(self): - """ - A ProgramAdmin member of any other program can create a new program - in the same organization. - """ - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=factories.WorkflowLevel1( - organization=self.tola_user.organization), - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'Save the Children'} - request = self.factory.post('/api/workflowlevel1/', json.dumps(data), - content_type='application/json') - 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_program_team(self): - """ - A ProgramTeam member of any other program can create a new program in - the same organization. - """ - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=factories.WorkflowLevel1( - organization=self.tola_user.organization), - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - - 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_normal_user(self): - 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) - - -class WorkflowLevel1UpdateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_update_unexisting_workflowlevel1(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - data = {'salary': '10'} - request = self.factory.post('/api/workflowlevel1/', data) - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'post': 'update'}) - response = view(request, pk=288) - self.assertEqual(response.status_code, 404) - - def test_update_workflowlevel1_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - wflvl1 = factories.WorkflowLevel1() - data = {'name': 'Save the Lennons'} - request = self.factory.post('/api/workflowlevel1/', data) - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'post': 'update'}) - response = view(request, pk=wflvl1.pk) - self.assertEqual(response.status_code, 200) - - wflvl1 = WorkflowLevel1.objects.get(pk=response.data['id']) - self.assertEquals(wflvl1.name, data['name']) - - def test_update_workflowlevel1_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - data = {'name': 'Save the Lennons'} - request = self.factory.post('/api/workflowlevel1/', data) - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'post': 'update'}) - response = view(request, pk=wflvl1.pk) - self.assertEqual(response.status_code, 200) - - wflvl1 = WorkflowLevel1.objects.get(pk=response.data['id']) - self.assertEquals(wflvl1.name, data['name']) - - def test_update_workflowlevel1_different_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1( - organization=factories.Organization(name='Other Org')) - data = {'name': 'Save the Lennons'} - request = self.factory.post('/api/workflowlevel1/', data) - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'post': 'update'}) - response = view(request, pk=wflvl1.pk) - self.assertEqual(response.status_code, 403) - - def test_update_workflowlevel1_program_admin(self): - 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) - - data = {'name': 'Save the Lennons'} - request = self.factory.post('/api/workflowlevel1/', data) - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'post': 'update'}) - response = view(request, pk=response.data['id']) - self.assertEqual(response.status_code, 200) - - wflvl1 = WorkflowLevel1.objects.get(pk=response.data['id']) - self.assertEquals(wflvl1.name, data['name']) - - def test_update_workflowlevel1_program_admin_json(self): - data = {'name': 'Save the Children'} - request = self.factory.post('/api/workflowlevel1/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'post': 'create'}) - response = view(request) - - data = {'name': 'Save the Lennons'} - request = self.factory.post('/api/workflowlevel1/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'post': 'update'}) - response = view(request, pk=response.data['id']) - self.assertEqual(response.status_code, 200) - - wflvl1 = WorkflowLevel1.objects.get(pk=response.data['id']) - self.assertEquals(wflvl1.name, data['name']) - - def test_update_workflowlevel1_program_team(self): - wflvl1 = factories.WorkflowLevel1() - group_program_team = factories.Group(name=ROLE_PROGRAM_TEAM) - wflvl1.organization = self.tola_user.organization - wflvl1.user_access.add(self.tola_user) - wflvl1.save() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, workflowlevel1=wflvl1, - role=group_program_team) - - data = {'name': 'Save the Lennons'} - request = self.factory.post('/api/workflowlevel1/', data) - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'post': 'update'}) - response = view(request, pk=wflvl1.pk) - self.assertEqual(response.status_code, 200) - - wflvl1 = WorkflowLevel1.objects.get(pk=response.data['id']) - self.assertEquals(wflvl1.name, data['name']) - - def test_update_workflowlevel1_same_org_different_program_team(self): - wflvl1_other = factories.WorkflowLevel1() - group_program_team = factories.Group(name=ROLE_PROGRAM_TEAM) - wflvl1_other.organization = self.tola_user.organization - wflvl1_other.user_access.add(self.tola_user) - wflvl1_other.save() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, workflowlevel1=wflvl1_other, - role=group_program_team) - - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - - data = {'name': 'Save the Lennons'} - request = self.factory.post('/api/workflowlevel1/', data) - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'post': 'update'}) - response = view(request, pk=wflvl1.pk) - self.assertEqual(response.status_code, 403) - - -class WorkflowLevel1DeleteViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_delete_workflowlevel1_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - wflvl1 = factories.WorkflowLevel1() - request = self.factory.delete('/api/workflowlevel1/') - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=wflvl1.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - WorkflowLevel1.DoesNotExist, - WorkflowLevel1.objects.get, pk=wflvl1.pk) - - def test_delete_workflowlevel1_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - request = self.factory.delete('/api/workflowlevel1/') - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=wflvl1.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - WorkflowLevel1.DoesNotExist, - WorkflowLevel1.objects.get, pk=wflvl1.pk) - - def test_delete_workflowlevel1_different_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - org_other = factories.Organization(name='Other Org') - wflvl1 = factories.WorkflowLevel1(organization=org_other) - request = self.factory.delete('/api/workflowlevel1/') - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=wflvl1.pk) - self.assertEquals(response.status_code, 403) - WorkflowLevel1.objects.get(pk=wflvl1.pk) - - def test_delete_workflowlevel1_program_admin(self): - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - partner_org=self.tola_user.organization, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - # Create a program - 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) - - # Delete the program created before - request = self.factory.delete('/api/workflowlevel1/') - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'delete': 'destroy'}) - view(request, pk=response.data['id']) - self.assertRaises( - WorkflowLevel1.DoesNotExist, - WorkflowLevel1.objects.get, pk=response.data['id']) - - def test_delete_workflowlevel1_different_org(self): - group_other = factories.Group(name='other') - self.tola_user.user.groups.add(group_other) - - wflvl1 = factories.WorkflowLevel1() - request = self.factory.delete('/api/workflowlevel1/') - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=wflvl1.pk) - self.assertEquals(response.status_code, 403) - WorkflowLevel1.objects.get(pk=wflvl1.pk) - - def test_delete_workflowlevel1_normal_user(self): - wflvl1 = factories.WorkflowLevel1() - request = self.factory.delete('/api/workflowlevel1/') - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=wflvl1.pk) - self.assertEquals(response.status_code, 403) - WorkflowLevel1.objects.get(pk=wflvl1.pk) - - def test_delete_workflowlevel1_program_admin_just_one(self): - # Create a program - data = {'name': 'Save the Pandas'} - request = self.factory.post('/api/workflowlevel1/', data) - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'post': 'create'}) - response = view(request) - first_program_id = response.data['id'] - - # Create another program - 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) - - # Delete only the latter program - request = self.factory.delete('/api/workflowlevel1/') - request.user = self.tola_user.user - view = WorkflowLevel1ViewSet.as_view({'delete': 'destroy'}) - view(request, pk=response.data['id']) - self.assertRaises( - WorkflowLevel1.DoesNotExist, - WorkflowLevel1.objects.get, pk=response.data['id']) - WorkflowLevel1.objects.get(pk=first_program_id) - - -class WorkflowLevel1FilterViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_filter_workflowlevel1_superuser(self): - wflvl1 = factories.WorkflowLevel1() - factories.WorkflowLevel1(name='Population Health Initiative') - - 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/?name=%s' % - wflvl1.name) - 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) - - def test_filter_workflowlevel1_org_admin(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - factories.WorkflowLevel1(name='Population Health Initiative', - organization=self.tola_user.organization) - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.get('/api/workflowlevel1/?name=%s' % - wflvl1.name) - 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) diff --git a/feed/tests/test_workflowlevel2sortview.py b/feed/tests/test_workflowlevel2sortview.py deleted file mode 100644 index 908951c13..000000000 --- a/feed/tests/test_workflowlevel2sortview.py +++ /dev/null @@ -1,227 +0,0 @@ -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import factories -from feed.views import WorkflowLevel2SortViewSet -from workflow.models import WorkflowLevel2Sort, WorkflowTeam, \ - ROLE_ORGANIZATION_ADMIN - - -class WorkflowLevel2SortListViewsTest(TestCase): - def setUp(self): - factories.WorkflowLevel2Sort.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_workflowlevel2sort_superuser(self): - request = self.factory.get('/api/workflowlevel2sort/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = WorkflowLevel2SortViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_workflowlevel2sort_normal_user(self): - request = self.factory.get('/api/workflowlevel2sort/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1) - request.user = self.tola_user.user - view = WorkflowLevel2SortViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.WorkflowLevel2Sort(workflowlevel1=wflvl1) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class WorkflowLevel2SortCreateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_create_workflowlevel2_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.post('/api/workflowlevel2sort/') - wflvl1 = factories.WorkflowLevel1() - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'workflowlevel2_id': 1, - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2SortViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['workflowlevel2_id'], 1) - - def test_create_workflowlevel2sort_normal_user(self): - request = self.factory.post('/api/workflowlevel2sort/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'workflowlevel2_id': 1, - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2SortViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['workflowlevel2_id'], 1) - - -class WorkflowLevel2SortUpdateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_update_unexisting_workflowlevel2sort(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - data = {'workflowlevel2_id': 1} - - request = self.factory.post('/api/workflowlevel2sort/', data) - request.user = self.tola_user.user - view = WorkflowLevel2SortViewSet.as_view({'post': 'update'}) - response = view(request, pk=288) - self.assertEqual(response.status_code, 404) - - def test_update_workflowlevel2sort_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.post('/api/workflowlevel2sort/') - wflvl1 = factories.WorkflowLevel1() - workflowlevel2sort = factories.WorkflowLevel2Sort(workflowlevel1=wflvl1) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'workflowlevel2_id': 1, - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2sort/', data) - request.user = self.tola_user.user - view = WorkflowLevel2SortViewSet.as_view({'post': 'update'}) - response = view(request, pk=workflowlevel2sort.pk) - self.assertEqual(response.status_code, 200) - - workflowlevel2sort = WorkflowLevel2Sort.objects.get( - pk=response.data['id']) - self.assertEquals(workflowlevel2sort.workflowlevel2_id, - data['workflowlevel2_id']) - - def test_update_workflowlevel2sort_normal_user(self): - request = self.factory.post('/api/workflowlevel2sort/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - workflowlevel2sort = factories.WorkflowLevel2Sort(workflowlevel1=wflvl1) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'workflowlevel2_id': 1, - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2sort/', data) - request.user = self.tola_user.user - view = WorkflowLevel2SortViewSet.as_view({'post': 'update'}) - response = view(request, pk=workflowlevel2sort.pk) - self.assertEqual(response.status_code, 200) - - workflowlevel2sort = WorkflowLevel2Sort.objects.get( - pk=response.data['id']) - self.assertEquals(workflowlevel2sort.workflowlevel2_id, - data['workflowlevel2_id']) - - def test_update_workflowlevel2sort_diff_org_normal_user(self): - request = self.factory.post('/api/workflowlevel2sort/') - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - workflowlevel2sort = factories.WorkflowLevel2Sort(workflowlevel1=wflvl1) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'workflowlevel2_id': 1, - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2sort/', data) - request.user = self.tola_user.user - view = WorkflowLevel2SortViewSet.as_view({'post': 'update'}) - response = view(request, pk=workflowlevel2sort.pk) - self.assertEqual(response.status_code, 403) - - -class WorkflowLevel2SortDeleteViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_delete_workflowlevel2sort_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - workflowlevel2sort = factories.WorkflowLevel2Sort() - request = self.factory.delete('/api/workflowlevel2sort/') - request.user = self.tola_user.user - view = WorkflowLevel2SortViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=workflowlevel2sort.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - WorkflowLevel2Sort.DoesNotExist, - WorkflowLevel2Sort.objects.get, pk=workflowlevel2sort.pk) - - def test_delete_workflowlevel2sort_normal_user(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - workflowlevel2sort = factories.WorkflowLevel2Sort(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/workflowlevel2sort/') - request.user = self.tola_user.user - view = WorkflowLevel2SortViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=workflowlevel2sort.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - WorkflowLevel2Sort.DoesNotExist, - WorkflowLevel2Sort.objects.get, pk=workflowlevel2sort.pk) - - def test_delete_workflowlevel2sort_diff_org_normal_user(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - workflowlevel2sort = factories.WorkflowLevel2Sort(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/workflowlevel2sort/') - request.user = self.tola_user.user - view = WorkflowLevel2SortViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=workflowlevel2sort.pk) - self.assertEquals(response.status_code, 403) - WorkflowLevel2Sort.objects.get(pk=workflowlevel2sort.pk) diff --git a/feed/tests/test_workflowlevel2view.py b/feed/tests/test_workflowlevel2view.py deleted file mode 100644 index c19504e73..000000000 --- a/feed/tests/test_workflowlevel2view.py +++ /dev/null @@ -1,679 +0,0 @@ -import json - -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.reverse import reverse - -import factories -from feed.views import WorkflowLevel2ViewSet -from workflow.models import WorkflowLevel2, WorkflowTeam, ROLE_VIEW_ONLY, \ - ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_ADMIN, ROLE_PROGRAM_TEAM - - -class WorkflowLevel2ListViewsTest(TestCase): - def setUp(self): - factories.WorkflowLevel2.create_batch(2) - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_list_workflowlevel2_superuser(self): - request = self.factory.get('/api/workflowlevel2/') - request.user = factories.User.build(is_superuser=True, - is_staff=True) - view = WorkflowLevel2ViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_workflowlevel2_org_admin(self): - request = self.factory.get('/api/workflowlevel2/') - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1() - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1) - factories.WorkflowLevel2(workflowlevel1=wflvl1) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - wflvl1.organization = self.tola_user.organization - wflvl1.save() - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_workflowlevel2_program_admin(self): - request = self.factory.get('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.WorkflowLevel2(workflowlevel1=wflvl1) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_workflowlevel2_program_team(self): - request = self.factory.get('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.WorkflowLevel2(workflowlevel1=wflvl1) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_workflowlevel2_view_only(self): - request = self.factory.get('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - factories.WorkflowLevel2(workflowlevel1=wflvl1) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - -class WorkflowLevel2CreateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_create_workflowlevel2_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1() - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Help Syrians', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Help Syrians') - - def test_create_workflowlevel2_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Help Syrians', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Help Syrians') - - def test_create_workflowlevel2_program_admin(self): - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - tolauser_url = reverse('tolauser-detail', - kwargs={'pk': self.tola_user.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'Help Syrians', - 'workflowlevel1': wflvl1_url, - 'staff_responsible': tolauser_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Help Syrians') - self.assertEqual(response.data['staff_responsible'], tolauser_url) - - def test_create_workflowlevel2_program_admin_json(self): - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'name': 'Help Syrians', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Help Syrians') - - def test_create_workflowlevel2_program_team(self): - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - - data = {'name': 'Help Syrians', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['name'], u'Help Syrians') - - def test_create_workflowlevel2_view_only(self): - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - - data = {'name': 'Help Syrians', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'create'}) - response = view(request) - - self.assertEqual(response.status_code, 403) - - -class WorkflowLevel2UpdateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_update_unexisting_workflowlevel2(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - data = {'name': 'Community awareness program conducted to plant trees'} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'update'}) - response = view(request, pk=288) - self.assertEqual(response.status_code, 404) - - def test_update_workflowlevel2_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1() - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Community awareness program conducted to plant trees', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'update'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEqual(response.status_code, 200) - - workflowlevel2 = WorkflowLevel2.objects.get(pk=response.data['id']) - self.assertEquals(workflowlevel2.name, data['name']) - - def test_update_workflowlevel2_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Community awareness program conducted to plant trees', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'update'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEqual(response.status_code, 200) - - workflowlevel2 = WorkflowLevel2.objects.get(pk=response.data['id']) - self.assertEquals(workflowlevel2.name, data['name']) - - def test_update_workflowlevel2_diff_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.post('/api/workflowlevel2/') - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Community awareness program conducted to plant trees', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'update'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEqual(response.status_code, 403) - - def test_update_workflowlevel2_program_admin(self): - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Community awareness program conducted to plant trees', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'update'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEqual(response.status_code, 200) - - workflowlevel2 = WorkflowLevel2.objects.get(pk=response.data['id']) - self.assertEquals(workflowlevel2.name, data['name']) - - def test_update_workflowlevel2_program_admin_json(self): - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Community awareness program conducted to plant trees', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/indicator/', json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'update'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEqual(response.status_code, 200) - - workflowlevel2 = WorkflowLevel2.objects.get(pk=response.data['id']) - self.assertEquals(workflowlevel2.name, data['name']) - - def test_update_workflowlevel2_program_team(self): - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Community awareness program conducted to plant trees', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'update'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEqual(response.status_code, 200) - - workflowlevel2 = WorkflowLevel2.objects.get(pk=response.data['id']) - self.assertEquals(workflowlevel2.name, data['name']) - - def test_update_workflowlevel2_view_only(self): - request = self.factory.post('/api/workflowlevel2/') - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': wflvl1.id}, - request=request) - - data = {'name': 'Community awareness program conducted to plant trees', - 'workflowlevel1': wflvl1_url} - - request = self.factory.post('/api/workflowlevel2/', data) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'post': 'update'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEqual(response.status_code, 403) - - -class WorkflowLevel2DeleteViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - factories.Group() - - def test_delete_workflowlevel2_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - workflowlevel2 = factories.WorkflowLevel2() - request = self.factory.delete('/api/workflowlevel2/') - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - WorkflowLevel2.DoesNotExist, - WorkflowLevel2.objects.get, pk=workflowlevel2.pk) - - def test_delete_workflowlevel2_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/workflowlevel2/') - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - WorkflowLevel2.DoesNotExist, - WorkflowLevel2.objects.get, pk=workflowlevel2.pk) - - def test_delete_workflowlevel2_diff_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/workflowlevel2/') - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEquals(response.status_code, 403) - WorkflowLevel2.objects.get(pk=workflowlevel2.pk) - - def test_delete_workflowlevel2_program_admin(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/workflowlevel2/') - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEquals(response.status_code, 204) - self.assertRaises( - WorkflowLevel2.DoesNotExist, - WorkflowLevel2.objects.get, pk=workflowlevel2.pk) - - def test_delete_workflowlevel2_diff_org(self): - another_org = factories.Organization(name='Another Org') - wflvl1 = factories.WorkflowLevel1(organization=another_org) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/workflowlevel2/') - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEquals(response.status_code, 403) - WorkflowLevel2.objects.get(pk=workflowlevel2.pk) - - def test_delete_workflowlevel2_program_team(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/workflowlevel2/') - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEquals(response.status_code, 403) - WorkflowLevel2.objects.get(pk=workflowlevel2.pk) - - def test_delete_workflowlevel2_view_only(self): - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - workflowlevel2 = factories.WorkflowLevel2(workflowlevel1=wflvl1) - - request = self.factory.delete('/api/workflowlevel2/') - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEquals(response.status_code, 403) - WorkflowLevel2.objects.get(pk=workflowlevel2.pk) - - def test_delete_workflowlevel2_normal_user(self): - workflowlevel2 = factories.WorkflowLevel2() - request = self.factory.delete('/api/workflowlevel2/') - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=workflowlevel2.pk) - self.assertEquals(response.status_code, 403) - WorkflowLevel2.objects.get(pk=workflowlevel2.pk) - - -class WorkflowLevel2FilterViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - def test_filter_workflowlevel2_wkflvl1_country_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - country1 = factories.Country(country='Brazil', code='BR') - country2 = factories.Country(country='Germany', code='DE') - wkflvl1_1 = factories.WorkflowLevel1(country=[country1]) - wkflvl1_2 = factories.WorkflowLevel1(country=[country2]) - wkflvl2 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_1) - factories.WorkflowLevel2( - name='Develop brief survey', workflowlevel1=wkflvl1_2) - - request = self.factory.get( - '/api/workflowlevel2/?workflowlevel1__country__country=%s' - % country1.country) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.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'], wkflvl2.name) - - def test_filter_workflowlevel2_wkflvl1_name_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl1_2 = factories.WorkflowLevel1( - name='Construction Project', - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1_1) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1_2) - wkflvl2 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_1) - factories.WorkflowLevel2( - name='Develop brief survey', workflowlevel1=wkflvl1_2) - - request = self.factory.get( - '/api/workflowlevel2/?workflowlevel1__name=%s' - % wkflvl1_1.name) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.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'], wkflvl2.name) - - def test_filter_workflowlevel2_wkflvl1_id_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl1_2 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1_1) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1_2) - wkflvl2 = factories.WorkflowLevel2(workflowlevel1=wkflvl1_1) - factories.WorkflowLevel2( - name='Develop brief survey', workflowlevel1=wkflvl1_2) - - request = self.factory.get( - '/api/workflowlevel2/?workflowlevel1__id=%s' - % wkflvl1_1.pk) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.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'], wkflvl2.name) - - def test_filter_workflowlevel2_level2_uuid_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wkflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - WorkflowTeam.objects.create( - workflow_user=self.tola_user, - workflowlevel1=wkflvl1) - wkflvl2 = factories.WorkflowLevel2( - level2_uuid=111, workflowlevel1=wkflvl1) - factories.WorkflowLevel2( - name='Develop brief survey', level2_uuid=222, - workflowlevel1=wkflvl1) - - request = self.factory.get( - '/api/workflowlevel2/?level2_uuid=%s' - % wkflvl2.level2_uuid) - request.user = self.tola_user.user - view = WorkflowLevel2ViewSet.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'], wkflvl2.name) diff --git a/feed/tests/test_workflowteamview.py b/feed/tests/test_workflowteamview.py deleted file mode 100755 index 287f577ce..000000000 --- a/feed/tests/test_workflowteamview.py +++ /dev/null @@ -1,573 +0,0 @@ -from django.test import TestCase -from rest_framework.reverse import reverse -from rest_framework.test import APIRequestFactory - -import json -import factories -from feed.views import WorkflowTeamViewSet -from workflow.models import (WorkflowTeam, ROLE_ORGANIZATION_ADMIN, - ROLE_PROGRAM_ADMIN, ROLE_PROGRAM_TEAM, - ROLE_VIEW_ONLY) - - -class WorkflowTeamListViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - user_ringo = factories.User(first_name='Ringo', last_name='Starr') - tola_user_ringo = factories.TolaUser( - user=user_ringo, organization=factories.Organization()) - self.wflvl1 = factories.WorkflowLevel1( - organization=tola_user_ringo.organization) - factories.WorkflowTeam(workflow_user=tola_user_ringo, - workflowlevel1=self.wflvl1, - partner_org=self.wflvl1.organization, - role=factories.Group(name=ROLE_VIEW_ONLY)) - - def test_list_workflowteam_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - request_get = self.factory.get('/api/workflowteam/') - request_get.user = self.tola_user.user - response = view(request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_workflowteam_superuser_and_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - request_get = self.factory.get('/api/workflowteam/') - request_get.user = self.tola_user.user - response = view(request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_workflowteam_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - - # Create a workflow team having a diff partner org - factories.WorkflowTeam(workflow_user=self.tola_user, - workflowlevel1=wflvl1) - - request_get = self.factory.get('/api/workflowteam/') - request_get.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - response = view(request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_workflowteam_org_admin_diff_user_org(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - # Create a user belonging to other Project in other Org - another_org = factories.Organization(name='Another Org') - user_george = factories.User(first_name='George', last_name='Harrison') - tola_user_george = factories.TolaUser( - user=user_george, organization=another_org) - wflvl1_other = factories.WorkflowLevel1(organization=another_org) - factories.WorkflowTeam(workflow_user=tola_user_george, - workflowlevel1=wflvl1_other) - - request_get = self.factory.get('/api/workflowteam/') - request_get.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - response = view(request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - def test_list_workflowteam_org_admin_diff_user_same_org(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - # Create a user belonging to other Project in other Org - user_george = factories.User(first_name='George', last_name='Harrison') - tola_user_george = factories.TolaUser( - user=user_george, organization=self.tola_user.organization) - wflvl1_other = factories.WorkflowLevel1( - organization=self.tola_user.organization) - factories.WorkflowTeam(workflow_user=tola_user_george, - workflowlevel1=wflvl1_other) - - request_get = self.factory.get('/api/workflowteam/') - request_get.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - response = view(request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_workflowteam_org_admin_diff_partner_org(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - - # Create a workflow team having a diff partner org - another_org = factories.Organization(name='Another Org') - factories.WorkflowTeam(workflow_user=self.tola_user, - workflowlevel1=wflvl1, - partner_org=another_org) - - request_get = self.factory.get('/api/workflowteam/') - request_get.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - response = view(request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - - def test_list_workflowteam_program_admin(self): - WorkflowTeam.objects.create( - workflow_user=self.tola_user, workflowlevel1=self.wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - request_get = self.factory.get('/api/workflowteam/') - request_get.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - response = view(request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_workflowteam_program_team(self): - WorkflowTeam.objects.create( - workflow_user=self.tola_user, workflowlevel1=self.wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - - request_get = self.factory.get('/api/workflowteam/') - request_get.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - response = view(request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_workflowteam_view_only(self): - WorkflowTeam.objects.create( - workflow_user=self.tola_user, workflowlevel1=self.wflvl1, - role=factories.Group(name=ROLE_VIEW_ONLY)) - - request_get = self.factory.get('/api/workflowteam/') - request_get.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - response = view(request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 2) - - def test_list_workflowteam_normaluser(self): - request_get = self.factory.get('/api/workflowteam/') - request_get.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - response = view(request_get) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) - - -class WorkflowTeamCreateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - self.wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - - def test_create_workflowteam_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': self.wflvl1.id}) - tolauser_url = reverse('tolauser-detail', - kwargs={'pk': self.tola_user.id}) - role = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - role_url = reverse('group-detail', kwargs={'pk': role.id}) - data = { - 'role': role_url, - 'workflow_user': tolauser_url, - 'workflowlevel1': wflvl1_url, - } - - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - - WorkflowTeam.objects.get( - workflowlevel1=self.wflvl1, - workflow_user=self.tola_user, - role=role, - ) - - def test_create_workflowteam_org_admin(self): - WorkflowTeam.objects.create( - workflow_user=self.tola_user, workflowlevel1=self.wflvl1, - role=factories.Group(name=ROLE_ORGANIZATION_ADMIN)) - - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': self.wflvl1.id}) - user_george = factories.User(first_name='George', last_name='Harrison') - tola_user_george = factories.TolaUser( - user=user_george, organization=factories.Organization()) - tolauser_url = reverse('tolauser-detail', - kwargs={'pk': tola_user_george.id}) - role = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - role_url = reverse('group-detail', kwargs={'pk': role.id}) - data = { - 'role': role_url, - 'workflow_user': tolauser_url, - 'workflowlevel1': wflvl1_url, - } - - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - - WorkflowTeam.objects.get( - workflowlevel1=self.wflvl1, - workflow_user=tola_user_george, - role=role, - ) - - def test_create_workflowteam_program_admin(self): - WorkflowTeam.objects.create( - workflow_user=self.tola_user, workflowlevel1=self.wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': self.wflvl1.id}) - tolauser_url = reverse('tolauser-detail', - kwargs={'pk': self.tola_user.id}) - role = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - role_url = reverse('group-detail', kwargs={'pk': role.id}) - data = { - 'role': role_url, - 'workflow_user': tolauser_url, - 'workflowlevel1': wflvl1_url, - } - - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - - WorkflowTeam.objects.get( - workflowlevel1=self.wflvl1, - workflow_user=self.tola_user, - role=role, - ) - - def test_create_workflowteam_program_admin_json(self): - WorkflowTeam.objects.create( - workflow_user=self.tola_user, workflowlevel1=self.wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': self.wflvl1.id}) - tolauser_url = reverse('tolauser-detail', - kwargs={'pk': self.tola_user.id}) - role = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - role_url = reverse('group-detail', kwargs={'pk': role.id}) - data = { - 'role': role_url, - 'workflow_user': tolauser_url, - 'workflowlevel1': wflvl1_url, - } - - request = self.factory.post(None, json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 201) - - WorkflowTeam.objects.get( - workflowlevel1=self.wflvl1, - workflow_user=self.tola_user, - role=role, - ) - - def test_create_workflowteam_other_user(self): - role_without_benefits = ROLE_PROGRAM_TEAM - WorkflowTeam.objects.create( - workflow_user=self.tola_user, workflowlevel1=self.wflvl1, - role=factories.Group(name=role_without_benefits)) - - wflvl1_url = reverse('workflowlevel1-detail', - kwargs={'pk': self.wflvl1.id}) - tolauser_url = reverse('tolauser-detail', - kwargs={'pk': self.tola_user.id}) - role = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - role_url = reverse('group-detail', kwargs={'pk': role.id}) - data = { - 'role': role_url, - 'workflow_user': tolauser_url, - 'workflowlevel1': wflvl1_url, - } - - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'post': 'create'}) - response = view(request) - self.assertEqual(response.status_code, 403) - - self.assertRaises( - WorkflowTeam.DoesNotExist, - WorkflowTeam.objects.get, workflowlevel1=self.wflvl1, - workflow_user=self.tola_user, - role=role, - ) - - -class WorkflowTeamUpdateViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - user_ringo = factories.User(first_name='Ringo', last_name='Starr') - tola_user_ringo = factories.TolaUser( - user=user_ringo, organization=self.tola_user.organization) - self.wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - self.workflowteam = factories.WorkflowTeam( - workflow_user=tola_user_ringo, - workflowlevel1=self.wflvl1, - partner_org=self.wflvl1.organization, - role=factories.Group(name=ROLE_VIEW_ONLY)) - - def test_update_unexisting_workflowteam(self): - data = {'salary': '100'} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'post': 'update'}) - response = view(request, pk=288) - self.assertEqual(response.status_code, 404) - - def test_update_workflowteam_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - data = {'salary': '100'} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'post': 'update'}) - response = view(request, pk=self.workflowteam.pk) - self.assertEqual(response.status_code, 200) - - salary_updated = WorkflowTeam.objects.\ - values_list('salary', flat=True).get(pk=self.workflowteam.pk) - self.assertEqual(salary_updated, '100') - - def test_update_workflowteam_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - data = {'salary': '100'} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'post': 'update'}) - response = view(request, pk=self.workflowteam.pk) - self.assertEqual(response.status_code, 200) - - salary_updated = WorkflowTeam.objects.\ - values_list('salary', flat=True).get(pk=self.workflowteam.pk) - self.assertEqual(salary_updated, '100') - - def test_update_workflowteam_program_admin(self): - factories.WorkflowTeam( - workflow_user=self.tola_user, - workflowlevel1=self.wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'salary': '100'} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'post': 'update'}) - response = view(request, pk=self.workflowteam.pk) - self.assertEqual(response.status_code, 200) - - salary_updated = WorkflowTeam.objects.\ - values_list('salary', flat=True).get(pk=self.workflowteam.pk) - self.assertEqual(salary_updated, '100') - - def test_update_workflowteam_program_admin_json(self): - factories.WorkflowTeam( - workflow_user=self.tola_user, - workflowlevel1=self.wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - data = {'salary': '100'} - request = self.factory.post(None, json.dumps(data), - content_type='application/json') - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'post': 'update'}) - response = view(request, pk=self.workflowteam.pk) - self.assertEqual(response.status_code, 200) - - salary_updated = WorkflowTeam.objects.\ - values_list('salary', flat=True).get(pk=self.workflowteam.pk) - self.assertEqual(salary_updated, '100') - - def test_update_workflowteam_other_user(self): - role_without_benefits = ROLE_PROGRAM_TEAM - self.workflowteam.role = factories.Group(name=role_without_benefits) - self.workflowteam.save() - - data = {'salary': '100'} - request = self.factory.post(None, data) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'post': 'update'}) - response = view(request, pk=self.workflowteam.pk) - self.assertEqual(response.status_code, 403) - - -class WorkflowTeamDeleteViewsTest(TestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.tola_user = factories.TolaUser() - - user_ringo = factories.User(first_name='Ringo', last_name='Starr') - tola_user_ringo = factories.TolaUser( - user=user_ringo, organization=self.tola_user.organization) - self.wflvl1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - self.workflowteam = factories.WorkflowTeam( - workflow_user=tola_user_ringo, - workflowlevel1=self.wflvl1, - partner_org=self.wflvl1.organization, - role=factories.Group(name=ROLE_VIEW_ONLY)) - - def test_delete_workflowteam_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - request = self.factory.delete(None) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=self.workflowteam.pk) - self.assertEqual(response.status_code, 204) - - self.assertRaises( - WorkflowTeam.DoesNotExist, - WorkflowTeam.objects.get, pk=self.workflowteam.pk) - - def test_delete_workflowteam_org_admin(self): - group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) - self.tola_user.user.groups.add(group_org_admin) - - request = self.factory.delete(None) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=self.workflowteam.pk) - self.assertEqual(response.status_code, 204) - - self.assertRaises( - WorkflowTeam.DoesNotExist, - WorkflowTeam.objects.get, pk=self.workflowteam.pk) - - def test_delete_workflowteam_program_admin(self): - factories.WorkflowTeam( - workflow_user=self.tola_user, - workflowlevel1=self.wflvl1, - role=factories.Group(name=ROLE_PROGRAM_ADMIN)) - - request = self.factory.delete(None) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=self.workflowteam.pk) - self.assertEqual(response.status_code, 204) - - self.assertRaises( - WorkflowTeam.DoesNotExist, - WorkflowTeam.objects.get, pk=self.workflowteam.pk) - - def test_delete_workflowteam_role_without_benefit(self): - factories.WorkflowTeam( - workflow_user=self.tola_user, - workflowlevel1=self.wflvl1, - role=factories.Group(name=ROLE_PROGRAM_TEAM)) - - request = self.factory.delete(None) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'delete': 'destroy'}) - response = view(request, pk=self.workflowteam.pk) - self.assertEqual(response.status_code, 403) - WorkflowTeam.objects.get(pk=self.workflowteam.pk) - - -class WorkflowTeamFilterViewTest(TestCase): - def setUp(self): - self.tola_user = factories.TolaUser(tola_user_uuid='987654321') - self.factory = APIRequestFactory() - - def test_filter_workflowteam_superuser(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - another_org = factories.Organization(name='Another Org') - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wkflvl1_2 = factories.WorkflowLevel1( - organization=another_org) - workflowteam1 = factories.WorkflowTeam(workflow_user=self.tola_user, - salary=1111, - workflowlevel1=wkflvl1_1) - factories.WorkflowTeam(workflow_user=self.tola_user, - salary=2222, - workflowlevel1=wkflvl1_2) - - request = self.factory.get( - '/api/workflowteam/?workflowlevel1__organization__id=%s' % - self.tola_user.organization.pk) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['salary'], str(workflowteam1.salary)) - - def test_filter_workflowteam_tola_user_uuid(self): - self.tola_user.user.is_staff = True - self.tola_user.user.is_superuser = True - self.tola_user.user.save() - - wkflvl1_1 = factories.WorkflowLevel1( - organization=self.tola_user.organization) - wfteam1 = factories.WorkflowTeam(workflow_user=self.tola_user, - workflowlevel1=wkflvl1_1) - - request = self.factory.get( - '/api/workflowteam/?workflow_user__tola_user_uuid=%s' % - self.tola_user.tola_user_uuid) - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['id'], wfteam1.id) - - request = self.factory.get( - '/api/workflowteam/?workflow_user__tola_user_uuid=123456789') - request.user = self.tola_user.user - view = WorkflowTeamViewSet.as_view({'get': 'list'}) - response = view(request) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data), 0) diff --git a/feed/views.py b/feed/views.py deleted file mode 100755 index cb899fc82..000000000 --- a/feed/views.py +++ /dev/null @@ -1,1397 +0,0 @@ -import json -import logging -from urlparse import urljoin - -from django.conf import settings -from django.contrib.auth.models import Group -from django.db.models import Q, Sum -from django.shortcuts import get_object_or_404 -from django.utils.decorators import method_decorator -from django.views.decorators.csrf import csrf_exempt -import django_filters -import requests -from rest_framework import status, viewsets -from rest_framework.response import Response -from rest_framework.pagination import PageNumberPagination - -from formlibrary.models import ( - Beneficiary, CustomForm, CustomFormField, FieldType) -from indicators.models import ( - Indicator, Frequency, IndicatorType, Objective, DisaggregationType, - Level, ExternalService, ExternalServiceRecord, StrategicObjective, - PeriodicTarget, CollectedData, TolaTable, DisaggregationValue, - DisaggregationLabel) - -from workflow import models as wfm -from .permissions import IsOrgMember, AllowTolaRoles -from . import serializers - -logger = logging.getLogger(__name__) - - -class StandardResultsSetPagination(PageNumberPagination): - page_size = 100 - page_size_query_param = 'page_size' - max_page_size = 1000 - - -class SmallResultsSetPagination(PageNumberPagination): - page_size = 20 - page_size_query_param = 'page_size' - max_page_size = 50 - - -class ProgramIndicatorReadOnlyViewSet(viewsets.ReadOnlyModelViewSet): - serializer_class = serializers.ProgramIndicatorSerializer - pagination_class = StandardResultsSetPagination - - def get_queryset(self): - queryset = wfm.WorkflowLevel1.objects.prefetch_related( - 'indicator_set', - 'indicator_set__indicator_type', - 'indicator_set__sector', 'indicator_set__level', - 'indicator_set__collecteddata_set').all() - return queryset - - -class UserViewSet(viewsets.ModelViewSet): - """ - A ViewSet for listing or retrieving users. - """ - queryset = wfm.User.objects.all() - serializer_class = serializers.UserSerializer - - -class GroupViewSet(viewsets.ReadOnlyModelViewSet): - """ - A ViewSet for listing or retrieving users. - """ - queryset = wfm.Group.objects.all() - serializer_class = serializers.GroupSerializer - - -class WorkflowLevel1ViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - search by country name and workflowlevel1 name - limit to users logged in country permissions - """ - # Remove CSRF request verification for posts to this API - @method_decorator(csrf_exempt) - def dispatch(self, *args, **kwargs): - return super(WorkflowLevel1ViewSet, self).dispatch(*args, **kwargs) - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()).annotate( - budget=Sum('workflowlevel2__total_estimated_budget'), - actuals=Sum('workflowlevel2__actual_cost')) - if not request.user.is_superuser: - if wfm.ROLE_ORGANIZATION_ADMIN in request.user.groups.values_list( - 'name', flat=True): - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - else: - wflvl1_ids = wfm.WorkflowTeam.objects.filter( - workflow_user__user=request.user).values_list( - 'workflowlevel1__id', flat=True) - queryset = queryset.filter(id__in=wflvl1_ids) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def create(self, request, *args, **kwargs): - serializer = self.get_serializer(data=request.data) - serializer.is_valid(raise_exception=True) - self.perform_create(serializer) # inherited from CreateModelMixin - - # Assign the user to multiple properties of the Program - group_program_admin = Group.objects.get(name=wfm.ROLE_PROGRAM_ADMIN) - wflvl1 = wfm.WorkflowLevel1.objects.get( - level1_uuid=serializer.data['level1_uuid']) - wfm.WorkflowTeam.objects.create( - workflow_user=request.user.tola_user, workflowlevel1=wflvl1, - role=group_program_admin) - - headers = self.get_success_headers(serializer.data) - return Response(serializer.data, status=status.HTTP_201_CREATED, - headers=headers) - - def perform_create(self, serializer): - organization = self.request.user.tola_user.organization - obj = serializer.save(organization=organization) - obj.user_access.add(self.request.user.tola_user) - - def destroy(self, request, pk): - workflowlevel1 = self.get_object() - workflowlevel1.delete() - return Response(status=status.HTTP_204_NO_CONTENT) - - ordering_fields = ('country__country', 'name') - filter_fields = ('country__country', 'name', 'level1_uuid') - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - - queryset = wfm.WorkflowLevel1.objects.all().annotate( - budget=Sum('workflowlevel2__total_estimated_budget'), - actuals=Sum('workflowlevel2__actual_cost')) - permission_classes = (AllowTolaRoles, IsOrgMember) - serializer_class = serializers.WorkflowLevel1Serializer - - -class SectorViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def __get__(self, instance, owner): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not self.request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=self.request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - serializer.save(created_by=self.request.user) - - filter_fields = ('organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.Sector.objects.all() - serializer_class = serializers.SectorSerializer - - -class ProjectTypeViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.ProjectType.objects.all() - serializer_class = serializers.ProjectTypeSerializer - - -class OfficeViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - filter_fields = ('country__country',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - queryset = wfm.Office.objects.all() - serializer_class = serializers.OfficeSerializer - - -class SiteProfileViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - Search by country name and program name - limit to users logged in country permissions - """ - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=self.request.user) - serializer.save(organization_id=organization_id, - created_by=self.request.user) - - filter_fields = ('country__country',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.SiteProfile.objects.all() - serializer_class = serializers.SiteProfileSerializer - - -class CountryViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - queryset = wfm.Country.objects.all() - serializer_class = serializers.CountrySerializer - - -class IndicatorViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - Search by country name and program name - limit to users logged in country permissions - """ - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - if wfm.ROLE_ORGANIZATION_ADMIN in request.user.groups.values_list( - 'name', flat=True): - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter( - workflowlevel1__organization_id=organization_id) - else: - wflvl1_ids = wfm.WorkflowTeam.objects.filter( - workflow_user__user=request.user).values_list( - 'workflowlevel1__id', flat=True) - queryset = queryset.filter( - workflowlevel1__in=wflvl1_ids).annotate( - actuals=Sum('collecteddata__achieved')) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def destroy(self, request, pk): - indicator = self.get_object() - indicator.delete() - return Response(status=status.HTTP_204_NO_CONTENT) - - def get_queryset(self): - return Indicator.objects.annotate( - actuals=Sum('collecteddata__achieved')) - - def perform_create(self, serializer): - serializer.save(created_by=self.request.user) - - filter_fields = ('workflowlevel1__country__country', 'workflowlevel1__name', - 'indicator_uuid') - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (AllowTolaRoles, IsOrgMember) - queryset = Indicator.objects.all() - serializer_class = serializers.IndicatorSerializer - - -class FrequencyViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - organization_id = wfm.TolaUser.objects.\ - values_list('organization_id', flat=True).\ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(instance=queryset, - context={'request': request}, - many=True) - return Response(serializer.data) - - permission_classes = (IsOrgMember,) - queryset = Frequency.objects.all() - serializer_class = serializers.FrequencySerializer - - -class TolaUserViewSet(viewsets.ModelViewSet): - """ - A ViewSet for listing or retrieving wfm.TolaUsers. - - """ - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects.\ - values_list('organization_id', flat=True).\ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer( - instance=queryset, context={'request': request}, many=True) - return Response(serializer.data) - - def retrieve(self, request, pk=None): - queryset = self.queryset - user = get_object_or_404(queryset, pk=pk) - serializer = self.get_serializer(instance=user, - context={'request': request}) - return Response(serializer.data) - - filter_fields = ('organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - queryset = wfm.TolaUser.objects.all() - serializer_class = serializers.TolaUserSerializer - - -class IndicatorTypeViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects.\ - values_list('organization_id', flat=True).\ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = IndicatorType.objects.all() - serializer_class = serializers.IndicatorTypeSerializer - - -class ObjectiveViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter( - workflowlevel1__organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - serializer.save(created_by=self.request.user) - - filter_fields = ('workflowlevel1__organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (AllowTolaRoles, IsOrgMember) - queryset = Objective.objects.all() - serializer_class = serializers.ObjectiveSerializer - - -class FundCodeViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.FundCode.objects.all() - serializer_class = serializers.FundCodeSerializer - - -class DisaggregationTypeViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter( - organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=self.request.user) - serializer.save(organization_id=organization_id) - - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = DisaggregationType.objects.all() - serializer_class = serializers.DisaggregationTypeSerializer - - -class LevelViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - serializer.save(created_by=self.request.user) - - filter_fields = ('organization__id', 'country__country') - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (AllowTolaRoles, IsOrgMember) - - queryset = Level.objects.all() - serializer_class = serializers.LevelSerializer - - -class StakeholderViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - wflvl1_ids = wfm.WorkflowTeam.objects.filter( - workflow_user__user=request.user).values_list( - 'workflowlevel1__id', flat=True) - queryset = queryset.filter(workflowlevel1__in=wflvl1_ids).distinct() - - nested = request.GET.get('nested_models') - if nested is not None and (nested.lower() == 'true' or nested == '1'): - self.serializer_class = serializers.StakeholderFullSerializer - - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def retrieve(self, request, *args, **kwargs): - instance = self.get_object() - - nested = request.GET.get('nested_models') - if nested is not None and (nested.lower() == 'true' or nested == '1'): - self.serializer_class = serializers.StakeholderFullSerializer - - serializer = self.get_serializer(instance) - return Response(serializer.data) - - def perform_create(self, serializer): - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=self.request.user) - serializer.save(organization_id=organization_id, - created_by=self.request.user) - - filter_fields = ('workflowlevel1__name',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - queryset = wfm.Stakeholder.objects.all() - serializer_class = serializers.StakeholderSerializer - - -class ExternalServiceViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter( - organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = ExternalService.objects.all() - serializer_class = serializers.ExternalServiceSerializer - - -class ExternalServiceRecordViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - queryset = ExternalServiceRecord.objects.all() - serializer_class = serializers.ExternalServiceRecordSerializer - - -class StrategicObjectiveViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=self.request.user) - serializer.save(organization_id=organization_id) - - filter_fields = ('organization__id', 'country__country') - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = StrategicObjective.objects.all() - serializer_class = serializers.StrategicObjectiveSerializer - - -class StakeholderTypeViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.StakeholderType.objects.all() - serializer_class = serializers.StakeholderTypeSerializer - - -class ProfileTypeViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.ProfileType.objects.all() - serializer_class = serializers.ProfileTypeSerializer - - -class ProvinceViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - queryset = wfm.AdminLevelOne.objects.all() - serializer_class = serializers.ProvinceSerializer - - -class DistrictViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - queryset = wfm.AdminLevelTwo.objects.all() - serializer_class = serializers.DistrictSerializer - - -class AdminLevelThreeViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - queryset = wfm.AdminLevelThree.objects.all() - serializer_class = serializers.AdminLevelThreeSerializer - - -class VillageViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - queryset = wfm.AdminLevelFour.objects.all() - serializer_class = serializers.VillageSerializer - - -class ContactViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - wflvl1_ids = wfm.WorkflowTeam.objects.filter( - workflow_user__user=request.user).values_list( - 'workflowlevel1__id', flat=True) - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id, - workflowlevel1__in=wflvl1_ids) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('name',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (AllowTolaRoles, IsOrgMember) - queryset = wfm.Contact.objects.all() - serializer_class = serializers.ContactSerializer - - -class DocumentationViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - wflvl1_ids = wfm.WorkflowTeam.objects.filter( - workflow_user__user=request.user).values_list( - 'workflowlevel1__id', flat=True) - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter( - workflowlevel1__organization_id=organization_id - ).filter( - workflowlevel1__in=wflvl1_ids) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - - serializer.save(created_by=self.request.user) - - filter_fields = ('workflowlevel1__country__country', - 'workflowlevel1__organization__id') - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.Documentation.objects.all() - serializer_class = serializers.DocumentationSerializer - - -class PeriodicTargetViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - queryset = PeriodicTarget.objects.all() - serializer_class = serializers.PeriodicTargetSerializer - - -class CollectedDataViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - wflvl1_ids = wfm.WorkflowTeam.objects.filter( - workflow_user__user=request.user).values_list( - 'workflowlevel1__id', flat=True) - queryset = queryset.filter(indicator__workflowlevel1__in=wflvl1_ids) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - serializer.save(created_by=self.request.user) - - filter_fields = ('indicator__workflowlevel1__country__country', - 'indicator__workflowlevel1__name', 'indicator', - 'indicator__workflowlevel1__organization__id') - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - queryset = CollectedData.objects.all() - permission_classes = (AllowTolaRoles, IsOrgMember) - serializer_class = serializers.CollectedDataSerializer - pagination_class = SmallResultsSetPagination - - -class TolaTableViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - def get_queryset(self): - wflvl1_ids = wfm.WorkflowTeam.objects.filter( - workflow_user__user=self.request.user).values_list( - 'workflowlevel1__id', flat=True) - queryset = TolaTable.objects.filter(workflowlevel1__in=wflvl1_ids) - table_id = self.request.query_params.get('table_id', None) - if table_id is not None: - queryset = queryset.filter(table_id=table_id) - return queryset - - filter_fields = ('table_id', 'country__country', - 'collecteddata__indicator__workflowlevel1__name', - 'organization__id') - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - serializer_class = serializers.TolaTableSerializer - permission_classes = (IsOrgMember,) - pagination_class = StandardResultsSetPagination - queryset = TolaTable.objects.all() - - -class DisaggregationValueViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - filter_fields = ('disaggregation_label__disaggregation_type',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - queryset = DisaggregationValue.objects.all() - serializer_class = serializers.DisaggregationValueSerializer - - -class DisaggregationLabelViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - """ - - queryset = DisaggregationLabel.objects.all() - serializer_class = serializers.DisaggregationLabelSerializer - - -class ChecklistViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter( - workflowlevel2__workflowlevel1__organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - serializer.save(owner=self.request.user) - - filter_fields = ('workflowlevel2__name', - 'workflowlevel2__workflowlevel1__organization__id', - 'workflowlevel2__workflowlevel1__country__country', - 'owner') - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.Checklist.objects.all() - serializer_class = serializers.ChecklistSerializer - - -class OrganizationViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.Organization.objects.all() - serializer_class = serializers.OrganizationSerializer - - -class WorkflowLevel2ViewSet(viewsets.ModelViewSet): - """ - This viewset automatically provides `list`, `create`, `retrieve`, - `update` and `destroy` actions. - Search by country name and program name - limit to users logged in country permissions - """ - # Remove CSRF request verification for posts to this API - @method_decorator(csrf_exempt) - def dispatch(self, *args, **kwargs): - return super(WorkflowLevel2ViewSet, self).dispatch(*args, **kwargs) - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - if wfm.ROLE_ORGANIZATION_ADMIN in request.user.groups.values_list( - 'name', flat=True): - queryset = queryset.filter( - workflowlevel1__organization_id=organization_id) - else: - wflvl1_ids = wfm.WorkflowTeam.objects.filter( - workflow_user__user=request.user).values_list( - 'workflowlevel1__id', flat=True) - queryset = queryset.filter( - workflowlevel1__organization_id=organization_id, - workflowlevel1__in=wflvl1_ids) - - nested = request.GET.get('nested_models') - if nested is not None and (nested.lower() == 'true' or nested == '1'): - self.serializer_class = serializers.WorkflowLevel2FullSerializer - - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def retrieve(self, request, *args, **kwargs): - instance = self.get_object() - - nested = request.GET.get('nested_models') - if nested is not None and (nested.lower() == 'true' or nested == '1'): - self.serializer_class = serializers.WorkflowLevel2FullSerializer - - serializer = self.get_serializer(instance) - return Response(serializer.data) - - def perform_create(self, serializer): - serializer.save(created_by=self.request.user) - - filter_fields = ('workflowlevel1__country__country', 'workflowlevel1__name', - 'level2_uuid', 'workflowlevel1__id') - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - queryset = wfm.WorkflowLevel2.objects.all() - permission_classes = (AllowTolaRoles, IsOrgMember) - serializer_class = serializers.WorkflowLevel2Serializer - pagination_class = SmallResultsSetPagination - - -class WorkflowModulesViewSet(viewsets.ModelViewSet): - filter_fields = ('workflowlevel2__workflowlevel1__organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - queryset = wfm.WorkflowModules.objects.all() - serializer_class = serializers.WorkflowModulesSerializer - - -class WorkflowLevel2SortViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - wflvl1_ids = wfm.WorkflowTeam.objects.filter( - workflow_user__user=request.user).values_list( - 'workflowlevel1__id', flat=True) - queryset = queryset.filter(workflowlevel1__in=wflvl1_ids) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - queryset = wfm.WorkflowLevel2Sort.objects.all() - permission_classes = (IsOrgMember,) - serializer_class = serializers.WorkflowLevel2SortSerializer - - -class CurrencyViewSet(viewsets.ModelViewSet): - """ - Global Field so do not filter by Org - """ - queryset = wfm.Currency.objects.all() - serializer_class = serializers.CurrencySerializer - - -class ApprovalTypeViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.ApprovalType.objects.all() - serializer_class = serializers.ApprovalTypeSerializer - - -class ApprovalWorkflowViewSet(viewsets.ModelViewSet): - queryset = wfm.ApprovalWorkflow.objects.all() - serializer_class = serializers.ApprovalWorkflowSerializer - - def perform_create(self, serializer): - serializer.save(created_by=self.request.user) - - -class BeneficiaryViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter( - workflowlevel1__organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('workflowlevel1__organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = Beneficiary.objects.all() - serializer_class = serializers.BeneficiarySerializer - - -class CustomFormViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def update(self, request, *args, **kwargs): - partial = kwargs.pop('partial', False) - instance = self.get_object() - form_data = request.data.copy() - fields = form_data.get('fields') - headers = { - 'Authorization': 'Token {}'.format( - settings.TOLA_TRACK_TOKEN), - } - - if isinstance(fields, list): - fields = json.dumps(fields) - - # Serialize the program - if request.data.get('workflowlevel1'): - wflvl1_serializer = self.serializer_class().get_fields()[ - 'workflowlevel1'] - wkfl1 = wflvl1_serializer.run_validation(request.data.get( - 'workflowlevel1')) - else: - wkfl1 = None - - if instance.workflowlevel1: - if wkfl1 and wkfl1 != instance.workflowlevel1: - return Response({'detail': 'You cannot change the Program.'}, - status=status.HTTP_400_BAD_REQUEST) - # Check if there is already data in the table - url_subpath = 'api/customform/%s/has_data' % instance.silo_id - url = urljoin(settings.TOLA_TRACK_URL, url_subpath) - response = requests.get(url, headers=headers) - has_data = json.loads(response.content) - - if has_data == 'false': - # Update the table info in Track - data = {'name': form_data.get('name'), - 'description': form_data.get('description'), - 'fields': fields} - url_subpath = 'api/customform/%s' % instance.silo_id - url = urljoin(settings.TOLA_TRACK_URL, url_subpath) - requests.put(url, data=data, headers=headers) - else: - return Response( - {'detail': 'You already have data in the instance.'}, - status=status.HTTP_409_CONFLICT - ) - elif wkfl1: - tola_user = request.user.tola_user - silo_data = {'name': form_data.get('name'), - 'description': form_data.get('description'), - 'fields': fields, - 'level1_uuid': wkfl1.level1_uuid, - 'tola_user_uuid': tola_user.tola_user_uuid} - - # Make a POST request to Track to create a table - url_subpath = 'api/customform' - url = urljoin(settings.TOLA_TRACK_URL, url_subpath) - response = requests.post(url, data=silo_data, headers=headers) - - silo_json = json.loads(response.content) - form_data['silo_id'] = silo_json.get('id') - - serializer = self.get_serializer(instance, data=form_data, - partial=partial) - serializer.is_valid(raise_exception=True) - self.perform_update(serializer) - - return Response(serializer.data) - - def perform_create(self, serializer): - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=self.request.user) - serializer.save(organization_id=organization_id, - created_by=self.request.user) - - filter_fields = ('organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (AllowTolaRoles, IsOrgMember) - queryset = CustomForm.objects.all() - serializer_class = serializers.CustomFormSerializer - - -class CustomFormFieldViewSet(viewsets.ModelViewSet): - queryset = CustomFormField.objects.all() - serializer_class = serializers.CustomFormFieldSerializer - - -class FieldTypeViewSet(viewsets.ModelViewSet): - queryset = FieldType.objects.all() - serializer_class = serializers.FieldTypeSerializer - - -class BudgetViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter( - workflowlevel2__workflowlevel1__organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - serializer.save(created_by=self.request.user) - - filter_fields = ('workflowlevel2__workflowlevel1__organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.Budget.objects.all() - serializer_class = serializers.BudgetSerializer - - -class RiskRegisterViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter( - workflowlevel2__workflowlevel1__organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - queryset = wfm.RiskRegister.objects.all() - permission_classes = (IsOrgMember,) - serializer_class = serializers.RiskRegisterSerializer - - -class CodedFieldViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('workflowlevel2__workflowlevel1__organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.CodedField.objects.all() - serializer_class = serializers.CodedFieldSerializer - - -class CodedFieldValuesViewSet(viewsets.ModelViewSet): - queryset = wfm.CodedFieldValues.objects.all() - serializer_class = serializers.CodedFieldValuesSerializer - - -class IssueRegisterViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=self.request.user) - serializer.save(organization_id=organization_id) - - filter_fields = ('workflowlevel2__workflowlevel1__organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.IssueRegister.objects.all() - serializer_class = serializers.IssueRegisterSerializer - - -class LandTypeViewSet(viewsets.ModelViewSet): - """ - Global Field so do not filter by Org - """ - - queryset = wfm.LandType.objects.all() - serializer_class = serializers.LandTypeSerializer - - -class InternationalizationViewSet(viewsets.ModelViewSet): - """ - Global Field so do not filter by Org - """ - - queryset = wfm.Internationalization.objects.all() - serializer_class = serializers.InternationalizationSerializer - - -class TolaUserFilterViewSet(viewsets.ModelViewSet): - queryset = wfm.TolaUserFilter.objects.all() - serializer_class = serializers.TolaUserFilterSerializer - - -class AwardViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (IsOrgMember,) - queryset = wfm.Award.objects.all() - serializer_class = serializers.AwardSerializer - - -class WorkflowTeamViewSet(viewsets.ModelViewSet): - """ - This viewset provides `list`, `create`, `retrieve`, `update` and - `destroy` actions. - """ - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - if wfm.ROLE_ORGANIZATION_ADMIN in request.user.groups.values_list( - 'name', flat=True): - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter( - workflow_user__organization_id=organization_id) - else: - wflvl1_ids = wfm.WorkflowTeam.objects.filter( - workflow_user__user=request.user).values_list( - 'workflowlevel1__id', flat=True) - queryset = queryset.filter(workflowlevel1__in=wflvl1_ids) - - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('workflowlevel1__organization__id', - 'workflow_user__tola_user_uuid') - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - permission_classes = (AllowTolaRoles,) - queryset = wfm.WorkflowTeam.objects.all() - serializer_class = serializers.WorkflowTeamSerializer - - -class MilestoneViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - serializer.save(created_by=self.request.user) - - permission_classes = (IsOrgMember,) - queryset = wfm.Milestone.objects.all() - serializer_class = serializers.MilestoneSerializer - - -class PortfolioViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter(organization_id=organization_id) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def create(self, request, *args, **kwargs): - serializer = self.get_serializer(data=request.data) - serializer.is_valid(raise_exception=True) - self.perform_create(serializer) # inherited from CreateModelMixin - - portfolio = wfm.Portfolio.objects.get(pk=serializer.data['id']) - portfolio.organization = request.user.tola_user.organization - portfolio.save() - - headers = self.get_success_headers(serializer.data) - return Response(serializer.data, status=status.HTTP_201_CREATED, - headers=headers) - - permission_classes = (AllowTolaRoles, IsOrgMember) - queryset = wfm.Portfolio.objects.all() - serializer_class = serializers.PortfolioSerializer - - -class DashboardViewSet(viewsets.ModelViewSet): - - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - if not request.user.is_superuser: - tolauser_id = wfm.TolaUser.objects.values_list( - 'id', flat=True).get(user=request.user) - organization_id = wfm.TolaUser.objects. \ - values_list('organization_id', flat=True). \ - get(user=request.user) - queryset = queryset.filter( - Q(user_id=tolauser_id) | Q(share__id=tolauser_id) | - Q(public=wfm.Dashboard.PUBLIC_ALL) | - Q(public=wfm.Dashboard.PUBLIC_ORG, - user__organization_id=organization_id)).distinct() - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - def perform_create(self, serializer): - serializer.save(user=self.request.user.tola_user) - - filter_fields = ('user', 'share', 'public') - queryset = wfm.Dashboard.objects.all() - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - serializer_class = serializers.DashboardSerializer - - -class WidgetViewSet(viewsets.ModelViewSet): - def list(self, request): - # Use this queryset or the django-filters lib will not work - queryset = self.filter_queryset(self.get_queryset()) - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - - filter_fields = ('dashboard',) - queryset = wfm.Widget.objects.all() - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - serializer_class = serializers.WidgetSerializer - - -class SectorRelatedViewSet(viewsets.ModelViewSet): - - filter_fields = ('sector', 'organization__id',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - queryset = wfm.SectorRelated.objects.all() - serializer_class = serializers.SectorRelatedSerializer - - -class WorkflowLevel1SectorViewSet(viewsets.ModelViewSet): - - queryset = wfm.WorkflowLevel1Sector.objects.all() - filter_fields = ('sector', 'workflowlevel1',) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) - serializer_class = serializers.WorkflowLevel1SectorSerializer diff --git a/tola/settings/base.py b/tola/settings/base.py index d21e11a75..ca5dcd2b4 100755 --- a/tola/settings/base.py +++ b/tola/settings/base.py @@ -197,9 +197,6 @@ 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.TokenAuthentication', ), - 'DEFAULT_PERMISSION_CLASSES': ( - 'feed.permissions.IsSuperUserBrowseableAPI', - ) } ########## END REST CONFIGURATION @@ -248,7 +245,6 @@ 'workflow', 'formlibrary', 'tola', - 'feed', 'indicators', 'customdashboard', 'reports', diff --git a/tola/urls.py b/tola/urls.py index 0a45ec4a3..8e7d67fb1 100755 --- a/tola/urls.py +++ b/tola/urls.py @@ -10,7 +10,6 @@ from formlibrary.views import BinaryFieldViewSet, binary_test from tola import views as tola_views -from feed import views as feed_views from tola import DEMO_BRANCH admin.autodiscover() @@ -19,70 +18,6 @@ # REST FRAMEWORK router = routers.DefaultRouter() -router.register(r'users', feed_views.UserViewSet) -router.register(r'groups', feed_views.GroupViewSet) -router.register(r'tolauser', feed_views.TolaUserViewSet) -router.register(r'tolauserfilter', feed_views.TolaUserFilterViewSet) -router.register(r'organization', feed_views.OrganizationViewSet) -router.register(r'country', feed_views.CountryViewSet) -router.register(r'award', feed_views.AwardViewSet) -router.register(r'workflowlevel1', feed_views.WorkflowLevel1ViewSet) -router.register(r'workflowlevel2', feed_views.WorkflowLevel2ViewSet) -router.register(r'workflowlevel2sort', feed_views.WorkflowLevel2SortViewSet) -router.register(r'workflowmodules', feed_views.WorkflowModulesViewSet) -router.register(r'workflowteam', feed_views.WorkflowTeamViewSet) -router.register(r'workflowlevel1sector', feed_views.WorkflowLevel1SectorViewSet) -router.register(r'approvaltype', feed_views.ApprovalTypeViewSet) -router.register(r'approvalworkflow', feed_views.ApprovalWorkflowViewSet) -router.register(r'milestone', feed_views.MilestoneViewSet) -router.register(r'checklist', feed_views.ChecklistViewSet) -router.register(r'sector', feed_views.SectorViewSet) -router.register(r'projecttype', feed_views.ProjectTypeViewSet) -router.register(r'office', feed_views.OfficeViewSet) -router.register(r'budget', feed_views.BudgetViewSet) -router.register(r'fundcode', feed_views.FundCodeViewSet) -router.register(r'siteprofile', feed_views.SiteProfileViewSet) -router.register(r'adminboundaryone', feed_views.ProvinceViewSet) -router.register(r'adminboundarytwo', feed_views.DistrictViewSet) -router.register(r'adminboundarythree', feed_views.AdminLevelThreeViewSet) -router.register(r'adminboundaryfour', feed_views.VillageViewSet) -router.register(r'frequency', feed_views.FrequencyViewSet) -router.register(r'indicatortype', feed_views.IndicatorTypeViewSet) -router.register(r'indicator', feed_views.IndicatorViewSet) -router.register(r'objective', feed_views.ObjectiveViewSet) -router.register(r'strategicobjective', feed_views.StrategicObjectiveViewSet) -router.register(r'level', feed_views.LevelViewSet) -router.register(r'externalservice', feed_views.ExternalServiceViewSet) -router.register(r'externalservicerecord', feed_views.ExternalServiceRecordViewSet) -router.register(r'stakeholder', feed_views.StakeholderViewSet) -router.register(r'stakeholdertype', feed_views.StakeholderTypeViewSet) -router.register(r'profiletype', feed_views.ProfileTypeViewSet) -router.register(r'contact', feed_views.ContactViewSet) -router.register(r'documentation', feed_views.DocumentationViewSet) -router.register(r'collecteddata', feed_views.CollectedDataViewSet) -router.register(r'periodictarget', feed_views.PeriodicTargetViewSet) -router.register(r'tolatable', feed_views.TolaTableViewSet, base_name='tolatable') -router.register(r'disaggregationtype', feed_views.DisaggregationTypeViewSet) -router.register(r'disaggregationlabel', feed_views.DisaggregationLabelViewSet) -router.register(r'disaggregationvalue', feed_views.DisaggregationValueViewSet) -router.register(r'checklist', feed_views.ChecklistViewSet) -router.register(r'organization', feed_views.OrganizationViewSet) -router.register(r'currency', feed_views.CurrencyViewSet) -router.register(r'beneficiary', feed_views.BeneficiaryViewSet) -router.register(r'riskregister', feed_views.RiskRegisterViewSet) -router.register(r'issueregister', feed_views.IssueRegisterViewSet) -router.register(r'fieldtype', feed_views.FieldTypeViewSet) -router.register(r'customformfield', feed_views.CustomFormFieldViewSet) -router.register(r'customform', feed_views.CustomFormViewSet) -router.register(r'codedfield', feed_views.CodedFieldViewSet) -router.register(r'codedfieldvalues', feed_views.CodedFieldValuesViewSet) -router.register(r'landtype', feed_views.LandTypeViewSet) -router.register(r'internationalization', feed_views.InternationalizationViewSet) -router.register(r'dashboard', feed_views.DashboardViewSet) -router.register(r'widget', feed_views.WidgetViewSet) -router.register(r'portfolio', feed_views.PortfolioViewSet) -router.register(r'sectorrelated', feed_views.SectorRelatedViewSet) -router.register(r'pindicators', feed_views.ProgramIndicatorReadOnlyViewSet, base_name='pindicators') # router.register(r'search', search_views.SearchView, base_name='search') router.register(r'binary', BinaryFieldViewSet, base_name='binary') diff --git a/tola/views.py b/tola/views.py index cc4d48999..093a325f9 100755 --- a/tola/views.py +++ b/tola/views.py @@ -25,8 +25,6 @@ from tola import DEMO_BRANCH from tola.track_sync import register_user -from feed.serializers import TolaUserSerializer, OrganizationSerializer, \ - CountrySerializer from tola.forms import RegistrationForm, NewUserRegistrationForm, \ NewTolaUserRegistrationForm, BookmarkForm from workflow.models import (Organization, TolaSites, TolaUser, TolaBookmarks, @@ -346,23 +344,7 @@ def oauth_user_view(request): class OAuthUserEndpoint(ProtectedResourceView): def get(self, request, *args, **kwargs): - user = request.user - body = { - 'username': user.username, - 'email': user.email, - 'id': user.id, - 'first_name': user.first_name, - 'last_name': user.last_name, - - } - tola_user = TolaUser.objects.all().filter(user=user) - if len(tola_user) == 1: - body["tola_user"] = TolaUserSerializer(instance=tola_user[0], context={'request': request}).data - body["organization"] = OrganizationSerializer(instance=tola_user[0].organization, - context={'request': request}).data - body["country"] = CountrySerializer(instance=tola_user[0].country, context={'request': request}).data - - return HttpResponse(json.dumps(body)) + pass class TolaTrackSiloProxy(ProtectedResourceView):