From 4cdc9b5cbbddc3b497fac36ff6c6237b27cae85b Mon Sep 17 00:00:00 2001 From: chdorner Date: Mon, 20 Mar 2017 12:45:34 +0100 Subject: [PATCH 1/2] Refactor WorldGroup tests into class tests --- tests/h/groups/util_test.py | 40 +++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/tests/h/groups/util_test.py b/tests/h/groups/util_test.py index b7eb760108c..c9c93a6479a 100644 --- a/tests/h/groups/util_test.py +++ b/tests/h/groups/util_test.py @@ -2,34 +2,30 @@ from __future__ import unicode_literals +import pytest + from pyramid import security from h.groups import util -def test_world_group_acl(): - group = util.WorldGroup('example.com') - - assert group.__acl__() == [ - (security.Allow, security.Everyone, 'read'), - (security.Allow, 'authority:example.com', 'write'), - security.DENY_ALL, - ] - - -def test_world_group_name(): - group = util.WorldGroup('example.com') - - assert group.name == 'Public' - - -def test_world_group_pubid(): - group = util.WorldGroup('example.com') +class TestWorldGroup(object): + def test_acl(self, group): + assert group.__acl__() == [ + (security.Allow, security.Everyone, 'read'), + (security.Allow, 'authority:example.com', 'write'), + security.DENY_ALL, + ] - assert group.pubid == '__world__' + def test_name(self, group): + assert group.name == 'Public' + def test_pubid(self, group): + assert group.pubid == '__world__' -def test_world_group_is_public(): - group = util.WorldGroup('example.com') + def test_is_public(self, group): + assert group.is_public - assert group.is_public + @pytest.fixture + def group(self): + return util.WorldGroup('example.com') From d8e627f04f3083cae9084793d3e2157831053bf5 Mon Sep 17 00:00:00 2001 From: chdorner Date: Mon, 20 Mar 2017 13:20:26 +0100 Subject: [PATCH 2/2] Refactor session tests into test classes --- tests/h/session_test.py | 308 ++++++++++++++++++---------------------- 1 file changed, 140 insertions(+), 168 deletions(-) diff --git a/tests/h/session_test.py b/tests/h/session_test.py index 440f3c249af..9a4278642c5 100644 --- a/tests/h/session_test.py +++ b/tests/h/session_test.py @@ -12,223 +12,213 @@ def __init__(self, pubid, name, is_public=False): self.is_public = is_public -def test_model_sorts_groups(authenticated_request): - authenticated_request.set_groups([ - FakeGroup('c', 'Group A'), - FakeGroup('b', 'Group B'), - FakeGroup('a', 'Group B'), - ]) - session_model = session.model(authenticated_request) +class TestModel(object): + def test_sorts_groups(self, authenticated_request): + authenticated_request.set_groups([ + FakeGroup('c', 'Group A'), + FakeGroup('b', 'Group B'), + FakeGroup('a', 'Group B'), + ]) + session_model = session.model(authenticated_request) - ids = [group['id'] for group in session_model['groups']] + ids = [group['id'] for group in session_model['groups']] - assert ids == ['__world__', 'c', 'a', 'b'] + assert ids == ['__world__', 'c', 'a', 'b'] + def test_world_group_is_public(self, authenticated_request): + model = session.model(authenticated_request) + world_group = [g for g in model['groups'] if g['id'] == '__world__'][0] -def test_world_group_is_public_in_model(authenticated_request): - model = session.model(authenticated_request) - world_group = [g for g in model['groups'] if g['id'] == '__world__'][0] + assert world_group['public'] is True - assert world_group['public'] is True + def test_private_group_is_not_public(self, authenticated_request): + authenticated_request.set_groups([FakeGroup('a', 'Group A')]) + model = session.model(authenticated_request) + private_group = [g for g in model['groups'] if g['id'] == 'a'][0] -def test_private_group_is_not_public_in_model(authenticated_request): - authenticated_request.set_groups([FakeGroup('a', 'Group A')]) + assert private_group['public'] is False - model = session.model(authenticated_request) - private_group = [g for g in model['groups'] if g['id'] == 'a'][0] + def test_world_group_has_no_url(self, authenticated_request): + model = session.model(authenticated_request) + world_group = [g for g in model['groups'] if g['id'] == '__world__'][0] - assert private_group['public'] is False + assert 'url' not in world_group + def test_private_group_has_url(self, authenticated_request): + authenticated_request.set_groups([FakeGroup('a', 'Group A')]) -def test_world_group_has_no_url_in_model(authenticated_request): - model = session.model(authenticated_request) - world_group = [g for g in model['groups'] if g['id'] == '__world__'][0] + model = session.model(authenticated_request) + private_group = [g for g in model['groups'] if g['id'] == 'a'][0] - assert 'url' not in world_group + assert private_group['url'] + def test_includes_features(self, authenticated_request): + feature_dict = { + 'feature_one': True, + 'feature_two': False, + } + authenticated_request.set_features(feature_dict) -def test_private_group_has_url_in_model(authenticated_request): - authenticated_request.set_groups([FakeGroup('a', 'Group A')]) + assert session.model(authenticated_request)['features'] == feature_dict - model = session.model(authenticated_request) - private_group = [g for g in model['groups'] if g['id'] == 'a'][0] + def test_anonymous_hides_sidebar_tutorial(self, unauthenticated_request): + preferences = session.model(unauthenticated_request)['preferences'] - assert private_group['url'] - - -def test_model_includes_features(authenticated_request): - feature_dict = { - 'feature_one': True, - 'feature_two': False, - } - authenticated_request.set_features(feature_dict) - - assert session.model(authenticated_request)['features'] == feature_dict - - -def test_anonymous_model_hides_sidebar_tutorial(unauthenticated_request): - preferences = session.model(unauthenticated_request)['preferences'] - - assert 'show_sidebar_tutorial' not in preferences - - -@pytest.mark.parametrize('dismissed', [True, False]) -def test_authenticated_model_sidebar_tutorial(authenticated_request, dismissed): - authenticated_request.set_sidebar_tutorial_dismissed(dismissed) - - preferences = session.model(authenticated_request)['preferences'] - - if dismissed: assert 'show_sidebar_tutorial' not in preferences - else: - assert preferences['show_sidebar_tutorial'] is True - - -def test_profile_userid_unauthenticated(unauthenticated_request): - assert session.profile(unauthenticated_request)['userid'] is None - - -def test_profile_userid_authenticated(authenticated_request): - profile = session.profile(authenticated_request) - assert profile['userid'] == u'acct:user@example.com' - - -def test_profile_sorts_groups(authenticated_request): - authenticated_request.set_groups([ - FakeGroup('c', 'Group A'), - FakeGroup('b', 'Group B'), - FakeGroup('a', 'Group B'), - ]) - profile = session.profile(authenticated_request) - - ids = [group['id'] for group in profile['groups']] - - assert ids == ['__world__', 'c', 'a', 'b'] + @pytest.mark.parametrize('dismissed', [True, False]) + def test_authenticated_sidebar_tutorial(self, authenticated_request, dismissed): + authenticated_request.set_sidebar_tutorial_dismissed(dismissed) -def test_world_group_in_authenticated_profile(authenticated_request): - result = session.profile(authenticated_request) + preferences = session.model(authenticated_request)['preferences'] - assert '__world__' in [g['id'] for g in result['groups']] - - -def test_world_group_in_anonymous_profile(unauthenticated_request): - result = session.profile(unauthenticated_request) - - assert '__world__' in [g['id'] for g in result['groups']] - - -def test_world_group_not_in_third_party_profile(third_party_request): - result = session.profile(third_party_request) - - assert '__world__' not in [g['id'] for g in result['groups']] + if dismissed: + assert 'show_sidebar_tutorial' not in preferences + else: + assert preferences['show_sidebar_tutorial'] is True -def test_world_group_is_public_in_profile(authenticated_request): - profile = session.profile(authenticated_request) - world_group = [g for g in profile['groups'] if g['id'] == '__world__'][0] +class TestProfile(object): + def test_userid_unauthenticated(self, unauthenticated_request): + assert session.profile(unauthenticated_request)['userid'] is None - assert world_group['public'] is True + def test_userid_authenticated(self, authenticated_request): + profile = session.profile(authenticated_request) + assert profile['userid'] == u'acct:user@example.com' + def test_sorts_groups(self, authenticated_request): + authenticated_request.set_groups([ + FakeGroup('c', 'Group A'), + FakeGroup('b', 'Group B'), + FakeGroup('a', 'Group B'), + ]) + profile = session.profile(authenticated_request) -def test_private_group_is_not_public_in_profile(authenticated_request): - authenticated_request.set_groups([FakeGroup('a', 'Group A')]) + ids = [group['id'] for group in profile['groups']] - profile = session.profile(authenticated_request) - private_group = [g for g in profile['groups'] if g['id'] == 'a'][0] + assert ids == ['__world__', 'c', 'a', 'b'] - assert private_group['public'] is False + def test_authenticated_world_group(self, authenticated_request): + result = session.profile(authenticated_request) + assert '__world__' in [g['id'] for g in result['groups']] -def test_publisher_group_is_public_in_profile(third_party_request, publisher_group): - profile = session.profile(third_party_request) - group = [g for g in profile['groups'] if g['id'] == publisher_group.pubid][0] + def test_anonymous_world_group(self, unauthenticated_request): + result = session.profile(unauthenticated_request) - assert group['public'] is True + assert '__world__' in [g['id'] for g in result['groups']] + def test_third_party_missing_world_group(self, third_party_request): + result = session.profile(third_party_request) -def test_world_group_has_no_url_in_profile(authenticated_request): - profile = session.profile(authenticated_request) - world_group = [g for g in profile['groups'] if g['id'] == '__world__'][0] + assert '__world__' not in [g['id'] for g in result['groups']] - assert 'url' not in world_group + def test_world_group_is_public(self, authenticated_request): + profile = session.profile(authenticated_request) + world_group = [g for g in profile['groups'] if g['id'] == '__world__'][0] + assert world_group['public'] is True -def test_private_group_has_url_in_profile(authenticated_request): - authenticated_request.set_groups([FakeGroup('a', 'Group A')]) + def test_private_group_is_not_public(self, authenticated_request): + authenticated_request.set_groups([FakeGroup('a', 'Group A')]) - profile = session.profile(authenticated_request) - private_group = [g for g in profile['groups'] if g['id'] == 'a'][0] + profile = session.profile(authenticated_request) + private_group = [g for g in profile['groups'] if g['id'] == 'a'][0] - assert private_group['url'] + assert private_group['public'] is False + def test_publisher_group_is_public(self, third_party_request, publisher_group): + profile = session.profile(third_party_request) + group = [g for g in profile['groups'] if g['id'] == publisher_group.pubid][0] -def test_publisher_group_has_no_url_in_profile(third_party_request, publisher_group): - profile = session.profile(third_party_request) - group = [g for g in profile['groups'] if g['id'] == publisher_group.pubid][0] + assert group['public'] is True - assert 'url' not in group + def test_world_group_has_no_url(self, authenticated_request): + profile = session.profile(authenticated_request) + world_group = [g for g in profile['groups'] if g['id'] == '__world__'][0] + assert 'url' not in world_group -def test_profile_includes_features(authenticated_request): - feature_dict = { - 'feature_one': True, - 'feature_two': False, - } - authenticated_request.set_features(feature_dict) + def test_private_group_has_url(self, authenticated_request): + authenticated_request.set_groups([FakeGroup('a', 'Group A')]) - assert session.profile(authenticated_request)['features'] == feature_dict + profile = session.profile(authenticated_request) + private_group = [g for g in profile['groups'] if g['id'] == 'a'][0] + assert private_group['url'] -def test_anonymous_profile_hides_sidebar_tutorial(unauthenticated_request): - preferences = session.profile(unauthenticated_request)['preferences'] + def test_publisher_group_has_no_url(self, third_party_request, publisher_group): + profile = session.profile(third_party_request) + group = [g for g in profile['groups'] if g['id'] == publisher_group.pubid][0] - assert 'show_sidebar_tutorial' not in preferences + assert 'url' not in group + def test_includes_features(self, authenticated_request): + feature_dict = { + 'feature_one': True, + 'feature_two': False, + } + authenticated_request.set_features(feature_dict) -@pytest.mark.parametrize('dismissed', [True, False]) -def test_authenticated_profile_sidebar_tutorial(authenticated_request, dismissed): - authenticated_request.set_sidebar_tutorial_dismissed(dismissed) + assert session.profile(authenticated_request)['features'] == feature_dict - preferences = session.profile(authenticated_request)['preferences'] + def test_anonymous_hides_sidebar_tutorial(self, unauthenticated_request): + preferences = session.profile(unauthenticated_request)['preferences'] - if dismissed: assert 'show_sidebar_tutorial' not in preferences - else: - assert preferences['show_sidebar_tutorial'] is True + @pytest.mark.parametrize('dismissed', [True, False]) + def test_authenticated_sidebar_tutorial(self, authenticated_request, dismissed): + authenticated_request.set_sidebar_tutorial_dismissed(dismissed) -def test_authority_in_anonymous_profile(unauthenticated_request, auth_domain): - assert session.profile(unauthenticated_request)['authority'] == auth_domain + preferences = session.profile(authenticated_request)['preferences'] + if dismissed: + assert 'show_sidebar_tutorial' not in preferences + else: + assert preferences['show_sidebar_tutorial'] is True -def test_authority_override(unauthenticated_request): - unauthenticated_request.set_public_groups({'foo.com': []}) + def test_anonymous_authority(self, unauthenticated_request, auth_domain): + assert session.profile(unauthenticated_request)['authority'] == auth_domain - profile = session.profile(unauthenticated_request, 'foo.com') + def test_authority_override(self, unauthenticated_request): + unauthenticated_request.set_public_groups({'foo.com': []}) - assert profile['authority'] == 'foo.com' + profile = session.profile(unauthenticated_request, 'foo.com') + assert profile['authority'] == 'foo.com' -def test_authority_in_authenticated_profile(authenticated_request, auth_domain): - assert session.profile(authenticated_request)['authority'] == auth_domain + def test_authenticated_authority(self, authenticated_request, auth_domain): + assert session.profile(authenticated_request)['authority'] == auth_domain + def test_authenticated_ignores_authority_override(self, authenticated_request, auth_domain): + profile = session.profile(authenticated_request, 'foo.com') -def test_authority_ignored_for_authenticated_profile(authenticated_request, auth_domain): - profile = session.profile(authenticated_request, 'foo.com') + assert profile['authority'] == auth_domain - assert profile['authority'] == auth_domain + def test_third_party_authority(self, third_party_request, third_party_domain): + assert session.profile(third_party_request)['authority'] == third_party_domain + def test_third_party_ingores_authority_override(self, third_party_request, third_party_domain): + profile = session.profile(third_party_request, 'foo.com') -def test_authority_in_third_party_profile(third_party_request, third_party_domain): - assert session.profile(third_party_request)['authority'] == third_party_domain + assert profile['authority'] == third_party_domain + @pytest.fixture + def third_party_domain(self): + return u'thirdparty.example.org' -def test_authority_ignored_for_third_party_profile(third_party_request, third_party_domain): - profile = session.profile(third_party_request, 'foo.com') + @pytest.fixture + def third_party_request(self, auth_domain, third_party_domain, publisher_group): + return FakeRequest(auth_domain, + u'acct:user@{}'.format(third_party_domain), + third_party_domain, + {third_party_domain: [publisher_group]}) - assert profile['authority'] == third_party_domain + @pytest.fixture + def publisher_group(self): + return FakeGroup('abcdef', 'Publisher group', is_public=True) class FakeAuthorityGroupService(object): @@ -282,21 +272,11 @@ def auth_domain(): return u'example.com' -@pytest.fixture -def third_party_domain(): - return u'thirdparty.example.org' - - @pytest.fixture def world_group(): return FakeGroup('__world__', 'Public', is_public=True) -@pytest.fixture -def publisher_group(): - return FakeGroup('abcdef', 'Publisher group', is_public=True) - - @pytest.fixture def unauthenticated_request(auth_domain, world_group): return FakeRequest(auth_domain, None, None, {auth_domain: [world_group]}) @@ -308,11 +288,3 @@ def authenticated_request(auth_domain, world_group): u'acct:user@{}'.format(auth_domain), auth_domain, {auth_domain: [world_group]}) - - -@pytest.fixture -def third_party_request(auth_domain, third_party_domain, publisher_group): - return FakeRequest(auth_domain, - u'acct:user@{}'.format(third_party_domain), - third_party_domain, - {third_party_domain: [publisher_group]})