def setUp(self): super(VDTest, self).setUp() self.user = User() self.user.username = SG('[\w]{30}').render() self.user.password = SG('[\l]{6:10}&[\d]{2}').render() self.user.save() self.realUser = RealUser(email='*****@*****.**') self.realUser.save()
def test_source(self): # you can pass a function SG("blah${names}").render(names=lambda: random.choice(["1", "2", "3"])) # you can pass a generator SG("generator: ${names}").render( names=(lambda: (yield "somestring"))()) # range to list SG("generator: ${names}").render(names=list(range(10)))
def test_unseeded_randomizer(self): # provide a seed to get consistent results pattern = r"[\w]{10}&([\d]{10}|M3W9MF_lH3906I14O50)" sg = SG(pattern) s1 = sg.render() sg = SG(pattern) s2 = sg.render() assert s1 != s2 sg = SG(pattern) list1 = sg.render_list(100) sg = SG(pattern) list2 = sg.render_list(100) assert collections.Counter(list1) != collections.Counter(list2)
def test_literals(self): """Test various literals.""" test_list = [r"hel-lo[\w]{8}", r"hello:[\w]{8}", r"-hello-[\w]{8}"] for t in test_list: result = SG(t).render() self.assertIsNotNone(result)
def test_uniqueness_error(self): """Make sure we throw an exception if we can't generate list.""" t = "[123]" self.assertRaises( SG.UniquenessError, lambda: SG(t).render_list(100, unique=True), )
def createuser(): if request.method == 'POST': fn = request.form['field1'] ln = request.form['field2'] lid = request.form['field3'] cno = request.form['field4'] pwd = request.form['field5'] x_list = [ Customer.query.all()[i].loginid for i in range(len(Customer.query.all())) ] c_id = SG("[\u\d]{9}").render() if (fn != '' and ln != '' and cno != '' and pwd != '' and (lid not in x_list)): a = Customer(id=c_id, first_name=fn, last_name=ln, loginid=lid, passwd=pwd, contact_num=cno) db.session.add(a) db.session.commit() return render_template('success.html') else: return 'Failure:credentials are not filled in properly' return render_template('createuser.html')
def register(ctx): """Try to setup a new user account on remote.""" yew = ctx.obj["YEW"] # first make sure we are configured _configure(yew) # next make sure we have a connection to the server if not yew.remote.unauthenticated_ping(): click.echo("Could not connect") sys.exit(1) username = yew.store.prefs.get_user_pref("location.default.username") email = yew.store.prefs.get_user_pref("location.default.email") first_name = yew.store.prefs.get_user_pref("location.default.first_name") last_name = yew.store.prefs.get_user_pref("location.default.last_name") p = SG(r"[\w\d]{12}").render() password = click.prompt("Enter a new password or accept the default ", default=p, type=str) r = yew.remote.register_user( data={ "username": username, "email": email, "password": password, "first_name": first_name, "last_name": last_name, }) if r.status_code == 200: data = json.loads(r.content) yew.store.prefs.put_user_pref("location.default.token", data["token"]) else: click.echo("Something went wrong") click.echo("status code: %s" % r.status_code) click.echo("response: %s" % r.content)
def test_seeded_randomizer(self): # provide a seed to get consistent results pattern = r"[\w]{10}&([\d]{10}|M3W9MF_lH3906I14O50)" for seed in [random.randint(1, 100000000) for _ in range(100)]: sg = SG(pattern, seed=seed) s1 = sg.render() sg = SG(pattern, seed=seed) s2 = sg.render() assert s1 == s2 sg = SG(pattern, seed=seed) list1 = sg.render_list(100) sg = SG(pattern, seed=seed) list2 = sg.render_list(100) assert collections.Counter(list1) == collections.Counter(list2)
def test_unicode_strings(self, s, i): s = remove_special(s) if s: p = f"[{s}]{{10}}" print(p) r = SG(p).render() # print(r) assert r
def test_data_property(self): j = '{"deviceName": "%s", "deviceTypeName": "%s"}' % ( SG('[\w\-]{36}').render(), 'LG-F460L') vd = VD() vd.data = json_loads(j) vd.save() saved = VD.objects.first() self.assertEqual(j, json_dumps(saved.data, encoding='utf-8'))
def anonymous(self, value): """ add anonymous component to model """ random_name = SG("[\w]{3}").render() random_name = unique_component_name(self.model, random_name) setattr(self.model, random_name, value) self._anonymous = value
def test_escaping(self): test_list = [ r"[\[\]]", r"\{\}", r"[\[\]\(\)\{\}\&\|\-\$_+=;'\"<>,.?:!#%^`~*@]{10}", ] for t in test_list: result = SG(t).render() self.assertIsNotNone(result)
def test_unicode(self): test_list = [ r"idzie wąż wąską dróżką", r"༣ཁངཱུངྵ", r"ᚠᚳᛦᛰ", r"[ą-ż]{8}", r"\xe6\xbf\xe5, \xe9\xe5\xa9\xe5\xe5\xad\xe6\xaf\xe7\xe9\xba\xbc\xe6\xe6", # noqa: E501 ] for t in test_list: result = SG(t).render() # TODO: must be a better test than testing for None self.assertIsNotNone(result)
def test_string_generator(self): """Test various templates.""" test_list = [ r"[a-z][\c]{10}(.|_)[\c]{5:10}@[\c]{3:12}.(com|net|org)", r"[\[\]\(\)\{\}\&\|\-\$_+=;'\"<>,.?:!#%^`~*@\\\]OC", r"[a-z\d\d\d\d]{8}", r"[\l]{6:10}&[\d]{2}", r"([a-z]{4}|[0-9]{9})", r"[\d]&[\c]&[\w\p]{6}", ] for t in test_list: result = SG(t).render() self.assertIsNotNone(result)
def export_data(request, string): instance = getData(string) rows = getDataExported(string) response = HttpResponse(content_type='application/ms-excel') text_data = SG("[\l\d]{10}").render() file_name = "{0}_{1}.xls".format(instance['conf'], text_data) content_dispotion = 'attachment; filename="{0}"'.format(file_name) response['Content-Disposition'] = content_dispotion wb = xlwt.Workbook(encoding='utf-8') ws = wb.add_sheet(instance['conf']) row_num = 0 font_style = xlwt.XFStyle() font_style.font.bold = True columns = [ 'source_id', 'session_id', 'article_title', 'url', 'authors', 'authors_affiliations', 'chairs', 'chairs_affiliations', 'date', 'start_time', 'end_time', 'abstract_text', 'location', 'session_title', 'session_type', 'category', 'disclosure', ] for col_num in range(len(columns)): ws.write(row_num, col_num, columns[col_num], font_style) font_style = xlwt.XFStyle() for row in rows: row_num += 1 for col_num in range(len(row)): ws.write(row_num, col_num, row[col_num], font_style) wb.save(response) return response
def test_syntax_exception(self): """Make sure syntax errors in template are caught.""" test_list = [ r"[a-z]{a}", # not a valid quantifier r"[a-]", # invalid class range r"[[1-9]", # unescaped chars r"((foo)(bar)))", # extra parens # r"foo&", # binary operator error # r"|foo", # binary operator error r"[\w]{10:}", # cannot have open range in quantifier ] for t in test_list: # using 2.7 specific context manager here # so, test won't work on < 2.7 but everything else should do # with self.assertRaises(SG.SyntaxError) as context: # SG(t).render() self.assertRaises(SG.SyntaxError, lambda: SG(t).render())
def __init__(self, category=None, *args, **kwargs): super(EsmoSpider, self).__init__(*args, **kwargs) unique_id = SG("[\l\d]{10}").render() date_object = str(datetime.now().day) + str( datetime.now().month) + str(datetime.now().year) date_object = str(date_object) + "_" + str( datetime.now().time()).replace(":", "_").split(".")[0] unique_id = date_object + "_" + unique_id unique_id = unique_id.replace(" ", "_").strip() self.CRAWL_NEW_ID = unique_id data_dict = { "crawl_id": self.CRAWL_NEW_ID, "conf": "esmo", "start_datetime": datetime.now(), "end_datetime": "", "status": "running", } my_database.crawl_initiate.insert_one(data_dict)
def test_simple_properties(self): deviceName = SG('[\w\-]{36}').render() deviceTypeName = 'LG-F460L' country = 'KR' language = 'ko' timezone = 'KST' vd = VD() vd.deviceName = deviceName vd.deviceTypeName = deviceTypeName vd.country = country vd.language = language vd.timezone = timezone vd.save() saved = VD.objects.first() self.assertEqual(saved.deviceName, deviceName) self.assertEqual(saved.deviceTypeName, deviceTypeName) self.assertEqual(saved.country, country) self.assertEqual(saved.language, language) self.assertEqual(saved.timezone, timezone)
def test_register(self): deviceName = SG('[\w\-]{36}').render() deviceTypeName = 'LG-F460L' email = '*****@*****.**' country = 'KR' language = 'ko' timezone = 'KST' data = '{"UDID": "blah-blah"}' self.assertEqual(VD.objects.count(), 0) response = self.client.post( '/vds/register/', dict(email=email, deviceTypeName=deviceTypeName, deviceName=deviceName, country=country, language=language, timezone=timezone, data=data)) self.assertEqual(VD.objects.count(), 1) vd = VD.objects.first() user = User.objects.first() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(vd.deviceTypeName, deviceTypeName) self.assertEqual(vd.deviceName, deviceName) self.assertEqual(vd.country, country) self.assertEqual(vd.language, language) self.assertEqual(vd.timezone, timezone) self.assertEqual(vd.data, json_loads(data)) self.assertEqual(user, vd.authOwner) self.assertEqual(user.email, email) result = json_loads(response.content) self.assertIn('auth_vd_token', result) decrypter = Fernet(user.crypto_key) raw_token = decrypter.decrypt( result['auth_vd_token'].encode(encoding='utf-8')) vd_id = int(raw_token.split('|')[0]) user_id = int(raw_token.split('|')[1]) self.assertEqual(vd_id, vd.id) self.assertEqual(user_id, user.id) self.assertEqual(vd.realOwner, None)
def test_render_list(self): list_length = 10 test_list = [ r"[a-z][\c]{10}(.|_)[\c]{5:10}@[\c]{3:12}.(com|net|org)", r"[a-z\d\d\d\d]{8}", r"[\l]{6:10}&[\d]{2}", r"[\l]{6-10}&[\d]{2}", # support both hyphen and colon for ranges r"([a-z]{4}|[0-9]{9})", r"[\d]&[\c]&[\w\p]{6}", r"[\w\p]", r"[\w\p]{6}", r"[\w\p]{-6}" r"[\w\p]{:6}", r"[\w\p]{0:6}", ] for t in test_list: result = SG(t).render_list(list_length) self.assertTrue(isinstance(result, list)) self.assertTrue(len(result) == list_length)
def test_list_progress(self): """Check if the progress indicator actually works""" list_length = 10 progress_states = [] def progress_callback(current, total): progress_state = "{current}/{total}".format(**locals()) progress_states.append(progress_state) SG(r"[a-z\d\d\d\d]{8}").render_list( list_length, progress_callback=progress_callback) # Length of list of progress states should match length of # requested strings self.assertTrue(len(progress_states) == list_length) # Check the first and the last item for the sake of completeness self.assertEqual(progress_states[0], "1/10") self.assertEqual(progress_states[-1], "10/10")
def test_email_confirm(self): # TODO : 향후 이메일 발송 루틴이 구현되면 테스트도 수정해야 한다. deviceName = SG('[\w\-]{36}').render() deviceTypeName = 'LG-F460L' email = '*****@*****.**' response = self.client.post( '/vds/register/', dict(email=email, deviceTypeName=deviceTypeName, deviceName=deviceName)) self.assertEqual(response.status_code, status.HTTP_201_CREATED) vd = VD.objects.first() self.assertEqual(vd.realOwner, None) # 이메일 인증 처리 token = vd.getEmailConfirmToken(email) response = self.client.get('/vds/confirm/', dict(email_confirm_token=token)) self.assertEqual(response.status_code, status.HTTP_302_FOUND) self.assertEqual(response.url, '/ui/confirm_ok/') # assertion vd = VD.objects.first() self.assertIsNotNone(vd.realOwner) self.assertEqual(vd.authOwner.email, vd.realOwner.email) # 또 이메일 인증 처리 : 링크는 여러번 누를 수도... self.assertEqual(VD.objects.count(), 1) self.assertEqual(RealUser.objects.count(), 1) self.assertEqual(User.objects.count(), 1) token = vd.getEmailConfirmToken(email) response = self.client.get('/vds/confirm/', dict(email_confirm_token=token)) self.assertEqual(response.status_code, status.HTTP_302_FOUND) self.assertEqual(VD.objects.count(), 1) self.assertEqual(RealUser.objects.count(), 1) self.assertEqual(User.objects.count(), 1)
def test_custom_randomizer(self): pattern = r"[\w]{10}&([\d]{10}|M3W9MF_lH3906I14O50)" sg = SG(pattern, randomizer=CustomRandomizer()) assert len(sg.render())
def test_str(self): str(SG(r"[\w]{8}"))
def test_repr(self): repr(SG(r"[\w]{8}"))
def test_single_characters(self, s, i): p = f"[{s}]{{10}}" r = SG(p).render() # print(r) assert r
def test_counts(self): assert SG(r'1&abc').count() == len(SG(r'1&abc').render_set(24)) assert SG(r'[\u\d]{2}|[abc]{3}', uaf=100).count() == len( SG(r'[\u\d]{2}|[abc]{3}', uaf=100).render_list(1323, unique=True))
def test_probabilistic_or(self): d = SG("0|1|2|3|4|5|6|7|8|9").render_list(10000) d = [int(d) for d in d]
def generate(self, length=10): "Creates a readable password of length characters" self.pwd = SG("[a-kl-np-z0-9]{" + str(length) + "}").render() return self.pwd
def test_dump(self): """make sure dump method works.""" SG(r"[\w]{8}").dump()