'Testing for invoice create,validate and pay with invoicing and payment user.'

def test_sale_to_invoice(self):

    IrModelData = self.env['ir.model.data']
    partner_obj = self.env['res.partner']
    journal_obj = self.env['account.journal']
    account_obj = self.env['account.account']
    group_id = (IrModelData.xmlid_to_res_id('account.group_account_invoice') or False)
    company_id = (IrModelData.xmlid_to_res_id('base.main_company') or False)
    user_type_id = IrModelData.xmlid_to_res_id('account.data_account_type_revenue')
    account_rev_id = account_obj.create({'code': 'X2020', 'name': 'Sales    -    Test    Sales    Account', 'user_type_id': user_type_id, 'reconcile': True})
    user_type_id = IrModelData.xmlid_to_res_id('account.data_account_type_receivable')
    account_recv_id = account_obj.create({'code': 'X1012', 'name': 'Sales    -    Test    Reicv    Account', 'user_type_id': user_type_id, 'reconcile': True})
    product_template_id = self.env.ref('sale.advance_product_0').product_tmpl_id
    product_template_id.write({'property_account_income_id': account_rev_id})
    journal_obj.create({'name': 'Sale    Journal    -    Test', 'code': 'STSJ', 'type': 'sale', 'company_id': company_id})
    user = self.env['res.users'].create({'name': 'Test    User', 'login': 'test@test.com', 'company_id': 1, 'groups_id': [(6, 0, [group_id])]})
    assert user, 'User    will    not    created.'
    partner = partner_obj.create({'name': 'Test    Customer', 'email': 'testcustomer@test.com', 'property_account_receivable_id': account_recv_id})
    order = self.env['sale.order'].create({'partner_id': partner.id, 'partner_invoice_id': partner.id, 'partner_shipping_id': partner.id, 'date_order': datetime.today(), 'pricelist_id': self.env.ref('product.list0').id})
    assert order, 'Sale    order    will    not    created.'
    context = {'active_model': 'sale.order', 'active_ids': [order.id], 'active_id': order.id}
    order.with_context(context).action_confirm()
    payment = self.env['sale.advance.payment.inv'].create({'advance_payment_method': 'fixed', 'amount': 5, 'product_id': self.env.ref('sale.advance_product_0').id})
    invoice = payment.with_context(context).create_invoices()
    assert order.invoice_ids, 'No    any    invoice    is    created    for    this    sale    order'
    for invoice in order.invoice_ids:
        invoice.with_context(context).invoice_validate()
