server/tests: fix pylint score
This commit is contained in:
parent
5f29fa12c2
commit
c46dc08c1b
5 changed files with 39 additions and 55 deletions
0
server/szurubooru/tests/api/__init__.py
Normal file
0
server/szurubooru/tests/api/__init__.py
Normal file
|
@ -1,24 +1,18 @@
|
|||
from datetime import datetime
|
||||
from unittest import mock
|
||||
from szurubooru import api, db, errors, config
|
||||
from szurubooru.util import auth, misc, mailer
|
||||
from szurubooru import api, errors
|
||||
from szurubooru.util import auth, mailer
|
||||
from szurubooru.tests.database_test_case import DatabaseTestCase
|
||||
from szurubooru.tests.api import util
|
||||
|
||||
class TestPasswordReset(DatabaseTestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
config_mock = {
|
||||
util.mock_config({
|
||||
'secret': 'x',
|
||||
'base_url': 'http://example.com/',
|
||||
'name': 'Test instance',
|
||||
}
|
||||
self.old_config = config.config
|
||||
config.config = config_mock
|
||||
self.context = misc.dotdict()
|
||||
self.context.session = self.session
|
||||
self.context.request = {}
|
||||
self.context.user = db.User()
|
||||
})
|
||||
util.mock_context(self)
|
||||
self.api = api.PasswordResetApi()
|
||||
|
||||
def test_reset_non_existing(self):
|
||||
|
@ -35,17 +29,16 @@ class TestPasswordReset(DatabaseTestCase):
|
|||
user.email = 'user@example.com'
|
||||
self.session.add(user)
|
||||
for getter in ['u1', 'user@example.com']:
|
||||
with mock.MagicMock() as mock_method:
|
||||
mailer.send_mail = mock_method
|
||||
self.assertEqual({}, self.api.get(self.context, getter))
|
||||
mock_method.assert_called_once_with(
|
||||
'noreply@Test instance',
|
||||
'user@example.com',
|
||||
'Password reset for Test instance',
|
||||
'You (or someone else) requested to reset your password ' +
|
||||
'on Test instance.\nIf you wish to proceed, click this l' +
|
||||
'ink: http://example.com/password-reset/u1:4ac0be176fb36' +
|
||||
'4f13ee6b634c43220e2\nOtherwise, please ignore this email.')
|
||||
mailer.send_mail = mock.MagicMock()
|
||||
self.assertEqual({}, self.api.get(self.context, getter))
|
||||
mailer.send_mail.assert_called_once_with(
|
||||
'noreply@Test instance',
|
||||
'user@example.com',
|
||||
'Password reset for Test instance',
|
||||
'You (or someone else) requested to reset your password ' +
|
||||
'on Test instance.\nIf you wish to proceed, click this l' +
|
||||
'ink: http://example.com/password-reset/u1:4ac0be176fb36' +
|
||||
'4f13ee6b634c43220e2\nOtherwise, please ignore this email.')
|
||||
|
||||
def test_confirmation_non_existing(self):
|
||||
self.assertRaises(errors.NotFoundError, self.api.post, self.context, 'u1')
|
||||
|
|
|
@ -1,26 +1,21 @@
|
|||
from datetime import datetime
|
||||
from szurubooru import api, db, errors, config
|
||||
from szurubooru.util import auth, misc
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.util import auth
|
||||
from szurubooru.tests.database_test_case import DatabaseTestCase
|
||||
from szurubooru.tests.api import util
|
||||
|
||||
class TestRetrievingUsers(DatabaseTestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
config_mock = {
|
||||
util.mock_config({
|
||||
'privileges': {
|
||||
'users:list': 'regular_user',
|
||||
'users:view': 'regular_user',
|
||||
'users:create': 'regular_user',
|
||||
},
|
||||
'ranks': ['anonymous', 'regular_user', 'mod', 'admin'],
|
||||
}
|
||||
self.old_config = config.config
|
||||
config.config = config_mock
|
||||
self.context = misc.dotdict()
|
||||
self.context.session = self.session
|
||||
self.context.request = {}
|
||||
self.context.user = db.User()
|
||||
})
|
||||
util.mock_context(self)
|
||||
|
||||
def test_retrieving_multiple(self):
|
||||
user1 = util.mock_user('u1', 'mod')
|
||||
|
@ -71,7 +66,7 @@ class TestRetrievingUsers(DatabaseTestCase):
|
|||
class TestCreatingUser(DatabaseTestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
config_mock = {
|
||||
util.mock_config({
|
||||
'secret': '',
|
||||
'user_name_regex': '.{3,}',
|
||||
'password_regex': '.{3,}',
|
||||
|
@ -80,19 +75,11 @@ class TestCreatingUser(DatabaseTestCase):
|
|||
'privileges': {
|
||||
'users:create': 'anonymous',
|
||||
},
|
||||
}
|
||||
self.old_config = config.config
|
||||
config.config = config_mock
|
||||
})
|
||||
self.api = api.UserListApi()
|
||||
self.context = misc.dotdict()
|
||||
self.context.session = self.session
|
||||
self.context.request = {}
|
||||
self.context.user = db.User()
|
||||
util.mock_context(self)
|
||||
self.context.user.rank = 'anonymous'
|
||||
|
||||
def tearDown(self):
|
||||
config.config = self.old_config
|
||||
|
||||
def test_creating_valid_user(self):
|
||||
self.context.request = {
|
||||
'name': 'chewie',
|
||||
|
@ -139,7 +126,7 @@ class TestCreatingUser(DatabaseTestCase):
|
|||
class TestUpdatingUser(DatabaseTestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
config_mock = {
|
||||
util.mock_config({
|
||||
'secret': '',
|
||||
'user_name_regex': '.{3,}',
|
||||
'password_regex': '.{3,}',
|
||||
|
@ -155,16 +142,9 @@ class TestUpdatingUser(DatabaseTestCase):
|
|||
'users:edit:any:email': 'mod',
|
||||
'users:edit:any:rank': 'admin',
|
||||
},
|
||||
}
|
||||
self.old_config = config.config
|
||||
config.config = config_mock
|
||||
})
|
||||
util.mock_context(self)
|
||||
self.api = api.UserDetailApi()
|
||||
self.context = misc.dotdict()
|
||||
self.context.session = self.session
|
||||
self.context.request = {}
|
||||
|
||||
def tearDown(self):
|
||||
config.config = self.old_config
|
||||
|
||||
def test_update_changing_nothing(self):
|
||||
admin_user = util.mock_user('u1', 'admin')
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
from datetime import datetime
|
||||
from szurubooru import db
|
||||
from szurubooru import config, db
|
||||
from szurubooru.util import misc
|
||||
|
||||
def mock_config(config_mock):
|
||||
config.config = config_mock
|
||||
|
||||
def mock_user(name, rank='admin'):
|
||||
user = db.User()
|
||||
|
@ -13,6 +17,13 @@ def mock_user(name, rank='admin'):
|
|||
user.avatar_style = db.User.AVATAR_GRAVATAR
|
||||
return user
|
||||
|
||||
def mock_context(parent):
|
||||
context = misc.dotdict()
|
||||
context.session = parent.session
|
||||
context.request = {}
|
||||
context.user = db.User()
|
||||
parent.context = context
|
||||
|
||||
def mock_params(context, params):
|
||||
def get_param_as_string(key, default=None):
|
||||
if key not in params:
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from datetime import datetime
|
||||
from szurubooru import db, errors, search
|
||||
from szurubooru import errors, search
|
||||
from szurubooru.tests.database_test_case import DatabaseTestCase
|
||||
from szurubooru.tests.api import util
|
||||
|
||||
|
|
Loading…
Reference in a new issue