Completed
Push — master ( 1b8e23...81a01d )
by Vijay
9s
created

test_add_new_member_flow()   F

Complexity

Conditions 18

Size

Total Lines 134

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 18
c 1
b 0
f 1
dl 0
loc 134
rs 2

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