def test_contact_not_from_company(self):
        """
        Test that if the contact specified in data doesn't works
        at the company specified in data, the validation fails.
        """
        serializer = mock.Mock()
        company = serializer.instance.company
        new_contact = mock.Mock()  # doesn't work at `company`

        validator = ContactWorksAtCompanyValidator()
        validator.set_context(serializer)

        with pytest.raises(ValidationError):
            validator({
                'contact': new_contact,
                'company': company,
            })
    def test_contact_from_company(self):
        """
        Test that if the contact specified in data works
        at the company specified in data, the validation passes.
        """
        serializer = mock.Mock()
        company = serializer.instance.company
        new_contact = mock.Mock(company=company)

        validator = ContactWorksAtCompanyValidator()
        validator.set_context(serializer)

        try:
            validator({
                'contact': new_contact,
                'company': company,
            })
        except Exception:
            pytest.fail('Should not raise a validator error.')
    def test_with_different_field_names(self):
        """
        Test that the validation passes when using different field names.
        """
        serializer = mock.Mock()
        company = serializer.instance.company
        new_main_contact = mock.Mock(company=company)

        validator = ContactWorksAtCompanyValidator(
            contact_field='main_contact',
            company_field='main_company',
        )
        validator.set_context(serializer)

        try:
            validator({
                'main_contact': new_main_contact,
                'main_company': company,
            })
        except Exception:
            pytest.fail('Should not raise a validator error.')
Ejemplo n.º 4
0
 class Meta:
     model = Order
     fields = (
         'id',
         'reference',
         'status',
         'created_on',
         'created_by',
         'modified_on',
         'modified_by',
         'company',
         'contact',
         'primary_market',
         'sector',
         'uk_region',
         'service_types',
         'description',
         'contacts_not_to_approach',
         'contact_email',
         'contact_phone',
         'product_info',
         'further_info',
         'existing_agents',
         'permission_to_approach_contacts',
         'delivery_date',
         'po_number',
         'discount_value',
         'vat_status',
         'vat_number',
         'vat_verified',
         'net_cost',
         'subtotal_cost',
         'vat_cost',
         'total_cost',
         'billing_company_name',
         'billing_contact_name',
         'billing_email',
         'billing_phone',
         'billing_address_1',
         'billing_address_2',
         'billing_address_town',
         'billing_address_county',
         'billing_address_postcode',
         'billing_address_country',
         'archived_documents_url_path',
         'paid_on',
         'completed_by',
         'completed_on',
         'cancelled_by',
         'cancelled_on',
         'cancellation_reason',
     )
     read_only_fields = (
         'id',
         'reference',
         'status',
         'created_on',
         'created_by',
         'modified_on',
         'modified_by',
         'contact_email',
         'contact_phone',
         'product_info',
         'permission_to_approach_contacts',
         'discount_value',
         'net_cost',
         'subtotal_cost',
         'vat_cost',
         'total_cost',
         'archived_documents_url_path',
         'paid_on',
         'completed_by',
         'completed_on',
         'cancelled_by',
         'cancelled_on',
         'cancellation_reason',
         'billing_company_name',
         'billing_contact_name',
         'billing_email',
         'billing_phone',
     )
     validators = (
         # only some of the fields can be changed depending of the status
         OrderEditableFieldsValidator(
             {
                 OrderStatus.draft: {
                     *ORDER_FIELDS_INVOICE_RELATED,
                     'description',
                     'service_types',
                     'sector',
                     'uk_region',
                     'contacts_not_to_approach',
                     'contact',
                     'existing_agents',
                     'further_info',
                     'delivery_date',
                 },
                 OrderStatus.quote_awaiting_acceptance: {
                     *ORDER_FIELDS_INVOICE_RELATED,
                     'contact',
                 },
                 OrderStatus.quote_accepted: {
                     *ORDER_FIELDS_INVOICE_RELATED,
                     'contact',
                 },
                 OrderStatus.paid: {'contact'},
                 OrderStatus.complete: {},  # nothing can be changed
                 OrderStatus.cancelled: {},  # nothing can be changed
             }, ),
         # contact has to work at company
         ContactWorksAtCompanyValidator(),
         # validate billing address if edited
         AddressValidator(
             lazy=True,
             fields_mapping={
                 'billing_address_1': {
                     'required': True
                 },
                 'billing_address_2': {
                     'required': False
                 },
                 'billing_address_town': {
                     'required': True
                 },
                 'billing_address_county': {
                     'required': False
                 },
                 'billing_address_postcode': {
                     'required': False
                 },
                 'billing_address_country': {
                     'required': True
                 },
             },
         ),
     )