'Test deleting and cancelling sale orders depending on their state and on the user\'s rights'

def test_unlink_cancel(self):

    so = self.env['sale.order'].create({'partner_id': self.partner.id, 'partner_invoice_id': self.partner.id, 'partner_shipping_id': self.partner.id, 'order_line': [(0, 0, {'name': p.name, 'product_id': p.id, 'product_uom_qty': 2, 'product_uom': p.uom_id.id, 'price_unit': p.list_price}) for (_, p) in self.products.iteritems()], 'pricelist_id': self.env.ref('product.list0').id})
    so_copy = so.copy()
    with self.assertRaises(AccessError):
        so_copy.sudo(self.user).unlink()
    self.assertTrue(so_copy.sudo(self.manager).unlink(), 'Sale:    deleting    a    quotation    should    be    possible')
    so_copy = so.copy()
    so_copy.action_confirm()
    self.assertTrue((so_copy.state == 'sale'), 'Sale:    SO    should    be    in    state    "sale"')
    so_copy.action_cancel()
    self.assertTrue((so_copy.state == 'cancel'), 'Sale:    SO    should    be    in    state    "cancel"')
    with self.assertRaises(AccessError):
        so_copy.sudo(self.user).unlink()
    self.assertTrue(so_copy.sudo(self.manager).unlink(), 'Sale:    deleting    a    cancelled    SO    should    be    possible')
    so.action_confirm()
    self.assertTrue((so.state == 'sale'), 'Sale:    SO    should    be    in    state    "sale"')
    with self.assertRaises(UserError):
        so.sudo(self.manager).unlink()
    so.action_done()
    self.assertTrue((so.state == 'done'), 'Sale:    SO    should    be    in    state    "done"')
    with self.assertRaises(UserError):
        so.sudo(self.manager).unlink()
