test_staff_pages()   F
last analyzed

Complexity

Conditions 13

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
c 0
b 0
f 0
dl 0
loc 33
rs 2.7716

How to fix   Complexity   

Complexity

Complex classes like test_staff_pages() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import pytest
2
3
from .. import factories as f
4
5
pytestmark = pytest.mark.django_db
6
7
8
public_pages = [
9
    '/',
10
    '/about/',
11
    '/faq/',
12
    '/accounts/login/',
13
    '/accounts/signup/',
14
    '/accounts/password/reset/',
15
]
16
17
18
staff_pages = [
19
    '/region/',
20
    '/region/lead/create/',
21
    '/region/location/create/',
22
    '/region/state/create/',
23
]
24
25
26
def test_public_pages(client):
27
    # These urls are publically accessible and their urls shouldn't change
28
    # with time.
29
    for page_url in public_pages:
30
        response = client.get(page_url)
31
        assert response.status_code == 200, 'Failed for %s' % page_url
32
        assert 'Log In' in str(response.content)
33
34
35
def test_staff_pages(client, settings):
36
    settings.SITE_VARIABLES['site_name'] = 'My Test Website'
37
    f.create_usertype(slug='tutor', display_name='tutor')
38
    normal_user = f.UserFactory(is_staff=False)
39
    staff_user = f.UserFactory(is_staff=True)
40
41
    for page_url in staff_pages:
42
        response = client.get(page_url)
43
        assert response.status_code == 302, 'Failed for %s' % page_url
44
        assert '/accounts/login?next=%s' % page_url in response['Location']
45
46
        # The page should render find after login
47
        if page_url in staff_pages:
48
            client.login(normal_user)
49
            assert response.status_code == 302, 'Failed for %s' % page_url
50
            assert '/accounts/login?next=%s' % page_url in response['Location']
51
            client.logout()
52
53
            client.login(staff_user)
54
            response = client.get(page_url)
55
            assert response.status_code == 200, 'Failed for %s' % page_url
56
            assert normal_user.get_full_name() in str(response.content)
57
            assert settings.SITE_VARIABLES[
58
                'site_name'] in str(response.content)
59
            client.logout()
60
        else:
61
            client.login(normal_user)
62
            response = client.get(page_url)
63
            assert response.status_code == 200, 'Failed for %s' % page_url
64
            assert normal_user.get_full_name() in str(response.content)
65
            assert settings.SITE_VARIABLES[
66
                'site_name'] in str(response.content)
67
            client.logout()
68
69
70 View Code Duplication
def test_orgnisation_pages(client, settings):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
71
    f.create_usertype(slug='tutor', display_name='tutor')
72
    settings.SITE_VARIABLES['site_name'] = 'My Test Website'
73
    normal_user = f.UserFactory(is_staff=False)
74
75
    poc_type = f.create_usertype(slug='poc', display_name='poc')
76
    normal_user.profile.usertype.clear()
77
    normal_user.profile.usertype.add(poc_type)
78
    org = f.create_organisation()
79
    org.user.add(normal_user)
80
    org.save()
81
    normal_user.profile.location = org.location
82
    normal_user.profile.save()
83
84
    url_list = [
85
        '/organisation/',
86
        '/organisation/create/',
87
        '/organisation/{}/'.format(org.id),
88
        '/organisation/{}/edit/'.format(org.id),
89
    ]
90
91
    for page_url in url_list:
92
        client.login(normal_user)
93
        response = client.get(page_url)
94
        assert response.status_code == 200, 'Failed for %s' % page_url
95
        assert normal_user.get_full_name() in str(response.content)
96
        assert settings.SITE_VARIABLES[
97
            'site_name'] in str(response.content)
98
        client.logout()
99
100
101 View Code Duplication
def test_workshop_pages(client, settings):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
102
    settings.SITE_VARIABLES['site_name'] = 'My Test Website'
103
    f.create_usertype(slug='tutor', display_name='tutor')
104
    poc_user = f.UserFactory(is_staff=False)
105
    poc_type = f.create_usertype(slug='poc', display_name='poc')
106
    poc_user.profile.usertype.clear()
107
    poc_user.profile.usertype.add(poc_type)
108
    org = f.create_organisation()
109
    org.user.add(poc_user)
110
    org.save()
111
    poc_user.profile.location = org.location
112
    poc_user.profile.save()
113
    workshop = f.create_workshop(requester=org)
114
    workshop.presenter.add(poc_user)
115
    workshop.save()
116
117
    url_list = [
118
        '/workshop/',
119
        '/workshop/create/',
120
        '/workshop/{}/'.format(workshop.id),
121
    ]
122
123
    for page_url in url_list:
124
        client.login(poc_user)
125
        response = client.get(page_url)
126
        assert response.status_code == 200, 'Failed for %s' % page_url
127
        assert poc_user.get_full_name() in str(response.content)
128
        assert settings.SITE_VARIABLES[
129
            'site_name'] in str(response.content)
130
        client.logout()
131