Skip to content

Commit 1004712

Browse files
committed
Move APIUser test helper functs to conftest
... and implement as shared pytest fixtures.
1 parent e6d7e9a commit 1004712

2 files changed

Lines changed: 65 additions & 50 deletions

File tree

django/sierra/api/tests/test_models.py

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,8 @@
2222
# External fixtures used below can be found in
2323
# django/sierra/conftest.py:
2424
# apiuser_with_custom_defaults
25-
#
26-
27-
28-
def calculate_expected_apiuser_details(test_cls, new, start=None):
29-
"""
30-
Calculate and return the expected values for an APIUser instance.
31-
32-
This is a utility function used in a few of the tests. It
33-
determines the expected final attributes for an APIUser for tests
34-
that test create/update operations. `test_cls` is the APIUser test
35-
class that test uses. `new` is a dict containing data attributes
36-
for the new/updated APIUser. And `start` is a dict containing data
37-
attributes for the existing user that `new` is updating, if you're
38-
testing an update operation. Expected values are returned as a
39-
dict.
40-
"""
41-
exp, start = {}, (start or {})
42-
exp['permissions_dict'] = test_cls.permission_defaults.copy()
43-
exp['permissions_dict'].update(start.get('permissions_dict', None) or {})
44-
exp['permissions_dict'].update(new.get('permissions_dict', None) or {})
45-
exp['permissions'] = ujson.encode(exp['permissions_dict'])
46-
47-
key_fields = ('username', 'secret_text', 'password', 'email', 'first_name',
48-
'last_name')
49-
for k in key_fields:
50-
new_val = new.get(k, None)
51-
default = start.get(k, None) or ''
52-
exp[k] = new_val if new_val is not None else default
53-
54-
exp['secret'] = test_cls.encode_secret(exp['secret_text'])
55-
return exp
56-
57-
58-
def assert_apiuser_matches_expected_data(apiuser, expected):
59-
"""
60-
Assert that an `apiuser` obj matches `expected` data.
61-
"""
62-
assert apiuser.secret == expected['secret']
63-
assert apiuser.permissions == expected['permissions']
64-
assert authenticate(username=expected['username'],
65-
password=expected['password'])
66-
assert apiuser.user.email == expected['email']
67-
assert apiuser.user.first_name == expected['first_name']
68-
assert apiuser.user.last_name == expected['last_name']
69-
25+
# calculate_expected_apiuser_details
26+
# assert_apiuser_matches_expected_data
7027

7128
# TESTS
7229
# ---------------------------------------------------------------------
@@ -374,9 +331,10 @@ def test_apiuser_creation_permission_errors(apiuser_with_custom_defaults,
374331
'only new first_name value',
375332
'provided data is all blank strings'
376333
])
377-
def test_apiuser_updateandsave_works_correctly(apiuser_with_custom_defaults,
378-
secret_text, permissions_dict,
379-
pw, email, fn, ln):
334+
def test_apiuser_updateandsave_works(apiuser_with_custom_defaults,
335+
calculate_expected_apiuser_details,
336+
secret_text, permissions_dict, pw, email,
337+
fn, ln):
380338
"""
381339
The APIUser `update_and_save` method should update the APIUser
382340
and/or associated user based on the provided details. Missing
@@ -729,8 +687,10 @@ def test_apiuser_setallpermissionstovalue_errors(apiuser_with_custom_defaults):
729687
'some exist, update some, create some; no errors',
730688
'some exist, update one, create some; creation and update errors',
731689
])
732-
def test_apiusermgr_batchimport_works(apiuser_with_custom_defaults, fields,
733-
start_vals, new_vals, err_vals):
690+
def test_apiusermgr_batchimport_works(apiuser_with_custom_defaults,
691+
calculate_expected_apiuser_details,
692+
assert_apiuser_matches_expected_data,
693+
fields, start_vals, new_vals, err_vals):
734694
"""
735695
The APIUser manager's `batch_import_users` method should: create
736696
new Users/APIUsers for ones that don't already exist; update

django/sierra/conftest.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import hashlib
1111
import urllib
1212
import random
13+
import ujson
1314
from datetime import datetime
1415
from collections import OrderedDict
1516

1617
from django.conf import settings
18+
from django.contrib.auth import authenticate
1719
from django.contrib.auth.models import User
1820
from django.db.models import ObjectDoesNotExist
1921
from rest_framework import test as drftest
@@ -845,3 +847,56 @@ def _get_found_ids(solr_id_field, response):
845847
assert len(data) == total_found
846848
return [r[api_id_field] for r in data]
847849
return _get_found_ids
850+
851+
852+
@pytest.fixture(scope='function')
853+
def calculate_expected_apiuser_details():
854+
"""
855+
Pytest fixture. Returns a utility function that calculates and
856+
returns the expected values for an APIUser instance.
857+
858+
This is a utility function used in a few of the tests. It
859+
determines the expected final attributes for an APIUser for tests
860+
that test create/update operations. `test_cls` is the APIUser test
861+
class that test uses. `new` is a dict containing data attributes
862+
for the new/updated APIUser. And `start` is a dict containing data
863+
attributes for the existing user that `new` is updating, if you're
864+
testing an update operation. Expected values are returned as a
865+
dict.
866+
"""
867+
def _calculate_expected_apiuser_details(test_cls, new, start=None):
868+
exp, start = {}, (start or {})
869+
exp['permissions_dict'] = test_cls.permission_defaults.copy()
870+
exp['permissions_dict'].update(start.get('permissions_dict', None) or
871+
{})
872+
exp['permissions_dict'].update(new.get('permissions_dict', None) or
873+
{})
874+
exp['permissions'] = ujson.encode(exp['permissions_dict'])
875+
876+
key_fields = ('username', 'secret_text', 'password', 'email',
877+
'first_name', 'last_name')
878+
for k in key_fields:
879+
new_val = new.get(k, None)
880+
default = start.get(k, None) or ''
881+
exp[k] = new_val if new_val is not None else default
882+
883+
exp['secret'] = test_cls.encode_secret(exp['secret_text'])
884+
return exp
885+
return _calculate_expected_apiuser_details
886+
887+
888+
@pytest.fixture(scope='function')
889+
def assert_apiuser_matches_expected_data():
890+
"""
891+
Pytest fixture. Returns a function that runs through several
892+
assertions to assert that an `apiuser` obj matches `expected` data.
893+
"""
894+
def _assert_apiuser_matches_expected_data(apiuser, expected):
895+
assert apiuser.secret == expected['secret']
896+
assert apiuser.permissions == expected['permissions']
897+
assert authenticate(username=expected['username'],
898+
password=expected['password'])
899+
assert apiuser.user.email == expected['email']
900+
assert apiuser.user.first_name == expected['first_name']
901+
assert apiuser.user.last_name == expected['last_name']
902+
return _assert_apiuser_matches_expected_data

0 commit comments

Comments
 (0)