| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
from contextlib import closing |
| 4 |
|
| 5 |
import odoorpc |
| 6 |
from odoorpc.error import InternalError |
| 7 |
from odoorpc.models import Model |
| 8 |
from odoorpc.tests import BaseTestCase |
| 9 |
from odoorpc.tools import v |
| 10 |
|
| 11 |
|
| 12 |
class TestLogin(BaseTestCase): |
| 13 |
def test_login(self): |
| 14 |
odoo = odoorpc.ODOO( |
| 15 |
self.env['host'], |
| 16 |
protocol=self.env['protocol'], |
| 17 |
port=self.env['port'], |
| 18 |
version=self.env['version'], |
| 19 |
) |
| 20 |
odoo.login(self.env['db'], self.env['user'], self.env['pwd']) |
| 21 |
self.assertIsNotNone(odoo.env) |
| 22 |
self.assertIsInstance(odoo.env.user, Model) |
| 23 |
self.assertIn('res.users', odoo.env.registry) |
| 24 |
self.assertEqual(odoo.env.db, self.env['db']) |
| 25 |
|
| 26 |
def test_login_with_api_key(self): |
| 27 |
if v(self.odoo.version)[0] < 14: |
| 28 |
self.skipTest("API Keys are supported starting from Odoo >= 14") |
| 29 |
odoo = odoorpc.ODOO( |
| 30 |
self.env['host'], |
| 31 |
protocol=self.env['protocol'], |
| 32 |
port=self.env['port'], |
| 33 |
version=self.env['version'], |
| 34 |
) |
| 35 |
odoo.login(self.env['db'], self.env['user'], self.env['pwd']) |
| 36 |
odoo.json( |
| 37 |
# Required to use '/web/dataset/call_button' below |
| 38 |
"/web/session/authenticate", |
| 39 |
{ |
| 40 |
"db": self.env['db'], |
| 41 |
"login": self.env['user'], |
| 42 |
"password": self.env['pwd'], |
| 43 |
}, |
| 44 |
) |
| 45 |
# Generate an API key |
| 46 |
KeyGenerator = odoo.env["res.users.apikeys.description"] |
| 47 |
key_generator_id = KeyGenerator.create({"name": "TEST"}) |
| 48 |
data = odoo.json( |
| 49 |
# NOTE Workaround: act like the Odoo web client. |
| 50 |
# Use of '/web/dataset/call_button' instead of '/jsonrpc' to bypass |
| 51 |
# the 'check_identity' check for Odoo >= 16.0. |
| 52 |
"/web/dataset/call_button", |
| 53 |
params={ |
| 54 |
"args": [[key_generator_id]], |
| 55 |
"kwargs": {"context": odoo.env.context}, |
| 56 |
"method": "make_key", |
| 57 |
"model": "res.users.apikeys.description", |
| 58 |
}, |
| 59 |
) |
| 60 |
action = data["result"] |
| 61 |
IdentityCheck = odoo.env["res.users.identitycheck"] |
| 62 |
if action.get("res_model") == IdentityCheck._name: |
| 63 |
check = IdentityCheck.browse(action["res_id"]) |
| 64 |
check.password = self.env["pwd"] |
| 65 |
data = odoo.json( |
| 66 |
# NOTE Workaround: act like the Odoo web client. |
| 67 |
# Use of '/web/dataset/call_button' instead of '/jsonrpc' to bypass |
| 68 |
# the 'check_identity' check for Odoo >= 16.0. |
| 69 |
"/web/dataset/call_button", |
| 70 |
params={ |
| 71 |
"args": [[check.id]], |
| 72 |
"kwargs": {"context": odoo.env.context}, |
| 73 |
"method": "run_check", |
| 74 |
"model": check._name, |
| 75 |
}, |
| 76 |
) |
| 77 |
action = data["result"] |
| 78 |
self.assertEqual(action["res_model"], "res.users.apikeys.show") |
| 79 |
api_key = action["context"]["default_key"] |
| 80 |
# Login with the API key |
| 81 |
odoo.login(self.env['db'], self.env['user'], api_key) |
| 82 |
self.assertIsNotNone(odoo.env) |
| 83 |
self.assertIsInstance(odoo.env.user, Model) |
| 84 |
self.assertIn('res.users', odoo.env.registry) |
| 85 |
self.assertEqual(odoo.env.db, self.env['db']) |
| 86 |
|
| 87 |
def test_login_no_password(self): |
| 88 |
# login no password => Error |
| 89 |
odoo = odoorpc.ODOO( |
| 90 |
self.env['host'], |
| 91 |
protocol=self.env['protocol'], |
| 92 |
port=self.env['port'], |
| 93 |
version=self.env['version'], |
| 94 |
) |
| 95 |
self.assertRaises( |
| 96 |
odoorpc.error.Error, |
| 97 |
odoo.login, |
| 98 |
self.env['db'], |
| 99 |
self.env['user'], |
| 100 |
False, |
| 101 |
) |
| 102 |
|
| 103 |
def test_logout(self): |
| 104 |
odoo = odoorpc.ODOO( |
| 105 |
self.env['host'], |
| 106 |
protocol=self.env['protocol'], |
| 107 |
port=self.env['port'], |
| 108 |
version=self.env['version'], |
| 109 |
) |
| 110 |
odoo.login(self.env['db'], self.env['user'], self.env['pwd']) |
| 111 |
success = odoo.logout() |
| 112 |
self.assertTrue(success) |
| 113 |
|
| 114 |
def test_logout_without_login(self): |
| 115 |
odoo = odoorpc.ODOO( |
| 116 |
self.env['host'], |
| 117 |
protocol=self.env['protocol'], |
| 118 |
port=self.env['port'], |
| 119 |
version=self.env['version'], |
| 120 |
) |
| 121 |
success = odoo.logout() |
| 122 |
self.assertFalse(success) |
| 123 |
|
| 124 |
def test_logout_closing(self): |
| 125 |
odoo = odoorpc.ODOO( |
| 126 |
self.env['host'], |
| 127 |
protocol=self.env['protocol'], |
| 128 |
port=self.env['port'], |
| 129 |
version=self.env['version'], |
| 130 |
) |
| 131 |
odoo.login(self.env['db'], self.env['user'], self.env['pwd']) |
| 132 |
with closing(odoo): |
| 133 |
odoo._check_logged_user() |
| 134 |
self.assertRaises(InternalError, odoo._check_logged_user) |