def test_custom_update_user_is_staff_no_group(self): profile = ProfileFactory() self.assertFalse(profile.user.is_staff) res = models.UserProfile.custom_update_user(profile.user, {}, None) self.assertTrue(res) profile_updated = models.UserProfile.objects.get(pk=profile.pk) self.assertTrue(profile_updated.user.is_staff)
def test_create_application_raises_exception_if_apartment_data_does_not_exist( ): data = create_validated_application_data(ProfileFactory(), ApplicationType.HASO) data["apartments"] = [{"priority": 1, "identifier": "this-does-not-exist"}] with pytest.raises(InvalidElasticDataError): create_application(data)
def test_name_methods(self): profile = ProfileFactory() user = profile.user assert profile.first_name == user.first_name assert profile.last_name == user.last_name assert profile.full_name == user.get_full_name()
def test_application_post_writes_audit_log_if_not_authenticated(api_client, caplog): data = create_application_data(ProfileFactory()) api_client.post(reverse("application_form:application-list"), data, format="json") audit_event = get_audit_log_event(caplog) assert audit_event is not None, "no audit log entry was written" assert audit_event["actor"] == {"role": "ANONYMOUS", "profile_id": None} assert audit_event["operation"] == "CREATE" assert audit_event["target"] == {"id": None, "type": "Application"} assert audit_event["status"] == "FORBIDDEN"
def test_delete(self): profile = ProfileFactory() profile.partner_staff_member = profile.user.pk profile.save() models.delete_partner_relationship(None, profile.user) profile_updated = models.UserProfile.objects.get(pk=profile.pk) self.assertIsNone(profile_updated.partner_staff_member) user = models.User.objects.get(pk=profile.user.pk) self.assertFalse(user.is_active)
def test_application_post(api_client): profile = ProfileFactory() api_client.credentials(HTTP_AUTHORIZATION=f"Bearer {_create_token(profile)}") data = create_application_data(profile) response = api_client.post( reverse("application_form:application-list"), data, format="json" ) assert response.status_code == 201 assert response.data == {"application_uuid": data["application_uuid"]}
def test_to_representation(self): profile = ProfileFactory() data = ProfileSerializer(profile).data assert data["username"] == profile.user.username assert data["last_name"] == profile.user.last_name assert data["first_name"] == profile.user.first_name assert data["description"] == profile.description
def test_birth_date_validation(self): profile = ProfileFactory() assert profile.full_clean() is None profile.birth_date = timezone.now().date() + timezone.timedelta(days=2) with pytest.raises(ValidationError): profile.full_clean() profile.birth_date = timezone.datetime(199, 1, 1).date() with pytest.raises(ValidationError): profile.full_clean()
def test_save_model_oic(self): """If OIC provided, then set OIC""" mock_form = Mock() mock_form.data = {"oic": self.superuser.pk} obj = ProfileFactory() self.assertIsNone(obj.oic) self.admin.save_model( self.request, obj, mock_form, None) profile_updated = UserProfile.objects.get(pk=obj.pk) self.assertEqual(profile_updated.oic, self.superuser)
def test_save_model_supervisor(self): """If supervisor provided, then set supervisor""" mock_form = Mock() mock_form.data = {"supervisor": self.superuser.pk} obj = ProfileFactory() self.assertIsNone(obj.supervisor) self.admin.save_model( self.request, obj, mock_form, None) profile_updated = UserProfile.objects.get(pk=obj.pk) self.assertEqual(profile_updated.supervisor, self.superuser)
def test_status_queries(self): profile = ProfileFactory() profile.status = Profile.STATUS.active profile.save() assert Profile.objects.active().count() == 1 profile.status = Profile.STATUS.inactive profile.save() assert Profile.objects.inactive().count() == 1 profile.status = Profile.STATUS.banned profile.save() assert Profile.objects.banned().count() == 1
def test_username_uniqueness_validation(self): user = UserFactory() profile = ProfileFactory(user=user) new_user_data = UserFactory() data = {"username": new_user_data.username} serializer = ProfileSerializer(instance=profile, data=data, partial=True) assert not serializer.is_valid() assert "username" in serializer.errors
def test_clean_duplicate_email(self): """Duplicate email not allowed if user associated as staff member""" profile = ProfileFactory( partner_staff_member=10, ) self.data["email"] = profile.user.email form = forms.PartnerStaffMemberForm(self.data) self.assertFalse(form.is_valid()) self.assertIn( "This user already exists under a different partnership: {}".format( profile.user.email ), form.errors["__all__"] )
def test_application_post_writes_audit_log(api_client, caplog): profile = ProfileFactory() api_client.credentials(HTTP_AUTHORIZATION=f"Bearer {_create_token(profile)}") data = create_application_data(profile) api_client.post(reverse("application_form:application-list"), data, format="json") audit_event = get_audit_log_event(caplog) assert audit_event is not None, "no audit log entry was written" assert audit_event["actor"] == {"role": "USER", "profile_id": str(profile.pk)} assert audit_event["operation"] == "CREATE" assert audit_event["target"] == { "id": data["application_uuid"], "type": "Application", } assert audit_event["status"] == "SUCCESS"
def test_clean_activate_invalid(self): """If staff member made active, invalid if user already associated with another partner """ profile = ProfileFactory( partner_staff_member=10, ) partner = PartnerFactory() staff = PartnerStaffFactory( partner=partner, email=profile.user.email, active=False ) self.data["email"] = profile.user.email form = forms.PartnerStaffMemberForm(self.data, instance=staff) self.assertFalse(form.is_valid()) self.assertIn( "The Partner Staff member you are trying to activate is associated with a different partnership", form.errors["active"] )
def test_partial_update(self): user = UserFactory() profile = ProfileFactory(user=user) new_profile_data = ProfileFactory.build() new_user_data = UserFactory.build() data = { "description": new_profile_data.description, "last_name": new_user_data.last_name, } serializer = ProfileSerializer(instance=profile, data=data, partial=True) assert serializer.is_valid(), serializer.errors serializer.save() user.refresh_from_db() assert user.last_name == data["last_name"] profile.refresh_from_db() assert profile.description == data["description"]
def test_update(self): user = UserFactory() profile = ProfileFactory(user=user) new_profile_data = ProfileFactory.build() new_user_data = UserFactory.build() data = { "birth_date": new_profile_data.birth_date, "description": new_profile_data.description, "first_name": new_user_data.first_name, "last_name": new_user_data.last_name, "username": new_user_data.username, } serializer = ProfileSerializer(instance=profile, data=data) assert serializer.is_valid(), serializer.errors profile = serializer.save() assert profile.description == data["description"] assert profile.birth_date == data["birth_date"] assert profile.user.last_name == data["last_name"] assert profile.user.first_name == data["first_name"] assert profile.user.username == data["username"]
def profile() -> Profile: return ProfileFactory(id="73aa0891-32a3-42cb-a91f-284777bf1d7f")
def test_create_application_type(application_type): profile = ProfileFactory() data = create_validated_application_data(profile, application_type) application = create_application(data) assert application.type == application_type
def test_save_vendor_number(self): profile = ProfileFactory() profile.vendor_number = "" profile.save() self.assertIsNone(profile.vendor_number)
def test_create_application(num_applicants): profile = ProfileFactory() data = create_validated_application_data(profile, ApplicationType.HASO, num_applicants) application = create_application(data) # A new project should have been created assert Project.objects.count() == 1 project = Project.objects.get() assert project.identifiers.count() == 1 project_identifier = project.identifiers.get() assert project_identifier.schema_type == IdentifierSchemaType.ATT_PROJECT_ES assert project_identifier.identifier == str(data["project_id"]) # A new application should have been created assert application.external_uuid == data["external_uuid"] assert application.applicants_count == num_applicants assert application.applicants.count() == num_applicants assert application.type.value == data["type"].value assert application.right_of_residence == data["right_of_residence"] assert application.has_children == data["has_children"] assert application.profile == profile assert application.apartments.count() == 5 # The application should have linked apartments for each priority number for apartment_data in data["apartments"]: application_apartments = application.apartments.filter( identifiers__identifier=apartment_data["identifier"], identifiers__schema_type=IdentifierSchemaType.ATT_PROJECT_ES, ) assert application_apartments.count() == 1 # The application should always have a primary applicant assert application.applicants.filter( is_primary_applicant=True).count() == 1 applicant1 = application.applicants.get(is_primary_applicant=True) assert applicant1.first_name == profile.user.first_name assert applicant1.last_name == profile.user.last_name assert applicant1.email == profile.user.email assert applicant1.phone_number == profile.phone_number assert applicant1.street_address == profile.street_address assert applicant1.city == profile.city assert applicant1.postal_code == profile.postal_code assert applicant1.age in ( date.today().year - profile.date_of_birth.year, date.today().year - profile.date_of_birth.year - 1, ) # The application may have an additional applicant if num_applicants == 2: assert application.applicants.filter( is_primary_applicant=False).count() == 1 applicant2 = application.applicants.get(is_primary_applicant=False) assert applicant2.first_name == data["additional_applicant"][ "first_name"] assert applicant2.last_name == data["additional_applicant"][ "last_name"] assert applicant2.email == data["additional_applicant"]["email"] assert applicant2.phone_number == data["additional_applicant"][ "phone_number"] assert (applicant2.street_address == data["additional_applicant"] ["street_address"]) assert applicant2.city == data["additional_applicant"]["city"] assert applicant2.postal_code == data["additional_applicant"][ "postal_code"] assert applicant2.age in ( date.today().year - data["additional_applicant"]["date_of_birth"].year, date.today().year - data["additional_applicant"]["date_of_birth"].year - 1, )
def test_application_post_fails_if_not_authenticated(api_client): data = create_application_data(ProfileFactory()) response = api_client.post( reverse("application_form:application-list"), data, format="json" ) assert response.status_code == 401
def test_save_staff_id(self): profile = ProfileFactory() profile.staff_id = "" profile.save() self.assertIsNone(profile.staff_id)
def setUpTestData(cls): cls.country = CountryFactory() cls.profile = ProfileFactory(country=cls.country) cls.profile.countries_available.add(cls.country)
def test_custom_update_user_country_not_found(self): profile = ProfileFactory() res = models.UserProfile.custom_update_user( profile.user, {"businessAreaCode": "404"}, None) self.assertFalse(res)
def other_profile() -> Profile: return ProfileFactory(id="f5cf186a-f9a8-4671-a7a5-1ecbe758071f")
def test_create_application_raises_exception_if_project_data_does_not_exist(): data = create_validated_application_data(ProfileFactory(), ApplicationType.HASO) data["project_id"] = "this-does-not-exist" with pytest.raises(InvalidElasticDataError): create_application(data)