test_add_new_member_flow()   F
last analyzed

Complexity

Conditions 12

Size

Total Lines 129

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
dl 0
loc 129
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like test_add_new_member_flow() 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 re
2
from .. import factories as f
3
from django.contrib.auth.models import User
4
5
from .. utils import create_user_verify_login
6
7
8
def test_add_new_member_flow(base_url, browser, outbox):
9
    # ----------------- creating new user ------------------------
10
    f.create_usertype(slug='tutor', display_name='tutor')
11
    user = create_user_verify_login(base_url, browser, outbox)
12
    # ----------------------add user type -------------------
13
    location1 = f.create_locaiton(name='location1')
14
    state1 = f.create_state(name='state1')
15
    poc_type = f.create_usertype(slug='poc', display_name='poc')
16
    user.profile.usertype.clear()
17
    user.profile.usertype.add(poc_type)
18
    user.profile.location = location1
19
    user.profile.interested_states.add(state1)
20
    user.profile.save()
21
    user.save()
22
    # -----------------------creating organisation ---------------------
23
    url = base_url + '/organisation/'
24
    browser.fill('login', user.email)
25
    browser.fill('password', '123123')
26
    browser.find_by_css('[type=submit]')[0].click()
27
    url = base_url + '/organisation/'
28
    browser.visit(url)
29
    org_create_link = browser.find_by_text('Add Organisation')[0]
30
    assert org_create_link
31
    org_create_link.click()
32
    browser.select('organisation_type', 1)
33
    browser.fill('name', 'Org1')
34
    browser.fill('description', 'Description')
35
    browser.select('location', location1.id)
36
    browser.fill('organisation_role', 'Role1')
37
    browser.find_by_css('[type=submit]')[0].click()
38
39
    org = f.create_organisation(location=location1)
40
    org.user.add(user)
41
    org.save()
42
43
    # -------------------Adding member ------------------------
44
    # existing user
45
    browser.find_by_text('Org1')[0].click()
46
47
    # add to org
48
    browser.find_by_text('Add Users')[0].click()
49
    browser.fill('new_user', '[email protected]')
50
    browser.find_by_css('[type=submit]')[0].click()
51
    org.user.add(user)
52
    org.save()
53
54
    # invite mail
55
    # assert len(outbox) == 6
56
    mail = outbox[5]
57
    # print(mail.body)
58
    invite_link = re.findall(r'http.*/invitation/.*/', mail.body)
59
    assert invite_link
60
    browser.visit(invite_link[0])
61
    # asserting if it's the signup page or not
62
    assert 'Signup' in browser.title
63
64
    # fill sign up form
65
    browser.fill('first_name', 'random')
66
    browser.fill('last_name', 'person')
67
    browser.fill('username', 'randomnessprevails')
68
    browser.fill('password', 'secretpassword')
69
    browser.fill('password_confirm', 'secretpassword')
70
    browser.find_by_css('[type=submit]')[0].click()
71
72
    # check user was added
73
    browser.find_by_text('Org1')[0].click()
74
    user_list = browser.find_by_css('.list-silent')
75
    assert '[email protected]' in user_list[0].text
76
77
    # logout and login again to activate user
78
    logout_url = base_url + "/accounts/logout"
79
    browser.visit(logout_url)
80
81
    login_url = base_url + '/accounts/login/'
82
    browser.visit(login_url)
83
    browser.fill('login', '[email protected]')
84
    browser.fill('password', 'secretpassword')
85
    browser.find_by_css('[type=submit]')[0].click()
86
87
    # confirmation email sent
88
    assert browser.is_text_present(
89
        'We have sent an e-mail to you for verification')
90
    # assert len(outbox) == 7
91
    mail = outbox[-1]
92
93
    activate_link = re.findall(r'http.*/accounts/confirm-email/.*/', mail.body)
94
    assert activate_link
95
96
    # confirmation dialogue
97
    browser.visit(activate_link[0])
98
    assert "Confirm E-mail Address" in browser.title
99
    browser.find_by_css('[type=submit]')[0].click()
100
101
    # login
102
    assert "Login" in browser.title
103
    browser.fill('login', '[email protected]')
104
    browser.fill('password', 'secretpassword')
105
    browser.find_by_css('[type=submit]')[0].click()
106
107
    # edit profile
108
    # assert browser.is_text_present("My Profile")
109
110
    u = User.objects.get(email='[email protected]')
111
    u.profile.usertype.clear()
112
113
    poc_type = f.create_usertype(slug='poc', display_name='College POC')
114
    section1 = f.create_workshop_section(name='section1')
115
    location2 = f.create_locaiton(name='location2')
116
117
    url = base_url + '/profile/randomnessprevails/edit'
118
    browser.visit(url)
119
120
    browser.fill('mobile', '0812739120')
121
    browser.select('usertype', poc_type.id)
122
    browser.select('interested_sections', section1.id)
123
    browser.fill('occupation', 'occupation')
124
    browser.fill('work_location', 'work_location')
125
    browser.fill('work_experience', 1)
126
    browser.select('interested_states', state1.id)
127
    browser.select('location', location2.id)
128
    browser.find_by_css('[type=submit]')[0].click()
129
130
    assert browser.is_text_present('My Profile')
131
    assert browser.is_text_present('Graph')
132
133
    # Logging Out
134
    url = base_url + '/accounts/logout/'
135
    browser.visit(url)
136
    assert 'Home | PythonExpress' in browser.title
137
138
139
def test_add_existing_member_flow(base_url, browser, outbox):
140
    # ------------------creating new user ----------------------
141
    f.create_usertype(slug='tutor', display_name='tutor')
142
    user = create_user_verify_login(base_url, browser, outbox)
143
    location1 = f.create_locaiton(name='location1')
144
    state1 = f.create_state(name='state1')
145
    # ---------------------add user type -----------------------
146
    poc_type = f.create_usertype(slug='poc', display_name='poc')
147
    user.profile.usertype.add(poc_type)
148
    user.profile.usertype.clear()
149
    user.profile.location = location1
150
    user.profile.state = state1
151
    user.profile.save()
152
    user.save()
153
154
    # -------------------creating organisation --------------------
155
    url = base_url + '/organisation/'
156
    browser.fill('login', user.email)
157
    browser.fill('password', '123123')
158
    browser.find_by_css('[type=submit]')[0].click()
159
    browser.visit(url)
160
    org_create_link = browser.find_by_text('Add Organisation')[0]
161
    assert org_create_link
162
    org_create_link.click()
163
    browser.select('organisation_type', 1)
164
    browser.fill('name', 'Org1')
165
    browser.fill('description', 'Description')
166
    browser.select('location', location1.id)
167
    browser.fill('organisation_role', 'Role1')
168
    browser.find_by_css('[type=submit]')[0].click()
169
170
    org = f.create_organisation(location=location1)
171
    org.user.add(user)
172
    org.save()
173
174
    # -------------- Adding member ------------------------
175
176
    # create user
177
    user2 = f.create_user(is_active=True)
178
    user2.set_password('123123')
179
    user2.save()
180
    user2.profile.usertype.add(poc_type)
181
    user2.save()
182
183
    # add to org
184
    browser.find_by_text('Org1')[0].click()
185
    browser.find_by_text('Add Users')[0].click()
186
    browser.select_by_text('existing_user', user2.username)
187
    browser.find_by_css('[type=submit]')[0].click()
188
    org.user.add(user2)
189
    org.save()
190
191
    # check user was added
192
    browser.find_by_text('Org1')[0].click()
193
    user_list = browser.find_by_css('.list-silent')
194
    assert user2.email in user_list[0].text
195
196
    assert 'Organisation | PythonExpress' in browser.title
197