@ -1,17 +1,14 @@
# -*- coding: utf-8 -*-
# Copyright 2016 LasLabs Inc.
# Copyright 2016-2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import mock
from os import strerror
from errno import ENOENT
from contextlib import contextmanager
from odoo.tools.misc import mute_logger
from odoo.tests.common import TransactionCase
_package_path = ' odoo.addons.auth_session_timeout '
class EndTestException ( Exception ) :
""" It stops tests from continuing """
@ -38,31 +35,23 @@ class TestResUsers(TransactionCase):
) as mocks :
yield mocks
def _check_session_validity ( self ) :
""" It wraps ``_check_session_validity `` for easier calling """
def _auth_timeout_check ( self , http_mock ) :
""" It wraps ``_auth_timeout_check `` for easier calling """
self . db = mock . MagicMock ( )
self . uid = mock . MagicMock ( )
self . passwd = mock . MagicMock ( )
return self . ResUsers . _check_session_validity (
self . db , self . uid , self . passwd ,
)
self . path = ' /this/is/a/test/path '
get_filename = http_mock . root . session_store . get_session_filename
get_filename . return_value = self . path
return self . ResUsers . _auth_timeout_check ( )
def test_session_validity_no_request ( self ) :
""" It should return immediately if no request """
with self . _mock_assets ( ) as assets :
assets [ ' http ' ] . request = False
res = self . _check_session_validity ( )
res = self . _auth_timeout_check ( assets [ ' http ' ] )
self . assertFalse ( res )
def test_session_validity_gets_params ( self ) :
""" It should call ``get_session_parameters`` with db """
with self . _mock_assets ( ) as assets :
get_params = assets [ ' http ' ] . request . env [ ' ' ] . get_session_parameters
get_params . side_effect = EndTestException
with self . assertRaises ( EndTestException ) :
self . _check_session_validity ( )
get_params . assert_called_once_with ( )
def test_session_validity_gets_session_file ( self ) :
""" It should call get the session file for the session id """
with self . _mock_assets ( ) as assets :
@ -71,7 +60,7 @@ class TestResUsers(TransactionCase):
store = assets [ ' http ' ] . root . session_store
store . get_session_filename . side_effect = EndTestException
with self . assertRaises ( EndTestException ) :
self . _check_session_validity ( )
self . _auth_timeout_check ( assets [ ' http ' ] )
store . get_session_filename . assert_called_once_with (
assets [ ' http ' ] . request . session . sid ,
)
@ -82,7 +71,7 @@ class TestResUsers(TransactionCase):
get_params = assets [ ' http ' ] . request . env [ ' ' ] . get_session_parameters
get_params . return_value = - 9999 , [ ]
assets [ ' getmtime ' ] . return_value = 0
self . _check_session_validity ( )
self . _auth_timeout_check ( assets [ ' http ' ] )
assets [ ' http ' ] . request . session . logout . assert_called_once_with (
keep_db = True ,
)
@ -92,44 +81,28 @@ class TestResUsers(TransactionCase):
with self . _mock_assets ( [ ' http ' , ' getmtime ' , ' utime ' ] ) as assets :
get_params = assets [ ' http ' ] . request . env [ ' ' ] . get_session_parameters
get_params . return_value = 9999 , [ ]
self . _check_session_validity ( )
self . _auth_timeout_check ( assets [ ' http ' ] )
assets [ ' utime ' ] . assert_called_once_with (
assets [ ' http ' ] . root . session_store . get_session_filename ( ) ,
None ,
)
@mute_logger ( ' odoo.addons.auth_session_timeout.models.res_users ' )
def test_session_validity_os_error_guard ( self ) :
""" It should properly guard from OSError & return """
with self . _mock_assets ( [ ' http ' , ' utime ' , ' getmtime ' ] ) as assets :
get_params = assets [ ' http ' ] . request . env [ ' ' ] . get_session_parameters
get_params . return_value = 0 , [ ]
assets [ ' getmtime ' ] . side_effect = OSError
res = self . _check_session_validity ( )
res = self . _auth_timeout_check ( assets [ ' http ' ] )
self . assertFalse ( res )
@mock.patch ( _package_path + ' .models.res_users.request ' )
@mock.patch ( _package_path + ' .models.res_users.root ' )
@mock.patch ( _package_path + ' .models.res_users.getmtime ' )
def test_on_timeout_session_loggedout ( self , mock_getmtime ,
mock_root , mock_request ) :
mock_getmtime . return_value = 0
mock_request . session . uid = self . env . uid
mock_request . session . dbname = self . env . cr . dbname
mock_request . session . sid = 123
mock_request . session . logout = mock . Mock ( )
self . resUsers . _auth_timeout_check ( )
self . assertTrue ( mock_request . session . logout . called )
@mock.patch ( _package_path + ' .models.res_users.request ' )
@mock.patch ( _package_path + ' .models.res_users.root ' )
@mock.patch ( _package_path + ' .models.res_users.getmtime ' )
@mock.patch ( _package_path + ' .models.res_users.utime ' )
def test_sessionfile_io_exceptions_managed ( self , mock_utime , mock_getmtime ,
mock_root , mock_request ) :
mock_getmtime . side_effect = OSError (
ENOENT , strerror ( ENOENT ) , ' non-existent-filename ' )
mock_request . session . uid = self . env . uid
mock_request . session . dbname = self . env . cr . dbname
mock_request . session . sid = 123
self . resUsers . _auth_timeout_check ( )
def test_on_timeout_session_loggedout ( self ) :
with self . _mock_assets ( [ ' http ' , ' getmtime ' ] ) as assets :
assets [ ' getmtime ' ] . return_value = 0
assets [ ' http ' ] . request . session . uid = self . env . uid
assets [ ' http ' ] . request . session . dbname = self . env . cr . dbname
assets [ ' http ' ] . request . session . sid = 123
assets [ ' http ' ] . request . session . logout = mock . Mock ( )
self . ResUsers . _auth_timeout_check ( )
self . assertTrue ( assets [ ' http ' ] . request . session . logout . called )