test_signup.py 10.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# This test will only pass if the site and secret keys are set to 
# RECAPTCHA_SITE_KEY = 6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI
# RECAPTCHA_SECRET_KEY = 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe
# in agreelance/settings.py. See recaptcha docs for more information:
# https://developers.google.com/recaptcha/docs/faq

import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

import time
import os
import random 
import string
from time import sleep

url = "http://127.0.0.1:8000/user/signup"

class AgreelanceSignUp(unittest.TestCase):

    def setUp(self):
        if os.name == "nt":
            self.driver = webdriver.Chrome("./bin/chromedriver.exe")
        else:
            self.driver = webdriver.Chrome("./bin/chromedriver")
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    
    def test_two_way_postal_code_city(self):
        driver = self.driver
        values = self.getNominalValues()

        combinations = [
            ["Oslo", "0378", True],
            ["Oslo", "7018", False],
            ["Trondheim", "0378", False],
            ["Trondheim", "7018", True],
        ]

        for combination in combinations:
            values["city"] = combination[0]
            values["postal_code"] = combination[1]
            self.assertTrue(self.case_tester(combination) == combination[2])

    def test_two_way_city_country(self):
        driver = self.driver
        values = self.getNominalValues()

        combinations = [
            ["Oslo", "Norway", True],
            ["Oslo", "Sweden", False],
            ["Stockholm", "Norway", False],
            ["Stockholm", "Sweden", True],
        ]

        for combination in combinations:
            values["city"] = combination[0]
            values["country"] = combination[1]
            self.assertTrue(self.case_tester(combination) == combination[2])

    def test_two_way_password_verify(self):
        driver = self.driver
        values = self.getNominalValues()

        combinations = [
            ["qwerty123", "Qwerty123", False],
            ["qwerty123", "qwerty123", True],
        ]

        for combination in combinations:
            values["password"] = combination[0]
            values["password_confirmation"] = combination[1]
            self.assertTrue(self.case_tester(combination) == combination[2])
76

77
78
79
80
81
82
83
    def test_username(self):
        testcases = [
            ["username", "", False], # min-
            ["username", self.rString(1), True], # min
            ["username", self.rString(150), True], # max
            ["username", self.rString(151), False], # max+
        ]
84

85
86
87
88
89
90
91
92
93
        self.assertTrue(self.case_tester(testcases))
    
    def test_firstname(self):
        testcases = [
            ["firstname", "", False], # min-
            ["firstname", self.rString(1), True], # min
            ["firstname", self.rString(30), True], # max
            ["firstname", self.rString(31), False], # max+
        ]
94

95
        self.assertTrue(self.case_tester(testcases))
96

97
98
99
100
101
102
103
    def test_lastname(self):
        testcases = [
            ["lastname", "", False], # min-
            ["lastname", self.rString(1), True], # min
            ["lastname", self.rString(30), True], # max
            ["lastname", self.rString(31), False], # max+
        ]
Johannes Tomren Røsvik's avatar
Johannes Tomren Røsvik committed
104

105
106
107
        self.assertTrue(self.case_tester(testcases))
    
    def test_categories(self):
Johannes Tomren Røsvik's avatar
Johannes Tomren Røsvik committed
108
        testcases = [
109
            ["categories", None, False], # min-
Johannes Tomren Røsvik's avatar
Johannes Tomren Røsvik committed
110
111
        ]

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
        self.assertTrue(self.case_tester(testcases))
    
    def test_company(self):
        testcases = [
            ["company", "", True], # min
            ["company", "30", True], # max
            ["company", "30", False], # max+
        ]

        self.assertTrue(self.case_tester(testcases))

    def test_email(self):

        email_6 = self.rString(1) + "@" + self.rString(1) + ".co"
        email_254 = self.rString(248) + "@" + self.rString(1) + ".com"
        email_255 = self.rString(248) + "@" + self.rString(2) + ".com"

        testcases = [
            ["email", "", False], # min-
            ["email", email_6, True], # min
            ["email", email_254, True], # max
            ["email", email_255, False], # max+
        ]

        self.assertTrue(self.case_tester(testcases))

    def test_phone_number(self):
			
        testcases = [
            ["phone_number", "", False], # min-
            ["phone_number", self.rDigits(1), True], # min
            ["phone_number", self.rDigits(50), True], # max
            ["phone_number", self.rDigits(51), False], # max+
        ]

        self.assertTrue(self.case_tester(testcases))

    def test_country(self):
	
        testcases = [
            ["country", "", False], # min-
            ["country", self.rString(1), True], # min
            ["country", self.rString(50), True], # max
            ["country", self.rString(51), False], # max+
        ]

        self.assertTrue(self.case_tester(testcases))

    def test_state(self):
		
        testcases = [
            ["state", "", False], # min-
            ["state", self.rString(1), True], # min
            ["state", self.rString(50), True], # max
            ["state", self.rString(51), False], # max+
        ]

        self.assertTrue(self.case_tester(testcases))

    def test_city(self):
			
        testcases = [
            ["city", "", False], # min-
            ["city", self.rString(1), True], # min
            ["city", self.rString(50), True], # max
            ["city", self.rString(51), False], # max+
        ]

        self.assertTrue(self.case_tester(testcases))

    def test_postal_code(self):
				
        testcases = [
            ["postal_code", "", False], # min-
            ["postal_code", self.rDigits(1), True], # min
            ["postal_code", self.rDigits(50), True], # max
            ["postal_code", self.rDigits(51), False], # max+
        ]

        self.assertTrue(self.case_tester(testcases))

    def test_street_address(self):
					
        testcases = [
            ["street_address", "", False], # min-
            ["street_address", self.rString(1), True], # min
            ["street_address", self.rString(50), True], # max
            ["street_address", self.rString(51), False], # max+
        ]

        self.assertTrue(self.case_tester(testcases))


    def case_tester(self, testcases):
        driver = self.driver

Johannes Tomren Røsvik's avatar
Johannes Tomren Røsvik committed
208
        for case in testcases:
Johannes Tomren Røsvik's avatar
Johannes Tomren Røsvik committed
209
            values = self.getNominalValues()
Johannes Tomren Røsvik's avatar
Johannes Tomren Røsvik committed
210
            values[case[0]] = case[1]
Johannes Tomren Røsvik's avatar
Johannes Tomren Røsvik committed
211
            formstate = self.fill_form(values)
212
213
214
215
216
            if formstate != case[2]: 
                print(values)
                return False
        return True

Johannes Tomren Røsvik's avatar
Johannes Tomren Røsvik committed
217
    
218
    def fill_form(self, values):
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
        driver = self.driver
        driver.get(url)

        username = driver.find_element_by_id("id_username")
        firstname = driver.find_element_by_id("id_first_name")
        lastname = driver.find_element_by_id("id_last_name")
        categories = Select(driver.find_element_by_id("id_categories"))
        company = driver.find_element_by_id("id_company")
        email = driver.find_element_by_id("id_email")
        email_confirmation = driver.find_element_by_id("id_email_confirmation")
        password = driver.find_element_by_id("id_password1")
        password_confirmation = driver.find_element_by_id("id_password2")
        phone_number = driver.find_element_by_id("id_phone_number")
        country = driver.find_element_by_id("id_country")
        state = driver.find_element_by_id("id_state")
        city = driver.find_element_by_id("id_city")
        postal_code = driver.find_element_by_id("id_postal_code")
        street_address = driver.find_element_by_id("id_street_address")
        
        recaptcha = driver.find_element_by_class_name("g-recaptcha")
        submit = driver.find_element_by_xpath("/html/body/div[1]/form/button")

241
242
243
        username.send_keys(values["username"])
        firstname.send_keys(values["firstname"])
        lastname.send_keys(values["lastname"])
244
245
        if values["categories"] != None: 
            categories.select_by_index(values["categories"])
246
247
        company.send_keys(values["company"])
        email.send_keys(values["email"])
248
        email_confirmation.send_keys(values["email"])
249
        password.send_keys(values["password"])
250
        password_confirmation.send_keys(values["password"])
251
252
253
254
255
256
257
        phone_number.send_keys(values["phone_number"])
        country.send_keys(values["country"])
        state.send_keys(values["state"])
        city.send_keys(values["city"])
        postal_code.send_keys(values["postal_code"])
        street_address.send_keys(values["street_address"])
        # recaptcha.click()
258
259
        submit.click()
        
260
        timeout = 1
261
262
263
        try:
            element_present = EC.presence_of_element_located((By.CLASS_NAME, 'alert'))
            WebDriverWait(driver, timeout).until(element_present)
264
265
            alert = driver.find_element_by_class_name("alert")
            return "Your account has been created" in alert.text
266
        except TimeoutException:
267
            # Timed out
268
269
270
            return False

    def getNominalValues(self):
271

272
        email_ = self.rString(10) + "@" + self.rString(10) + ".com"
273
        password_ = self.rString(20)
274

275
        values = {
276
277
278
            "username": self.rString(20),
            "firstname": self.rString(20),
            "lastname": self.rString(20),
279
            "categories": 0,
280
            "company": self.rString(20),
281
282
283
284
285
            "email": email_,
            "email_confirmation": email_,
            "password": password_,
            "password_confirmation": password_,
            "phone_number": self.rDigits(8),
286
287
288
            "country": self.rString(20),
            "state": self.rString(20),
            "city": self.rString(20),
289
            "postal_code": self.rDigits(4),
290
            "street_address": self.rString(20)
291
292
293
        }

        return values
294
295
296
297
298
299
300
301
302
303
304
305
306
307
        
    def tearDown(self):
        self.driver.close()

    @staticmethod
    def rString(length):
        return ''.join(random.choices(string.ascii_lowercase, k=length))

    @staticmethod
    def rDigits(length):
        return ''.join(random.choices(string.digits, k=length))

if __name__ == "__main__":
    unittest.main()